blob: 42ddd70e26cae52d35d25d68609f855a283cf10d [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * X.509 certificate and private key decoding
3 *
Paul Bakkerefc30292011-11-10 14:43:23 +00004 * Copyright (C) 2006-2011, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * The ITU-T X.509 standard defines a certificat format for PKI.
27 *
28 * http://www.ietf.org/rfc/rfc2459.txt
29 * http://www.ietf.org/rfc/rfc3279.txt
30 *
31 * ftp://ftp.rsasecurity.com/pub/pkcs/ascii/pkcs-1v2.asc
32 *
33 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
34 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
35 */
36
Paul Bakker40e46942009-01-03 21:51:57 +000037#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Paul Bakker40e46942009-01-03 21:51:57 +000039#if defined(POLARSSL_X509_PARSE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000040
Paul Bakker40e46942009-01-03 21:51:57 +000041#include "polarssl/x509.h"
Paul Bakkerefc30292011-11-10 14:43:23 +000042#include "polarssl/asn1.h"
Paul Bakker96743fc2011-02-12 14:30:57 +000043#include "polarssl/pem.h"
Paul Bakker40e46942009-01-03 21:51:57 +000044#include "polarssl/des.h"
Paul Bakkerf6bff2a2013-03-11 16:05:32 +010045#if defined(POLARSSL_MD2_C)
Paul Bakker40e46942009-01-03 21:51:57 +000046#include "polarssl/md2.h"
Paul Bakkerf6bff2a2013-03-11 16:05:32 +010047#endif
48#if defined(POLARSSL_MD4_C)
Paul Bakker40e46942009-01-03 21:51:57 +000049#include "polarssl/md4.h"
Paul Bakkerf6bff2a2013-03-11 16:05:32 +010050#endif
51#if defined(POLARSSL_MD5_C)
Paul Bakker40e46942009-01-03 21:51:57 +000052#include "polarssl/md5.h"
Paul Bakkerf6bff2a2013-03-11 16:05:32 +010053#endif
54#if defined(POLARSSL_SHA1_C)
Paul Bakker40e46942009-01-03 21:51:57 +000055#include "polarssl/sha1.h"
Paul Bakkerf6bff2a2013-03-11 16:05:32 +010056#endif
57#if defined(POLARSSL_SHA2_C)
Paul Bakker026c03b2009-03-28 17:53:03 +000058#include "polarssl/sha2.h"
Paul Bakkerf6bff2a2013-03-11 16:05:32 +010059#endif
60#if defined(POLARSSL_SHA4_C)
Paul Bakker026c03b2009-03-28 17:53:03 +000061#include "polarssl/sha4.h"
Paul Bakkerf6bff2a2013-03-11 16:05:32 +010062#endif
Paul Bakker1b57b062011-01-06 15:48:19 +000063#include "polarssl/dhm.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000064
65#include <string.h>
66#include <stdlib.h>
Paul Bakker4f229e52011-12-04 22:11:35 +000067#if defined(_WIN32)
Paul Bakkercce9d772011-11-18 14:26:47 +000068#include <windows.h>
69#else
Paul Bakker5121ce52009-01-03 21:22:43 +000070#include <time.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000071#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000072
Paul Bakker335db3f2011-04-25 15:28:35 +000073#if defined(POLARSSL_FS_IO)
74#include <stdio.h>
75#endif
76
Paul Bakker5121ce52009-01-03 21:22:43 +000077/*
Paul Bakker5121ce52009-01-03 21:22:43 +000078 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
79 */
80static int x509_get_version( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000081 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +000082 int *ver )
83{
Paul Bakker23986e52011-04-24 08:57:21 +000084 int ret;
85 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +000086
87 if( ( ret = asn1_get_tag( p, end, &len,
88 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
89 {
Paul Bakker40e46942009-01-03 21:51:57 +000090 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +000091 {
92 *ver = 0;
93 return( 0 );
94 }
Paul Bakker5121ce52009-01-03 21:22:43 +000095
96 return( ret );
97 }
98
99 end = *p + len;
100
101 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000102 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
104 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000105 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION +
Paul Bakker40e46942009-01-03 21:51:57 +0000106 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
108 return( 0 );
109}
110
111/*
Paul Bakkerfae618f2011-10-12 11:53:52 +0000112 * Version ::= INTEGER { v1(0), v2(1) }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000113 */
114static int x509_crl_get_version( unsigned char **p,
115 const unsigned char *end,
116 int *ver )
117{
118 int ret;
119
120 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
121 {
122 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker2a1c5f52011-10-19 14:15:17 +0000123 {
124 *ver = 0;
125 return( 0 );
126 }
Paul Bakker3329d1f2011-10-12 09:55:01 +0000127
128 return( POLARSSL_ERR_X509_CERT_INVALID_VERSION + ret );
129 }
130
131 return( 0 );
132}
133
134/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 * CertificateSerialNumber ::= INTEGER
136 */
137static int x509_get_serial( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000138 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 x509_buf *serial )
140{
141 int ret;
142
143 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000144 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000145 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
147 if( **p != ( ASN1_CONTEXT_SPECIFIC | ASN1_PRIMITIVE | 2 ) &&
148 **p != ASN1_INTEGER )
Paul Bakker9d781402011-05-09 16:17:09 +0000149 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL +
Paul Bakker40e46942009-01-03 21:51:57 +0000150 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
152 serial->tag = *(*p)++;
153
154 if( ( ret = asn1_get_len( p, end, &serial->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000155 return( POLARSSL_ERR_X509_CERT_INVALID_SERIAL + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
157 serial->p = *p;
158 *p += serial->len;
159
160 return( 0 );
161}
162
163/*
164 * AlgorithmIdentifier ::= SEQUENCE {
165 * algorithm OBJECT IDENTIFIER,
166 * parameters ANY DEFINED BY algorithm OPTIONAL }
167 */
168static int x509_get_alg( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000169 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000170 x509_buf *alg )
171{
Paul Bakker23986e52011-04-24 08:57:21 +0000172 int ret;
173 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000174
175 if( ( ret = asn1_get_tag( p, end, &len,
176 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000177 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000178
179 end = *p + len;
180 alg->tag = **p;
181
182 if( ( ret = asn1_get_tag( p, end, &alg->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000183 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000184
185 alg->p = *p;
186 *p += alg->len;
187
188 if( *p == end )
189 return( 0 );
190
191 /*
192 * assume the algorithm parameters must be NULL
193 */
194 if( ( ret = asn1_get_tag( p, end, &len, ASN1_NULL ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000195 return( POLARSSL_ERR_X509_CERT_INVALID_ALG + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000196
197 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000198 return( POLARSSL_ERR_X509_CERT_INVALID_ALG +
Paul Bakker40e46942009-01-03 21:51:57 +0000199 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000200
201 return( 0 );
202}
203
204/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 * AttributeTypeAndValue ::= SEQUENCE {
206 * type AttributeType,
207 * value AttributeValue }
208 *
209 * AttributeType ::= OBJECT IDENTIFIER
210 *
211 * AttributeValue ::= ANY DEFINED BY AttributeType
212 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000213static int x509_get_attr_type_value( unsigned char **p,
214 const unsigned char *end,
215 x509_name *cur )
Paul Bakker5121ce52009-01-03 21:22:43 +0000216{
Paul Bakker23986e52011-04-24 08:57:21 +0000217 int ret;
218 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000219 x509_buf *oid;
220 x509_buf *val;
221
222 if( ( ret = asn1_get_tag( p, end, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000223 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000224 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000225
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 oid = &cur->oid;
227 oid->tag = **p;
228
229 if( ( ret = asn1_get_tag( p, end, &oid->len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000230 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000231
232 oid->p = *p;
233 *p += oid->len;
234
235 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000236 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000237 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000238
239 if( **p != ASN1_BMP_STRING && **p != ASN1_UTF8_STRING &&
240 **p != ASN1_T61_STRING && **p != ASN1_PRINTABLE_STRING &&
241 **p != ASN1_IA5_STRING && **p != ASN1_UNIVERSAL_STRING )
Paul Bakker9d781402011-05-09 16:17:09 +0000242 return( POLARSSL_ERR_X509_CERT_INVALID_NAME +
Paul Bakker40e46942009-01-03 21:51:57 +0000243 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000244
245 val = &cur->val;
246 val->tag = *(*p)++;
247
248 if( ( ret = asn1_get_len( p, end, &val->len ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000249 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000250
251 val->p = *p;
252 *p += val->len;
253
254 cur->next = NULL;
255
Paul Bakker400ff6f2011-02-20 10:40:16 +0000256 return( 0 );
257}
258
259/*
260 * RelativeDistinguishedName ::=
261 * SET OF AttributeTypeAndValue
262 *
263 * AttributeTypeAndValue ::= SEQUENCE {
264 * type AttributeType,
265 * value AttributeValue }
266 *
267 * AttributeType ::= OBJECT IDENTIFIER
268 *
269 * AttributeValue ::= ANY DEFINED BY AttributeType
270 */
271static int x509_get_name( unsigned char **p,
272 const unsigned char *end,
273 x509_name *cur )
274{
Paul Bakker23986e52011-04-24 08:57:21 +0000275 int ret;
276 size_t len;
Paul Bakker400ff6f2011-02-20 10:40:16 +0000277 const unsigned char *end2;
278 x509_name *use;
279
280 if( ( ret = asn1_get_tag( p, end, &len,
281 ASN1_CONSTRUCTED | ASN1_SET ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000282 return( POLARSSL_ERR_X509_CERT_INVALID_NAME + ret );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000283
284 end2 = end;
285 end = *p + len;
286 use = cur;
287
288 do
289 {
290 if( ( ret = x509_get_attr_type_value( p, end, use ) ) != 0 )
291 return( ret );
292
293 if( *p != end )
294 {
295 use->next = (x509_name *) malloc(
296 sizeof( x509_name ) );
297
298 if( use->next == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +0000299 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker400ff6f2011-02-20 10:40:16 +0000300
301 memset( use->next, 0, sizeof( x509_name ) );
302
303 use = use->next;
304 }
305 }
306 while( *p != end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000307
308 /*
309 * recurse until end of SEQUENCE is reached
310 */
311 if( *p == end2 )
312 return( 0 );
313
314 cur->next = (x509_name *) malloc(
315 sizeof( x509_name ) );
316
317 if( cur->next == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +0000318 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000319
Paul Bakker07156682012-05-30 07:33:30 +0000320 memset( cur->next, 0, sizeof( x509_name ) );
321
Paul Bakker5121ce52009-01-03 21:22:43 +0000322 return( x509_get_name( p, end2, cur->next ) );
323}
324
325/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000326 * Time ::= CHOICE {
327 * utcTime UTCTime,
328 * generalTime GeneralizedTime }
329 */
Paul Bakker91200182010-02-18 21:26:15 +0000330static int x509_get_time( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000331 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000332 x509_time *time )
333{
Paul Bakker23986e52011-04-24 08:57:21 +0000334 int ret;
335 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000336 char date[64];
Paul Bakker91200182010-02-18 21:26:15 +0000337 unsigned char tag;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000338
Paul Bakker91200182010-02-18 21:26:15 +0000339 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000340 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
341 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000342
Paul Bakker91200182010-02-18 21:26:15 +0000343 tag = **p;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000344
Paul Bakker91200182010-02-18 21:26:15 +0000345 if ( tag == ASN1_UTC_TIME )
346 {
347 (*p)++;
348 ret = asn1_get_len( p, end, &len );
349
350 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000351 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000352
Paul Bakker91200182010-02-18 21:26:15 +0000353 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000354 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
355 len : sizeof( date ) - 1 );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000356
Paul Bakker91200182010-02-18 21:26:15 +0000357 if( sscanf( date, "%2d%2d%2d%2d%2d%2d",
358 &time->year, &time->mon, &time->day,
359 &time->hour, &time->min, &time->sec ) < 5 )
360 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000361
Paul Bakker400ff6f2011-02-20 10:40:16 +0000362 time->year += 100 * ( time->year < 50 );
Paul Bakker91200182010-02-18 21:26:15 +0000363 time->year += 1900;
364
365 *p += len;
366
367 return( 0 );
368 }
369 else if ( tag == ASN1_GENERALIZED_TIME )
370 {
371 (*p)++;
372 ret = asn1_get_len( p, end, &len );
373
374 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000375 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker91200182010-02-18 21:26:15 +0000376
377 memset( date, 0, sizeof( date ) );
Paul Bakker27fdf462011-06-09 13:55:13 +0000378 memcpy( date, *p, ( len < sizeof( date ) - 1 ) ?
379 len : sizeof( date ) - 1 );
Paul Bakker91200182010-02-18 21:26:15 +0000380
381 if( sscanf( date, "%4d%2d%2d%2d%2d%2d",
382 &time->year, &time->mon, &time->day,
383 &time->hour, &time->min, &time->sec ) < 5 )
384 return( POLARSSL_ERR_X509_CERT_INVALID_DATE );
385
386 *p += len;
387
388 return( 0 );
389 }
390 else
Paul Bakker9d781402011-05-09 16:17:09 +0000391 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000392}
393
394
395/*
396 * Validity ::= SEQUENCE {
397 * notBefore Time,
398 * notAfter Time }
399 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000400static int x509_get_dates( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000401 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000402 x509_time *from,
403 x509_time *to )
404{
Paul Bakker23986e52011-04-24 08:57:21 +0000405 int ret;
406 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000407
408 if( ( ret = asn1_get_tag( p, end, &len,
409 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000410 return( POLARSSL_ERR_X509_CERT_INVALID_DATE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000411
412 end = *p + len;
413
Paul Bakker91200182010-02-18 21:26:15 +0000414 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000415 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000416
Paul Bakker91200182010-02-18 21:26:15 +0000417 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000418 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000419
420 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000421 return( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker40e46942009-01-03 21:51:57 +0000422 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000423
424 return( 0 );
425}
426
427/*
428 * SubjectPublicKeyInfo ::= SEQUENCE {
429 * algorithm AlgorithmIdentifier,
430 * subjectPublicKey BIT STRING }
431 */
432static int x509_get_pubkey( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000433 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000434 x509_buf *pk_alg_oid,
435 mpi *N, mpi *E )
436{
Paul Bakker23986e52011-04-24 08:57:21 +0000437 int ret, can_handle;
438 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000439 unsigned char *end2;
440
441 if( ( ret = x509_get_alg( p, end, pk_alg_oid ) ) != 0 )
442 return( ret );
443
444 /*
445 * only RSA public keys handled at this time
446 */
Paul Bakker400ff6f2011-02-20 10:40:16 +0000447 can_handle = 0;
448
449 if( pk_alg_oid->len == 9 &&
450 memcmp( pk_alg_oid->p, OID_PKCS1_RSA, 9 ) == 0 )
451 can_handle = 1;
452
453 if( pk_alg_oid->len == 9 &&
454 memcmp( pk_alg_oid->p, OID_PKCS1, 8 ) == 0 )
455 {
456 if( pk_alg_oid->p[8] >= 2 && pk_alg_oid->p[8] <= 5 )
457 can_handle = 1;
458
459 if ( pk_alg_oid->p[8] >= 11 && pk_alg_oid->p[8] <= 14 )
460 can_handle = 1;
461 }
462
463 if( pk_alg_oid->len == 5 &&
464 memcmp( pk_alg_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
465 can_handle = 1;
466
467 if( can_handle == 0 )
Paul Bakkered56b222011-07-13 11:26:43 +0000468 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
Paul Bakker5121ce52009-01-03 21:22:43 +0000469
470 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000471 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000472
473 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000474 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000475 POLARSSL_ERR_ASN1_OUT_OF_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000476
477 end2 = *p + len;
478
479 if( *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000480 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000481
482 /*
483 * RSAPublicKey ::= SEQUENCE {
484 * modulus INTEGER, -- n
485 * publicExponent INTEGER -- e
486 * }
487 */
488 if( ( ret = asn1_get_tag( p, end2, &len,
489 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000490 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
492 if( *p + len != end2 )
Paul Bakker9d781402011-05-09 16:17:09 +0000493 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000494 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000495
496 if( ( ret = asn1_get_mpi( p, end2, N ) ) != 0 ||
497 ( ret = asn1_get_mpi( p, end2, E ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000498 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000499
500 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000501 return( POLARSSL_ERR_X509_CERT_INVALID_PUBKEY +
Paul Bakker40e46942009-01-03 21:51:57 +0000502 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000503
504 return( 0 );
505}
506
507static int x509_get_sig( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000508 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000509 x509_buf *sig )
510{
Paul Bakker23986e52011-04-24 08:57:21 +0000511 int ret;
512 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000513
514 sig->tag = **p;
515
516 if( ( ret = asn1_get_tag( p, end, &len, ASN1_BIT_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000517 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000518
Paul Bakker74111d32011-01-15 16:57:55 +0000519
Paul Bakker5121ce52009-01-03 21:22:43 +0000520 if( --len < 1 || *(*p)++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000521 return( POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000522
523 sig->len = len;
524 sig->p = *p;
525
526 *p += len;
527
528 return( 0 );
529}
530
531/*
532 * X.509 v2/v3 unique identifier (not parsed)
533 */
534static int x509_get_uid( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000535 const unsigned char *end,
Paul Bakker5121ce52009-01-03 21:22:43 +0000536 x509_buf *uid, int n )
537{
538 int ret;
539
540 if( *p == end )
541 return( 0 );
542
543 uid->tag = **p;
544
545 if( ( ret = asn1_get_tag( p, end, &uid->len,
546 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
547 {
Paul Bakker40e46942009-01-03 21:51:57 +0000548 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker5121ce52009-01-03 21:22:43 +0000549 return( 0 );
550
551 return( ret );
552 }
553
554 uid->p = *p;
555 *p += uid->len;
556
557 return( 0 );
558}
559
560/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000561 * X.509 Extensions (No parsing of extensions, pointer should
562 * be either manually updated or extensions should be parsed!
Paul Bakker5121ce52009-01-03 21:22:43 +0000563 */
564static int x509_get_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000565 const unsigned char *end,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000566 x509_buf *ext, int tag )
Paul Bakker5121ce52009-01-03 21:22:43 +0000567{
Paul Bakker23986e52011-04-24 08:57:21 +0000568 int ret;
569 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000570
571 if( *p == end )
572 return( 0 );
573
574 ext->tag = **p;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000575
Paul Bakker5121ce52009-01-03 21:22:43 +0000576 if( ( ret = asn1_get_tag( p, end, &ext->len,
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000577 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | tag ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000578 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000579
580 ext->p = *p;
581 end = *p + ext->len;
582
583 /*
584 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
585 *
586 * Extension ::= SEQUENCE {
587 * extnID OBJECT IDENTIFIER,
588 * critical BOOLEAN DEFAULT FALSE,
589 * extnValue OCTET STRING }
590 */
591 if( ( ret = asn1_get_tag( p, end, &len,
592 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000593 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000594
595 if( end != *p + len )
Paul Bakker9d781402011-05-09 16:17:09 +0000596 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000597 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000598
Paul Bakkerd98030e2009-05-02 15:13:40 +0000599 return( 0 );
600}
601
602/*
603 * X.509 CRL v2 extensions (no extensions parsed yet.)
604 */
605static int x509_get_crl_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000606 const unsigned char *end,
607 x509_buf *ext )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000608{
Paul Bakker23986e52011-04-24 08:57:21 +0000609 int ret;
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000610 size_t len = 0;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000611
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000612 /* Get explicit tag */
613 if( ( ret = x509_get_ext( p, end, ext, 0) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000614 {
615 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
616 return( 0 );
617
618 return( ret );
619 }
620
621 while( *p < end )
622 {
623 if( ( ret = asn1_get_tag( p, end, &len,
624 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000625 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +0000626
627 *p += len;
628 }
629
630 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000631 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerd98030e2009-05-02 15:13:40 +0000632 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
633
634 return( 0 );
635}
636
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000637/*
638 * X.509 CRL v2 entry extensions (no extensions parsed yet.)
639 */
640static int x509_get_crl_entry_ext( unsigned char **p,
641 const unsigned char *end,
642 x509_buf *ext )
643{
644 int ret;
645 size_t len = 0;
646
647 /* OPTIONAL */
648 if (end <= *p)
649 return( 0 );
650
651 ext->tag = **p;
652 ext->p = *p;
653
654 /*
655 * Get CRL-entry extension sequence header
656 * crlEntryExtensions Extensions OPTIONAL -- if present, MUST be v2
657 */
658 if( ( ret = asn1_get_tag( p, end, &ext->len,
659 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
660 {
661 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
662 {
663 ext->p = NULL;
664 return( 0 );
665 }
666 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
667 }
668
669 end = *p + ext->len;
670
671 if( end != *p + ext->len )
672 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
673 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
674
675 while( *p < end )
676 {
677 if( ( ret = asn1_get_tag( p, end, &len,
678 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
679 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
680
681 *p += len;
682 }
683
684 if( *p != end )
685 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
686 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
687
688 return( 0 );
689}
690
Paul Bakker74111d32011-01-15 16:57:55 +0000691static int x509_get_basic_constraints( unsigned char **p,
692 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000693 int *ca_istrue,
694 int *max_pathlen )
695{
Paul Bakker23986e52011-04-24 08:57:21 +0000696 int ret;
697 size_t len;
Paul Bakker74111d32011-01-15 16:57:55 +0000698
699 /*
700 * BasicConstraints ::= SEQUENCE {
701 * cA BOOLEAN DEFAULT FALSE,
702 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
703 */
Paul Bakker3cccddb2011-01-16 21:46:31 +0000704 *ca_istrue = 0; /* DEFAULT FALSE */
Paul Bakker74111d32011-01-15 16:57:55 +0000705 *max_pathlen = 0; /* endless */
706
707 if( ( ret = asn1_get_tag( p, end, &len,
708 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000709 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000710
711 if( *p == end )
712 return 0;
713
Paul Bakker3cccddb2011-01-16 21:46:31 +0000714 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000715 {
716 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker3cccddb2011-01-16 21:46:31 +0000717 ret = asn1_get_int( p, end, ca_istrue );
Paul Bakker74111d32011-01-15 16:57:55 +0000718
719 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000720 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000721
Paul Bakker3cccddb2011-01-16 21:46:31 +0000722 if( *ca_istrue != 0 )
723 *ca_istrue = 1;
Paul Bakker74111d32011-01-15 16:57:55 +0000724 }
725
726 if( *p == end )
727 return 0;
728
729 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000730 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000731
732 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000733 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000734 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
735
736 (*max_pathlen)++;
737
Paul Bakker74111d32011-01-15 16:57:55 +0000738 return 0;
739}
740
741static int x509_get_ns_cert_type( unsigned char **p,
742 const unsigned char *end,
743 unsigned char *ns_cert_type)
744{
745 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000746 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000747
748 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000749 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000750
751 if( bs.len != 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000752 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000753 POLARSSL_ERR_ASN1_INVALID_LENGTH );
754
755 /* Get actual bitstring */
756 *ns_cert_type = *bs.p;
757 return 0;
758}
759
760static int x509_get_key_usage( unsigned char **p,
761 const unsigned char *end,
762 unsigned char *key_usage)
763{
764 int ret;
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000765 x509_bitstring bs = { 0, 0, NULL };
Paul Bakker74111d32011-01-15 16:57:55 +0000766
767 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000768 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000769
Paul Bakkercebdf172011-11-11 15:01:31 +0000770 if( bs.len > 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000771 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000772 POLARSSL_ERR_ASN1_INVALID_LENGTH );
773
774 /* Get actual bitstring */
775 *key_usage = *bs.p;
776 return 0;
777}
778
Paul Bakkerd98030e2009-05-02 15:13:40 +0000779/*
Paul Bakker74111d32011-01-15 16:57:55 +0000780 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
781 *
782 * KeyPurposeId ::= OBJECT IDENTIFIER
783 */
784static int x509_get_ext_key_usage( unsigned char **p,
785 const unsigned char *end,
786 x509_sequence *ext_key_usage)
787{
788 int ret;
789
790 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000791 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker74111d32011-01-15 16:57:55 +0000792
793 /* Sequence length must be >= 1 */
794 if( ext_key_usage->buf.p == NULL )
Paul Bakker9d781402011-05-09 16:17:09 +0000795 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000796 POLARSSL_ERR_ASN1_INVALID_LENGTH );
797
798 return 0;
799}
800
801/*
802 * X.509 v3 extensions
803 *
804 * TODO: Perform all of the basic constraints tests required by the RFC
805 * TODO: Set values for undetected extensions to a sane default?
806 *
Paul Bakkerd98030e2009-05-02 15:13:40 +0000807 */
808static int x509_get_crt_ext( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000809 const unsigned char *end,
Paul Bakker74111d32011-01-15 16:57:55 +0000810 x509_cert *crt )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000811{
Paul Bakker23986e52011-04-24 08:57:21 +0000812 int ret;
813 size_t len;
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000814 unsigned char *end_ext_data, *end_ext_octet;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000815
Paul Bakkerfbc09f32011-10-12 09:56:41 +0000816 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000817 {
818 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
819 return( 0 );
820
821 return( ret );
822 }
823
Paul Bakker5121ce52009-01-03 21:22:43 +0000824 while( *p < end )
825 {
Paul Bakker74111d32011-01-15 16:57:55 +0000826 /*
827 * Extension ::= SEQUENCE {
828 * extnID OBJECT IDENTIFIER,
829 * critical BOOLEAN DEFAULT FALSE,
830 * extnValue OCTET STRING }
831 */
832 x509_buf extn_oid = {0, 0, NULL};
833 int is_critical = 0; /* DEFAULT FALSE */
834
Paul Bakker5121ce52009-01-03 21:22:43 +0000835 if( ( ret = asn1_get_tag( p, end, &len,
836 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000837 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000838
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000839 end_ext_data = *p + len;
840
Paul Bakker74111d32011-01-15 16:57:55 +0000841 /* Get extension ID */
842 extn_oid.tag = **p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000843
Paul Bakker74111d32011-01-15 16:57:55 +0000844 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000845 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000846
Paul Bakker74111d32011-01-15 16:57:55 +0000847 extn_oid.p = *p;
848 *p += extn_oid.len;
849
850 if( ( end - *p ) < 1 )
Paul Bakker9d781402011-05-09 16:17:09 +0000851 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000852 POLARSSL_ERR_ASN1_OUT_OF_DATA );
853
854 /* Get optional critical */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000855 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
Paul Bakker40e46942009-01-03 21:51:57 +0000856 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker9d781402011-05-09 16:17:09 +0000857 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000858
Paul Bakker74111d32011-01-15 16:57:55 +0000859 /* Data should be octet string type */
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000860 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
Paul Bakker5121ce52009-01-03 21:22:43 +0000861 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000862 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000863
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000864 end_ext_octet = *p + len;
Paul Bakkerff60ee62010-03-16 21:09:09 +0000865
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000866 if( end_ext_octet != end_ext_data )
Paul Bakker9d781402011-05-09 16:17:09 +0000867 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakkerc6ce8382009-07-27 21:34:45 +0000868 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000869
Paul Bakker74111d32011-01-15 16:57:55 +0000870 /*
871 * Detect supported extensions
872 */
873 if( ( OID_SIZE( OID_BASIC_CONSTRAINTS ) == extn_oid.len ) &&
874 memcmp( extn_oid.p, OID_BASIC_CONSTRAINTS, extn_oid.len ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000875 {
Paul Bakker74111d32011-01-15 16:57:55 +0000876 /* Parse basic constraints */
877 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
Paul Bakker3cccddb2011-01-16 21:46:31 +0000878 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakker74111d32011-01-15 16:57:55 +0000879 return ( ret );
880 crt->ext_types |= EXT_BASIC_CONSTRAINTS;
Paul Bakker5121ce52009-01-03 21:22:43 +0000881 }
Paul Bakker74111d32011-01-15 16:57:55 +0000882 else if( ( OID_SIZE( OID_NS_CERT_TYPE ) == extn_oid.len ) &&
883 memcmp( extn_oid.p, OID_NS_CERT_TYPE, extn_oid.len ) == 0 )
884 {
885 /* Parse netscape certificate type */
886 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
887 &crt->ns_cert_type ) ) != 0 )
888 return ( ret );
889 crt->ext_types |= EXT_NS_CERT_TYPE;
890 }
891 else if( ( OID_SIZE( OID_KEY_USAGE ) == extn_oid.len ) &&
892 memcmp( extn_oid.p, OID_KEY_USAGE, extn_oid.len ) == 0 )
893 {
894 /* Parse key usage */
895 if( ( ret = x509_get_key_usage( p, end_ext_octet,
896 &crt->key_usage ) ) != 0 )
897 return ( ret );
898 crt->ext_types |= EXT_KEY_USAGE;
899 }
900 else if( ( OID_SIZE( OID_EXTENDED_KEY_USAGE ) == extn_oid.len ) &&
901 memcmp( extn_oid.p, OID_EXTENDED_KEY_USAGE, extn_oid.len ) == 0 )
902 {
903 /* Parse extended key usage */
904 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
905 &crt->ext_key_usage ) ) != 0 )
906 return ( ret );
907 crt->ext_types |= EXT_EXTENDED_KEY_USAGE;
908 }
909 else
910 {
911 /* No parser found, skip extension */
912 *p = end_ext_octet;
Paul Bakker5121ce52009-01-03 21:22:43 +0000913
Paul Bakker5c721f92011-07-27 16:51:09 +0000914#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker74111d32011-01-15 16:57:55 +0000915 if( is_critical )
916 {
917 /* Data is marked as critical: fail */
Paul Bakker9d781402011-05-09 16:17:09 +0000918 return ( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker74111d32011-01-15 16:57:55 +0000919 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
920 }
Paul Bakker5c721f92011-07-27 16:51:09 +0000921#endif
Paul Bakker74111d32011-01-15 16:57:55 +0000922 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000923 }
924
925 if( *p != end )
Paul Bakker9d781402011-05-09 16:17:09 +0000926 return( POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS +
Paul Bakker40e46942009-01-03 21:51:57 +0000927 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +0000928
Paul Bakker5121ce52009-01-03 21:22:43 +0000929 return( 0 );
930}
931
932/*
Paul Bakkerd98030e2009-05-02 15:13:40 +0000933 * X.509 CRL Entries
934 */
935static int x509_get_entries( unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000936 const unsigned char *end,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000937 x509_crl_entry *entry )
938{
Paul Bakker23986e52011-04-24 08:57:21 +0000939 int ret;
940 size_t entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000941 x509_crl_entry *cur_entry = entry;
942
943 if( *p == end )
944 return( 0 );
945
Paul Bakker9be19372009-07-27 20:21:53 +0000946 if( ( ret = asn1_get_tag( p, end, &entry_len,
Paul Bakkerd98030e2009-05-02 15:13:40 +0000947 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
948 {
949 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
950 return( 0 );
951
952 return( ret );
953 }
954
Paul Bakker9be19372009-07-27 20:21:53 +0000955 end = *p + entry_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000956
957 while( *p < end )
958 {
Paul Bakker23986e52011-04-24 08:57:21 +0000959 size_t len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000960 const unsigned char *end2;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000961
962 if( ( ret = asn1_get_tag( p, end, &len2,
963 ASN1_SEQUENCE | ASN1_CONSTRUCTED ) ) != 0 )
964 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000965 return( ret );
966 }
967
Paul Bakker9be19372009-07-27 20:21:53 +0000968 cur_entry->raw.tag = **p;
969 cur_entry->raw.p = *p;
970 cur_entry->raw.len = len2;
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000971 end2 = *p + len2;
Paul Bakker9be19372009-07-27 20:21:53 +0000972
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000973 if( ( ret = x509_get_serial( p, end2, &cur_entry->serial ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000974 return( ret );
975
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000976 if( ( ret = x509_get_time( p, end2, &cur_entry->revocation_date ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000977 return( ret );
978
Paul Bakkerb5a11ab2011-10-12 09:58:41 +0000979 if( ( ret = x509_get_crl_entry_ext( p, end2, &cur_entry->entry_ext ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +0000980 return( ret );
981
Paul Bakker74111d32011-01-15 16:57:55 +0000982 if ( *p < end )
983 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000984 cur_entry->next = malloc( sizeof( x509_crl_entry ) );
Paul Bakkere2e36d32012-01-23 09:56:51 +0000985
986 if( cur_entry->next == NULL )
987 return( POLARSSL_ERR_X509_MALLOC_FAILED );
988
Paul Bakkerd98030e2009-05-02 15:13:40 +0000989 cur_entry = cur_entry->next;
990 memset( cur_entry, 0, sizeof( x509_crl_entry ) );
991 }
992 }
993
994 return( 0 );
995}
996
Paul Bakker27d66162010-03-17 06:56:01 +0000997static int x509_get_sig_alg( const x509_buf *sig_oid, int *sig_alg )
998{
999 if( sig_oid->len == 9 &&
1000 memcmp( sig_oid->p, OID_PKCS1, 8 ) == 0 )
1001 {
1002 if( sig_oid->p[8] >= 2 && sig_oid->p[8] <= 5 )
1003 {
1004 *sig_alg = sig_oid->p[8];
1005 return( 0 );
1006 }
1007
1008 if ( sig_oid->p[8] >= 11 && sig_oid->p[8] <= 14 )
1009 {
1010 *sig_alg = sig_oid->p[8];
1011 return( 0 );
1012 }
1013
1014 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1015 }
Paul Bakker400ff6f2011-02-20 10:40:16 +00001016 if( sig_oid->len == 5 &&
1017 memcmp( sig_oid->p, OID_RSA_SHA_OBS, 5 ) == 0 )
1018 {
1019 *sig_alg = SIG_RSA_SHA1;
1020 return( 0 );
1021 }
Paul Bakker27d66162010-03-17 06:56:01 +00001022
1023 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1024}
1025
Paul Bakkerd98030e2009-05-02 15:13:40 +00001026/*
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001027 * Parse and fill a single X.509 certificate in DER format
Paul Bakker5121ce52009-01-03 21:22:43 +00001028 */
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001029int x509parse_crt_der( x509_cert *crt, const unsigned char *buf, size_t buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001030{
Paul Bakker23986e52011-04-24 08:57:21 +00001031 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001032 size_t len;
Paul Bakker47f62612013-01-14 16:40:55 +01001033 unsigned char *p, *end, *crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001034
Paul Bakker320a4b52009-03-28 18:52:39 +00001035 /*
1036 * Check for valid input
1037 */
1038 if( crt == NULL || buf == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001039 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker320a4b52009-03-28 18:52:39 +00001040
Paul Bakker96743fc2011-02-12 14:30:57 +00001041 p = (unsigned char *) malloc( len = buflen );
1042
1043 if( p == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001044 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001045
1046 memcpy( p, buf, buflen );
1047
1048 buflen = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001049
1050 crt->raw.p = p;
1051 crt->raw.len = len;
1052 end = p + len;
1053
1054 /*
1055 * Certificate ::= SEQUENCE {
1056 * tbsCertificate TBSCertificate,
1057 * signatureAlgorithm AlgorithmIdentifier,
1058 * signatureValue BIT STRING }
1059 */
1060 if( ( ret = asn1_get_tag( &p, end, &len,
1061 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1062 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001063 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001064 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001065 }
1066
Paul Bakker47f62612013-01-14 16:40:55 +01001067 if( len > (size_t) ( end - p ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001068 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001069 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001070 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001071 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001072 }
Paul Bakker47f62612013-01-14 16:40:55 +01001073 crt_end = p + len;
1074
Paul Bakker5121ce52009-01-03 21:22:43 +00001075 /*
1076 * TBSCertificate ::= SEQUENCE {
1077 */
1078 crt->tbs.p = p;
1079
1080 if( ( ret = asn1_get_tag( &p, end, &len,
1081 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1082 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001083 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001084 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001085 }
1086
1087 end = p + len;
1088 crt->tbs.len = end - crt->tbs.p;
1089
1090 /*
1091 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1092 *
1093 * CertificateSerialNumber ::= INTEGER
1094 *
1095 * signature AlgorithmIdentifier
1096 */
1097 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
1098 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1099 ( ret = x509_get_alg( &p, end, &crt->sig_oid1 ) ) != 0 )
1100 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001101 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001102 return( ret );
1103 }
1104
1105 crt->version++;
1106
1107 if( crt->version > 3 )
1108 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001109 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001110 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
Paul Bakker5121ce52009-01-03 21:22:43 +00001111 }
1112
Paul Bakker27d66162010-03-17 06:56:01 +00001113 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_alg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001114 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001115 x509_free( crt );
Paul Bakker27d66162010-03-17 06:56:01 +00001116 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001117 }
1118
1119 /*
1120 * issuer Name
1121 */
1122 crt->issuer_raw.p = p;
1123
1124 if( ( ret = asn1_get_tag( &p, end, &len,
1125 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1126 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001127 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001128 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001129 }
1130
1131 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
1132 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001133 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001134 return( ret );
1135 }
1136
1137 crt->issuer_raw.len = p - crt->issuer_raw.p;
1138
1139 /*
1140 * Validity ::= SEQUENCE {
1141 * notBefore Time,
1142 * notAfter Time }
1143 *
1144 */
1145 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1146 &crt->valid_to ) ) != 0 )
1147 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001148 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001149 return( ret );
1150 }
1151
1152 /*
1153 * subject Name
1154 */
1155 crt->subject_raw.p = p;
1156
1157 if( ( ret = asn1_get_tag( &p, end, &len,
1158 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1159 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001160 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001161 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001162 }
1163
1164 if( ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
1165 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001166 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001167 return( ret );
1168 }
1169
1170 crt->subject_raw.len = p - crt->subject_raw.p;
1171
1172 /*
1173 * SubjectPublicKeyInfo ::= SEQUENCE
1174 * algorithm AlgorithmIdentifier,
1175 * subjectPublicKey BIT STRING }
1176 */
1177 if( ( ret = asn1_get_tag( &p, end, &len,
1178 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1179 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001180 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001181 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001182 }
1183
1184 if( ( ret = x509_get_pubkey( &p, p + len, &crt->pk_oid,
1185 &crt->rsa.N, &crt->rsa.E ) ) != 0 )
1186 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001187 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001188 return( ret );
1189 }
1190
1191 if( ( ret = rsa_check_pubkey( &crt->rsa ) ) != 0 )
1192 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001193 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001194 return( ret );
1195 }
1196
1197 crt->rsa.len = mpi_size( &crt->rsa.N );
1198
1199 /*
1200 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1201 * -- If present, version shall be v2 or v3
1202 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1203 * -- If present, version shall be v2 or v3
1204 * extensions [3] EXPLICIT Extensions OPTIONAL
1205 * -- If present, version shall be v3
1206 */
1207 if( crt->version == 2 || crt->version == 3 )
1208 {
1209 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1210 if( ret != 0 )
1211 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001212 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001213 return( ret );
1214 }
1215 }
1216
1217 if( crt->version == 2 || crt->version == 3 )
1218 {
1219 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1220 if( ret != 0 )
1221 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001222 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001223 return( ret );
1224 }
1225 }
1226
1227 if( crt->version == 3 )
1228 {
Paul Bakker74111d32011-01-15 16:57:55 +00001229 ret = x509_get_crt_ext( &p, end, crt);
Paul Bakker5121ce52009-01-03 21:22:43 +00001230 if( ret != 0 )
1231 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001232 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001233 return( ret );
1234 }
1235 }
1236
1237 if( p != end )
1238 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001239 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001240 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001241 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001242 }
1243
Paul Bakker47f62612013-01-14 16:40:55 +01001244 end = crt_end;
Paul Bakker5121ce52009-01-03 21:22:43 +00001245
1246 /*
1247 * signatureAlgorithm AlgorithmIdentifier,
1248 * signatureValue BIT STRING
1249 */
1250 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2 ) ) != 0 )
1251 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001252 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001253 return( ret );
1254 }
1255
Paul Bakker7261cba2013-01-16 12:39:54 +01001256 if( crt->sig_oid1.len != crt->sig_oid2.len ||
1257 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001258 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001259 x509_free( crt );
Paul Bakker40e46942009-01-03 21:51:57 +00001260 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001261 }
1262
1263 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
1264 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001265 x509_free( crt );
Paul Bakker5121ce52009-01-03 21:22:43 +00001266 return( ret );
1267 }
1268
1269 if( p != end )
1270 {
Paul Bakker7d06ad22009-05-02 15:53:56 +00001271 x509_free( crt );
Paul Bakker9d781402011-05-09 16:17:09 +00001272 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00001273 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00001274 }
1275
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001276 return( 0 );
1277}
1278
1279/*
1280 * Parse one or more PEM certificates from a buffer and add them to the chained list
1281 */
Paul Bakker732e1a82011-12-11 16:35:09 +00001282int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001283{
Paul Bakker732e1a82011-12-11 16:35:09 +00001284 int ret, success = 0, first_error = 0, total_failed = 0;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001285 x509_cert *crt, *prev = NULL;
1286 int buf_format = X509_FORMAT_DER;
1287
1288 crt = chain;
1289
1290 /*
1291 * Check for valid input
1292 */
1293 if( crt == NULL || buf == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001294 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001295
1296 while( crt->version != 0 && crt->next != NULL )
1297 {
1298 prev = crt;
1299 crt = crt->next;
1300 }
1301
1302 /*
1303 * Add new certificate on the end of the chain if needed.
1304 */
1305 if ( crt->version != 0 && crt->next == NULL)
Paul Bakker320a4b52009-03-28 18:52:39 +00001306 {
1307 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1308
Paul Bakker7d06ad22009-05-02 15:53:56 +00001309 if( crt->next == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001310 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker320a4b52009-03-28 18:52:39 +00001311
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001312 prev = crt;
Paul Bakker7d06ad22009-05-02 15:53:56 +00001313 crt = crt->next;
1314 memset( crt, 0, sizeof( x509_cert ) );
Paul Bakker320a4b52009-03-28 18:52:39 +00001315 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001316
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001317 /*
1318 * Determine buffer content. Buffer contains either one DER certificate or
1319 * one or more PEM certificates.
1320 */
1321#if defined(POLARSSL_PEM_C)
1322 if( strstr( (char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1323 buf_format = X509_FORMAT_PEM;
1324#endif
1325
1326 if( buf_format == X509_FORMAT_DER )
1327 return x509parse_crt_der( crt, buf, buflen );
1328
1329#if defined(POLARSSL_PEM_C)
1330 if( buf_format == X509_FORMAT_PEM )
1331 {
1332 pem_context pem;
1333
1334 while( buflen > 0 )
1335 {
1336 size_t use_len;
1337 pem_init( &pem );
1338
1339 ret = pem_read_buffer( &pem,
1340 "-----BEGIN CERTIFICATE-----",
1341 "-----END CERTIFICATE-----",
1342 buf, NULL, 0, &use_len );
1343
1344 if( ret == 0 )
1345 {
1346 /*
1347 * Was PEM encoded
1348 */
1349 buflen -= use_len;
1350 buf += use_len;
1351 }
Paul Bakker721f06d2013-06-19 12:07:42 +02001352 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
1353 {
1354 return( ret );
1355 }
Paul Bakker03a85bc2013-06-19 12:06:00 +02001356 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001357 {
1358 pem_free( &pem );
1359
Paul Bakker721f06d2013-06-19 12:07:42 +02001360 /*
1361 * PEM header and footer were found
1362 */
1363 buflen -= use_len;
1364 buf += use_len;
1365
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001366 if( first_error == 0 )
1367 first_error = ret;
1368
1369 continue;
1370 }
1371 else
1372 break;
1373
1374 ret = x509parse_crt_der( crt, pem.buf, pem.buflen );
1375
1376 pem_free( &pem );
1377
1378 if( ret != 0 )
1379 {
1380 /*
Paul Bakker732e1a82011-12-11 16:35:09 +00001381 * quit parsing on a memory error
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001382 */
Paul Bakker732e1a82011-12-11 16:35:09 +00001383 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001384 {
1385 if( prev )
1386 prev->next = NULL;
1387
1388 if( crt != chain )
1389 free( crt );
1390
1391 return( ret );
1392 }
1393
1394 if( first_error == 0 )
1395 first_error = ret;
Paul Bakker732e1a82011-12-11 16:35:09 +00001396
1397 total_failed++;
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001398
1399 memset( crt, 0, sizeof( x509_cert ) );
1400 continue;
1401 }
1402
1403 success = 1;
1404
1405 /*
1406 * Add new certificate to the list
1407 */
1408 crt->next = (x509_cert *) malloc( sizeof( x509_cert ) );
1409
1410 if( crt->next == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001411 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001412
1413 prev = crt;
1414 crt = crt->next;
1415 memset( crt, 0, sizeof( x509_cert ) );
1416 }
1417 }
1418#endif
1419
1420 if( crt->version == 0 )
1421 {
1422 if( prev )
1423 prev->next = NULL;
1424
1425 if( crt != chain )
1426 free( crt );
1427 }
1428
1429 if( success )
Paul Bakker732e1a82011-12-11 16:35:09 +00001430 return( total_failed );
Paul Bakker6c0ceb32011-12-04 12:24:18 +00001431 else if( first_error )
1432 return( first_error );
1433 else
1434 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker5121ce52009-01-03 21:22:43 +00001435}
1436
1437/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00001438 * Parse one or more CRLs and add them to the chained list
1439 */
Paul Bakker23986e52011-04-24 08:57:21 +00001440int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001441{
Paul Bakker23986e52011-04-24 08:57:21 +00001442 int ret;
Paul Bakker5690efc2011-05-26 13:16:06 +00001443 size_t len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001444 unsigned char *p, *end;
1445 x509_crl *crl;
Paul Bakker96743fc2011-02-12 14:30:57 +00001446#if defined(POLARSSL_PEM_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001447 size_t use_len;
Paul Bakker96743fc2011-02-12 14:30:57 +00001448 pem_context pem;
1449#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001450
1451 crl = chain;
1452
1453 /*
1454 * Check for valid input
1455 */
1456 if( crl == NULL || buf == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001457 return( POLARSSL_ERR_X509_INVALID_INPUT );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001458
1459 while( crl->version != 0 && crl->next != NULL )
1460 crl = crl->next;
1461
1462 /*
1463 * Add new CRL on the end of the chain if needed.
1464 */
1465 if ( crl->version != 0 && crl->next == NULL)
1466 {
1467 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1468
Paul Bakker7d06ad22009-05-02 15:53:56 +00001469 if( crl->next == NULL )
1470 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001471 x509_crl_free( crl );
Paul Bakker732e1a82011-12-11 16:35:09 +00001472 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001473 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001474
Paul Bakker7d06ad22009-05-02 15:53:56 +00001475 crl = crl->next;
1476 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001477 }
1478
Paul Bakker96743fc2011-02-12 14:30:57 +00001479#if defined(POLARSSL_PEM_C)
1480 pem_init( &pem );
1481 ret = pem_read_buffer( &pem,
1482 "-----BEGIN X509 CRL-----",
1483 "-----END X509 CRL-----",
1484 buf, NULL, 0, &use_len );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001485
Paul Bakker96743fc2011-02-12 14:30:57 +00001486 if( ret == 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001487 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001488 /*
1489 * Was PEM encoded
1490 */
1491 buflen -= use_len;
1492 buf += use_len;
Paul Bakkerd98030e2009-05-02 15:13:40 +00001493
1494 /*
Paul Bakker96743fc2011-02-12 14:30:57 +00001495 * Steal PEM buffer
Paul Bakkerd98030e2009-05-02 15:13:40 +00001496 */
Paul Bakker96743fc2011-02-12 14:30:57 +00001497 p = pem.buf;
1498 pem.buf = NULL;
1499 len = pem.buflen;
1500 pem_free( &pem );
1501 }
Paul Bakker03a85bc2013-06-19 12:06:00 +02001502 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker96743fc2011-02-12 14:30:57 +00001503 {
1504 pem_free( &pem );
1505 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001506 }
1507 else
1508 {
1509 /*
1510 * nope, copy the raw DER data
1511 */
1512 p = (unsigned char *) malloc( len = buflen );
1513
1514 if( p == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001515 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001516
1517 memcpy( p, buf, buflen );
1518
1519 buflen = 0;
1520 }
Paul Bakker96743fc2011-02-12 14:30:57 +00001521#else
1522 p = (unsigned char *) malloc( len = buflen );
1523
1524 if( p == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001525 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker96743fc2011-02-12 14:30:57 +00001526
1527 memcpy( p, buf, buflen );
1528
1529 buflen = 0;
1530#endif
Paul Bakkerd98030e2009-05-02 15:13:40 +00001531
1532 crl->raw.p = p;
1533 crl->raw.len = len;
1534 end = p + len;
1535
1536 /*
1537 * CertificateList ::= SEQUENCE {
1538 * tbsCertList TBSCertList,
1539 * signatureAlgorithm AlgorithmIdentifier,
1540 * signatureValue BIT STRING }
1541 */
1542 if( ( ret = asn1_get_tag( &p, end, &len,
1543 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1544 {
1545 x509_crl_free( crl );
1546 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT );
1547 }
1548
Paul Bakker23986e52011-04-24 08:57:21 +00001549 if( len != (size_t) ( end - p ) )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001550 {
1551 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001552 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001553 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1554 }
1555
1556 /*
1557 * TBSCertList ::= SEQUENCE {
1558 */
1559 crl->tbs.p = p;
1560
1561 if( ( ret = asn1_get_tag( &p, end, &len,
1562 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1563 {
1564 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001565 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001566 }
1567
1568 end = p + len;
1569 crl->tbs.len = end - crl->tbs.p;
1570
1571 /*
1572 * Version ::= INTEGER OPTIONAL { v1(0), v2(1) }
1573 * -- if present, MUST be v2
1574 *
1575 * signature AlgorithmIdentifier
1576 */
Paul Bakker3329d1f2011-10-12 09:55:01 +00001577 if( ( ret = x509_crl_get_version( &p, end, &crl->version ) ) != 0 ||
Paul Bakkerd98030e2009-05-02 15:13:40 +00001578 ( ret = x509_get_alg( &p, end, &crl->sig_oid1 ) ) != 0 )
1579 {
1580 x509_crl_free( crl );
1581 return( ret );
1582 }
1583
1584 crl->version++;
1585
1586 if( crl->version > 2 )
1587 {
1588 x509_crl_free( crl );
1589 return( POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION );
1590 }
1591
Paul Bakker27d66162010-03-17 06:56:01 +00001592 if( ( ret = x509_get_sig_alg( &crl->sig_oid1, &crl->sig_alg ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001593 {
1594 x509_crl_free( crl );
1595 return( POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG );
1596 }
1597
1598 /*
1599 * issuer Name
1600 */
1601 crl->issuer_raw.p = p;
1602
1603 if( ( ret = asn1_get_tag( &p, end, &len,
1604 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1605 {
1606 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001607 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001608 }
1609
1610 if( ( ret = x509_get_name( &p, p + len, &crl->issuer ) ) != 0 )
1611 {
1612 x509_crl_free( crl );
1613 return( ret );
1614 }
1615
1616 crl->issuer_raw.len = p - crl->issuer_raw.p;
1617
1618 /*
1619 * thisUpdate Time
1620 * nextUpdate Time OPTIONAL
1621 */
Paul Bakker91200182010-02-18 21:26:15 +00001622 if( ( ret = x509_get_time( &p, end, &crl->this_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001623 {
1624 x509_crl_free( crl );
1625 return( ret );
1626 }
1627
Paul Bakker91200182010-02-18 21:26:15 +00001628 if( ( ret = x509_get_time( &p, end, &crl->next_update ) ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001629 {
Paul Bakker9d781402011-05-09 16:17:09 +00001630 if ( ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001631 POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) &&
Paul Bakker9d781402011-05-09 16:17:09 +00001632 ret != ( POLARSSL_ERR_X509_CERT_INVALID_DATE +
Paul Bakker9be19372009-07-27 20:21:53 +00001633 POLARSSL_ERR_ASN1_OUT_OF_DATA ) )
Paul Bakker635f4b42009-07-20 20:34:41 +00001634 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001635 x509_crl_free( crl );
1636 return( ret );
1637 }
1638 }
1639
1640 /*
1641 * revokedCertificates SEQUENCE OF SEQUENCE {
1642 * userCertificate CertificateSerialNumber,
1643 * revocationDate Time,
1644 * crlEntryExtensions Extensions OPTIONAL
1645 * -- if present, MUST be v2
1646 * } OPTIONAL
1647 */
1648 if( ( ret = x509_get_entries( &p, end, &crl->entry ) ) != 0 )
1649 {
1650 x509_crl_free( crl );
1651 return( ret );
1652 }
1653
1654 /*
1655 * crlExtensions EXPLICIT Extensions OPTIONAL
1656 * -- if present, MUST be v2
1657 */
1658 if( crl->version == 2 )
1659 {
1660 ret = x509_get_crl_ext( &p, end, &crl->crl_ext );
1661
1662 if( ret != 0 )
1663 {
1664 x509_crl_free( crl );
1665 return( ret );
1666 }
1667 }
1668
1669 if( p != end )
1670 {
1671 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001672 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001673 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1674 }
1675
1676 end = crl->raw.p + crl->raw.len;
1677
1678 /*
1679 * signatureAlgorithm AlgorithmIdentifier,
1680 * signatureValue BIT STRING
1681 */
1682 if( ( ret = x509_get_alg( &p, end, &crl->sig_oid2 ) ) != 0 )
1683 {
1684 x509_crl_free( crl );
1685 return( ret );
1686 }
1687
Paul Bakker7261cba2013-01-16 12:39:54 +01001688 if( crl->sig_oid1.len != crl->sig_oid2.len ||
1689 memcmp( crl->sig_oid1.p, crl->sig_oid2.p, crl->sig_oid1.len ) != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001690 {
1691 x509_crl_free( crl );
1692 return( POLARSSL_ERR_X509_CERT_SIG_MISMATCH );
1693 }
1694
1695 if( ( ret = x509_get_sig( &p, end, &crl->sig ) ) != 0 )
1696 {
1697 x509_crl_free( crl );
1698 return( ret );
1699 }
1700
1701 if( p != end )
1702 {
1703 x509_crl_free( crl );
Paul Bakker9d781402011-05-09 16:17:09 +00001704 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT +
Paul Bakkerd98030e2009-05-02 15:13:40 +00001705 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
1706 }
1707
1708 if( buflen > 0 )
1709 {
1710 crl->next = (x509_crl *) malloc( sizeof( x509_crl ) );
1711
Paul Bakker7d06ad22009-05-02 15:53:56 +00001712 if( crl->next == NULL )
1713 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00001714 x509_crl_free( crl );
Paul Bakker732e1a82011-12-11 16:35:09 +00001715 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker7d06ad22009-05-02 15:53:56 +00001716 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00001717
Paul Bakker7d06ad22009-05-02 15:53:56 +00001718 crl = crl->next;
1719 memset( crl, 0, sizeof( x509_crl ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001720
1721 return( x509parse_crl( crl, buf, buflen ) );
1722 }
1723
1724 return( 0 );
1725}
1726
Paul Bakker335db3f2011-04-25 15:28:35 +00001727#if defined(POLARSSL_FS_IO)
Paul Bakkerd98030e2009-05-02 15:13:40 +00001728/*
Paul Bakker2b245eb2009-04-19 18:44:26 +00001729 * Load all data from a file into a given buffer.
1730 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001731int load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker2b245eb2009-04-19 18:44:26 +00001732{
Paul Bakkerd98030e2009-05-02 15:13:40 +00001733 FILE *f;
Paul Bakker2b245eb2009-04-19 18:44:26 +00001734
Paul Bakkerd98030e2009-05-02 15:13:40 +00001735 if( ( f = fopen( path, "rb" ) ) == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001736 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001737
Paul Bakkerd98030e2009-05-02 15:13:40 +00001738 fseek( f, 0, SEEK_END );
1739 *n = (size_t) ftell( f );
1740 fseek( f, 0, SEEK_SET );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001741
Paul Bakkerd98030e2009-05-02 15:13:40 +00001742 if( ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
Paul Bakker732e1a82011-12-11 16:35:09 +00001743 return( POLARSSL_ERR_X509_MALLOC_FAILED );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001744
Paul Bakkerd98030e2009-05-02 15:13:40 +00001745 if( fread( *buf, 1, *n, f ) != *n )
1746 {
1747 fclose( f );
1748 free( *buf );
Paul Bakker732e1a82011-12-11 16:35:09 +00001749 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001750 }
Paul Bakker2b245eb2009-04-19 18:44:26 +00001751
Paul Bakkerd98030e2009-05-02 15:13:40 +00001752 fclose( f );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001753
Paul Bakkerd98030e2009-05-02 15:13:40 +00001754 (*buf)[*n] = '\0';
Paul Bakker2b245eb2009-04-19 18:44:26 +00001755
Paul Bakkerd98030e2009-05-02 15:13:40 +00001756 return( 0 );
Paul Bakker2b245eb2009-04-19 18:44:26 +00001757}
1758
1759/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001760 * Load one or more certificates and add them to the chained list
1761 */
Paul Bakker732e1a82011-12-11 16:35:09 +00001762int x509parse_crtfile( x509_cert *chain, const char *path )
Paul Bakker5121ce52009-01-03 21:22:43 +00001763{
1764 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001765 size_t n;
1766 unsigned char *buf;
1767
Paul Bakker732e1a82011-12-11 16:35:09 +00001768 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1769 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001770
Paul Bakker732e1a82011-12-11 16:35:09 +00001771 ret = x509parse_crt( chain, buf, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001772
1773 memset( buf, 0, n + 1 );
1774 free( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001775
1776 return( ret );
1777}
1778
Paul Bakkerd98030e2009-05-02 15:13:40 +00001779/*
1780 * Load one or more CRLs and add them to the chained list
1781 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00001782int x509parse_crlfile( x509_crl *chain, const char *path )
Paul Bakkerd98030e2009-05-02 15:13:40 +00001783{
1784 int ret;
1785 size_t n;
1786 unsigned char *buf;
1787
Paul Bakker732e1a82011-12-11 16:35:09 +00001788 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1789 return( ret );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001790
Paul Bakker27fdf462011-06-09 13:55:13 +00001791 ret = x509parse_crl( chain, buf, n );
Paul Bakkerd98030e2009-05-02 15:13:40 +00001792
1793 memset( buf, 0, n + 1 );
1794 free( buf );
1795
1796 return( ret );
1797}
1798
Paul Bakker5121ce52009-01-03 21:22:43 +00001799/*
Paul Bakker335db3f2011-04-25 15:28:35 +00001800 * Load and parse a private RSA key
1801 */
1802int x509parse_keyfile( rsa_context *rsa, const char *path, const char *pwd )
1803{
1804 int ret;
1805 size_t n;
1806 unsigned char *buf;
1807
Paul Bakker732e1a82011-12-11 16:35:09 +00001808 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1809 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001810
1811 if( pwd == NULL )
Paul Bakker27fdf462011-06-09 13:55:13 +00001812 ret = x509parse_key( rsa, buf, n, NULL, 0 );
Paul Bakker335db3f2011-04-25 15:28:35 +00001813 else
Paul Bakker27fdf462011-06-09 13:55:13 +00001814 ret = x509parse_key( rsa, buf, n,
Paul Bakker335db3f2011-04-25 15:28:35 +00001815 (unsigned char *) pwd, strlen( pwd ) );
1816
1817 memset( buf, 0, n + 1 );
1818 free( buf );
1819
1820 return( ret );
1821}
1822
1823/*
1824 * Load and parse a public RSA key
1825 */
1826int x509parse_public_keyfile( rsa_context *rsa, const char *path )
1827{
1828 int ret;
1829 size_t n;
1830 unsigned char *buf;
1831
Paul Bakker732e1a82011-12-11 16:35:09 +00001832 if ( (ret = load_file( path, &buf, &n ) ) != 0 )
1833 return( ret );
Paul Bakker335db3f2011-04-25 15:28:35 +00001834
Paul Bakker27fdf462011-06-09 13:55:13 +00001835 ret = x509parse_public_key( rsa, buf, n );
Paul Bakker335db3f2011-04-25 15:28:35 +00001836
1837 memset( buf, 0, n + 1 );
1838 free( buf );
1839
1840 return( ret );
1841}
1842#endif /* POLARSSL_FS_IO */
1843
1844/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001845 * Parse a private RSA key
1846 */
Paul Bakker23986e52011-04-24 08:57:21 +00001847int x509parse_key( rsa_context *rsa, const unsigned char *key, size_t keylen,
1848 const unsigned char *pwd, size_t pwdlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001849{
Paul Bakker23986e52011-04-24 08:57:21 +00001850 int ret;
1851 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001852 unsigned char *p, *end;
Paul Bakkered56b222011-07-13 11:26:43 +00001853 unsigned char *p_alt;
1854 x509_buf pk_alg_oid;
1855
Paul Bakker96743fc2011-02-12 14:30:57 +00001856#if defined(POLARSSL_PEM_C)
1857 pem_context pem;
Paul Bakker5121ce52009-01-03 21:22:43 +00001858
Paul Bakker96743fc2011-02-12 14:30:57 +00001859 pem_init( &pem );
1860 ret = pem_read_buffer( &pem,
1861 "-----BEGIN RSA PRIVATE KEY-----",
1862 "-----END RSA PRIVATE KEY-----",
1863 key, pwd, pwdlen, &len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001864
Paul Bakker03a85bc2013-06-19 12:06:00 +02001865 if( ret == POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkered56b222011-07-13 11:26:43 +00001866 {
1867 ret = pem_read_buffer( &pem,
1868 "-----BEGIN PRIVATE KEY-----",
1869 "-----END PRIVATE KEY-----",
1870 key, pwd, pwdlen, &len );
1871 }
1872
Paul Bakker96743fc2011-02-12 14:30:57 +00001873 if( ret == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001874 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001875 /*
1876 * Was PEM encoded
1877 */
1878 keylen = pem.buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001879 }
Paul Bakker03a85bc2013-06-19 12:06:00 +02001880 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakkerff60ee62010-03-16 21:09:09 +00001881 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001882 pem_free( &pem );
1883 return( ret );
Paul Bakkerff60ee62010-03-16 21:09:09 +00001884 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001885
Paul Bakker96743fc2011-02-12 14:30:57 +00001886 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
1887#else
Paul Bakker5690efc2011-05-26 13:16:06 +00001888 ((void) pwd);
1889 ((void) pwdlen);
Paul Bakker96743fc2011-02-12 14:30:57 +00001890 p = (unsigned char *) key;
1891#endif
1892 end = p + keylen;
1893
Paul Bakker5121ce52009-01-03 21:22:43 +00001894 /*
Paul Bakkered56b222011-07-13 11:26:43 +00001895 * Note: Depending on the type of private key file one can expect either a
1896 * PrivatKeyInfo object (PKCS#8) or a RSAPrivateKey (PKCS#1) directly.
1897 *
1898 * PrivateKeyInfo ::= SEQUENCE {
Paul Bakker5c721f92011-07-27 16:51:09 +00001899 * version Version,
Paul Bakkered56b222011-07-13 11:26:43 +00001900 * algorithm AlgorithmIdentifier,
1901 * PrivateKey BIT STRING
1902 * }
1903 *
1904 * AlgorithmIdentifier ::= SEQUENCE {
1905 * algorithm OBJECT IDENTIFIER,
1906 * parameters ANY DEFINED BY algorithm OPTIONAL
1907 * }
1908 *
Paul Bakker5121ce52009-01-03 21:22:43 +00001909 * RSAPrivateKey ::= SEQUENCE {
1910 * version Version,
1911 * modulus INTEGER, -- n
1912 * publicExponent INTEGER, -- e
1913 * privateExponent INTEGER, -- d
1914 * prime1 INTEGER, -- p
1915 * prime2 INTEGER, -- q
1916 * exponent1 INTEGER, -- d mod (p-1)
1917 * exponent2 INTEGER, -- d mod (q-1)
1918 * coefficient INTEGER, -- (inverse of q) mod p
1919 * otherPrimeInfos OtherPrimeInfos OPTIONAL
1920 * }
1921 */
1922 if( ( ret = asn1_get_tag( &p, end, &len,
1923 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1924 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001925#if defined(POLARSSL_PEM_C)
1926 pem_free( &pem );
1927#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001928 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001929 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001930 }
1931
1932 end = p + len;
1933
1934 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
1935 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001936#if defined(POLARSSL_PEM_C)
1937 pem_free( &pem );
1938#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001939 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001940 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001941 }
1942
1943 if( rsa->ver != 0 )
1944 {
Paul Bakker96743fc2011-02-12 14:30:57 +00001945#if defined(POLARSSL_PEM_C)
1946 pem_free( &pem );
1947#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001948 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00001949 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001950 }
1951
Paul Bakkered56b222011-07-13 11:26:43 +00001952 p_alt = p;
1953
1954 if( ( ret = x509_get_alg( &p_alt, end, &pk_alg_oid ) ) != 0 )
1955 {
1956 // Assume that we have the PKCS#1 format if wrong
1957 // tag was encountered
1958 //
1959 if( ret != POLARSSL_ERR_X509_CERT_INVALID_ALG +
1960 POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
1961 {
1962#if defined(POLARSSL_PEM_C)
1963 pem_free( &pem );
1964#endif
1965 rsa_free( rsa );
1966 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
1967 }
1968 }
1969 else
1970 {
1971 int can_handle;
1972
1973 /*
1974 * only RSA keys handled at this time
1975 */
1976 can_handle = 0;
1977
1978 if( pk_alg_oid.len == 9 &&
1979 memcmp( pk_alg_oid.p, OID_PKCS1_RSA, 9 ) == 0 )
1980 can_handle = 1;
1981
1982 if( pk_alg_oid.len == 9 &&
1983 memcmp( pk_alg_oid.p, OID_PKCS1, 8 ) == 0 )
1984 {
1985 if( pk_alg_oid.p[8] >= 2 && pk_alg_oid.p[8] <= 5 )
1986 can_handle = 1;
1987
1988 if ( pk_alg_oid.p[8] >= 11 && pk_alg_oid.p[8] <= 14 )
1989 can_handle = 1;
1990 }
1991
1992 if( pk_alg_oid.len == 5 &&
1993 memcmp( pk_alg_oid.p, OID_RSA_SHA_OBS, 5 ) == 0 )
1994 can_handle = 1;
1995
1996 if( can_handle == 0 )
1997 return( POLARSSL_ERR_X509_UNKNOWN_PK_ALG );
1998
1999 /*
2000 * Parse the PKCS#8 format
2001 */
2002
2003 p = p_alt;
2004 if( ( ret = asn1_get_tag( &p, end, &len, ASN1_OCTET_STRING ) ) != 0 )
2005 {
2006#if defined(POLARSSL_PEM_C)
2007 pem_free( &pem );
2008#endif
2009 rsa_free( rsa );
2010 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2011 }
2012
2013 if( ( end - p ) < 1 )
2014 {
2015#if defined(POLARSSL_PEM_C)
2016 pem_free( &pem );
2017#endif
2018 rsa_free( rsa );
2019 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
2020 POLARSSL_ERR_ASN1_OUT_OF_DATA );
2021 }
2022
2023 end = p + len;
2024
2025 if( ( ret = asn1_get_tag( &p, end, &len,
2026 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2027 {
2028#if defined(POLARSSL_PEM_C)
2029 pem_free( &pem );
2030#endif
2031 rsa_free( rsa );
2032 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2033 }
2034
2035 end = p + len;
2036
2037 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
2038 {
2039#if defined(POLARSSL_PEM_C)
2040 pem_free( &pem );
2041#endif
2042 rsa_free( rsa );
2043 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
2044 }
2045
2046 if( rsa->ver != 0 )
2047 {
2048#if defined(POLARSSL_PEM_C)
2049 pem_free( &pem );
2050#endif
2051 rsa_free( rsa );
2052 return( POLARSSL_ERR_X509_KEY_INVALID_VERSION + ret );
2053 }
2054 }
2055
Paul Bakker5121ce52009-01-03 21:22:43 +00002056 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
2057 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
2058 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
2059 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
2060 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
2061 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
2062 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
2063 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
2064 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002065#if defined(POLARSSL_PEM_C)
2066 pem_free( &pem );
2067#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002068 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002069 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002070 }
2071
2072 rsa->len = mpi_size( &rsa->N );
2073
2074 if( p != end )
2075 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002076#if defined(POLARSSL_PEM_C)
2077 pem_free( &pem );
2078#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002079 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002080 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker40e46942009-01-03 21:51:57 +00002081 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker5121ce52009-01-03 21:22:43 +00002082 }
2083
2084 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )
2085 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002086#if defined(POLARSSL_PEM_C)
2087 pem_free( &pem );
2088#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002089 rsa_free( rsa );
2090 return( ret );
2091 }
2092
Paul Bakker96743fc2011-02-12 14:30:57 +00002093#if defined(POLARSSL_PEM_C)
2094 pem_free( &pem );
2095#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002096
2097 return( 0 );
2098}
2099
2100/*
Paul Bakker53019ae2011-03-25 13:58:48 +00002101 * Parse a public RSA key
2102 */
Paul Bakker23986e52011-04-24 08:57:21 +00002103int x509parse_public_key( rsa_context *rsa, const unsigned char *key, size_t keylen )
Paul Bakker53019ae2011-03-25 13:58:48 +00002104{
Paul Bakker23986e52011-04-24 08:57:21 +00002105 int ret;
2106 size_t len;
Paul Bakker53019ae2011-03-25 13:58:48 +00002107 unsigned char *p, *end;
2108 x509_buf alg_oid;
2109#if defined(POLARSSL_PEM_C)
2110 pem_context pem;
2111
2112 pem_init( &pem );
2113 ret = pem_read_buffer( &pem,
2114 "-----BEGIN PUBLIC KEY-----",
2115 "-----END PUBLIC KEY-----",
2116 key, NULL, 0, &len );
2117
2118 if( ret == 0 )
2119 {
2120 /*
2121 * Was PEM encoded
2122 */
2123 keylen = pem.buflen;
2124 }
Paul Bakker03a85bc2013-06-19 12:06:00 +02002125 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker53019ae2011-03-25 13:58:48 +00002126 {
2127 pem_free( &pem );
2128 return( ret );
2129 }
2130
2131 p = ( ret == 0 ) ? pem.buf : (unsigned char *) key;
2132#else
2133 p = (unsigned char *) key;
2134#endif
2135 end = p + keylen;
2136
2137 /*
2138 * PublicKeyInfo ::= SEQUENCE {
2139 * algorithm AlgorithmIdentifier,
2140 * PublicKey BIT STRING
2141 * }
2142 *
2143 * AlgorithmIdentifier ::= SEQUENCE {
2144 * algorithm OBJECT IDENTIFIER,
2145 * parameters ANY DEFINED BY algorithm OPTIONAL
2146 * }
2147 *
2148 * RSAPublicKey ::= SEQUENCE {
2149 * modulus INTEGER, -- n
2150 * publicExponent INTEGER -- e
2151 * }
2152 */
2153
2154 if( ( ret = asn1_get_tag( &p, end, &len,
2155 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2156 {
2157#if defined(POLARSSL_PEM_C)
2158 pem_free( &pem );
2159#endif
2160 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002161 return( POLARSSL_ERR_X509_CERT_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002162 }
2163
2164 if( ( ret = x509_get_pubkey( &p, end, &alg_oid, &rsa->N, &rsa->E ) ) != 0 )
2165 {
2166#if defined(POLARSSL_PEM_C)
2167 pem_free( &pem );
2168#endif
2169 rsa_free( rsa );
Paul Bakker9d781402011-05-09 16:17:09 +00002170 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker53019ae2011-03-25 13:58:48 +00002171 }
2172
2173 if( ( ret = rsa_check_pubkey( rsa ) ) != 0 )
2174 {
2175#if defined(POLARSSL_PEM_C)
2176 pem_free( &pem );
2177#endif
2178 rsa_free( rsa );
2179 return( ret );
2180 }
2181
2182 rsa->len = mpi_size( &rsa->N );
2183
2184#if defined(POLARSSL_PEM_C)
2185 pem_free( &pem );
2186#endif
2187
2188 return( 0 );
2189}
2190
Paul Bakkereaa89f82011-04-04 21:36:15 +00002191#if defined(POLARSSL_DHM_C)
Paul Bakker53019ae2011-03-25 13:58:48 +00002192/*
Paul Bakker1b57b062011-01-06 15:48:19 +00002193 * Parse DHM parameters
2194 */
Paul Bakker23986e52011-04-24 08:57:21 +00002195int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
Paul Bakker1b57b062011-01-06 15:48:19 +00002196{
Paul Bakker23986e52011-04-24 08:57:21 +00002197 int ret;
2198 size_t len;
Paul Bakker1b57b062011-01-06 15:48:19 +00002199 unsigned char *p, *end;
Paul Bakker96743fc2011-02-12 14:30:57 +00002200#if defined(POLARSSL_PEM_C)
2201 pem_context pem;
Paul Bakker1b57b062011-01-06 15:48:19 +00002202
Paul Bakker96743fc2011-02-12 14:30:57 +00002203 pem_init( &pem );
Paul Bakker1b57b062011-01-06 15:48:19 +00002204
Paul Bakker96743fc2011-02-12 14:30:57 +00002205 ret = pem_read_buffer( &pem,
2206 "-----BEGIN DH PARAMETERS-----",
2207 "-----END DH PARAMETERS-----",
2208 dhmin, NULL, 0, &dhminlen );
2209
2210 if( ret == 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00002211 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002212 /*
2213 * Was PEM encoded
2214 */
2215 dhminlen = pem.buflen;
Paul Bakker1b57b062011-01-06 15:48:19 +00002216 }
Paul Bakker03a85bc2013-06-19 12:06:00 +02002217 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1b57b062011-01-06 15:48:19 +00002218 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002219 pem_free( &pem );
2220 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002221 }
2222
Paul Bakker96743fc2011-02-12 14:30:57 +00002223 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
2224#else
2225 p = (unsigned char *) dhmin;
2226#endif
2227 end = p + dhminlen;
2228
Paul Bakker1b57b062011-01-06 15:48:19 +00002229 memset( dhm, 0, sizeof( dhm_context ) );
2230
Paul Bakker1b57b062011-01-06 15:48:19 +00002231 /*
2232 * DHParams ::= SEQUENCE {
2233 * prime INTEGER, -- P
2234 * generator INTEGER, -- g
2235 * }
2236 */
2237 if( ( ret = asn1_get_tag( &p, end, &len,
2238 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
2239 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002240#if defined(POLARSSL_PEM_C)
2241 pem_free( &pem );
2242#endif
Paul Bakker9d781402011-05-09 16:17:09 +00002243 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002244 }
2245
2246 end = p + len;
2247
2248 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
2249 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
2250 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002251#if defined(POLARSSL_PEM_C)
2252 pem_free( &pem );
2253#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002254 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002255 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT + ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002256 }
2257
2258 if( p != end )
2259 {
Paul Bakker96743fc2011-02-12 14:30:57 +00002260#if defined(POLARSSL_PEM_C)
2261 pem_free( &pem );
2262#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002263 dhm_free( dhm );
Paul Bakker9d781402011-05-09 16:17:09 +00002264 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT +
Paul Bakker1b57b062011-01-06 15:48:19 +00002265 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
2266 }
2267
Paul Bakker96743fc2011-02-12 14:30:57 +00002268#if defined(POLARSSL_PEM_C)
2269 pem_free( &pem );
2270#endif
Paul Bakker1b57b062011-01-06 15:48:19 +00002271
2272 return( 0 );
2273}
2274
Paul Bakker335db3f2011-04-25 15:28:35 +00002275#if defined(POLARSSL_FS_IO)
Paul Bakker1b57b062011-01-06 15:48:19 +00002276/*
2277 * Load and parse a private RSA key
2278 */
2279int x509parse_dhmfile( dhm_context *dhm, const char *path )
2280{
2281 int ret;
2282 size_t n;
2283 unsigned char *buf;
2284
Paul Bakker732e1a82011-12-11 16:35:09 +00002285 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
2286 return( ret );
Paul Bakker1b57b062011-01-06 15:48:19 +00002287
Paul Bakker27fdf462011-06-09 13:55:13 +00002288 ret = x509parse_dhm( dhm, buf, n );
Paul Bakker1b57b062011-01-06 15:48:19 +00002289
2290 memset( buf, 0, n + 1 );
2291 free( buf );
2292
2293 return( ret );
2294}
Paul Bakker335db3f2011-04-25 15:28:35 +00002295#endif /* POLARSSL_FS_IO */
Paul Bakkereaa89f82011-04-04 21:36:15 +00002296#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002297
Paul Bakker5121ce52009-01-03 21:22:43 +00002298#if defined _MSC_VER && !defined snprintf
Paul Bakkerd98030e2009-05-02 15:13:40 +00002299#include <stdarg.h>
2300
2301#if !defined vsnprintf
2302#define vsnprintf _vsnprintf
2303#endif // vsnprintf
2304
2305/*
2306 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
2307 * Result value is not size of buffer needed, but -1 if no fit is possible.
2308 *
2309 * This fuction tries to 'fix' this by at least suggesting enlarging the
2310 * size by 20.
2311 */
2312int compat_snprintf(char *str, size_t size, const char *format, ...)
2313{
2314 va_list ap;
2315 int res = -1;
2316
2317 va_start( ap, format );
2318
2319 res = vsnprintf( str, size, format, ap );
2320
2321 va_end( ap );
2322
2323 // No quick fix possible
2324 if ( res < 0 )
Paul Bakker23986e52011-04-24 08:57:21 +00002325 return( (int) size + 20 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002326
2327 return res;
2328}
2329
2330#define snprintf compat_snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +00002331#endif
2332
Paul Bakkerd98030e2009-05-02 15:13:40 +00002333#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
2334
2335#define SAFE_SNPRINTF() \
2336{ \
2337 if( ret == -1 ) \
2338 return( -1 ); \
2339 \
Paul Bakker23986e52011-04-24 08:57:21 +00002340 if ( (unsigned int) ret > n ) { \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002341 p[n - 1] = '\0'; \
2342 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
2343 } \
2344 \
Paul Bakker23986e52011-04-24 08:57:21 +00002345 n -= (unsigned int) ret; \
2346 p += (unsigned int) ret; \
Paul Bakkerd98030e2009-05-02 15:13:40 +00002347}
2348
Paul Bakker5121ce52009-01-03 21:22:43 +00002349/*
2350 * Store the name in printable form into buf; no more
Paul Bakkerd98030e2009-05-02 15:13:40 +00002351 * than size characters will be written
Paul Bakker5121ce52009-01-03 21:22:43 +00002352 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002353int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn )
Paul Bakker5121ce52009-01-03 21:22:43 +00002354{
Paul Bakker23986e52011-04-24 08:57:21 +00002355 int ret;
2356 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002357 unsigned char c;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002358 const x509_name *name;
Paul Bakker5121ce52009-01-03 21:22:43 +00002359 char s[128], *p;
2360
2361 memset( s, 0, sizeof( s ) );
2362
2363 name = dn;
2364 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002365 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002366
2367 while( name != NULL )
2368 {
Paul Bakker74111d32011-01-15 16:57:55 +00002369 if( name != dn )
2370 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002371 ret = snprintf( p, n, ", " );
2372 SAFE_SNPRINTF();
2373 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002374
Paul Bakker7261cba2013-01-16 12:39:54 +01002375 if( name->oid.len == 3 &&
2376 memcmp( name->oid.p, OID_X520, 2 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002377 {
2378 switch( name->oid.p[2] )
2379 {
2380 case X520_COMMON_NAME:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002381 ret = snprintf( p, n, "CN=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002382
2383 case X520_COUNTRY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002384 ret = snprintf( p, n, "C=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002385
2386 case X520_LOCALITY:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002387 ret = snprintf( p, n, "L=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002388
2389 case X520_STATE:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002390 ret = snprintf( p, n, "ST=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002391
2392 case X520_ORGANIZATION:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002393 ret = snprintf( p, n, "O=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002394
2395 case X520_ORG_UNIT:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002396 ret = snprintf( p, n, "OU=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002397
2398 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002399 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002400 name->oid.p[2] );
2401 break;
2402 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002403 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002404 }
Paul Bakker7261cba2013-01-16 12:39:54 +01002405 else if( name->oid.len == 9 &&
2406 memcmp( name->oid.p, OID_PKCS9, 8 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002407 {
2408 switch( name->oid.p[8] )
2409 {
2410 case PKCS9_EMAIL:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002411 ret = snprintf( p, n, "emailAddress=" ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002412
2413 default:
Paul Bakkerd98030e2009-05-02 15:13:40 +00002414 ret = snprintf( p, n, "0x%02X=",
Paul Bakker5121ce52009-01-03 21:22:43 +00002415 name->oid.p[8] );
2416 break;
2417 }
Paul Bakkerd98030e2009-05-02 15:13:40 +00002418 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002419 }
2420 else
Paul Bakker74111d32011-01-15 16:57:55 +00002421 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002422 ret = snprintf( p, n, "\?\?=" );
Paul Bakker74111d32011-01-15 16:57:55 +00002423 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002424 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002425
2426 for( i = 0; i < name->val.len; i++ )
2427 {
Paul Bakker27fdf462011-06-09 13:55:13 +00002428 if( i >= sizeof( s ) - 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002429 break;
2430
2431 c = name->val.p[i];
2432 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
2433 s[i] = '?';
2434 else s[i] = c;
2435 }
2436 s[i] = '\0';
Paul Bakkerd98030e2009-05-02 15:13:40 +00002437 ret = snprintf( p, n, "%s", s );
2438 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002439 name = name->next;
2440 }
2441
Paul Bakker23986e52011-04-24 08:57:21 +00002442 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002443}
2444
2445/*
Paul Bakkerdd476992011-01-16 21:34:59 +00002446 * Store the serial in printable form into buf; no more
2447 * than size characters will be written
2448 */
2449int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial )
2450{
Paul Bakker23986e52011-04-24 08:57:21 +00002451 int ret;
2452 size_t i, n, nr;
Paul Bakkerdd476992011-01-16 21:34:59 +00002453 char *p;
2454
2455 p = buf;
2456 n = size;
2457
2458 nr = ( serial->len <= 32 )
Paul Bakker03c7c252011-11-25 12:37:37 +00002459 ? serial->len : 28;
Paul Bakkerdd476992011-01-16 21:34:59 +00002460
2461 for( i = 0; i < nr; i++ )
2462 {
Paul Bakker93048802011-12-05 14:38:06 +00002463 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002464 continue;
2465
Paul Bakkerdd476992011-01-16 21:34:59 +00002466 ret = snprintf( p, n, "%02X%s",
2467 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
2468 SAFE_SNPRINTF();
2469 }
2470
Paul Bakker03c7c252011-11-25 12:37:37 +00002471 if( nr != serial->len )
2472 {
2473 ret = snprintf( p, n, "...." );
2474 SAFE_SNPRINTF();
2475 }
2476
Paul Bakker23986e52011-04-24 08:57:21 +00002477 return( (int) ( size - n ) );
Paul Bakkerdd476992011-01-16 21:34:59 +00002478}
2479
2480/*
Paul Bakkerd98030e2009-05-02 15:13:40 +00002481 * Return an informational string about the certificate.
Paul Bakker5121ce52009-01-03 21:22:43 +00002482 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002483int x509parse_cert_info( char *buf, size_t size, const char *prefix,
2484 const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +00002485{
Paul Bakker23986e52011-04-24 08:57:21 +00002486 int ret;
2487 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002488 char *p;
Paul Bakker5121ce52009-01-03 21:22:43 +00002489
2490 p = buf;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002491 n = size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002492
Paul Bakkerd98030e2009-05-02 15:13:40 +00002493 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker5121ce52009-01-03 21:22:43 +00002494 prefix, crt->version );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002495 SAFE_SNPRINTF();
2496 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker5121ce52009-01-03 21:22:43 +00002497 prefix );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002498 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002499
Paul Bakkerdd476992011-01-16 21:34:59 +00002500 ret = x509parse_serial_gets( p, n, &crt->serial);
2501 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002502
Paul Bakkerd98030e2009-05-02 15:13:40 +00002503 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2504 SAFE_SNPRINTF();
2505 ret = x509parse_dn_gets( p, n, &crt->issuer );
2506 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002507
Paul Bakkerd98030e2009-05-02 15:13:40 +00002508 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
2509 SAFE_SNPRINTF();
2510 ret = x509parse_dn_gets( p, n, &crt->subject );
2511 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002512
Paul Bakkerd98030e2009-05-02 15:13:40 +00002513 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002514 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2515 crt->valid_from.year, crt->valid_from.mon,
2516 crt->valid_from.day, crt->valid_from.hour,
2517 crt->valid_from.min, crt->valid_from.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002518 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002519
Paul Bakkerd98030e2009-05-02 15:13:40 +00002520 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker5121ce52009-01-03 21:22:43 +00002521 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2522 crt->valid_to.year, crt->valid_to.mon,
2523 crt->valid_to.day, crt->valid_to.hour,
2524 crt->valid_to.min, crt->valid_to.sec );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002525 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002526
Paul Bakkerd98030e2009-05-02 15:13:40 +00002527 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2528 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002529
Paul Bakker27d66162010-03-17 06:56:01 +00002530 switch( crt->sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002531 {
Paul Bakkerd98030e2009-05-02 15:13:40 +00002532 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2533 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2534 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2535 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2536 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2537 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2538 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2539 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2540 default: ret = snprintf( p, n, "???" ); break;
2541 }
2542 SAFE_SNPRINTF();
2543
2544 ret = snprintf( p, n, "\n%sRSA key size : %d bits\n", prefix,
Paul Bakkerf4f69682011-04-24 16:08:12 +00002545 (int) crt->rsa.N.n * (int) sizeof( unsigned long ) * 8 );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002546 SAFE_SNPRINTF();
2547
Paul Bakker23986e52011-04-24 08:57:21 +00002548 return( (int) ( size - n ) );
Paul Bakkerd98030e2009-05-02 15:13:40 +00002549}
2550
Paul Bakker74111d32011-01-15 16:57:55 +00002551/* Compare a given OID string with an OID x509_buf * */
2552#define OID_CMP(oid_str, oid_buf) \
2553 ( ( OID_SIZE(oid_str) == (oid_buf)->len ) && \
2554 memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) == 0)
2555
2556/*
2557 * Return an informational string describing the given OID
2558 */
2559const char *x509_oid_get_description( x509_buf *oid )
2560{
2561 if ( oid == NULL )
2562 return ( NULL );
2563
2564 else if( OID_CMP( OID_SERVER_AUTH, oid ) )
2565 return( STRING_SERVER_AUTH );
2566
2567 else if( OID_CMP( OID_CLIENT_AUTH, oid ) )
2568 return( STRING_CLIENT_AUTH );
2569
2570 else if( OID_CMP( OID_CODE_SIGNING, oid ) )
2571 return( STRING_CODE_SIGNING );
2572
2573 else if( OID_CMP( OID_EMAIL_PROTECTION, oid ) )
2574 return( STRING_EMAIL_PROTECTION );
2575
2576 else if( OID_CMP( OID_TIME_STAMPING, oid ) )
2577 return( STRING_TIME_STAMPING );
2578
2579 else if( OID_CMP( OID_OCSP_SIGNING, oid ) )
2580 return( STRING_OCSP_SIGNING );
2581
2582 return( NULL );
2583}
2584
2585/* Return the x.y.z.... style numeric string for the given OID */
2586int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid )
2587{
Paul Bakker23986e52011-04-24 08:57:21 +00002588 int ret;
2589 size_t i, n;
Paul Bakker74111d32011-01-15 16:57:55 +00002590 unsigned int value;
2591 char *p;
2592
2593 p = buf;
2594 n = size;
2595
2596 /* First byte contains first two dots */
2597 if( oid->len > 0 )
2598 {
2599 ret = snprintf( p, n, "%d.%d", oid->p[0]/40, oid->p[0]%40 );
2600 SAFE_SNPRINTF();
2601 }
2602
2603 /* TODO: value can overflow in value. */
2604 value = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002605 for( i = 1; i < oid->len; i++ )
Paul Bakker74111d32011-01-15 16:57:55 +00002606 {
2607 value <<= 7;
2608 value += oid->p[i] & 0x7F;
2609
2610 if( !( oid->p[i] & 0x80 ) )
2611 {
2612 /* Last byte */
2613 ret = snprintf( p, n, ".%d", value );
2614 SAFE_SNPRINTF();
2615 value = 0;
2616 }
2617 }
2618
Paul Bakker23986e52011-04-24 08:57:21 +00002619 return( (int) ( size - n ) );
Paul Bakker74111d32011-01-15 16:57:55 +00002620}
2621
Paul Bakkerd98030e2009-05-02 15:13:40 +00002622/*
2623 * Return an informational string about the CRL.
2624 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002625int x509parse_crl_info( char *buf, size_t size, const char *prefix,
2626 const x509_crl *crl )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002627{
Paul Bakker23986e52011-04-24 08:57:21 +00002628 int ret;
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002629 size_t n;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002630 char *p;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002631 const x509_crl_entry *entry;
Paul Bakkerd98030e2009-05-02 15:13:40 +00002632
2633 p = buf;
2634 n = size;
2635
2636 ret = snprintf( p, n, "%sCRL version : %d",
2637 prefix, crl->version );
2638 SAFE_SNPRINTF();
2639
2640 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
2641 SAFE_SNPRINTF();
2642 ret = x509parse_dn_gets( p, n, &crl->issuer );
2643 SAFE_SNPRINTF();
2644
2645 ret = snprintf( p, n, "\n%sthis update : " \
2646 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2647 crl->this_update.year, crl->this_update.mon,
2648 crl->this_update.day, crl->this_update.hour,
2649 crl->this_update.min, crl->this_update.sec );
2650 SAFE_SNPRINTF();
2651
2652 ret = snprintf( p, n, "\n%snext update : " \
2653 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2654 crl->next_update.year, crl->next_update.mon,
2655 crl->next_update.day, crl->next_update.hour,
2656 crl->next_update.min, crl->next_update.sec );
2657 SAFE_SNPRINTF();
2658
2659 entry = &crl->entry;
2660
2661 ret = snprintf( p, n, "\n%sRevoked certificates:",
2662 prefix );
2663 SAFE_SNPRINTF();
2664
Paul Bakker9be19372009-07-27 20:21:53 +00002665 while( entry != NULL && entry->raw.len != 0 )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002666 {
2667 ret = snprintf( p, n, "\n%sserial number: ",
2668 prefix );
2669 SAFE_SNPRINTF();
2670
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002671 ret = x509parse_serial_gets( p, n, &entry->serial);
2672 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002673
Paul Bakkerd98030e2009-05-02 15:13:40 +00002674 ret = snprintf( p, n, " revocation date: " \
2675 "%04d-%02d-%02d %02d:%02d:%02d",
2676 entry->revocation_date.year, entry->revocation_date.mon,
2677 entry->revocation_date.day, entry->revocation_date.hour,
2678 entry->revocation_date.min, entry->revocation_date.sec );
Paul Bakkerc8ffbe72011-12-05 14:22:49 +00002679 SAFE_SNPRINTF();
Paul Bakkerd98030e2009-05-02 15:13:40 +00002680
2681 entry = entry->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002682 }
2683
Paul Bakkerd98030e2009-05-02 15:13:40 +00002684 ret = snprintf( p, n, "\n%ssigned using : RSA+", prefix );
2685 SAFE_SNPRINTF();
Paul Bakker5121ce52009-01-03 21:22:43 +00002686
Paul Bakker27d66162010-03-17 06:56:01 +00002687 switch( crl->sig_alg )
Paul Bakkerd98030e2009-05-02 15:13:40 +00002688 {
2689 case SIG_RSA_MD2 : ret = snprintf( p, n, "MD2" ); break;
2690 case SIG_RSA_MD4 : ret = snprintf( p, n, "MD4" ); break;
2691 case SIG_RSA_MD5 : ret = snprintf( p, n, "MD5" ); break;
2692 case SIG_RSA_SHA1 : ret = snprintf( p, n, "SHA1" ); break;
2693 case SIG_RSA_SHA224 : ret = snprintf( p, n, "SHA224" ); break;
2694 case SIG_RSA_SHA256 : ret = snprintf( p, n, "SHA256" ); break;
2695 case SIG_RSA_SHA384 : ret = snprintf( p, n, "SHA384" ); break;
2696 case SIG_RSA_SHA512 : ret = snprintf( p, n, "SHA512" ); break;
2697 default: ret = snprintf( p, n, "???" ); break;
2698 }
2699 SAFE_SNPRINTF();
2700
Paul Bakker1e27bb22009-07-19 20:25:25 +00002701 ret = snprintf( p, n, "\n" );
2702 SAFE_SNPRINTF();
2703
Paul Bakker23986e52011-04-24 08:57:21 +00002704 return( (int) ( size - n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002705}
2706
2707/*
Paul Bakker40ea7de2009-05-03 10:18:48 +00002708 * Return 0 if the x509_time is still valid, or 1 otherwise.
Paul Bakker5121ce52009-01-03 21:22:43 +00002709 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002710int x509parse_time_expired( const x509_time *to )
Paul Bakker5121ce52009-01-03 21:22:43 +00002711{
Paul Bakkercce9d772011-11-18 14:26:47 +00002712 int year, mon, day;
2713 int hour, min, sec;
2714
2715#if defined(_WIN32)
2716 SYSTEMTIME st;
2717
2718 GetLocalTime(&st);
2719
2720 year = st.wYear;
2721 mon = st.wMonth;
2722 day = st.wDay;
2723 hour = st.wHour;
2724 min = st.wMinute;
2725 sec = st.wSecond;
2726#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002727 struct tm *lt;
2728 time_t tt;
2729
2730 tt = time( NULL );
2731 lt = localtime( &tt );
2732
Paul Bakkercce9d772011-11-18 14:26:47 +00002733 year = lt->tm_year + 1900;
2734 mon = lt->tm_mon + 1;
2735 day = lt->tm_mday;
2736 hour = lt->tm_hour;
2737 min = lt->tm_min;
2738 sec = lt->tm_sec;
2739#endif
2740
2741 if( year > to->year )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002742 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002743
Paul Bakkercce9d772011-11-18 14:26:47 +00002744 if( year == to->year &&
2745 mon > to->mon )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002746 return( 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002747
Paul Bakkercce9d772011-11-18 14:26:47 +00002748 if( year == to->year &&
2749 mon == to->mon &&
2750 day > to->day )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002751 return( 1 );
2752
Paul Bakkercce9d772011-11-18 14:26:47 +00002753 if( year == to->year &&
2754 mon == to->mon &&
2755 day == to->day &&
2756 hour > to->hour )
Paul Bakkerb6194992011-01-16 21:40:22 +00002757 return( 1 );
2758
Paul Bakkercce9d772011-11-18 14:26:47 +00002759 if( year == to->year &&
2760 mon == to->mon &&
2761 day == to->day &&
2762 hour == to->hour &&
2763 min > to->min )
Paul Bakkerb6194992011-01-16 21:40:22 +00002764 return( 1 );
2765
Paul Bakkercce9d772011-11-18 14:26:47 +00002766 if( year == to->year &&
2767 mon == to->mon &&
2768 day == to->day &&
2769 hour == to->hour &&
2770 min == to->min &&
2771 sec > to->sec )
Paul Bakkerb6194992011-01-16 21:40:22 +00002772 return( 1 );
2773
Paul Bakker40ea7de2009-05-03 10:18:48 +00002774 return( 0 );
2775}
2776
2777/*
2778 * Return 1 if the certificate is revoked, or 0 otherwise.
2779 */
Paul Bakkerff60ee62010-03-16 21:09:09 +00002780int x509parse_revoked( const x509_cert *crt, const x509_crl *crl )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002781{
Paul Bakkerff60ee62010-03-16 21:09:09 +00002782 const x509_crl_entry *cur = &crl->entry;
Paul Bakker40ea7de2009-05-03 10:18:48 +00002783
2784 while( cur != NULL && cur->serial.len != 0 )
2785 {
Paul Bakkera056efc2011-01-16 21:38:35 +00002786 if( crt->serial.len == cur->serial.len &&
2787 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
Paul Bakker40ea7de2009-05-03 10:18:48 +00002788 {
2789 if( x509parse_time_expired( &cur->revocation_date ) )
2790 return( 1 );
2791 }
2792
2793 cur = cur->next;
2794 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002795
2796 return( 0 );
2797}
2798
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002799/*
2800 * Wrapper for x509 hashes.
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002801 */
Paul Bakker23986e52011-04-24 08:57:21 +00002802static void x509_hash( const unsigned char *in, size_t len, int alg,
Paul Bakker5121ce52009-01-03 21:22:43 +00002803 unsigned char *out )
2804{
2805 switch( alg )
2806 {
Paul Bakker40e46942009-01-03 21:51:57 +00002807#if defined(POLARSSL_MD2_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002808 case SIG_RSA_MD2 : md2( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002809#endif
Paul Bakker40e46942009-01-03 21:51:57 +00002810#if defined(POLARSSL_MD4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002811 case SIG_RSA_MD4 : md4( in, len, out ); break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002812#endif
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002813#if defined(POLARSSL_MD5_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002814 case SIG_RSA_MD5 : md5( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002815#endif
2816#if defined(POLARSSL_SHA1_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002817 case SIG_RSA_SHA1 : sha1( in, len, out ); break;
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002818#endif
Paul Bakker4593aea2009-02-09 22:32:35 +00002819#if defined(POLARSSL_SHA2_C)
2820 case SIG_RSA_SHA224 : sha2( in, len, out, 1 ); break;
2821 case SIG_RSA_SHA256 : sha2( in, len, out, 0 ); break;
2822#endif
Paul Bakkerfe1aea72009-10-03 20:09:14 +00002823#if defined(POLARSSL_SHA4_C)
Paul Bakker4593aea2009-02-09 22:32:35 +00002824 case SIG_RSA_SHA384 : sha4( in, len, out, 1 ); break;
2825 case SIG_RSA_SHA512 : sha4( in, len, out, 0 ); break;
2826#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002827 default:
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00002828 memset( out, '\xFF', 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002829 break;
2830 }
2831}
2832
2833/*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002834 * Check that the given certificate is valid accoring to the CRL.
2835 */
2836static int x509parse_verifycrl(x509_cert *crt, x509_cert *ca,
2837 x509_crl *crl_list)
2838{
2839 int flags = 0;
2840 int hash_id;
2841 unsigned char hash[64];
2842
2843 /*
2844 * TODO: What happens if no CRL is present?
2845 * Suggestion: Revocation state should be unknown if no CRL is present.
2846 * For backwards compatibility this is not yet implemented.
2847 */
2848
2849 while( ca != NULL && crl_list != NULL && crl_list->version != 0 )
2850 {
2851 if( crl_list->issuer_raw.len != ca->subject_raw.len ||
2852 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
2853 crl_list->issuer_raw.len ) != 0 )
2854 {
2855 crl_list = crl_list->next;
2856 continue;
2857 }
2858
2859 /*
2860 * Check if CRL is correctly signed by the trusted CA
2861 */
2862 hash_id = crl_list->sig_alg;
2863
2864 x509_hash( crl_list->tbs.p, crl_list->tbs.len, hash_id, hash );
2865
2866 if( !rsa_pkcs1_verify( &ca->rsa, RSA_PUBLIC, hash_id,
2867 0, hash, crl_list->sig.p ) == 0 )
2868 {
2869 /*
2870 * CRL is not trusted
2871 */
2872 flags |= BADCRL_NOT_TRUSTED;
2873 break;
2874 }
2875
2876 /*
2877 * Check for validity of CRL (Do not drop out)
2878 */
2879 if( x509parse_time_expired( &crl_list->next_update ) )
2880 flags |= BADCRL_EXPIRED;
2881
2882 /*
2883 * Check if certificate is revoked
2884 */
2885 if( x509parse_revoked(crt, crl_list) )
2886 {
2887 flags |= BADCERT_REVOKED;
2888 break;
2889 }
2890
2891 crl_list = crl_list->next;
2892 }
2893 return flags;
2894}
2895
2896/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002897 * Verify the certificate validity
2898 */
2899int x509parse_verify( x509_cert *crt,
2900 x509_cert *trust_ca,
Paul Bakker40ea7de2009-05-03 10:18:48 +00002901 x509_crl *ca_crl,
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002902 const char *cn, int *flags,
2903 int (*f_vrfy)(void *, x509_cert *, int, int),
2904 void *p_vrfy )
Paul Bakker5121ce52009-01-03 21:22:43 +00002905{
Paul Bakker23986e52011-04-24 08:57:21 +00002906 size_t cn_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002907 int hash_id;
2908 int pathlen;
Paul Bakker76fd75a2011-01-16 21:12:10 +00002909 x509_cert *parent;
Paul Bakker5121ce52009-01-03 21:22:43 +00002910 x509_name *name;
Paul Bakker4593aea2009-02-09 22:32:35 +00002911 unsigned char hash[64];
Paul Bakker5121ce52009-01-03 21:22:43 +00002912
Paul Bakker40ea7de2009-05-03 10:18:48 +00002913 *flags = 0;
2914
2915 if( x509parse_time_expired( &crt->valid_to ) )
2916 *flags = BADCERT_EXPIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002917
2918 if( cn != NULL )
2919 {
2920 name = &crt->subject;
2921 cn_len = strlen( cn );
2922
2923 while( name != NULL )
2924 {
Paul Bakker7261cba2013-01-16 12:39:54 +01002925 if( name->oid.len == 3 &&
2926 memcmp( name->oid.p, OID_CN, 3 ) == 0 &&
2927 name->val.len == cn_len &&
2928 memcmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002929 break;
2930
2931 name = name->next;
2932 }
2933
2934 if( name == NULL )
2935 *flags |= BADCERT_CN_MISMATCH;
2936 }
2937
Paul Bakker5121ce52009-01-03 21:22:43 +00002938 /*
2939 * Iterate upwards in the given cert chain,
2940 * ignoring any upper cert with CA != TRUE.
2941 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00002942 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002943
2944 pathlen = 1;
2945
Paul Bakker76fd75a2011-01-16 21:12:10 +00002946 while( parent != NULL && parent->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002947 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002948 if( parent->ca_istrue == 0 ||
2949 crt->issuer_raw.len != parent->subject_raw.len ||
2950 memcmp( crt->issuer_raw.p, parent->subject_raw.p,
Paul Bakker5121ce52009-01-03 21:22:43 +00002951 crt->issuer_raw.len ) != 0 )
2952 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002953 parent = parent->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002954 continue;
2955 }
2956
Paul Bakker27d66162010-03-17 06:56:01 +00002957 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00002958
2959 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
2960
Paul Bakker76fd75a2011-01-16 21:12:10 +00002961 if( rsa_pkcs1_verify( &parent->rsa, RSA_PUBLIC, hash_id, 0, hash,
2962 crt->sig.p ) != 0 )
2963 *flags |= BADCERT_NOT_TRUSTED;
2964
2965 /* Check trusted CA's CRL for the given crt */
2966 *flags |= x509parse_verifycrl(crt, parent, ca_crl);
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002967
2968 /* crt is verified to be a child of the parent cur, call verify callback */
Paul Bakker74111d32011-01-15 16:57:55 +00002969 if( NULL != f_vrfy )
2970 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00002971 if( f_vrfy( p_vrfy, crt, pathlen - 1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002972 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00002973 else
2974 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00002975 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00002976 else if( *flags != 0 )
2977 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002978
2979 pathlen++;
2980
Paul Bakker76fd75a2011-01-16 21:12:10 +00002981 crt = parent;
2982 parent = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002983 }
2984
2985 /*
Paul Bakker76fd75a2011-01-16 21:12:10 +00002986 * Attempt to validate topmost cert with our CA chain.
Paul Bakker5121ce52009-01-03 21:22:43 +00002987 */
Paul Bakker76fd75a2011-01-16 21:12:10 +00002988 *flags |= BADCERT_NOT_TRUSTED;
2989
Paul Bakker7c6d4a42009-03-28 20:35:47 +00002990 while( trust_ca != NULL && trust_ca->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002991 {
2992 if( crt->issuer_raw.len != trust_ca->subject_raw.len ||
2993 memcmp( crt->issuer_raw.p, trust_ca->subject_raw.p,
2994 crt->issuer_raw.len ) != 0 )
2995 {
2996 trust_ca = trust_ca->next;
2997 continue;
2998 }
2999
3000 if( trust_ca->max_pathlen > 0 &&
3001 trust_ca->max_pathlen < pathlen )
3002 break;
3003
Paul Bakker27d66162010-03-17 06:56:01 +00003004 hash_id = crt->sig_alg;
Paul Bakker5121ce52009-01-03 21:22:43 +00003005
3006 x509_hash( crt->tbs.p, crt->tbs.len, hash_id, hash );
3007
3008 if( rsa_pkcs1_verify( &trust_ca->rsa, RSA_PUBLIC, hash_id,
3009 0, hash, crt->sig.p ) == 0 )
3010 {
3011 /*
3012 * cert. is signed by a trusted CA
3013 */
3014 *flags &= ~BADCERT_NOT_TRUSTED;
3015 break;
3016 }
3017
3018 trust_ca = trust_ca->next;
3019 }
3020
Paul Bakker76fd75a2011-01-16 21:12:10 +00003021 /* Check trusted CA's CRL for the given crt */
3022 *flags |= x509parse_verifycrl( crt, trust_ca, ca_crl );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003023
3024 /* Verification succeeded, call callback on top cert */
Paul Bakker74111d32011-01-15 16:57:55 +00003025 if( NULL != f_vrfy )
3026 {
Paul Bakker76fd75a2011-01-16 21:12:10 +00003027 if( f_vrfy(p_vrfy, crt, pathlen-1, ( *flags == 0 ) ) != 0 )
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003028 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker76fd75a2011-01-16 21:12:10 +00003029 else
3030 *flags = 0;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003031 }
Paul Bakker76fd75a2011-01-16 21:12:10 +00003032 else if( *flags != 0 )
3033 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003034
Paul Bakker5121ce52009-01-03 21:22:43 +00003035 return( 0 );
3036}
3037
3038/*
3039 * Unallocate all certificate data
3040 */
3041void x509_free( x509_cert *crt )
3042{
3043 x509_cert *cert_cur = crt;
3044 x509_cert *cert_prv;
3045 x509_name *name_cur;
3046 x509_name *name_prv;
Paul Bakker74111d32011-01-15 16:57:55 +00003047 x509_sequence *seq_cur;
3048 x509_sequence *seq_prv;
Paul Bakker5121ce52009-01-03 21:22:43 +00003049
3050 if( crt == NULL )
3051 return;
3052
3053 do
3054 {
3055 rsa_free( &cert_cur->rsa );
3056
3057 name_cur = cert_cur->issuer.next;
3058 while( name_cur != NULL )
3059 {
3060 name_prv = name_cur;
3061 name_cur = name_cur->next;
3062 memset( name_prv, 0, sizeof( x509_name ) );
3063 free( name_prv );
3064 }
3065
3066 name_cur = cert_cur->subject.next;
3067 while( name_cur != NULL )
3068 {
3069 name_prv = name_cur;
3070 name_cur = name_cur->next;
3071 memset( name_prv, 0, sizeof( x509_name ) );
3072 free( name_prv );
3073 }
3074
Paul Bakker74111d32011-01-15 16:57:55 +00003075 seq_cur = cert_cur->ext_key_usage.next;
3076 while( seq_cur != NULL )
3077 {
3078 seq_prv = seq_cur;
3079 seq_cur = seq_cur->next;
3080 memset( seq_prv, 0, sizeof( x509_sequence ) );
3081 free( seq_prv );
3082 }
3083
Paul Bakker5121ce52009-01-03 21:22:43 +00003084 if( cert_cur->raw.p != NULL )
3085 {
3086 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
3087 free( cert_cur->raw.p );
3088 }
3089
3090 cert_cur = cert_cur->next;
3091 }
3092 while( cert_cur != NULL );
3093
3094 cert_cur = crt;
3095 do
3096 {
3097 cert_prv = cert_cur;
3098 cert_cur = cert_cur->next;
3099
3100 memset( cert_prv, 0, sizeof( x509_cert ) );
3101 if( cert_prv != crt )
3102 free( cert_prv );
3103 }
3104 while( cert_cur != NULL );
3105}
3106
Paul Bakkerd98030e2009-05-02 15:13:40 +00003107/*
3108 * Unallocate all CRL data
3109 */
3110void x509_crl_free( x509_crl *crl )
3111{
3112 x509_crl *crl_cur = crl;
3113 x509_crl *crl_prv;
3114 x509_name *name_cur;
3115 x509_name *name_prv;
3116 x509_crl_entry *entry_cur;
3117 x509_crl_entry *entry_prv;
3118
3119 if( crl == NULL )
3120 return;
3121
3122 do
3123 {
3124 name_cur = crl_cur->issuer.next;
3125 while( name_cur != NULL )
3126 {
3127 name_prv = name_cur;
3128 name_cur = name_cur->next;
3129 memset( name_prv, 0, sizeof( x509_name ) );
3130 free( name_prv );
3131 }
3132
3133 entry_cur = crl_cur->entry.next;
3134 while( entry_cur != NULL )
3135 {
3136 entry_prv = entry_cur;
3137 entry_cur = entry_cur->next;
3138 memset( entry_prv, 0, sizeof( x509_crl_entry ) );
3139 free( entry_prv );
3140 }
3141
3142 if( crl_cur->raw.p != NULL )
3143 {
3144 memset( crl_cur->raw.p, 0, crl_cur->raw.len );
3145 free( crl_cur->raw.p );
3146 }
3147
3148 crl_cur = crl_cur->next;
3149 }
3150 while( crl_cur != NULL );
3151
3152 crl_cur = crl;
3153 do
3154 {
3155 crl_prv = crl_cur;
3156 crl_cur = crl_cur->next;
3157
3158 memset( crl_prv, 0, sizeof( x509_crl ) );
3159 if( crl_prv != crl )
3160 free( crl_prv );
3161 }
3162 while( crl_cur != NULL );
3163}
3164
Paul Bakker40e46942009-01-03 21:51:57 +00003165#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00003166
Paul Bakker40e46942009-01-03 21:51:57 +00003167#include "polarssl/certs.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00003168
3169/*
3170 * Checkup routine
3171 */
3172int x509_self_test( int verbose )
3173{
Paul Bakker5690efc2011-05-26 13:16:06 +00003174#if defined(POLARSSL_CERTS_C) && defined(POLARSSL_MD5_C)
Paul Bakker23986e52011-04-24 08:57:21 +00003175 int ret;
3176 int flags;
3177 size_t i, j;
Paul Bakker5121ce52009-01-03 21:22:43 +00003178 x509_cert cacert;
3179 x509_cert clicert;
3180 rsa_context rsa;
Paul Bakker5690efc2011-05-26 13:16:06 +00003181#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003182 dhm_context dhm;
Paul Bakker5690efc2011-05-26 13:16:06 +00003183#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003184
3185 if( verbose != 0 )
3186 printf( " X.509 certificate load: " );
3187
3188 memset( &clicert, 0, sizeof( x509_cert ) );
3189
3190 ret = x509parse_crt( &clicert, (unsigned char *) test_cli_crt,
Paul Bakker732e1a82011-12-11 16:35:09 +00003191 strlen( test_cli_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003192 if( ret != 0 )
3193 {
3194 if( verbose != 0 )
3195 printf( "failed\n" );
3196
3197 return( ret );
3198 }
3199
3200 memset( &cacert, 0, sizeof( x509_cert ) );
3201
3202 ret = x509parse_crt( &cacert, (unsigned char *) test_ca_crt,
Paul Bakker732e1a82011-12-11 16:35:09 +00003203 strlen( test_ca_crt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003204 if( ret != 0 )
3205 {
3206 if( verbose != 0 )
3207 printf( "failed\n" );
3208
3209 return( ret );
3210 }
3211
3212 if( verbose != 0 )
3213 printf( "passed\n X.509 private key load: " );
3214
3215 i = strlen( test_ca_key );
3216 j = strlen( test_ca_pwd );
3217
Paul Bakker66b78b22011-03-25 14:22:50 +00003218 rsa_init( &rsa, RSA_PKCS_V15, 0 );
3219
Paul Bakker5121ce52009-01-03 21:22:43 +00003220 if( ( ret = x509parse_key( &rsa,
3221 (unsigned char *) test_ca_key, i,
3222 (unsigned char *) test_ca_pwd, j ) ) != 0 )
3223 {
3224 if( verbose != 0 )
3225 printf( "failed\n" );
3226
3227 return( ret );
3228 }
3229
3230 if( verbose != 0 )
3231 printf( "passed\n X.509 signature verify: ");
3232
Paul Bakker23986e52011-04-24 08:57:21 +00003233 ret = x509parse_verify( &clicert, &cacert, NULL, "PolarSSL Client 2", &flags, NULL, NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +00003234 if( ret != 0 )
3235 {
Paul Bakker23986e52011-04-24 08:57:21 +00003236 printf("%02x", flags);
Paul Bakker5121ce52009-01-03 21:22:43 +00003237 if( verbose != 0 )
3238 printf( "failed\n" );
3239
3240 return( ret );
3241 }
3242
Paul Bakker5690efc2011-05-26 13:16:06 +00003243#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003244 if( verbose != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003245 printf( "passed\n X.509 DHM parameter load: " );
3246
3247 i = strlen( test_dhm_params );
3248 j = strlen( test_ca_pwd );
3249
3250 if( ( ret = x509parse_dhm( &dhm, (unsigned char *) test_dhm_params, i ) ) != 0 )
3251 {
3252 if( verbose != 0 )
3253 printf( "failed\n" );
3254
3255 return( ret );
3256 }
3257
3258 if( verbose != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003259 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00003260#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003261
3262 x509_free( &cacert );
3263 x509_free( &clicert );
3264 rsa_free( &rsa );
Paul Bakker5690efc2011-05-26 13:16:06 +00003265#if defined(POLARSSL_DHM_C)
Paul Bakker1b57b062011-01-06 15:48:19 +00003266 dhm_free( &dhm );
Paul Bakker5690efc2011-05-26 13:16:06 +00003267#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003268
3269 return( 0 );
Paul Bakkerde4d2ea2009-10-03 19:58:52 +00003270#else
3271 ((void) verbose);
3272 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
3273#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003274}
3275
3276#endif
3277
3278#endif