1d72bf49234b5aee044c94c3ea0784d7fcd42973
[reactos.git] / dll / 3rdparty / mbedtls / x509.c
1 /*
2 * X.509 common functions for parsing and verification
3 *
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: GPL-2.0
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * This file is part of mbed TLS (https://tls.mbed.org)
22 */
23 /*
24 * The ITU-T X.509 standard defines a certificate format for PKI.
25 *
26 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
27 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
28 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
29 *
30 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
31 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
32 */
33
34 #if !defined(MBEDTLS_CONFIG_FILE)
35 #include "mbedtls/config.h"
36 #else
37 #include MBEDTLS_CONFIG_FILE
38 #endif
39
40 #if defined(MBEDTLS_X509_USE_C)
41
42 #include "mbedtls/x509.h"
43 #include "mbedtls/asn1.h"
44 #include "mbedtls/oid.h"
45
46 #include <stdio.h>
47 #include <string.h>
48
49 #if defined(MBEDTLS_PEM_PARSE_C)
50 #include "mbedtls/pem.h"
51 #endif
52
53 #if defined(MBEDTLS_PLATFORM_C)
54 #include "mbedtls/platform.h"
55 #else
56 #include <stdio.h>
57 #include <stdlib.h>
58 #define mbedtls_free free
59 #define mbedtls_calloc calloc
60 #define mbedtls_printf printf
61 #define mbedtls_snprintf snprintf
62 #endif
63
64
65 #if defined(MBEDTLS_HAVE_TIME)
66 #include "mbedtls/platform_time.h"
67 #endif
68
69 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
70 #include <windows.h>
71 #else
72 #include <time.h>
73 #endif
74
75 #define CHECK(code) if( ( ret = code ) != 0 ){ return( ret ); }
76 #define CHECK_RANGE(min, max, val) if( val < min || val > max ){ return( ret ); }
77
78 /*
79 * CertificateSerialNumber ::= INTEGER
80 */
81 int mbedtls_x509_get_serial( unsigned char **p, const unsigned char *end,
82 mbedtls_x509_buf *serial )
83 {
84 int ret;
85
86 if( ( end - *p ) < 1 )
87 return( MBEDTLS_ERR_X509_INVALID_SERIAL +
88 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
89
90 if( **p != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2 ) &&
91 **p != MBEDTLS_ASN1_INTEGER )
92 return( MBEDTLS_ERR_X509_INVALID_SERIAL +
93 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
94
95 serial->tag = *(*p)++;
96
97 if( ( ret = mbedtls_asn1_get_len( p, end, &serial->len ) ) != 0 )
98 return( MBEDTLS_ERR_X509_INVALID_SERIAL + ret );
99
100 serial->p = *p;
101 *p += serial->len;
102
103 return( 0 );
104 }
105
106 /* Get an algorithm identifier without parameters (eg for signatures)
107 *
108 * AlgorithmIdentifier ::= SEQUENCE {
109 * algorithm OBJECT IDENTIFIER,
110 * parameters ANY DEFINED BY algorithm OPTIONAL }
111 */
112 int mbedtls_x509_get_alg_null( unsigned char **p, const unsigned char *end,
113 mbedtls_x509_buf *alg )
114 {
115 int ret;
116
117 if( ( ret = mbedtls_asn1_get_alg_null( p, end, alg ) ) != 0 )
118 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
119
120 return( 0 );
121 }
122
123 /*
124 * Parse an algorithm identifier with (optional) paramaters
125 */
126 int mbedtls_x509_get_alg( unsigned char **p, const unsigned char *end,
127 mbedtls_x509_buf *alg, mbedtls_x509_buf *params )
128 {
129 int ret;
130
131 if( ( ret = mbedtls_asn1_get_alg( p, end, alg, params ) ) != 0 )
132 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
133
134 return( 0 );
135 }
136
137 #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
138 /*
139 * HashAlgorithm ::= AlgorithmIdentifier
140 *
141 * AlgorithmIdentifier ::= SEQUENCE {
142 * algorithm OBJECT IDENTIFIER,
143 * parameters ANY DEFINED BY algorithm OPTIONAL }
144 *
145 * For HashAlgorithm, parameters MUST be NULL or absent.
146 */
147 static int x509_get_hash_alg( const mbedtls_x509_buf *alg, mbedtls_md_type_t *md_alg )
148 {
149 int ret;
150 unsigned char *p;
151 const unsigned char *end;
152 mbedtls_x509_buf md_oid;
153 size_t len;
154
155 /* Make sure we got a SEQUENCE and setup bounds */
156 if( alg->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
157 return( MBEDTLS_ERR_X509_INVALID_ALG +
158 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
159
160 p = (unsigned char *) alg->p;
161 end = p + alg->len;
162
163 if( p >= end )
164 return( MBEDTLS_ERR_X509_INVALID_ALG +
165 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
166
167 /* Parse md_oid */
168 md_oid.tag = *p;
169
170 if( ( ret = mbedtls_asn1_get_tag( &p, end, &md_oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
171 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
172
173 md_oid.p = p;
174 p += md_oid.len;
175
176 /* Get md_alg from md_oid */
177 if( ( ret = mbedtls_oid_get_md_alg( &md_oid, md_alg ) ) != 0 )
178 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
179
180 /* Make sure params is absent of NULL */
181 if( p == end )
182 return( 0 );
183
184 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_NULL ) ) != 0 || len != 0 )
185 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
186
187 if( p != end )
188 return( MBEDTLS_ERR_X509_INVALID_ALG +
189 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
190
191 return( 0 );
192 }
193
194 /*
195 * RSASSA-PSS-params ::= SEQUENCE {
196 * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
197 * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
198 * saltLength [2] INTEGER DEFAULT 20,
199 * trailerField [3] INTEGER DEFAULT 1 }
200 * -- Note that the tags in this Sequence are explicit.
201 *
202 * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
203 * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
204 * option. Enfore this at parsing time.
205 */
206 int mbedtls_x509_get_rsassa_pss_params( const mbedtls_x509_buf *params,
207 mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
208 int *salt_len )
209 {
210 int ret;
211 unsigned char *p;
212 const unsigned char *end, *end2;
213 size_t len;
214 mbedtls_x509_buf alg_id, alg_params;
215
216 /* First set everything to defaults */
217 *md_alg = MBEDTLS_MD_SHA1;
218 *mgf_md = MBEDTLS_MD_SHA1;
219 *salt_len = 20;
220
221 /* Make sure params is a SEQUENCE and setup bounds */
222 if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
223 return( MBEDTLS_ERR_X509_INVALID_ALG +
224 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
225
226 p = (unsigned char *) params->p;
227 end = p + params->len;
228
229 if( p == end )
230 return( 0 );
231
232 /*
233 * HashAlgorithm
234 */
235 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
236 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
237 {
238 end2 = p + len;
239
240 /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
241 if( ( ret = mbedtls_x509_get_alg_null( &p, end2, &alg_id ) ) != 0 )
242 return( ret );
243
244 if( ( ret = mbedtls_oid_get_md_alg( &alg_id, md_alg ) ) != 0 )
245 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
246
247 if( p != end2 )
248 return( MBEDTLS_ERR_X509_INVALID_ALG +
249 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
250 }
251 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
252 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
253
254 if( p == end )
255 return( 0 );
256
257 /*
258 * MaskGenAlgorithm
259 */
260 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
261 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
262 {
263 end2 = p + len;
264
265 /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
266 if( ( ret = mbedtls_x509_get_alg( &p, end2, &alg_id, &alg_params ) ) != 0 )
267 return( ret );
268
269 /* Only MFG1 is recognised for now */
270 if( MBEDTLS_OID_CMP( MBEDTLS_OID_MGF1, &alg_id ) != 0 )
271 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE +
272 MBEDTLS_ERR_OID_NOT_FOUND );
273
274 /* Parse HashAlgorithm */
275 if( ( ret = x509_get_hash_alg( &alg_params, mgf_md ) ) != 0 )
276 return( ret );
277
278 if( p != end2 )
279 return( MBEDTLS_ERR_X509_INVALID_ALG +
280 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
281 }
282 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
283 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
284
285 if( p == end )
286 return( 0 );
287
288 /*
289 * salt_len
290 */
291 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
292 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 2 ) ) == 0 )
293 {
294 end2 = p + len;
295
296 if( ( ret = mbedtls_asn1_get_int( &p, end2, salt_len ) ) != 0 )
297 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
298
299 if( p != end2 )
300 return( MBEDTLS_ERR_X509_INVALID_ALG +
301 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
302 }
303 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
304 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
305
306 if( p == end )
307 return( 0 );
308
309 /*
310 * trailer_field (if present, must be 1)
311 */
312 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
313 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 3 ) ) == 0 )
314 {
315 int trailer_field;
316
317 end2 = p + len;
318
319 if( ( ret = mbedtls_asn1_get_int( &p, end2, &trailer_field ) ) != 0 )
320 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
321
322 if( p != end2 )
323 return( MBEDTLS_ERR_X509_INVALID_ALG +
324 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
325
326 if( trailer_field != 1 )
327 return( MBEDTLS_ERR_X509_INVALID_ALG );
328 }
329 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
330 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
331
332 if( p != end )
333 return( MBEDTLS_ERR_X509_INVALID_ALG +
334 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
335
336 return( 0 );
337 }
338 #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
339
340 /*
341 * AttributeTypeAndValue ::= SEQUENCE {
342 * type AttributeType,
343 * value AttributeValue }
344 *
345 * AttributeType ::= OBJECT IDENTIFIER
346 *
347 * AttributeValue ::= ANY DEFINED BY AttributeType
348 */
349 static int x509_get_attr_type_value( unsigned char **p,
350 const unsigned char *end,
351 mbedtls_x509_name *cur )
352 {
353 int ret;
354 size_t len;
355 mbedtls_x509_buf *oid;
356 mbedtls_x509_buf *val;
357
358 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
359 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
360 return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
361
362 end = *p + len;
363
364 if( ( end - *p ) < 1 )
365 return( MBEDTLS_ERR_X509_INVALID_NAME +
366 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
367
368 oid = &cur->oid;
369 oid->tag = **p;
370
371 if( ( ret = mbedtls_asn1_get_tag( p, end, &oid->len, MBEDTLS_ASN1_OID ) ) != 0 )
372 return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
373
374 oid->p = *p;
375 *p += oid->len;
376
377 if( ( end - *p ) < 1 )
378 return( MBEDTLS_ERR_X509_INVALID_NAME +
379 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
380
381 if( **p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
382 **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
383 **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
384 **p != MBEDTLS_ASN1_BIT_STRING )
385 return( MBEDTLS_ERR_X509_INVALID_NAME +
386 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
387
388 val = &cur->val;
389 val->tag = *(*p)++;
390
391 if( ( ret = mbedtls_asn1_get_len( p, end, &val->len ) ) != 0 )
392 return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
393
394 val->p = *p;
395 *p += val->len;
396
397 if( *p != end )
398 {
399 return( MBEDTLS_ERR_X509_INVALID_NAME +
400 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
401 }
402
403 cur->next = NULL;
404
405 return( 0 );
406 }
407
408 /*
409 * Name ::= CHOICE { -- only one possibility for now --
410 * rdnSequence RDNSequence }
411 *
412 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
413 *
414 * RelativeDistinguishedName ::=
415 * SET OF AttributeTypeAndValue
416 *
417 * AttributeTypeAndValue ::= SEQUENCE {
418 * type AttributeType,
419 * value AttributeValue }
420 *
421 * AttributeType ::= OBJECT IDENTIFIER
422 *
423 * AttributeValue ::= ANY DEFINED BY AttributeType
424 *
425 * The data structure is optimized for the common case where each RDN has only
426 * one element, which is represented as a list of AttributeTypeAndValue.
427 * For the general case we still use a flat list, but we mark elements of the
428 * same set so that they are "merged" together in the functions that consume
429 * this list, eg mbedtls_x509_dn_gets().
430 */
431 int mbedtls_x509_get_name( unsigned char **p, const unsigned char *end,
432 mbedtls_x509_name *cur )
433 {
434 int ret;
435 size_t set_len;
436 const unsigned char *end_set;
437
438 /* don't use recursion, we'd risk stack overflow if not optimized */
439 while( 1 )
440 {
441 /*
442 * parse SET
443 */
444 if( ( ret = mbedtls_asn1_get_tag( p, end, &set_len,
445 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET ) ) != 0 )
446 return( MBEDTLS_ERR_X509_INVALID_NAME + ret );
447
448 end_set = *p + set_len;
449
450 while( 1 )
451 {
452 if( ( ret = x509_get_attr_type_value( p, end_set, cur ) ) != 0 )
453 return( ret );
454
455 if( *p == end_set )
456 break;
457
458 /* Mark this item as being no the only one in a set */
459 cur->next_merged = 1;
460
461 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
462
463 if( cur->next == NULL )
464 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
465
466 cur = cur->next;
467 }
468
469 /*
470 * continue until end of SEQUENCE is reached
471 */
472 if( *p == end )
473 return( 0 );
474
475 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_name ) );
476
477 if( cur->next == NULL )
478 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
479
480 cur = cur->next;
481 }
482 }
483
484 static int x509_parse_int( unsigned char **p, size_t n, int *res )
485 {
486 *res = 0;
487
488 for( ; n > 0; --n )
489 {
490 if( ( **p < '0') || ( **p > '9' ) )
491 return ( MBEDTLS_ERR_X509_INVALID_DATE );
492
493 *res *= 10;
494 *res += ( *(*p)++ - '0' );
495 }
496
497 return( 0 );
498 }
499
500 static int x509_date_is_valid(const mbedtls_x509_time *t )
501 {
502 int ret = MBEDTLS_ERR_X509_INVALID_DATE;
503 int month_len;
504
505 CHECK_RANGE( 0, 9999, t->year );
506 CHECK_RANGE( 0, 23, t->hour );
507 CHECK_RANGE( 0, 59, t->min );
508 CHECK_RANGE( 0, 59, t->sec );
509
510 switch( t->mon )
511 {
512 case 1: case 3: case 5: case 7: case 8: case 10: case 12:
513 month_len = 31;
514 break;
515 case 4: case 6: case 9: case 11:
516 month_len = 30;
517 break;
518 case 2:
519 if( ( !( t->year % 4 ) && t->year % 100 ) ||
520 !( t->year % 400 ) )
521 month_len = 29;
522 else
523 month_len = 28;
524 break;
525 default:
526 return( ret );
527 }
528 CHECK_RANGE( 1, month_len, t->day );
529
530 return( 0 );
531 }
532
533 /*
534 * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4)
535 * field.
536 */
537 static int x509_parse_time( unsigned char **p, size_t len, size_t yearlen,
538 mbedtls_x509_time *tm )
539 {
540 int ret;
541
542 /*
543 * Minimum length is 10 or 12 depending on yearlen
544 */
545 if ( len < yearlen + 8 )
546 return ( MBEDTLS_ERR_X509_INVALID_DATE );
547 len -= yearlen + 8;
548
549 /*
550 * Parse year, month, day, hour, minute
551 */
552 CHECK( x509_parse_int( p, yearlen, &tm->year ) );
553 if ( 2 == yearlen )
554 {
555 if ( tm->year < 50 )
556 tm->year += 100;
557
558 tm->year += 1900;
559 }
560
561 CHECK( x509_parse_int( p, 2, &tm->mon ) );
562 CHECK( x509_parse_int( p, 2, &tm->day ) );
563 CHECK( x509_parse_int( p, 2, &tm->hour ) );
564 CHECK( x509_parse_int( p, 2, &tm->min ) );
565
566 /*
567 * Parse seconds if present
568 */
569 if ( len >= 2 )
570 {
571 CHECK( x509_parse_int( p, 2, &tm->sec ) );
572 len -= 2;
573 }
574 else
575 return ( MBEDTLS_ERR_X509_INVALID_DATE );
576
577 /*
578 * Parse trailing 'Z' if present
579 */
580 if ( 1 == len && 'Z' == **p )
581 {
582 (*p)++;
583 len--;
584 }
585
586 /*
587 * We should have parsed all characters at this point
588 */
589 if ( 0 != len )
590 return ( MBEDTLS_ERR_X509_INVALID_DATE );
591
592 CHECK( x509_date_is_valid( tm ) );
593
594 return ( 0 );
595 }
596
597 /*
598 * Time ::= CHOICE {
599 * utcTime UTCTime,
600 * generalTime GeneralizedTime }
601 */
602 int mbedtls_x509_get_time( unsigned char **p, const unsigned char *end,
603 mbedtls_x509_time *tm )
604 {
605 int ret;
606 size_t len, year_len;
607 unsigned char tag;
608
609 if( ( end - *p ) < 1 )
610 return( MBEDTLS_ERR_X509_INVALID_DATE +
611 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
612
613 tag = **p;
614
615 if( tag == MBEDTLS_ASN1_UTC_TIME )
616 year_len = 2;
617 else if( tag == MBEDTLS_ASN1_GENERALIZED_TIME )
618 year_len = 4;
619 else
620 return( MBEDTLS_ERR_X509_INVALID_DATE +
621 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
622
623 (*p)++;
624 ret = mbedtls_asn1_get_len( p, end, &len );
625
626 if( ret != 0 )
627 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
628
629 return x509_parse_time( p, len, year_len, tm );
630 }
631
632 int mbedtls_x509_get_sig( unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig )
633 {
634 int ret;
635 size_t len;
636 int tag_type;
637
638 if( ( end - *p ) < 1 )
639 return( MBEDTLS_ERR_X509_INVALID_SIGNATURE +
640 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
641
642 tag_type = **p;
643
644 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
645 return( MBEDTLS_ERR_X509_INVALID_SIGNATURE + ret );
646
647 sig->tag = tag_type;
648 sig->len = len;
649 sig->p = *p;
650
651 *p += len;
652
653 return( 0 );
654 }
655
656 /*
657 * Get signature algorithm from alg OID and optional parameters
658 */
659 int mbedtls_x509_get_sig_alg( const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
660 mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
661 void **sig_opts )
662 {
663 int ret;
664
665 if( *sig_opts != NULL )
666 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
667
668 if( ( ret = mbedtls_oid_get_sig_alg( sig_oid, md_alg, pk_alg ) ) != 0 )
669 return( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + ret );
670
671 #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
672 if( *pk_alg == MBEDTLS_PK_RSASSA_PSS )
673 {
674 mbedtls_pk_rsassa_pss_options *pss_opts;
675
676 pss_opts = mbedtls_calloc( 1, sizeof( mbedtls_pk_rsassa_pss_options ) );
677 if( pss_opts == NULL )
678 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
679
680 ret = mbedtls_x509_get_rsassa_pss_params( sig_params,
681 md_alg,
682 &pss_opts->mgf1_hash_id,
683 &pss_opts->expected_salt_len );
684 if( ret != 0 )
685 {
686 mbedtls_free( pss_opts );
687 return( ret );
688 }
689
690 *sig_opts = (void *) pss_opts;
691 }
692 else
693 #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
694 {
695 /* Make sure parameters are absent or NULL */
696 if( ( sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0 ) ||
697 sig_params->len != 0 )
698 return( MBEDTLS_ERR_X509_INVALID_ALG );
699 }
700
701 return( 0 );
702 }
703
704 /*
705 * X.509 Extensions (No parsing of extensions, pointer should
706 * be either manually updated or extensions should be parsed!)
707 */
708 int mbedtls_x509_get_ext( unsigned char **p, const unsigned char *end,
709 mbedtls_x509_buf *ext, int tag )
710 {
711 int ret;
712 size_t len;
713
714 /* Extension structure use EXPLICIT tagging. That is, the actual
715 * `Extensions` structure is wrapped by a tag-length pair using
716 * the respective context-specific tag. */
717 ret = mbedtls_asn1_get_tag( p, end, &ext->len,
718 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag );
719 if( ret != 0 )
720 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
721
722 ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag;
723 ext->p = *p;
724 end = *p + ext->len;
725
726 /*
727 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
728 */
729 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
730 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
731 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
732
733 if( end != *p + len )
734 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
735 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
736
737 return( 0 );
738 }
739
740 /*
741 * Store the name in printable form into buf; no more
742 * than size characters will be written
743 */
744 int mbedtls_x509_dn_gets( char *buf, size_t size, const mbedtls_x509_name *dn )
745 {
746 int ret;
747 size_t i, n;
748 unsigned char c, merge = 0;
749 const mbedtls_x509_name *name;
750 const char *short_name = NULL;
751 char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
752
753 memset( s, 0, sizeof( s ) );
754
755 name = dn;
756 p = buf;
757 n = size;
758
759 while( name != NULL )
760 {
761 if( !name->oid.p )
762 {
763 name = name->next;
764 continue;
765 }
766
767 if( name != dn )
768 {
769 ret = mbedtls_snprintf( p, n, merge ? " + " : ", " );
770 MBEDTLS_X509_SAFE_SNPRINTF;
771 }
772
773 ret = mbedtls_oid_get_attr_short_name( &name->oid, &short_name );
774
775 if( ret == 0 )
776 ret = mbedtls_snprintf( p, n, "%s=", short_name );
777 else
778 ret = mbedtls_snprintf( p, n, "\?\?=" );
779 MBEDTLS_X509_SAFE_SNPRINTF;
780
781 for( i = 0; i < name->val.len; i++ )
782 {
783 if( i >= sizeof( s ) - 1 )
784 break;
785
786 c = name->val.p[i];
787 if( c < 32 || c == 127 || ( c > 128 && c < 160 ) )
788 s[i] = '?';
789 else s[i] = c;
790 }
791 s[i] = '\0';
792 ret = mbedtls_snprintf( p, n, "%s", s );
793 MBEDTLS_X509_SAFE_SNPRINTF;
794
795 merge = name->next_merged;
796 name = name->next;
797 }
798
799 return( (int) ( size - n ) );
800 }
801
802 /*
803 * Store the serial in printable form into buf; no more
804 * than size characters will be written
805 */
806 int mbedtls_x509_serial_gets( char *buf, size_t size, const mbedtls_x509_buf *serial )
807 {
808 int ret;
809 size_t i, n, nr;
810 char *p;
811
812 p = buf;
813 n = size;
814
815 nr = ( serial->len <= 32 )
816 ? serial->len : 28;
817
818 for( i = 0; i < nr; i++ )
819 {
820 if( i == 0 && nr > 1 && serial->p[i] == 0x0 )
821 continue;
822
823 ret = mbedtls_snprintf( p, n, "%02X%s",
824 serial->p[i], ( i < nr - 1 ) ? ":" : "" );
825 MBEDTLS_X509_SAFE_SNPRINTF;
826 }
827
828 if( nr != serial->len )
829 {
830 ret = mbedtls_snprintf( p, n, "...." );
831 MBEDTLS_X509_SAFE_SNPRINTF;
832 }
833
834 return( (int) ( size - n ) );
835 }
836
837 /*
838 * Helper for writing signature algorithms
839 */
840 int mbedtls_x509_sig_alg_gets( char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
841 mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
842 const void *sig_opts )
843 {
844 int ret;
845 char *p = buf;
846 size_t n = size;
847 const char *desc = NULL;
848
849 ret = mbedtls_oid_get_sig_alg_desc( sig_oid, &desc );
850 if( ret != 0 )
851 ret = mbedtls_snprintf( p, n, "???" );
852 else
853 ret = mbedtls_snprintf( p, n, "%s", desc );
854 MBEDTLS_X509_SAFE_SNPRINTF;
855
856 #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
857 if( pk_alg == MBEDTLS_PK_RSASSA_PSS )
858 {
859 const mbedtls_pk_rsassa_pss_options *pss_opts;
860 const mbedtls_md_info_t *md_info, *mgf_md_info;
861
862 pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts;
863
864 md_info = mbedtls_md_info_from_type( md_alg );
865 mgf_md_info = mbedtls_md_info_from_type( pss_opts->mgf1_hash_id );
866
867 ret = mbedtls_snprintf( p, n, " (%s, MGF1-%s, 0x%02X)",
868 md_info ? mbedtls_md_get_name( md_info ) : "???",
869 mgf_md_info ? mbedtls_md_get_name( mgf_md_info ) : "???",
870 pss_opts->expected_salt_len );
871 MBEDTLS_X509_SAFE_SNPRINTF;
872 }
873 #else
874 ((void) pk_alg);
875 ((void) md_alg);
876 ((void) sig_opts);
877 #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
878
879 return( (int)( size - n ) );
880 }
881
882 /*
883 * Helper for writing "RSA key size", "EC key size", etc
884 */
885 int mbedtls_x509_key_size_helper( char *buf, size_t buf_size, const char *name )
886 {
887 char *p = buf;
888 size_t n = buf_size;
889 int ret;
890
891 ret = mbedtls_snprintf( p, n, "%s key size", name );
892 MBEDTLS_X509_SAFE_SNPRINTF;
893
894 return( 0 );
895 }
896
897 #if defined(MBEDTLS_HAVE_TIME_DATE)
898 /*
899 * Set the time structure to the current time.
900 * Return 0 on success, non-zero on failure.
901 */
902 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
903 static int x509_get_current_time( mbedtls_x509_time *now )
904 {
905 SYSTEMTIME st;
906
907 GetSystemTime( &st );
908
909 now->year = st.wYear;
910 now->mon = st.wMonth;
911 now->day = st.wDay;
912 now->hour = st.wHour;
913 now->min = st.wMinute;
914 now->sec = st.wSecond;
915
916 return( 0 );
917 }
918 #else
919 static int x509_get_current_time( mbedtls_x509_time *now )
920 {
921 struct tm *lt;
922 mbedtls_time_t tt;
923 int ret = 0;
924
925 #if defined(MBEDTLS_THREADING_C)
926 if( mbedtls_mutex_lock( &mbedtls_threading_gmtime_mutex ) != 0 )
927 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
928 #endif
929
930 tt = mbedtls_time( NULL );
931 lt = gmtime( &tt );
932
933 if( lt == NULL )
934 ret = -1;
935 else
936 {
937 now->year = lt->tm_year + 1900;
938 now->mon = lt->tm_mon + 1;
939 now->day = lt->tm_mday;
940 now->hour = lt->tm_hour;
941 now->min = lt->tm_min;
942 now->sec = lt->tm_sec;
943 }
944
945 #if defined(MBEDTLS_THREADING_C)
946 if( mbedtls_mutex_unlock( &mbedtls_threading_gmtime_mutex ) != 0 )
947 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
948 #endif
949
950 return( ret );
951 }
952 #endif /* _WIN32 && !EFIX64 && !EFI32 */
953
954 /*
955 * Return 0 if before <= after, 1 otherwise
956 */
957 static int x509_check_time( const mbedtls_x509_time *before, const mbedtls_x509_time *after )
958 {
959 if( before->year > after->year )
960 return( 1 );
961
962 if( before->year == after->year &&
963 before->mon > after->mon )
964 return( 1 );
965
966 if( before->year == after->year &&
967 before->mon == after->mon &&
968 before->day > after->day )
969 return( 1 );
970
971 if( before->year == after->year &&
972 before->mon == after->mon &&
973 before->day == after->day &&
974 before->hour > after->hour )
975 return( 1 );
976
977 if( before->year == after->year &&
978 before->mon == after->mon &&
979 before->day == after->day &&
980 before->hour == after->hour &&
981 before->min > after->min )
982 return( 1 );
983
984 if( before->year == after->year &&
985 before->mon == after->mon &&
986 before->day == after->day &&
987 before->hour == after->hour &&
988 before->min == after->min &&
989 before->sec > after->sec )
990 return( 1 );
991
992 return( 0 );
993 }
994
995 int mbedtls_x509_time_is_past( const mbedtls_x509_time *to )
996 {
997 mbedtls_x509_time now;
998
999 if( x509_get_current_time( &now ) != 0 )
1000 return( 1 );
1001
1002 return( x509_check_time( &now, to ) );
1003 }
1004
1005 int mbedtls_x509_time_is_future( const mbedtls_x509_time *from )
1006 {
1007 mbedtls_x509_time now;
1008
1009 if( x509_get_current_time( &now ) != 0 )
1010 return( 1 );
1011
1012 return( x509_check_time( from, &now ) );
1013 }
1014
1015 #else /* MBEDTLS_HAVE_TIME_DATE */
1016
1017 int mbedtls_x509_time_is_past( const mbedtls_x509_time *to )
1018 {
1019 ((void) to);
1020 return( 0 );
1021 }
1022
1023 int mbedtls_x509_time_is_future( const mbedtls_x509_time *from )
1024 {
1025 ((void) from);
1026 return( 0 );
1027 }
1028 #endif /* MBEDTLS_HAVE_TIME_DATE */
1029
1030 #if defined(MBEDTLS_SELF_TEST)
1031
1032 #include "mbedtls/x509_crt.h"
1033 #include "mbedtls/certs.h"
1034
1035 /*
1036 * Checkup routine
1037 */
1038 int mbedtls_x509_self_test( int verbose )
1039 {
1040 int ret = 0;
1041 #if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_SHA256_C)
1042 uint32_t flags;
1043 mbedtls_x509_crt cacert;
1044 mbedtls_x509_crt clicert;
1045
1046 if( verbose != 0 )
1047 mbedtls_printf( " X.509 certificate load: " );
1048
1049 mbedtls_x509_crt_init( &cacert );
1050 mbedtls_x509_crt_init( &clicert );
1051
1052 ret = mbedtls_x509_crt_parse( &clicert, (const unsigned char *) mbedtls_test_cli_crt,
1053 mbedtls_test_cli_crt_len );
1054 if( ret != 0 )
1055 {
1056 if( verbose != 0 )
1057 mbedtls_printf( "failed\n" );
1058
1059 goto cleanup;
1060 }
1061
1062 ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) mbedtls_test_ca_crt,
1063 mbedtls_test_ca_crt_len );
1064 if( ret != 0 )
1065 {
1066 if( verbose != 0 )
1067 mbedtls_printf( "failed\n" );
1068
1069 goto cleanup;
1070 }
1071
1072 if( verbose != 0 )
1073 mbedtls_printf( "passed\n X.509 signature verify: ");
1074
1075 ret = mbedtls_x509_crt_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL );
1076 if( ret != 0 )
1077 {
1078 if( verbose != 0 )
1079 mbedtls_printf( "failed\n" );
1080
1081 goto cleanup;
1082 }
1083
1084 if( verbose != 0 )
1085 mbedtls_printf( "passed\n\n");
1086
1087 cleanup:
1088 mbedtls_x509_crt_free( &cacert );
1089 mbedtls_x509_crt_free( &clicert );
1090 #else
1091 ((void) verbose);
1092 #endif /* MBEDTLS_CERTS_C && MBEDTLS_SHA1_C */
1093 return( ret );
1094 }
1095
1096 #endif /* MBEDTLS_SELF_TEST */
1097
1098 #endif /* MBEDTLS_X509_USE_C */