1 /* 2 * X.509 certificate parsing and verification 3 * 4 * Copyright The Mbed TLS Contributors 5 * SPDX-License-Identifier: Apache-2.0 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may 8 * not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 */ 19 /* 20 * The ITU-T X.509 standard defines a certificate format for PKI. 21 * 22 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs) 23 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs) 24 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10) 25 * 26 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf 27 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf 28 * 29 * [SIRO] https://cabforum.org/wp-content/uploads/Chunghwatelecom201503cabforumV4.pdf 30 */ 31 32 #include "common.h" 33 34 #if defined(MBEDTLS_X509_CRT_PARSE_C) 35 36 #include "mbedtls/x509_crt.h" 37 #include "mbedtls/error.h" 38 #include "mbedtls/oid.h" 39 #include "mbedtls/platform_util.h" 40 41 #include <string.h> 42 43 #if defined(MBEDTLS_PEM_PARSE_C) 44 #include "mbedtls/pem.h" 45 #endif 46 47 #if defined(MBEDTLS_USE_PSA_CRYPTO) 48 #include "psa/crypto.h" 49 #include "mbedtls/psa_util.h" 50 #endif 51 52 #if defined(MBEDTLS_PLATFORM_C) 53 #include "mbedtls/platform.h" 54 #else 55 #include <stdio.h> 56 #include <stdlib.h> 57 #define mbedtls_free free 58 #define mbedtls_calloc calloc 59 #define mbedtls_snprintf snprintf 60 #endif 61 62 #if defined(MBEDTLS_THREADING_C) 63 #include "mbedtls/threading.h" 64 #endif 65 66 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) 67 #include <windows.h> 68 #else 69 #include <time.h> 70 #endif 71 72 #if defined(MBEDTLS_FS_IO) 73 #include <stdio.h> 74 #if !defined(_WIN32) || defined(EFIX64) || defined(EFI32) 75 #include <sys/types.h> 76 #include <sys/stat.h> 77 #include <dirent.h> 78 #endif /* !_WIN32 || EFIX64 || EFI32 */ 79 #endif 80 81 /* 82 * Item in a verification chain: cert and flags for it 83 */ 84 typedef struct { 85 mbedtls_x509_crt *crt; 86 uint32_t flags; 87 } x509_crt_verify_chain_item; 88 89 /* 90 * Max size of verification chain: end-entity + intermediates + trusted root 91 */ 92 #define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 ) 93 94 /* Default profile. Do not remove items unless there are serious security 95 * concerns. */ 96 const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default = 97 { 98 #if defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_CERTIFICATES) 99 /* Allow SHA-1 (weak, but still safe in controlled environments) */ 100 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) | 101 #endif 102 /* Only SHA-2 hashes */ 103 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) | 104 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) | 105 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) | 106 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ), 107 0xFFFFFFF, /* Any PK alg */ 108 0xFFFFFFF, /* Any curve */ 109 2048, 110 }; 111 112 /* 113 * Next-default profile 114 */ 115 const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next = 116 { 117 /* Hashes from SHA-256 and above */ 118 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) | 119 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) | 120 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ), 121 0xFFFFFFF, /* Any PK alg */ 122 #if defined(MBEDTLS_ECP_C) 123 /* Curves at or above 128-bit security level */ 124 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) | 125 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) | 126 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) | 127 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) | 128 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) | 129 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) | 130 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ), 131 #else 132 0, 133 #endif 134 2048, 135 }; 136 137 /* 138 * NSA Suite B Profile 139 */ 140 const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb = 141 { 142 /* Only SHA-256 and 384 */ 143 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) | 144 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ), 145 /* Only ECDSA */ 146 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) | 147 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ), 148 #if defined(MBEDTLS_ECP_C) 149 /* Only NIST P-256 and P-384 */ 150 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) | 151 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ), 152 #else 153 0, 154 #endif 155 0, 156 }; 157 158 /* 159 * Check md_alg against profile 160 * Return 0 if md_alg is acceptable for this profile, -1 otherwise 161 */ 162 static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile, 163 mbedtls_md_type_t md_alg ) 164 { 165 if( md_alg == MBEDTLS_MD_NONE ) 166 return( -1 ); 167 168 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 ) 169 return( 0 ); 170 171 return( -1 ); 172 } 173 174 /* 175 * Check pk_alg against profile 176 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise 177 */ 178 static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile, 179 mbedtls_pk_type_t pk_alg ) 180 { 181 if( pk_alg == MBEDTLS_PK_NONE ) 182 return( -1 ); 183 184 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 ) 185 return( 0 ); 186 187 return( -1 ); 188 } 189 190 /* 191 * Check key against profile 192 * Return 0 if pk is acceptable for this profile, -1 otherwise 193 */ 194 static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile, 195 const mbedtls_pk_context *pk ) 196 { 197 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk ); 198 199 #if defined(MBEDTLS_RSA_C) 200 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS ) 201 { 202 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen ) 203 return( 0 ); 204 205 return( -1 ); 206 } 207 #endif 208 209 #if defined(MBEDTLS_ECP_C) 210 if( pk_alg == MBEDTLS_PK_ECDSA || 211 pk_alg == MBEDTLS_PK_ECKEY || 212 pk_alg == MBEDTLS_PK_ECKEY_DH ) 213 { 214 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id; 215 216 if( gid == MBEDTLS_ECP_DP_NONE ) 217 return( -1 ); 218 219 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 ) 220 return( 0 ); 221 222 return( -1 ); 223 } 224 #endif 225 226 return( -1 ); 227 } 228 229 /* 230 * Like memcmp, but case-insensitive and always returns -1 if different 231 */ 232 static int x509_memcasecmp( const void *s1, const void *s2, size_t len ) 233 { 234 size_t i; 235 unsigned char diff; 236 const unsigned char *n1 = s1, *n2 = s2; 237 238 for( i = 0; i < len; i++ ) 239 { 240 diff = n1[i] ^ n2[i]; 241 242 if( diff == 0 ) 243 continue; 244 245 if( diff == 32 && 246 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) || 247 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) ) 248 { 249 continue; 250 } 251 252 return( -1 ); 253 } 254 255 return( 0 ); 256 } 257 258 /* 259 * Return 0 if name matches wildcard, -1 otherwise 260 */ 261 static int x509_check_wildcard( const char *cn, const mbedtls_x509_buf *name ) 262 { 263 size_t i; 264 size_t cn_idx = 0, cn_len = strlen( cn ); 265 266 /* We can't have a match if there is no wildcard to match */ 267 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' ) 268 return( -1 ); 269 270 for( i = 0; i < cn_len; ++i ) 271 { 272 if( cn[i] == '.' ) 273 { 274 cn_idx = i; 275 break; 276 } 277 } 278 279 if( cn_idx == 0 ) 280 return( -1 ); 281 282 if( cn_len - cn_idx == name->len - 1 && 283 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 ) 284 { 285 return( 0 ); 286 } 287 288 return( -1 ); 289 } 290 291 /* 292 * Compare two X.509 strings, case-insensitive, and allowing for some encoding 293 * variations (but not all). 294 * 295 * Return 0 if equal, -1 otherwise. 296 */ 297 static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b ) 298 { 299 if( a->tag == b->tag && 300 a->len == b->len && 301 memcmp( a->p, b->p, b->len ) == 0 ) 302 { 303 return( 0 ); 304 } 305 306 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) && 307 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) && 308 a->len == b->len && 309 x509_memcasecmp( a->p, b->p, b->len ) == 0 ) 310 { 311 return( 0 ); 312 } 313 314 return( -1 ); 315 } 316 317 /* 318 * Compare two X.509 Names (aka rdnSequence). 319 * 320 * See RFC 5280 section 7.1, though we don't implement the whole algorithm: 321 * we sometimes return unequal when the full algorithm would return equal, 322 * but never the other way. (In particular, we don't do Unicode normalisation 323 * or space folding.) 324 * 325 * Return 0 if equal, -1 otherwise. 326 */ 327 static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b ) 328 { 329 /* Avoid recursion, it might not be optimised by the compiler */ 330 while( a != NULL || b != NULL ) 331 { 332 if( a == NULL || b == NULL ) 333 return( -1 ); 334 335 /* type */ 336 if( a->oid.tag != b->oid.tag || 337 a->oid.len != b->oid.len || 338 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 ) 339 { 340 return( -1 ); 341 } 342 343 /* value */ 344 if( x509_string_cmp( &a->val, &b->val ) != 0 ) 345 return( -1 ); 346 347 /* structure of the list of sets */ 348 if( a->next_merged != b->next_merged ) 349 return( -1 ); 350 351 a = a->next; 352 b = b->next; 353 } 354 355 /* a == NULL == b */ 356 return( 0 ); 357 } 358 359 /* 360 * Reset (init or clear) a verify_chain 361 */ 362 static void x509_crt_verify_chain_reset( 363 mbedtls_x509_crt_verify_chain *ver_chain ) 364 { 365 size_t i; 366 367 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ ) 368 { 369 ver_chain->items[i].crt = NULL; 370 ver_chain->items[i].flags = (uint32_t) -1; 371 } 372 373 ver_chain->len = 0; 374 375 #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) 376 ver_chain->trust_ca_cb_result = NULL; 377 #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ 378 } 379 380 /* 381 * Version ::= INTEGER { v1(0), v2(1), v3(2) } 382 */ 383 static int x509_get_version( unsigned char **p, 384 const unsigned char *end, 385 int *ver ) 386 { 387 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 388 size_t len; 389 390 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, 391 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 ) 392 { 393 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) 394 { 395 *ver = 0; 396 return( 0 ); 397 } 398 399 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) ); 400 } 401 402 end = *p + len; 403 404 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 ) 405 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION, ret ) ); 406 407 if( *p != end ) 408 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION, 409 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); 410 411 return( 0 ); 412 } 413 414 /* 415 * Validity ::= SEQUENCE { 416 * notBefore Time, 417 * notAfter Time } 418 */ 419 static int x509_get_dates( unsigned char **p, 420 const unsigned char *end, 421 mbedtls_x509_time *from, 422 mbedtls_x509_time *to ) 423 { 424 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 425 size_t len; 426 427 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, 428 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) 429 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, ret ) ); 430 431 end = *p + len; 432 433 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 ) 434 return( ret ); 435 436 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 ) 437 return( ret ); 438 439 if( *p != end ) 440 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, 441 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); 442 443 return( 0 ); 444 } 445 446 /* 447 * X.509 v2/v3 unique identifier (not parsed) 448 */ 449 static int x509_get_uid( unsigned char **p, 450 const unsigned char *end, 451 mbedtls_x509_buf *uid, int n ) 452 { 453 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 454 455 if( *p == end ) 456 return( 0 ); 457 458 uid->tag = **p; 459 460 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len, 461 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 ) 462 { 463 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) 464 return( 0 ); 465 466 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) ); 467 } 468 469 uid->p = *p; 470 *p += uid->len; 471 472 return( 0 ); 473 } 474 475 static int x509_get_basic_constraints( unsigned char **p, 476 const unsigned char *end, 477 int *ca_istrue, 478 int *max_pathlen ) 479 { 480 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 481 size_t len; 482 483 /* 484 * BasicConstraints ::= SEQUENCE { 485 * cA BOOLEAN DEFAULT FALSE, 486 * pathLenConstraint INTEGER (0..MAX) OPTIONAL } 487 */ 488 *ca_istrue = 0; /* DEFAULT FALSE */ 489 *max_pathlen = 0; /* endless */ 490 491 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, 492 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) 493 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); 494 495 if( *p == end ) 496 return( 0 ); 497 498 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 ) 499 { 500 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) 501 ret = mbedtls_asn1_get_int( p, end, ca_istrue ); 502 503 if( ret != 0 ) 504 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); 505 506 if( *ca_istrue != 0 ) 507 *ca_istrue = 1; 508 } 509 510 if( *p == end ) 511 return( 0 ); 512 513 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 ) 514 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); 515 516 if( *p != end ) 517 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, 518 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); 519 520 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer 521 * overflow, which is an undefined behavior. */ 522 if( *max_pathlen == INT_MAX ) 523 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, 524 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) ); 525 526 (*max_pathlen)++; 527 528 return( 0 ); 529 } 530 531 static int x509_get_ns_cert_type( unsigned char **p, 532 const unsigned char *end, 533 unsigned char *ns_cert_type) 534 { 535 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 536 mbedtls_x509_bitstring bs = { 0, 0, NULL }; 537 538 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 ) 539 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); 540 541 if( bs.len != 1 ) 542 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, 543 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) ); 544 545 /* Get actual bitstring */ 546 *ns_cert_type = *bs.p; 547 return( 0 ); 548 } 549 550 static int x509_get_key_usage( unsigned char **p, 551 const unsigned char *end, 552 unsigned int *key_usage) 553 { 554 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 555 size_t i; 556 mbedtls_x509_bitstring bs = { 0, 0, NULL }; 557 558 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 ) 559 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); 560 561 if( bs.len < 1 ) 562 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, 563 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) ); 564 565 /* Get actual bitstring */ 566 *key_usage = 0; 567 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ ) 568 { 569 *key_usage |= (unsigned int) bs.p[i] << (8*i); 570 } 571 572 return( 0 ); 573 } 574 575 /* 576 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId 577 * 578 * KeyPurposeId ::= OBJECT IDENTIFIER 579 */ 580 static int x509_get_ext_key_usage( unsigned char **p, 581 const unsigned char *end, 582 mbedtls_x509_sequence *ext_key_usage) 583 { 584 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 585 586 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 ) 587 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); 588 589 /* Sequence length must be >= 1 */ 590 if( ext_key_usage->buf.p == NULL ) 591 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, 592 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) ); 593 594 return( 0 ); 595 } 596 597 /* 598 * SubjectAltName ::= GeneralNames 599 * 600 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName 601 * 602 * GeneralName ::= CHOICE { 603 * otherName [0] OtherName, 604 * rfc822Name [1] IA5String, 605 * dNSName [2] IA5String, 606 * x400Address [3] ORAddress, 607 * directoryName [4] Name, 608 * ediPartyName [5] EDIPartyName, 609 * uniformResourceIdentifier [6] IA5String, 610 * iPAddress [7] OCTET STRING, 611 * registeredID [8] OBJECT IDENTIFIER } 612 * 613 * OtherName ::= SEQUENCE { 614 * type-id OBJECT IDENTIFIER, 615 * value [0] EXPLICIT ANY DEFINED BY type-id } 616 * 617 * EDIPartyName ::= SEQUENCE { 618 * nameAssigner [0] DirectoryString OPTIONAL, 619 * partyName [1] DirectoryString } 620 * 621 * NOTE: we list all types, but only use dNSName and otherName 622 * of type HwModuleName, as defined in RFC 4108, at this point. 623 */ 624 static int x509_get_subject_alt_name( unsigned char **p, 625 const unsigned char *end, 626 mbedtls_x509_sequence *subject_alt_name ) 627 { 628 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 629 size_t len, tag_len; 630 mbedtls_asn1_buf *buf; 631 unsigned char tag; 632 mbedtls_asn1_sequence *cur = subject_alt_name; 633 634 /* Get main sequence tag */ 635 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, 636 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) 637 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); 638 639 if( *p + len != end ) 640 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, 641 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); 642 643 while( *p < end ) 644 { 645 mbedtls_x509_subject_alternative_name dummy_san_buf; 646 memset( &dummy_san_buf, 0, sizeof( dummy_san_buf ) ); 647 648 tag = **p; 649 (*p)++; 650 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 ) 651 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); 652 653 if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) != 654 MBEDTLS_ASN1_CONTEXT_SPECIFIC ) 655 { 656 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, 657 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) ); 658 } 659 660 /* 661 * Check that the SAN is structured correctly. 662 */ 663 ret = mbedtls_x509_parse_subject_alt_name( &(cur->buf), &dummy_san_buf ); 664 /* 665 * In case the extension is malformed, return an error, 666 * and clear the allocated sequences. 667 */ 668 if( ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE ) 669 { 670 mbedtls_x509_sequence *seq_cur = subject_alt_name->next; 671 mbedtls_x509_sequence *seq_prv; 672 while( seq_cur != NULL ) 673 { 674 seq_prv = seq_cur; 675 seq_cur = seq_cur->next; 676 mbedtls_platform_zeroize( seq_prv, 677 sizeof( mbedtls_x509_sequence ) ); 678 mbedtls_free( seq_prv ); 679 } 680 subject_alt_name->next = NULL; 681 return( ret ); 682 } 683 684 /* Allocate and assign next pointer */ 685 if( cur->buf.p != NULL ) 686 { 687 if( cur->next != NULL ) 688 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS ); 689 690 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) ); 691 692 if( cur->next == NULL ) 693 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, 694 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) ); 695 696 cur = cur->next; 697 } 698 699 buf = &(cur->buf); 700 buf->tag = tag; 701 buf->p = *p; 702 buf->len = tag_len; 703 *p += buf->len; 704 } 705 706 /* Set final sequence entry's next pointer to NULL */ 707 cur->next = NULL; 708 709 if( *p != end ) 710 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, 711 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); 712 713 return( 0 ); 714 } 715 716 /* 717 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 } 718 * 719 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 } 720 * 721 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation 722 * 723 * PolicyInformation ::= SEQUENCE { 724 * policyIdentifier CertPolicyId, 725 * policyQualifiers SEQUENCE SIZE (1..MAX) OF 726 * PolicyQualifierInfo OPTIONAL } 727 * 728 * CertPolicyId ::= OBJECT IDENTIFIER 729 * 730 * PolicyQualifierInfo ::= SEQUENCE { 731 * policyQualifierId PolicyQualifierId, 732 * qualifier ANY DEFINED BY policyQualifierId } 733 * 734 * -- policyQualifierIds for Internet policy qualifiers 735 * 736 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 } 737 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 } 738 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 } 739 * 740 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice ) 741 * 742 * Qualifier ::= CHOICE { 743 * cPSuri CPSuri, 744 * userNotice UserNotice } 745 * 746 * CPSuri ::= IA5String 747 * 748 * UserNotice ::= SEQUENCE { 749 * noticeRef NoticeReference OPTIONAL, 750 * explicitText DisplayText OPTIONAL } 751 * 752 * NoticeReference ::= SEQUENCE { 753 * organization DisplayText, 754 * noticeNumbers SEQUENCE OF INTEGER } 755 * 756 * DisplayText ::= CHOICE { 757 * ia5String IA5String (SIZE (1..200)), 758 * visibleString VisibleString (SIZE (1..200)), 759 * bmpString BMPString (SIZE (1..200)), 760 * utf8String UTF8String (SIZE (1..200)) } 761 * 762 * NOTE: we only parse and use anyPolicy without qualifiers at this point 763 * as defined in RFC 5280. 764 */ 765 static int x509_get_certificate_policies( unsigned char **p, 766 const unsigned char *end, 767 mbedtls_x509_sequence *certificate_policies ) 768 { 769 int ret, parse_ret = 0; 770 size_t len; 771 mbedtls_asn1_buf *buf; 772 mbedtls_asn1_sequence *cur = certificate_policies; 773 774 /* Get main sequence tag */ 775 ret = mbedtls_asn1_get_tag( p, end, &len, 776 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ); 777 if( ret != 0 ) 778 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); 779 780 if( *p + len != end ) 781 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, 782 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); 783 784 /* 785 * Cannot be an empty sequence. 786 */ 787 if( len == 0 ) 788 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, 789 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); 790 791 while( *p < end ) 792 { 793 mbedtls_x509_buf policy_oid; 794 const unsigned char *policy_end; 795 796 /* 797 * Get the policy sequence 798 */ 799 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, 800 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) 801 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); 802 803 policy_end = *p + len; 804 805 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len, 806 MBEDTLS_ASN1_OID ) ) != 0 ) 807 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); 808 809 policy_oid.tag = MBEDTLS_ASN1_OID; 810 policy_oid.len = len; 811 policy_oid.p = *p; 812 813 /* 814 * Only AnyPolicy is currently supported when enforcing policy. 815 */ 816 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_POLICY, &policy_oid ) != 0 ) 817 { 818 /* 819 * Set the parsing return code but continue parsing, in case this 820 * extension is critical and MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION 821 * is configured. 822 */ 823 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE; 824 } 825 826 /* Allocate and assign next pointer */ 827 if( cur->buf.p != NULL ) 828 { 829 if( cur->next != NULL ) 830 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS ); 831 832 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) ); 833 834 if( cur->next == NULL ) 835 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, 836 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) ); 837 838 cur = cur->next; 839 } 840 841 buf = &( cur->buf ); 842 buf->tag = policy_oid.tag; 843 buf->p = policy_oid.p; 844 buf->len = policy_oid.len; 845 846 *p += len; 847 848 /* 849 * If there is an optional qualifier, then *p < policy_end 850 * Check the Qualifier len to verify it doesn't exceed policy_end. 851 */ 852 if( *p < policy_end ) 853 { 854 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len, 855 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) 856 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); 857 /* 858 * Skip the optional policy qualifiers. 859 */ 860 *p += len; 861 } 862 863 if( *p != policy_end ) 864 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, 865 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); 866 } 867 868 /* Set final sequence entry's next pointer to NULL */ 869 cur->next = NULL; 870 871 if( *p != end ) 872 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, 873 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); 874 875 return( parse_ret ); 876 } 877 878 /* 879 * X.509 v3 extensions 880 * 881 */ 882 static int x509_get_crt_ext( unsigned char **p, 883 const unsigned char *end, 884 mbedtls_x509_crt *crt, 885 mbedtls_x509_crt_ext_cb_t cb, 886 void *p_ctx ) 887 { 888 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 889 size_t len; 890 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet; 891 892 if( *p == end ) 893 return( 0 ); 894 895 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 ) 896 return( ret ); 897 898 end = crt->v3_ext.p + crt->v3_ext.len; 899 while( *p < end ) 900 { 901 /* 902 * Extension ::= SEQUENCE { 903 * extnID OBJECT IDENTIFIER, 904 * critical BOOLEAN DEFAULT FALSE, 905 * extnValue OCTET STRING } 906 */ 907 mbedtls_x509_buf extn_oid = {0, 0, NULL}; 908 int is_critical = 0; /* DEFAULT FALSE */ 909 int ext_type = 0; 910 911 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, 912 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) 913 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); 914 915 end_ext_data = *p + len; 916 917 /* Get extension ID */ 918 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &extn_oid.len, 919 MBEDTLS_ASN1_OID ) ) != 0 ) 920 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); 921 922 extn_oid.tag = MBEDTLS_ASN1_OID; 923 extn_oid.p = *p; 924 *p += extn_oid.len; 925 926 /* Get optional critical */ 927 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 && 928 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) ) 929 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); 930 931 /* Data should be octet string type */ 932 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len, 933 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ) 934 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); 935 936 start_ext_octet = *p; 937 end_ext_octet = *p + len; 938 939 if( end_ext_octet != end_ext_data ) 940 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, 941 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); 942 943 /* 944 * Detect supported extensions 945 */ 946 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type ); 947 948 if( ret != 0 ) 949 { 950 /* Give the callback (if any) a chance to handle the extension */ 951 if( cb != NULL ) 952 { 953 ret = cb( p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet ); 954 if( ret != 0 && is_critical ) 955 return( ret ); 956 *p = end_ext_octet; 957 continue; 958 } 959 960 /* No parser found, skip extension */ 961 *p = end_ext_octet; 962 963 #if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION) 964 if( is_critical ) 965 { 966 /* Data is marked as critical: fail */ 967 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, 968 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) ); 969 } 970 #endif 971 continue; 972 } 973 974 /* Forbid repeated extensions */ 975 if( ( crt->ext_types & ext_type ) != 0 ) 976 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS ); 977 978 crt->ext_types |= ext_type; 979 980 switch( ext_type ) 981 { 982 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS: 983 /* Parse basic constraints */ 984 if( ( ret = x509_get_basic_constraints( p, end_ext_octet, 985 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 ) 986 return( ret ); 987 break; 988 989 case MBEDTLS_X509_EXT_KEY_USAGE: 990 /* Parse key usage */ 991 if( ( ret = x509_get_key_usage( p, end_ext_octet, 992 &crt->key_usage ) ) != 0 ) 993 return( ret ); 994 break; 995 996 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE: 997 /* Parse extended key usage */ 998 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet, 999 &crt->ext_key_usage ) ) != 0 ) 1000 return( ret ); 1001 break; 1002 1003 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME: 1004 /* Parse subject alt name */ 1005 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet, 1006 &crt->subject_alt_names ) ) != 0 ) 1007 return( ret ); 1008 break; 1009 1010 case MBEDTLS_X509_EXT_NS_CERT_TYPE: 1011 /* Parse netscape certificate type */ 1012 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet, 1013 &crt->ns_cert_type ) ) != 0 ) 1014 return( ret ); 1015 break; 1016 1017 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES: 1018 /* Parse certificate policies type */ 1019 if( ( ret = x509_get_certificate_policies( p, end_ext_octet, 1020 &crt->certificate_policies ) ) != 0 ) 1021 { 1022 /* Give the callback (if any) a chance to handle the extension 1023 * if it contains unsupported policies */ 1024 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL && 1025 cb( p_ctx, crt, &extn_oid, is_critical, 1026 start_ext_octet, end_ext_octet ) == 0 ) 1027 break; 1028 1029 #if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION) 1030 if( is_critical ) 1031 return( ret ); 1032 else 1033 #endif 1034 /* 1035 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we 1036 * cannot interpret or enforce the policy. However, it is up to 1037 * the user to choose how to enforce the policies, 1038 * unless the extension is critical. 1039 */ 1040 if( ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE ) 1041 return( ret ); 1042 } 1043 break; 1044 1045 default: 1046 /* 1047 * If this is a non-critical extension, which the oid layer 1048 * supports, but there isn't an x509 parser for it, 1049 * skip the extension. 1050 */ 1051 #if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION) 1052 if( is_critical ) 1053 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE ); 1054 else 1055 #endif 1056 *p = end_ext_octet; 1057 } 1058 } 1059 1060 if( *p != end ) 1061 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, 1062 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); 1063 1064 return( 0 ); 1065 } 1066 1067 /* 1068 * Parse and fill a single X.509 certificate in DER format 1069 */ 1070 static int x509_crt_parse_der_core( mbedtls_x509_crt *crt, 1071 const unsigned char *buf, 1072 size_t buflen, 1073 int make_copy, 1074 mbedtls_x509_crt_ext_cb_t cb, 1075 void *p_ctx ) 1076 { 1077 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 1078 size_t len; 1079 unsigned char *p, *end, *crt_end; 1080 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2; 1081 1082 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) ); 1083 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) ); 1084 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) ); 1085 1086 /* 1087 * Check for valid input 1088 */ 1089 if( crt == NULL || buf == NULL ) 1090 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); 1091 1092 /* Use the original buffer until we figure out actual length. */ 1093 p = (unsigned char*) buf; 1094 len = buflen; 1095 end = p + len; 1096 1097 /* 1098 * Certificate ::= SEQUENCE { 1099 * tbsCertificate TBSCertificate, 1100 * signatureAlgorithm AlgorithmIdentifier, 1101 * signatureValue BIT STRING } 1102 */ 1103 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, 1104 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) 1105 { 1106 mbedtls_x509_crt_free( crt ); 1107 return( MBEDTLS_ERR_X509_INVALID_FORMAT ); 1108 } 1109 1110 end = crt_end = p + len; 1111 crt->raw.len = crt_end - buf; 1112 if( make_copy != 0 ) 1113 { 1114 /* Create and populate a new buffer for the raw field. */ 1115 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len ); 1116 if( crt->raw.p == NULL ) 1117 return( MBEDTLS_ERR_X509_ALLOC_FAILED ); 1118 1119 memcpy( crt->raw.p, buf, crt->raw.len ); 1120 crt->own_buffer = 1; 1121 1122 p += crt->raw.len - len; 1123 end = crt_end = p + len; 1124 } 1125 else 1126 { 1127 crt->raw.p = (unsigned char*) buf; 1128 crt->own_buffer = 0; 1129 } 1130 1131 /* 1132 * TBSCertificate ::= SEQUENCE { 1133 */ 1134 crt->tbs.p = p; 1135 1136 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, 1137 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) 1138 { 1139 mbedtls_x509_crt_free( crt ); 1140 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) ); 1141 } 1142 1143 end = p + len; 1144 crt->tbs.len = end - crt->tbs.p; 1145 1146 /* 1147 * Version ::= INTEGER { v1(0), v2(1), v3(2) } 1148 * 1149 * CertificateSerialNumber ::= INTEGER 1150 * 1151 * signature AlgorithmIdentifier 1152 */ 1153 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 || 1154 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 || 1155 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid, 1156 &sig_params1 ) ) != 0 ) 1157 { 1158 mbedtls_x509_crt_free( crt ); 1159 return( ret ); 1160 } 1161 1162 if( crt->version < 0 || crt->version > 2 ) 1163 { 1164 mbedtls_x509_crt_free( crt ); 1165 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION ); 1166 } 1167 1168 crt->version++; 1169 1170 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1, 1171 &crt->sig_md, &crt->sig_pk, 1172 &crt->sig_opts ) ) != 0 ) 1173 { 1174 mbedtls_x509_crt_free( crt ); 1175 return( ret ); 1176 } 1177 1178 /* 1179 * issuer Name 1180 */ 1181 crt->issuer_raw.p = p; 1182 1183 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, 1184 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) 1185 { 1186 mbedtls_x509_crt_free( crt ); 1187 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) ); 1188 } 1189 1190 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 ) 1191 { 1192 mbedtls_x509_crt_free( crt ); 1193 return( ret ); 1194 } 1195 1196 crt->issuer_raw.len = p - crt->issuer_raw.p; 1197 1198 /* 1199 * Validity ::= SEQUENCE { 1200 * notBefore Time, 1201 * notAfter Time } 1202 * 1203 */ 1204 if( ( ret = x509_get_dates( &p, end, &crt->valid_from, 1205 &crt->valid_to ) ) != 0 ) 1206 { 1207 mbedtls_x509_crt_free( crt ); 1208 return( ret ); 1209 } 1210 1211 /* 1212 * subject Name 1213 */ 1214 crt->subject_raw.p = p; 1215 1216 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, 1217 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) 1218 { 1219 mbedtls_x509_crt_free( crt ); 1220 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) ); 1221 } 1222 1223 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 ) 1224 { 1225 mbedtls_x509_crt_free( crt ); 1226 return( ret ); 1227 } 1228 1229 crt->subject_raw.len = p - crt->subject_raw.p; 1230 1231 /* 1232 * SubjectPublicKeyInfo 1233 */ 1234 crt->pk_raw.p = p; 1235 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 ) 1236 { 1237 mbedtls_x509_crt_free( crt ); 1238 return( ret ); 1239 } 1240 crt->pk_raw.len = p - crt->pk_raw.p; 1241 1242 /* 1243 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL, 1244 * -- If present, version shall be v2 or v3 1245 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL, 1246 * -- If present, version shall be v2 or v3 1247 * extensions [3] EXPLICIT Extensions OPTIONAL 1248 * -- If present, version shall be v3 1249 */ 1250 if( crt->version == 2 || crt->version == 3 ) 1251 { 1252 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 ); 1253 if( ret != 0 ) 1254 { 1255 mbedtls_x509_crt_free( crt ); 1256 return( ret ); 1257 } 1258 } 1259 1260 if( crt->version == 2 || crt->version == 3 ) 1261 { 1262 ret = x509_get_uid( &p, end, &crt->subject_id, 2 ); 1263 if( ret != 0 ) 1264 { 1265 mbedtls_x509_crt_free( crt ); 1266 return( ret ); 1267 } 1268 } 1269 1270 #if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3) 1271 if( crt->version == 3 ) 1272 #endif 1273 { 1274 ret = x509_get_crt_ext( &p, end, crt, cb, p_ctx ); 1275 if( ret != 0 ) 1276 { 1277 mbedtls_x509_crt_free( crt ); 1278 return( ret ); 1279 } 1280 } 1281 1282 if( p != end ) 1283 { 1284 mbedtls_x509_crt_free( crt ); 1285 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, 1286 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); 1287 } 1288 1289 end = crt_end; 1290 1291 /* 1292 * } 1293 * -- end of TBSCertificate 1294 * 1295 * signatureAlgorithm AlgorithmIdentifier, 1296 * signatureValue BIT STRING 1297 */ 1298 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 ) 1299 { 1300 mbedtls_x509_crt_free( crt ); 1301 return( ret ); 1302 } 1303 1304 if( crt->sig_oid.len != sig_oid2.len || 1305 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 || 1306 sig_params1.tag != sig_params2.tag || 1307 sig_params1.len != sig_params2.len || 1308 ( sig_params1.len != 0 && 1309 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) ) 1310 { 1311 mbedtls_x509_crt_free( crt ); 1312 return( MBEDTLS_ERR_X509_SIG_MISMATCH ); 1313 } 1314 1315 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 ) 1316 { 1317 mbedtls_x509_crt_free( crt ); 1318 return( ret ); 1319 } 1320 1321 if( p != end ) 1322 { 1323 mbedtls_x509_crt_free( crt ); 1324 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, 1325 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); 1326 } 1327 1328 return( 0 ); 1329 } 1330 1331 /* 1332 * Parse one X.509 certificate in DER format from a buffer and add them to a 1333 * chained list 1334 */ 1335 static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain, 1336 const unsigned char *buf, 1337 size_t buflen, 1338 int make_copy, 1339 mbedtls_x509_crt_ext_cb_t cb, 1340 void *p_ctx ) 1341 { 1342 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 1343 mbedtls_x509_crt *crt = chain, *prev = NULL; 1344 1345 /* 1346 * Check for valid input 1347 */ 1348 if( crt == NULL || buf == NULL ) 1349 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); 1350 1351 while( crt->version != 0 && crt->next != NULL ) 1352 { 1353 prev = crt; 1354 crt = crt->next; 1355 } 1356 1357 /* 1358 * Add new certificate on the end of the chain if needed. 1359 */ 1360 if( crt->version != 0 && crt->next == NULL ) 1361 { 1362 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); 1363 1364 if( crt->next == NULL ) 1365 return( MBEDTLS_ERR_X509_ALLOC_FAILED ); 1366 1367 prev = crt; 1368 mbedtls_x509_crt_init( crt->next ); 1369 crt = crt->next; 1370 } 1371 1372 ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy, cb, p_ctx ); 1373 if( ret != 0 ) 1374 { 1375 if( prev ) 1376 prev->next = NULL; 1377 1378 if( crt != chain ) 1379 mbedtls_free( crt ); 1380 1381 return( ret ); 1382 } 1383 1384 return( 0 ); 1385 } 1386 1387 int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain, 1388 const unsigned char *buf, 1389 size_t buflen ) 1390 { 1391 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0, NULL, NULL ) ); 1392 } 1393 1394 int mbedtls_x509_crt_parse_der_with_ext_cb( mbedtls_x509_crt *chain, 1395 const unsigned char *buf, 1396 size_t buflen, 1397 int make_copy, 1398 mbedtls_x509_crt_ext_cb_t cb, 1399 void *p_ctx ) 1400 { 1401 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, make_copy, cb, p_ctx ) ); 1402 } 1403 1404 int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, 1405 const unsigned char *buf, 1406 size_t buflen ) 1407 { 1408 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1, NULL, NULL ) ); 1409 } 1410 1411 /* 1412 * Parse one or more PEM certificates from a buffer and add them to the chained 1413 * list 1414 */ 1415 int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, 1416 const unsigned char *buf, 1417 size_t buflen ) 1418 { 1419 #if defined(MBEDTLS_PEM_PARSE_C) 1420 int success = 0, first_error = 0, total_failed = 0; 1421 int buf_format = MBEDTLS_X509_FORMAT_DER; 1422 #endif 1423 1424 /* 1425 * Check for valid input 1426 */ 1427 if( chain == NULL || buf == NULL ) 1428 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); 1429 1430 /* 1431 * Determine buffer content. Buffer contains either one DER certificate or 1432 * one or more PEM certificates. 1433 */ 1434 #if defined(MBEDTLS_PEM_PARSE_C) 1435 if( buflen != 0 && buf[buflen - 1] == '\0' && 1436 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL ) 1437 { 1438 buf_format = MBEDTLS_X509_FORMAT_PEM; 1439 } 1440 1441 if( buf_format == MBEDTLS_X509_FORMAT_DER ) 1442 return mbedtls_x509_crt_parse_der( chain, buf, buflen ); 1443 #else 1444 return mbedtls_x509_crt_parse_der( chain, buf, buflen ); 1445 #endif 1446 1447 #if defined(MBEDTLS_PEM_PARSE_C) 1448 if( buf_format == MBEDTLS_X509_FORMAT_PEM ) 1449 { 1450 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 1451 mbedtls_pem_context pem; 1452 1453 /* 1 rather than 0 since the terminating NULL byte is counted in */ 1454 while( buflen > 1 ) 1455 { 1456 size_t use_len; 1457 mbedtls_pem_init( &pem ); 1458 1459 /* If we get there, we know the string is null-terminated */ 1460 ret = mbedtls_pem_read_buffer( &pem, 1461 "-----BEGIN CERTIFICATE-----", 1462 "-----END CERTIFICATE-----", 1463 buf, NULL, 0, &use_len ); 1464 1465 if( ret == 0 ) 1466 { 1467 /* 1468 * Was PEM encoded 1469 */ 1470 buflen -= use_len; 1471 buf += use_len; 1472 } 1473 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA ) 1474 { 1475 return( ret ); 1476 } 1477 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT ) 1478 { 1479 mbedtls_pem_free( &pem ); 1480 1481 /* 1482 * PEM header and footer were found 1483 */ 1484 buflen -= use_len; 1485 buf += use_len; 1486 1487 if( first_error == 0 ) 1488 first_error = ret; 1489 1490 total_failed++; 1491 continue; 1492 } 1493 else 1494 break; 1495 1496 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen ); 1497 1498 mbedtls_pem_free( &pem ); 1499 1500 if( ret != 0 ) 1501 { 1502 /* 1503 * Quit parsing on a memory error 1504 */ 1505 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED ) 1506 return( ret ); 1507 1508 if( first_error == 0 ) 1509 first_error = ret; 1510 1511 total_failed++; 1512 continue; 1513 } 1514 1515 success = 1; 1516 } 1517 } 1518 1519 if( success ) 1520 return( total_failed ); 1521 else if( first_error ) 1522 return( first_error ); 1523 else 1524 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT ); 1525 #endif /* MBEDTLS_PEM_PARSE_C */ 1526 } 1527 1528 #if defined(MBEDTLS_FS_IO) 1529 /* 1530 * Load one or more certificates and add them to the chained list 1531 */ 1532 int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path ) 1533 { 1534 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 1535 size_t n; 1536 unsigned char *buf; 1537 1538 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 ) 1539 return( ret ); 1540 1541 ret = mbedtls_x509_crt_parse( chain, buf, n ); 1542 1543 mbedtls_platform_zeroize( buf, n ); 1544 mbedtls_free( buf ); 1545 1546 return( ret ); 1547 } 1548 1549 int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path ) 1550 { 1551 int ret = 0; 1552 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) 1553 int w_ret; 1554 WCHAR szDir[MAX_PATH]; 1555 char filename[MAX_PATH]; 1556 char *p; 1557 size_t len = strlen( path ); 1558 1559 WIN32_FIND_DATAW file_data; 1560 HANDLE hFind; 1561 1562 if( len > MAX_PATH - 3 ) 1563 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); 1564 1565 memset( szDir, 0, sizeof(szDir) ); 1566 memset( filename, 0, MAX_PATH ); 1567 memcpy( filename, path, len ); 1568 filename[len++] = '\\'; 1569 p = filename + len; 1570 filename[len++] = '*'; 1571 1572 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir, 1573 MAX_PATH - 3 ); 1574 if( w_ret == 0 ) 1575 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); 1576 1577 hFind = FindFirstFileW( szDir, &file_data ); 1578 if( hFind == INVALID_HANDLE_VALUE ) 1579 return( MBEDTLS_ERR_X509_FILE_IO_ERROR ); 1580 1581 len = MAX_PATH - len; 1582 do 1583 { 1584 memset( p, 0, len ); 1585 1586 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) 1587 continue; 1588 1589 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName, 1590 lstrlenW( file_data.cFileName ), 1591 p, (int) len - 1, 1592 NULL, NULL ); 1593 if( w_ret == 0 ) 1594 { 1595 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR; 1596 goto cleanup; 1597 } 1598 1599 w_ret = mbedtls_x509_crt_parse_file( chain, filename ); 1600 if( w_ret < 0 ) 1601 ret++; 1602 else 1603 ret += w_ret; 1604 } 1605 while( FindNextFileW( hFind, &file_data ) != 0 ); 1606 1607 if( GetLastError() != ERROR_NO_MORE_FILES ) 1608 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR; 1609 1610 cleanup: 1611 FindClose( hFind ); 1612 #else /* _WIN32 */ 1613 int t_ret; 1614 int snp_ret; 1615 struct stat sb; 1616 struct dirent *entry; 1617 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN]; 1618 DIR *dir = opendir( path ); 1619 1620 if( dir == NULL ) 1621 return( MBEDTLS_ERR_X509_FILE_IO_ERROR ); 1622 1623 #if defined(MBEDTLS_THREADING_C) 1624 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 ) 1625 { 1626 closedir( dir ); 1627 return( ret ); 1628 } 1629 #endif /* MBEDTLS_THREADING_C */ 1630 1631 memset( &sb, 0, sizeof( sb ) ); 1632 1633 while( ( entry = readdir( dir ) ) != NULL ) 1634 { 1635 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name, 1636 "%s/%s", path, entry->d_name ); 1637 1638 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name ) 1639 { 1640 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL; 1641 goto cleanup; 1642 } 1643 else if( stat( entry_name, &sb ) == -1 ) 1644 { 1645 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR; 1646 goto cleanup; 1647 } 1648 1649 if( !S_ISREG( sb.st_mode ) ) 1650 continue; 1651 1652 // Ignore parse errors 1653 // 1654 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name ); 1655 if( t_ret < 0 ) 1656 ret++; 1657 else 1658 ret += t_ret; 1659 } 1660 1661 cleanup: 1662 closedir( dir ); 1663 1664 #if defined(MBEDTLS_THREADING_C) 1665 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 ) 1666 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR; 1667 #endif /* MBEDTLS_THREADING_C */ 1668 1669 #endif /* _WIN32 */ 1670 1671 return( ret ); 1672 } 1673 #endif /* MBEDTLS_FS_IO */ 1674 1675 /* 1676 * OtherName ::= SEQUENCE { 1677 * type-id OBJECT IDENTIFIER, 1678 * value [0] EXPLICIT ANY DEFINED BY type-id } 1679 * 1680 * HardwareModuleName ::= SEQUENCE { 1681 * hwType OBJECT IDENTIFIER, 1682 * hwSerialNum OCTET STRING } 1683 * 1684 * NOTE: we currently only parse and use otherName of type HwModuleName, 1685 * as defined in RFC 4108. 1686 */ 1687 static int x509_get_other_name( const mbedtls_x509_buf *subject_alt_name, 1688 mbedtls_x509_san_other_name *other_name ) 1689 { 1690 int ret = 0; 1691 size_t len; 1692 unsigned char *p = subject_alt_name->p; 1693 const unsigned char *end = p + subject_alt_name->len; 1694 mbedtls_x509_buf cur_oid; 1695 1696 if( ( subject_alt_name->tag & 1697 ( MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK ) ) != 1698 ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ) ) 1699 { 1700 /* 1701 * The given subject alternative name is not of type "othername". 1702 */ 1703 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); 1704 } 1705 1706 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, 1707 MBEDTLS_ASN1_OID ) ) != 0 ) 1708 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); 1709 1710 cur_oid.tag = MBEDTLS_ASN1_OID; 1711 cur_oid.p = p; 1712 cur_oid.len = len; 1713 1714 /* 1715 * Only HwModuleName is currently supported. 1716 */ 1717 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid ) != 0 ) 1718 { 1719 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE ); 1720 } 1721 1722 if( p + len >= end ) 1723 { 1724 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) ); 1725 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, 1726 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); 1727 } 1728 p += len; 1729 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, 1730 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 ) 1731 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); 1732 1733 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, 1734 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) 1735 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); 1736 1737 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OID ) ) != 0 ) 1738 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); 1739 1740 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID; 1741 other_name->value.hardware_module_name.oid.p = p; 1742 other_name->value.hardware_module_name.oid.len = len; 1743 1744 if( p + len >= end ) 1745 { 1746 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) ); 1747 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, 1748 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); 1749 } 1750 p += len; 1751 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, 1752 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ) 1753 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) ); 1754 1755 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING; 1756 other_name->value.hardware_module_name.val.p = p; 1757 other_name->value.hardware_module_name.val.len = len; 1758 p += len; 1759 if( p != end ) 1760 { 1761 mbedtls_platform_zeroize( other_name, 1762 sizeof( *other_name ) ); 1763 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, 1764 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) ); 1765 } 1766 return( 0 ); 1767 } 1768 1769 static int x509_info_subject_alt_name( char **buf, size_t *size, 1770 const mbedtls_x509_sequence 1771 *subject_alt_name, 1772 const char *prefix ) 1773 { 1774 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 1775 size_t n = *size; 1776 char *p = *buf; 1777 const mbedtls_x509_sequence *cur = subject_alt_name; 1778 mbedtls_x509_subject_alternative_name san; 1779 int parse_ret; 1780 1781 while( cur != NULL ) 1782 { 1783 memset( &san, 0, sizeof( san ) ); 1784 parse_ret = mbedtls_x509_parse_subject_alt_name( &cur->buf, &san ); 1785 if( parse_ret != 0 ) 1786 { 1787 if( parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE ) 1788 { 1789 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix ); 1790 MBEDTLS_X509_SAFE_SNPRINTF; 1791 } 1792 else 1793 { 1794 ret = mbedtls_snprintf( p, n, "\n%s <malformed>", prefix ); 1795 MBEDTLS_X509_SAFE_SNPRINTF; 1796 } 1797 cur = cur->next; 1798 continue; 1799 } 1800 1801 switch( san.type ) 1802 { 1803 /* 1804 * otherName 1805 */ 1806 case MBEDTLS_X509_SAN_OTHER_NAME: 1807 { 1808 mbedtls_x509_san_other_name *other_name = &san.san.other_name; 1809 1810 ret = mbedtls_snprintf( p, n, "\n%s otherName :", prefix ); 1811 MBEDTLS_X509_SAFE_SNPRINTF; 1812 1813 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME, 1814 &other_name->value.hardware_module_name.oid ) != 0 ) 1815 { 1816 ret = mbedtls_snprintf( p, n, "\n%s hardware module name :", prefix ); 1817 MBEDTLS_X509_SAFE_SNPRINTF; 1818 ret = mbedtls_snprintf( p, n, "\n%s hardware type : ", prefix ); 1819 MBEDTLS_X509_SAFE_SNPRINTF; 1820 1821 ret = mbedtls_oid_get_numeric_string( p, n, &other_name->value.hardware_module_name.oid ); 1822 MBEDTLS_X509_SAFE_SNPRINTF; 1823 1824 ret = mbedtls_snprintf( p, n, "\n%s hardware serial number : ", prefix ); 1825 MBEDTLS_X509_SAFE_SNPRINTF; 1826 1827 if( other_name->value.hardware_module_name.val.len >= n ) 1828 { 1829 *p = '\0'; 1830 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL ); 1831 } 1832 1833 memcpy( p, other_name->value.hardware_module_name.val.p, 1834 other_name->value.hardware_module_name.val.len ); 1835 p += other_name->value.hardware_module_name.val.len; 1836 1837 n -= other_name->value.hardware_module_name.val.len; 1838 1839 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */ 1840 } 1841 break; 1842 1843 /* 1844 * dNSName 1845 */ 1846 case MBEDTLS_X509_SAN_DNS_NAME: 1847 { 1848 ret = mbedtls_snprintf( p, n, "\n%s dNSName : ", prefix ); 1849 MBEDTLS_X509_SAFE_SNPRINTF; 1850 if( san.san.unstructured_name.len >= n ) 1851 { 1852 *p = '\0'; 1853 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL ); 1854 } 1855 1856 memcpy( p, san.san.unstructured_name.p, san.san.unstructured_name.len ); 1857 p += san.san.unstructured_name.len; 1858 n -= san.san.unstructured_name.len; 1859 } 1860 break; 1861 1862 /* 1863 * Type not supported, skip item. 1864 */ 1865 default: 1866 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix ); 1867 MBEDTLS_X509_SAFE_SNPRINTF; 1868 break; 1869 } 1870 1871 cur = cur->next; 1872 } 1873 1874 *p = '\0'; 1875 1876 *size = n; 1877 *buf = p; 1878 1879 return( 0 ); 1880 } 1881 1882 int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf, 1883 mbedtls_x509_subject_alternative_name *san ) 1884 { 1885 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 1886 switch( san_buf->tag & 1887 ( MBEDTLS_ASN1_TAG_CLASS_MASK | 1888 MBEDTLS_ASN1_TAG_VALUE_MASK ) ) 1889 { 1890 /* 1891 * otherName 1892 */ 1893 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ): 1894 { 1895 mbedtls_x509_san_other_name other_name; 1896 1897 ret = x509_get_other_name( san_buf, &other_name ); 1898 if( ret != 0 ) 1899 return( ret ); 1900 1901 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) ); 1902 san->type = MBEDTLS_X509_SAN_OTHER_NAME; 1903 memcpy( &san->san.other_name, 1904 &other_name, sizeof( other_name ) ); 1905 1906 } 1907 break; 1908 1909 /* 1910 * dNSName 1911 */ 1912 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME ): 1913 { 1914 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) ); 1915 san->type = MBEDTLS_X509_SAN_DNS_NAME; 1916 1917 memcpy( &san->san.unstructured_name, 1918 san_buf, sizeof( *san_buf ) ); 1919 1920 } 1921 break; 1922 1923 /* 1924 * Type not supported 1925 */ 1926 default: 1927 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE ); 1928 } 1929 return( 0 ); 1930 } 1931 1932 #define PRINT_ITEM(i) \ 1933 { \ 1934 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \ 1935 MBEDTLS_X509_SAFE_SNPRINTF; \ 1936 sep = ", "; \ 1937 } 1938 1939 #define CERT_TYPE(type,name) \ 1940 if( ns_cert_type & (type) ) \ 1941 PRINT_ITEM( name ); 1942 1943 static int x509_info_cert_type( char **buf, size_t *size, 1944 unsigned char ns_cert_type ) 1945 { 1946 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 1947 size_t n = *size; 1948 char *p = *buf; 1949 const char *sep = ""; 1950 1951 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" ); 1952 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" ); 1953 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" ); 1954 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" ); 1955 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" ); 1956 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" ); 1957 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" ); 1958 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" ); 1959 1960 *size = n; 1961 *buf = p; 1962 1963 return( 0 ); 1964 } 1965 1966 #define KEY_USAGE(code,name) \ 1967 if( key_usage & (code) ) \ 1968 PRINT_ITEM( name ); 1969 1970 static int x509_info_key_usage( char **buf, size_t *size, 1971 unsigned int key_usage ) 1972 { 1973 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 1974 size_t n = *size; 1975 char *p = *buf; 1976 const char *sep = ""; 1977 1978 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" ); 1979 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" ); 1980 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" ); 1981 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" ); 1982 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" ); 1983 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" ); 1984 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" ); 1985 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" ); 1986 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" ); 1987 1988 *size = n; 1989 *buf = p; 1990 1991 return( 0 ); 1992 } 1993 1994 static int x509_info_ext_key_usage( char **buf, size_t *size, 1995 const mbedtls_x509_sequence *extended_key_usage ) 1996 { 1997 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 1998 const char *desc; 1999 size_t n = *size; 2000 char *p = *buf; 2001 const mbedtls_x509_sequence *cur = extended_key_usage; 2002 const char *sep = ""; 2003 2004 while( cur != NULL ) 2005 { 2006 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 ) 2007 desc = "???"; 2008 2009 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc ); 2010 MBEDTLS_X509_SAFE_SNPRINTF; 2011 2012 sep = ", "; 2013 2014 cur = cur->next; 2015 } 2016 2017 *size = n; 2018 *buf = p; 2019 2020 return( 0 ); 2021 } 2022 2023 static int x509_info_cert_policies( char **buf, size_t *size, 2024 const mbedtls_x509_sequence *certificate_policies ) 2025 { 2026 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 2027 const char *desc; 2028 size_t n = *size; 2029 char *p = *buf; 2030 const mbedtls_x509_sequence *cur = certificate_policies; 2031 const char *sep = ""; 2032 2033 while( cur != NULL ) 2034 { 2035 if( mbedtls_oid_get_certificate_policies( &cur->buf, &desc ) != 0 ) 2036 desc = "???"; 2037 2038 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc ); 2039 MBEDTLS_X509_SAFE_SNPRINTF; 2040 2041 sep = ", "; 2042 2043 cur = cur->next; 2044 } 2045 2046 *size = n; 2047 *buf = p; 2048 2049 return( 0 ); 2050 } 2051 2052 /* 2053 * Return an informational string about the certificate. 2054 */ 2055 #define BEFORE_COLON 18 2056 #define BC "18" 2057 int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix, 2058 const mbedtls_x509_crt *crt ) 2059 { 2060 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 2061 size_t n; 2062 char *p; 2063 char key_size_str[BEFORE_COLON]; 2064 2065 p = buf; 2066 n = size; 2067 2068 if( NULL == crt ) 2069 { 2070 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" ); 2071 MBEDTLS_X509_SAFE_SNPRINTF; 2072 2073 return( (int) ( size - n ) ); 2074 } 2075 2076 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n", 2077 prefix, crt->version ); 2078 MBEDTLS_X509_SAFE_SNPRINTF; 2079 ret = mbedtls_snprintf( p, n, "%sserial number : ", 2080 prefix ); 2081 MBEDTLS_X509_SAFE_SNPRINTF; 2082 2083 ret = mbedtls_x509_serial_gets( p, n, &crt->serial ); 2084 MBEDTLS_X509_SAFE_SNPRINTF; 2085 2086 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix ); 2087 MBEDTLS_X509_SAFE_SNPRINTF; 2088 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer ); 2089 MBEDTLS_X509_SAFE_SNPRINTF; 2090 2091 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix ); 2092 MBEDTLS_X509_SAFE_SNPRINTF; 2093 ret = mbedtls_x509_dn_gets( p, n, &crt->subject ); 2094 MBEDTLS_X509_SAFE_SNPRINTF; 2095 2096 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \ 2097 "%04d-%02d-%02d %02d:%02d:%02d", prefix, 2098 crt->valid_from.year, crt->valid_from.mon, 2099 crt->valid_from.day, crt->valid_from.hour, 2100 crt->valid_from.min, crt->valid_from.sec ); 2101 MBEDTLS_X509_SAFE_SNPRINTF; 2102 2103 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \ 2104 "%04d-%02d-%02d %02d:%02d:%02d", prefix, 2105 crt->valid_to.year, crt->valid_to.mon, 2106 crt->valid_to.day, crt->valid_to.hour, 2107 crt->valid_to.min, crt->valid_to.sec ); 2108 MBEDTLS_X509_SAFE_SNPRINTF; 2109 2110 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix ); 2111 MBEDTLS_X509_SAFE_SNPRINTF; 2112 2113 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk, 2114 crt->sig_md, crt->sig_opts ); 2115 MBEDTLS_X509_SAFE_SNPRINTF; 2116 2117 /* Key size */ 2118 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON, 2119 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 ) 2120 { 2121 return( ret ); 2122 } 2123 2124 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str, 2125 (int) mbedtls_pk_get_bitlen( &crt->pk ) ); 2126 MBEDTLS_X509_SAFE_SNPRINTF; 2127 2128 /* 2129 * Optional extensions 2130 */ 2131 2132 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS ) 2133 { 2134 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix, 2135 crt->ca_istrue ? "true" : "false" ); 2136 MBEDTLS_X509_SAFE_SNPRINTF; 2137 2138 if( crt->max_pathlen > 0 ) 2139 { 2140 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 ); 2141 MBEDTLS_X509_SAFE_SNPRINTF; 2142 } 2143 } 2144 2145 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME ) 2146 { 2147 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name :", prefix ); 2148 MBEDTLS_X509_SAFE_SNPRINTF; 2149 2150 if( ( ret = x509_info_subject_alt_name( &p, &n, 2151 &crt->subject_alt_names, 2152 prefix ) ) != 0 ) 2153 return( ret ); 2154 } 2155 2156 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE ) 2157 { 2158 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix ); 2159 MBEDTLS_X509_SAFE_SNPRINTF; 2160 2161 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 ) 2162 return( ret ); 2163 } 2164 2165 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) 2166 { 2167 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix ); 2168 MBEDTLS_X509_SAFE_SNPRINTF; 2169 2170 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 ) 2171 return( ret ); 2172 } 2173 2174 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) 2175 { 2176 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix ); 2177 MBEDTLS_X509_SAFE_SNPRINTF; 2178 2179 if( ( ret = x509_info_ext_key_usage( &p, &n, 2180 &crt->ext_key_usage ) ) != 0 ) 2181 return( ret ); 2182 } 2183 2184 if( crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES ) 2185 { 2186 ret = mbedtls_snprintf( p, n, "\n%scertificate policies : ", prefix ); 2187 MBEDTLS_X509_SAFE_SNPRINTF; 2188 2189 if( ( ret = x509_info_cert_policies( &p, &n, 2190 &crt->certificate_policies ) ) != 0 ) 2191 return( ret ); 2192 } 2193 2194 ret = mbedtls_snprintf( p, n, "\n" ); 2195 MBEDTLS_X509_SAFE_SNPRINTF; 2196 2197 return( (int) ( size - n ) ); 2198 } 2199 2200 struct x509_crt_verify_string { 2201 int code; 2202 const char *string; 2203 }; 2204 2205 static const struct x509_crt_verify_string x509_crt_verify_strings[] = { 2206 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" }, 2207 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" }, 2208 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" }, 2209 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" }, 2210 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" }, 2211 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" }, 2212 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" }, 2213 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" }, 2214 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" }, 2215 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" }, 2216 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" }, 2217 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" }, 2218 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" }, 2219 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" }, 2220 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." }, 2221 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." }, 2222 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." }, 2223 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." }, 2224 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." }, 2225 { MBEDTLS_X509_BADCRL_BAD_KEY, "The CRL is signed with an unacceptable key (eg bad curve, RSA too short)." }, 2226 { 0, NULL } 2227 }; 2228 2229 int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix, 2230 uint32_t flags ) 2231 { 2232 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 2233 const struct x509_crt_verify_string *cur; 2234 char *p = buf; 2235 size_t n = size; 2236 2237 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ ) 2238 { 2239 if( ( flags & cur->code ) == 0 ) 2240 continue; 2241 2242 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string ); 2243 MBEDTLS_X509_SAFE_SNPRINTF; 2244 flags ^= cur->code; 2245 } 2246 2247 if( flags != 0 ) 2248 { 2249 ret = mbedtls_snprintf( p, n, "%sUnknown reason " 2250 "(this should not happen)\n", prefix ); 2251 MBEDTLS_X509_SAFE_SNPRINTF; 2252 } 2253 2254 return( (int) ( size - n ) ); 2255 } 2256 2257 #if defined(MBEDTLS_X509_CHECK_KEY_USAGE) 2258 int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt, 2259 unsigned int usage ) 2260 { 2261 unsigned int usage_must, usage_may; 2262 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY 2263 | MBEDTLS_X509_KU_DECIPHER_ONLY; 2264 2265 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 ) 2266 return( 0 ); 2267 2268 usage_must = usage & ~may_mask; 2269 2270 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must ) 2271 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); 2272 2273 usage_may = usage & may_mask; 2274 2275 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may ) 2276 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); 2277 2278 return( 0 ); 2279 } 2280 #endif 2281 2282 #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) 2283 int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt, 2284 const char *usage_oid, 2285 size_t usage_len ) 2286 { 2287 const mbedtls_x509_sequence *cur; 2288 2289 /* Extension is not mandatory, absent means no restriction */ 2290 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 ) 2291 return( 0 ); 2292 2293 /* 2294 * Look for the requested usage (or wildcard ANY) in our list 2295 */ 2296 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next ) 2297 { 2298 const mbedtls_x509_buf *cur_oid = &cur->buf; 2299 2300 if( cur_oid->len == usage_len && 2301 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 ) 2302 { 2303 return( 0 ); 2304 } 2305 2306 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 ) 2307 return( 0 ); 2308 } 2309 2310 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); 2311 } 2312 #endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */ 2313 2314 #if defined(MBEDTLS_X509_CRL_PARSE_C) 2315 /* 2316 * Return 1 if the certificate is revoked, or 0 otherwise. 2317 */ 2318 int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl ) 2319 { 2320 const mbedtls_x509_crl_entry *cur = &crl->entry; 2321 2322 while( cur != NULL && cur->serial.len != 0 ) 2323 { 2324 if( crt->serial.len == cur->serial.len && 2325 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 ) 2326 { 2327 return( 1 ); 2328 } 2329 2330 cur = cur->next; 2331 } 2332 2333 return( 0 ); 2334 } 2335 2336 /* 2337 * Check that the given certificate is not revoked according to the CRL. 2338 * Skip validation if no CRL for the given CA is present. 2339 */ 2340 static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca, 2341 mbedtls_x509_crl *crl_list, 2342 const mbedtls_x509_crt_profile *profile ) 2343 { 2344 int flags = 0; 2345 unsigned char hash[MBEDTLS_MD_MAX_SIZE]; 2346 const mbedtls_md_info_t *md_info; 2347 2348 if( ca == NULL ) 2349 return( flags ); 2350 2351 while( crl_list != NULL ) 2352 { 2353 if( crl_list->version == 0 || 2354 x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 ) 2355 { 2356 crl_list = crl_list->next; 2357 continue; 2358 } 2359 2360 /* 2361 * Check if the CA is configured to sign CRLs 2362 */ 2363 #if defined(MBEDTLS_X509_CHECK_KEY_USAGE) 2364 if( mbedtls_x509_crt_check_key_usage( ca, 2365 MBEDTLS_X509_KU_CRL_SIGN ) != 0 ) 2366 { 2367 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED; 2368 break; 2369 } 2370 #endif 2371 2372 /* 2373 * Check if CRL is correctly signed by the trusted CA 2374 */ 2375 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 ) 2376 flags |= MBEDTLS_X509_BADCRL_BAD_MD; 2377 2378 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 ) 2379 flags |= MBEDTLS_X509_BADCRL_BAD_PK; 2380 2381 md_info = mbedtls_md_info_from_type( crl_list->sig_md ); 2382 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 ) 2383 { 2384 /* Note: this can't happen except after an internal error */ 2385 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED; 2386 break; 2387 } 2388 2389 if( x509_profile_check_key( profile, &ca->pk ) != 0 ) 2390 flags |= MBEDTLS_X509_BADCERT_BAD_KEY; 2391 2392 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk, 2393 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ), 2394 crl_list->sig.p, crl_list->sig.len ) != 0 ) 2395 { 2396 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED; 2397 break; 2398 } 2399 2400 /* 2401 * Check for validity of CRL (Do not drop out) 2402 */ 2403 if( mbedtls_x509_time_is_past( &crl_list->next_update ) ) 2404 flags |= MBEDTLS_X509_BADCRL_EXPIRED; 2405 2406 if( mbedtls_x509_time_is_future( &crl_list->this_update ) ) 2407 flags |= MBEDTLS_X509_BADCRL_FUTURE; 2408 2409 /* 2410 * Check if certificate is revoked 2411 */ 2412 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) ) 2413 { 2414 flags |= MBEDTLS_X509_BADCERT_REVOKED; 2415 break; 2416 } 2417 2418 crl_list = crl_list->next; 2419 } 2420 2421 return( flags ); 2422 } 2423 #endif /* MBEDTLS_X509_CRL_PARSE_C */ 2424 2425 /* 2426 * Check the signature of a certificate by its parent 2427 */ 2428 static int x509_crt_check_signature( const mbedtls_x509_crt *child, 2429 mbedtls_x509_crt *parent, 2430 mbedtls_x509_crt_restart_ctx *rs_ctx ) 2431 { 2432 unsigned char hash[MBEDTLS_MD_MAX_SIZE]; 2433 size_t hash_len; 2434 #if !defined(MBEDTLS_USE_PSA_CRYPTO) 2435 const mbedtls_md_info_t *md_info; 2436 md_info = mbedtls_md_info_from_type( child->sig_md ); 2437 hash_len = mbedtls_md_get_size( md_info ); 2438 2439 /* Note: hash errors can happen only after an internal error */ 2440 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 ) 2441 return( -1 ); 2442 #else 2443 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; 2444 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( child->sig_md ); 2445 2446 if( psa_hash_setup( &hash_operation, hash_alg ) != PSA_SUCCESS ) 2447 return( -1 ); 2448 2449 if( psa_hash_update( &hash_operation, child->tbs.p, child->tbs.len ) 2450 != PSA_SUCCESS ) 2451 { 2452 return( -1 ); 2453 } 2454 2455 if( psa_hash_finish( &hash_operation, hash, sizeof( hash ), &hash_len ) 2456 != PSA_SUCCESS ) 2457 { 2458 return( -1 ); 2459 } 2460 #endif /* MBEDTLS_USE_PSA_CRYPTO */ 2461 /* Skip expensive computation on obvious mismatch */ 2462 if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) ) 2463 return( -1 ); 2464 2465 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) 2466 if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA ) 2467 { 2468 return( mbedtls_pk_verify_restartable( &parent->pk, 2469 child->sig_md, hash, hash_len, 2470 child->sig.p, child->sig.len, &rs_ctx->pk ) ); 2471 } 2472 #else 2473 (void) rs_ctx; 2474 #endif 2475 2476 return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk, 2477 child->sig_md, hash, hash_len, 2478 child->sig.p, child->sig.len ) ); 2479 } 2480 2481 /* 2482 * Check if 'parent' is a suitable parent (signing CA) for 'child'. 2483 * Return 0 if yes, -1 if not. 2484 * 2485 * top means parent is a locally-trusted certificate 2486 */ 2487 static int x509_crt_check_parent( const mbedtls_x509_crt *child, 2488 const mbedtls_x509_crt *parent, 2489 int top ) 2490 { 2491 int need_ca_bit; 2492 2493 /* Parent must be the issuer */ 2494 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 ) 2495 return( -1 ); 2496 2497 /* Parent must have the basicConstraints CA bit set as a general rule */ 2498 need_ca_bit = 1; 2499 2500 /* Exception: v1/v2 certificates that are locally trusted. */ 2501 if( top && parent->version < 3 ) 2502 need_ca_bit = 0; 2503 2504 if( need_ca_bit && ! parent->ca_istrue ) 2505 return( -1 ); 2506 2507 #if defined(MBEDTLS_X509_CHECK_KEY_USAGE) 2508 if( need_ca_bit && 2509 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 ) 2510 { 2511 return( -1 ); 2512 } 2513 #endif 2514 2515 return( 0 ); 2516 } 2517 2518 /* 2519 * Find a suitable parent for child in candidates, or return NULL. 2520 * 2521 * Here suitable is defined as: 2522 * 1. subject name matches child's issuer 2523 * 2. if necessary, the CA bit is set and key usage allows signing certs 2524 * 3. for trusted roots, the signature is correct 2525 * (for intermediates, the signature is checked and the result reported) 2526 * 4. pathlen constraints are satisfied 2527 * 2528 * If there's a suitable candidate which is also time-valid, return the first 2529 * such. Otherwise, return the first suitable candidate (or NULL if there is 2530 * none). 2531 * 2532 * The rationale for this rule is that someone could have a list of trusted 2533 * roots with two versions on the same root with different validity periods. 2534 * (At least one user reported having such a list and wanted it to just work.) 2535 * The reason we don't just require time-validity is that generally there is 2536 * only one version, and if it's expired we want the flags to state that 2537 * rather than NOT_TRUSTED, as would be the case if we required it here. 2538 * 2539 * The rationale for rule 3 (signature for trusted roots) is that users might 2540 * have two versions of the same CA with different keys in their list, and the 2541 * way we select the correct one is by checking the signature (as we don't 2542 * rely on key identifier extensions). (This is one way users might choose to 2543 * handle key rollover, another relies on self-issued certs, see [SIRO].) 2544 * 2545 * Arguments: 2546 * - [in] child: certificate for which we're looking for a parent 2547 * - [in] candidates: chained list of potential parents 2548 * - [out] r_parent: parent found (or NULL) 2549 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0 2550 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top 2551 * of the chain, 0 otherwise 2552 * - [in] path_cnt: number of intermediates seen so far 2553 * - [in] self_cnt: number of self-signed intermediates seen so far 2554 * (will never be greater than path_cnt) 2555 * - [in-out] rs_ctx: context for restarting operations 2556 * 2557 * Return value: 2558 * - 0 on success 2559 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise 2560 */ 2561 static int x509_crt_find_parent_in( 2562 mbedtls_x509_crt *child, 2563 mbedtls_x509_crt *candidates, 2564 mbedtls_x509_crt **r_parent, 2565 int *r_signature_is_good, 2566 int top, 2567 unsigned path_cnt, 2568 unsigned self_cnt, 2569 mbedtls_x509_crt_restart_ctx *rs_ctx ) 2570 { 2571 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 2572 mbedtls_x509_crt *parent, *fallback_parent; 2573 int signature_is_good = 0, fallback_signature_is_good; 2574 2575 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) 2576 /* did we have something in progress? */ 2577 if( rs_ctx != NULL && rs_ctx->parent != NULL ) 2578 { 2579 /* restore saved state */ 2580 parent = rs_ctx->parent; 2581 fallback_parent = rs_ctx->fallback_parent; 2582 fallback_signature_is_good = rs_ctx->fallback_signature_is_good; 2583 2584 /* clear saved state */ 2585 rs_ctx->parent = NULL; 2586 rs_ctx->fallback_parent = NULL; 2587 rs_ctx->fallback_signature_is_good = 0; 2588 2589 /* resume where we left */ 2590 goto check_signature; 2591 } 2592 #endif 2593 2594 fallback_parent = NULL; 2595 fallback_signature_is_good = 0; 2596 2597 for( parent = candidates; parent != NULL; parent = parent->next ) 2598 { 2599 /* basic parenting skills (name, CA bit, key usage) */ 2600 if( x509_crt_check_parent( child, parent, top ) != 0 ) 2601 continue; 2602 2603 /* +1 because stored max_pathlen is 1 higher that the actual value */ 2604 if( parent->max_pathlen > 0 && 2605 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt ) 2606 { 2607 continue; 2608 } 2609 2610 /* Signature */ 2611 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) 2612 check_signature: 2613 #endif 2614 ret = x509_crt_check_signature( child, parent, rs_ctx ); 2615 2616 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) 2617 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) 2618 { 2619 /* save state */ 2620 rs_ctx->parent = parent; 2621 rs_ctx->fallback_parent = fallback_parent; 2622 rs_ctx->fallback_signature_is_good = fallback_signature_is_good; 2623 2624 return( ret ); 2625 } 2626 #else 2627 (void) ret; 2628 #endif 2629 2630 signature_is_good = ret == 0; 2631 if( top && ! signature_is_good ) 2632 continue; 2633 2634 /* optional time check */ 2635 if( mbedtls_x509_time_is_past( &parent->valid_to ) || 2636 mbedtls_x509_time_is_future( &parent->valid_from ) ) 2637 { 2638 if( fallback_parent == NULL ) 2639 { 2640 fallback_parent = parent; 2641 fallback_signature_is_good = signature_is_good; 2642 } 2643 2644 continue; 2645 } 2646 2647 *r_parent = parent; 2648 *r_signature_is_good = signature_is_good; 2649 2650 break; 2651 } 2652 2653 if( parent == NULL ) 2654 { 2655 *r_parent = fallback_parent; 2656 *r_signature_is_good = fallback_signature_is_good; 2657 } 2658 2659 return( 0 ); 2660 } 2661 2662 /* 2663 * Find a parent in trusted CAs or the provided chain, or return NULL. 2664 * 2665 * Searches in trusted CAs first, and return the first suitable parent found 2666 * (see find_parent_in() for definition of suitable). 2667 * 2668 * Arguments: 2669 * - [in] child: certificate for which we're looking for a parent, followed 2670 * by a chain of possible intermediates 2671 * - [in] trust_ca: list of locally trusted certificates 2672 * - [out] parent: parent found (or NULL) 2673 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0 2674 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0 2675 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child) 2676 * - [in] self_cnt: number of self-signed certs in the chain so far 2677 * (will always be no greater than path_cnt) 2678 * - [in-out] rs_ctx: context for restarting operations 2679 * 2680 * Return value: 2681 * - 0 on success 2682 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise 2683 */ 2684 static int x509_crt_find_parent( 2685 mbedtls_x509_crt *child, 2686 mbedtls_x509_crt *trust_ca, 2687 mbedtls_x509_crt **parent, 2688 int *parent_is_trusted, 2689 int *signature_is_good, 2690 unsigned path_cnt, 2691 unsigned self_cnt, 2692 mbedtls_x509_crt_restart_ctx *rs_ctx ) 2693 { 2694 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 2695 mbedtls_x509_crt *search_list; 2696 2697 *parent_is_trusted = 1; 2698 2699 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) 2700 /* restore then clear saved state if we have some stored */ 2701 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 ) 2702 { 2703 *parent_is_trusted = rs_ctx->parent_is_trusted; 2704 rs_ctx->parent_is_trusted = -1; 2705 } 2706 #endif 2707 2708 while( 1 ) { 2709 search_list = *parent_is_trusted ? trust_ca : child->next; 2710 2711 ret = x509_crt_find_parent_in( child, search_list, 2712 parent, signature_is_good, 2713 *parent_is_trusted, 2714 path_cnt, self_cnt, rs_ctx ); 2715 2716 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) 2717 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) 2718 { 2719 /* save state */ 2720 rs_ctx->parent_is_trusted = *parent_is_trusted; 2721 return( ret ); 2722 } 2723 #else 2724 (void) ret; 2725 #endif 2726 2727 /* stop here if found or already in second iteration */ 2728 if( *parent != NULL || *parent_is_trusted == 0 ) 2729 break; 2730 2731 /* prepare second iteration */ 2732 *parent_is_trusted = 0; 2733 } 2734 2735 /* extra precaution against mistakes in the caller */ 2736 if( *parent == NULL ) 2737 { 2738 *parent_is_trusted = 0; 2739 *signature_is_good = 0; 2740 } 2741 2742 return( 0 ); 2743 } 2744 2745 /* 2746 * Check if an end-entity certificate is locally trusted 2747 * 2748 * Currently we require such certificates to be self-signed (actually only 2749 * check for self-issued as self-signatures are not checked) 2750 */ 2751 static int x509_crt_check_ee_locally_trusted( 2752 mbedtls_x509_crt *crt, 2753 mbedtls_x509_crt *trust_ca ) 2754 { 2755 mbedtls_x509_crt *cur; 2756 2757 /* must be self-issued */ 2758 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 ) 2759 return( -1 ); 2760 2761 /* look for an exact match with trusted cert */ 2762 for( cur = trust_ca; cur != NULL; cur = cur->next ) 2763 { 2764 if( crt->raw.len == cur->raw.len && 2765 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 ) 2766 { 2767 return( 0 ); 2768 } 2769 } 2770 2771 /* too bad */ 2772 return( -1 ); 2773 } 2774 2775 /* 2776 * Build and verify a certificate chain 2777 * 2778 * Given a peer-provided list of certificates EE, C1, ..., Cn and 2779 * a list of trusted certs R1, ... Rp, try to build and verify a chain 2780 * EE, Ci1, ... Ciq [, Rj] 2781 * such that every cert in the chain is a child of the next one, 2782 * jumping to a trusted root as early as possible. 2783 * 2784 * Verify that chain and return it with flags for all issues found. 2785 * 2786 * Special cases: 2787 * - EE == Rj -> return a one-element list containing it 2788 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root 2789 * -> return that chain with NOT_TRUSTED set on Ciq 2790 * 2791 * Tests for (aspects of) this function should include at least: 2792 * - trusted EE 2793 * - EE -> trusted root 2794 * - EE -> intermediate CA -> trusted root 2795 * - if relevant: EE untrusted 2796 * - if relevant: EE -> intermediate, untrusted 2797 * with the aspect under test checked at each relevant level (EE, int, root). 2798 * For some aspects longer chains are required, but usually length 2 is 2799 * enough (but length 1 is not in general). 2800 * 2801 * Arguments: 2802 * - [in] crt: the cert list EE, C1, ..., Cn 2803 * - [in] trust_ca: the trusted list R1, ..., Rp 2804 * - [in] ca_crl, profile: as in verify_with_profile() 2805 * - [out] ver_chain: the built and verified chain 2806 * Only valid when return value is 0, may contain garbage otherwise! 2807 * Restart note: need not be the same when calling again to resume. 2808 * - [in-out] rs_ctx: context for restarting operations 2809 * 2810 * Return value: 2811 * - non-zero if the chain could not be fully built and examined 2812 * - 0 is the chain was successfully built and examined, 2813 * even if it was found to be invalid 2814 */ 2815 static int x509_crt_verify_chain( 2816 mbedtls_x509_crt *crt, 2817 mbedtls_x509_crt *trust_ca, 2818 mbedtls_x509_crl *ca_crl, 2819 mbedtls_x509_crt_ca_cb_t f_ca_cb, 2820 void *p_ca_cb, 2821 const mbedtls_x509_crt_profile *profile, 2822 mbedtls_x509_crt_verify_chain *ver_chain, 2823 mbedtls_x509_crt_restart_ctx *rs_ctx ) 2824 { 2825 /* Don't initialize any of those variables here, so that the compiler can 2826 * catch potential issues with jumping ahead when restarting */ 2827 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 2828 uint32_t *flags; 2829 mbedtls_x509_crt_verify_chain_item *cur; 2830 mbedtls_x509_crt *child; 2831 mbedtls_x509_crt *parent; 2832 int parent_is_trusted; 2833 int child_is_trusted; 2834 int signature_is_good; 2835 unsigned self_cnt; 2836 mbedtls_x509_crt *cur_trust_ca = NULL; 2837 2838 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) 2839 /* resume if we had an operation in progress */ 2840 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent ) 2841 { 2842 /* restore saved state */ 2843 *ver_chain = rs_ctx->ver_chain; /* struct copy */ 2844 self_cnt = rs_ctx->self_cnt; 2845 2846 /* restore derived state */ 2847 cur = &ver_chain->items[ver_chain->len - 1]; 2848 child = cur->crt; 2849 flags = &cur->flags; 2850 2851 goto find_parent; 2852 } 2853 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ 2854 2855 child = crt; 2856 self_cnt = 0; 2857 parent_is_trusted = 0; 2858 child_is_trusted = 0; 2859 2860 while( 1 ) { 2861 /* Add certificate to the verification chain */ 2862 cur = &ver_chain->items[ver_chain->len]; 2863 cur->crt = child; 2864 cur->flags = 0; 2865 ver_chain->len++; 2866 flags = &cur->flags; 2867 2868 /* Check time-validity (all certificates) */ 2869 if( mbedtls_x509_time_is_past( &child->valid_to ) ) 2870 *flags |= MBEDTLS_X509_BADCERT_EXPIRED; 2871 2872 if( mbedtls_x509_time_is_future( &child->valid_from ) ) 2873 *flags |= MBEDTLS_X509_BADCERT_FUTURE; 2874 2875 /* Stop here for trusted roots (but not for trusted EE certs) */ 2876 if( child_is_trusted ) 2877 return( 0 ); 2878 2879 /* Check signature algorithm: MD & PK algs */ 2880 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 ) 2881 *flags |= MBEDTLS_X509_BADCERT_BAD_MD; 2882 2883 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 ) 2884 *flags |= MBEDTLS_X509_BADCERT_BAD_PK; 2885 2886 /* Special case: EE certs that are locally trusted */ 2887 if( ver_chain->len == 1 && 2888 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 ) 2889 { 2890 return( 0 ); 2891 } 2892 2893 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) 2894 find_parent: 2895 #endif 2896 2897 /* Obtain list of potential trusted signers from CA callback, 2898 * or use statically provided list. */ 2899 #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) 2900 if( f_ca_cb != NULL ) 2901 { 2902 mbedtls_x509_crt_free( ver_chain->trust_ca_cb_result ); 2903 mbedtls_free( ver_chain->trust_ca_cb_result ); 2904 ver_chain->trust_ca_cb_result = NULL; 2905 2906 ret = f_ca_cb( p_ca_cb, child, &ver_chain->trust_ca_cb_result ); 2907 if( ret != 0 ) 2908 return( MBEDTLS_ERR_X509_FATAL_ERROR ); 2909 2910 cur_trust_ca = ver_chain->trust_ca_cb_result; 2911 } 2912 else 2913 #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ 2914 { 2915 ((void) f_ca_cb); 2916 ((void) p_ca_cb); 2917 cur_trust_ca = trust_ca; 2918 } 2919 2920 /* Look for a parent in trusted CAs or up the chain */ 2921 ret = x509_crt_find_parent( child, cur_trust_ca, &parent, 2922 &parent_is_trusted, &signature_is_good, 2923 ver_chain->len - 1, self_cnt, rs_ctx ); 2924 2925 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) 2926 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS ) 2927 { 2928 /* save state */ 2929 rs_ctx->in_progress = x509_crt_rs_find_parent; 2930 rs_ctx->self_cnt = self_cnt; 2931 rs_ctx->ver_chain = *ver_chain; /* struct copy */ 2932 2933 return( ret ); 2934 } 2935 #else 2936 (void) ret; 2937 #endif 2938 2939 /* No parent? We're done here */ 2940 if( parent == NULL ) 2941 { 2942 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED; 2943 return( 0 ); 2944 } 2945 2946 /* Count intermediate self-issued (not necessarily self-signed) certs. 2947 * These can occur with some strategies for key rollover, see [SIRO], 2948 * and should be excluded from max_pathlen checks. */ 2949 if( ver_chain->len != 1 && 2950 x509_name_cmp( &child->issuer, &child->subject ) == 0 ) 2951 { 2952 self_cnt++; 2953 } 2954 2955 /* path_cnt is 0 for the first intermediate CA, 2956 * and if parent is trusted it's not an intermediate CA */ 2957 if( ! parent_is_trusted && 2958 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA ) 2959 { 2960 /* return immediately to avoid overflow the chain array */ 2961 return( MBEDTLS_ERR_X509_FATAL_ERROR ); 2962 } 2963 2964 /* signature was checked while searching parent */ 2965 if( ! signature_is_good ) 2966 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED; 2967 2968 /* check size of signing key */ 2969 if( x509_profile_check_key( profile, &parent->pk ) != 0 ) 2970 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY; 2971 2972 #if defined(MBEDTLS_X509_CRL_PARSE_C) 2973 /* Check trusted CA's CRL for the given crt */ 2974 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile ); 2975 #else 2976 (void) ca_crl; 2977 #endif 2978 2979 /* prepare for next iteration */ 2980 child = parent; 2981 parent = NULL; 2982 child_is_trusted = parent_is_trusted; 2983 signature_is_good = 0; 2984 } 2985 } 2986 2987 /* 2988 * Check for CN match 2989 */ 2990 static int x509_crt_check_cn( const mbedtls_x509_buf *name, 2991 const char *cn, size_t cn_len ) 2992 { 2993 /* try exact match */ 2994 if( name->len == cn_len && 2995 x509_memcasecmp( cn, name->p, cn_len ) == 0 ) 2996 { 2997 return( 0 ); 2998 } 2999 3000 /* try wildcard match */ 3001 if( x509_check_wildcard( cn, name ) == 0 ) 3002 { 3003 return( 0 ); 3004 } 3005 3006 return( -1 ); 3007 } 3008 3009 /* 3010 * Check for SAN match, see RFC 5280 Section 4.2.1.6 3011 */ 3012 static int x509_crt_check_san( const mbedtls_x509_buf *name, 3013 const char *cn, size_t cn_len ) 3014 { 3015 const unsigned char san_type = (unsigned char) name->tag & 3016 MBEDTLS_ASN1_TAG_VALUE_MASK; 3017 3018 /* dNSName */ 3019 if( san_type == MBEDTLS_X509_SAN_DNS_NAME ) 3020 return( x509_crt_check_cn( name, cn, cn_len ) ); 3021 3022 /* (We may handle other types here later.) */ 3023 3024 /* Unrecognized type */ 3025 return( -1 ); 3026 } 3027 3028 /* 3029 * Verify the requested CN - only call this if cn is not NULL! 3030 */ 3031 static void x509_crt_verify_name( const mbedtls_x509_crt *crt, 3032 const char *cn, 3033 uint32_t *flags ) 3034 { 3035 const mbedtls_x509_name *name; 3036 const mbedtls_x509_sequence *cur; 3037 size_t cn_len = strlen( cn ); 3038 3039 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME ) 3040 { 3041 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next ) 3042 { 3043 if( x509_crt_check_san( &cur->buf, cn, cn_len ) == 0 ) 3044 break; 3045 } 3046 3047 if( cur == NULL ) 3048 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH; 3049 } 3050 else 3051 { 3052 for( name = &crt->subject; name != NULL; name = name->next ) 3053 { 3054 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 && 3055 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 ) 3056 { 3057 break; 3058 } 3059 } 3060 3061 if( name == NULL ) 3062 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH; 3063 } 3064 } 3065 3066 /* 3067 * Merge the flags for all certs in the chain, after calling callback 3068 */ 3069 static int x509_crt_merge_flags_with_cb( 3070 uint32_t *flags, 3071 const mbedtls_x509_crt_verify_chain *ver_chain, 3072 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), 3073 void *p_vrfy ) 3074 { 3075 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 3076 unsigned i; 3077 uint32_t cur_flags; 3078 const mbedtls_x509_crt_verify_chain_item *cur; 3079 3080 for( i = ver_chain->len; i != 0; --i ) 3081 { 3082 cur = &ver_chain->items[i-1]; 3083 cur_flags = cur->flags; 3084 3085 if( NULL != f_vrfy ) 3086 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 ) 3087 return( ret ); 3088 3089 *flags |= cur_flags; 3090 } 3091 3092 return( 0 ); 3093 } 3094 3095 /* 3096 * Verify the certificate validity, with profile, restartable version 3097 * 3098 * This function: 3099 * - checks the requested CN (if any) 3100 * - checks the type and size of the EE cert's key, 3101 * as that isn't done as part of chain building/verification currently 3102 * - builds and verifies the chain 3103 * - then calls the callback and merges the flags 3104 * 3105 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb` 3106 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the 3107 * verification routine to search for trusted signers, and CRLs will 3108 * be disabled. Otherwise, `trust_ca` will be used as the static list 3109 * of trusted signers, and `ca_crl` will be use as the static list 3110 * of CRLs. 3111 */ 3112 static int x509_crt_verify_restartable_ca_cb( mbedtls_x509_crt *crt, 3113 mbedtls_x509_crt *trust_ca, 3114 mbedtls_x509_crl *ca_crl, 3115 mbedtls_x509_crt_ca_cb_t f_ca_cb, 3116 void *p_ca_cb, 3117 const mbedtls_x509_crt_profile *profile, 3118 const char *cn, uint32_t *flags, 3119 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), 3120 void *p_vrfy, 3121 mbedtls_x509_crt_restart_ctx *rs_ctx ) 3122 { 3123 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 3124 mbedtls_pk_type_t pk_type; 3125 mbedtls_x509_crt_verify_chain ver_chain; 3126 uint32_t ee_flags; 3127 3128 *flags = 0; 3129 ee_flags = 0; 3130 x509_crt_verify_chain_reset( &ver_chain ); 3131 3132 if( profile == NULL ) 3133 { 3134 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA; 3135 goto exit; 3136 } 3137 3138 /* check name if requested */ 3139 if( cn != NULL ) 3140 x509_crt_verify_name( crt, cn, &ee_flags ); 3141 3142 /* Check the type and size of the key */ 3143 pk_type = mbedtls_pk_get_type( &crt->pk ); 3144 3145 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 ) 3146 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK; 3147 3148 if( x509_profile_check_key( profile, &crt->pk ) != 0 ) 3149 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY; 3150 3151 /* Check the chain */ 3152 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl, 3153 f_ca_cb, p_ca_cb, profile, 3154 &ver_chain, rs_ctx ); 3155 3156 if( ret != 0 ) 3157 goto exit; 3158 3159 /* Merge end-entity flags */ 3160 ver_chain.items[0].flags |= ee_flags; 3161 3162 /* Build final flags, calling callback on the way if any */ 3163 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy ); 3164 3165 exit: 3166 3167 #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) 3168 mbedtls_x509_crt_free( ver_chain.trust_ca_cb_result ); 3169 mbedtls_free( ver_chain.trust_ca_cb_result ); 3170 ver_chain.trust_ca_cb_result = NULL; 3171 #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ 3172 3173 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) 3174 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS ) 3175 mbedtls_x509_crt_restart_free( rs_ctx ); 3176 #endif 3177 3178 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by 3179 * the SSL module for authmode optional, but non-zero return from the 3180 * callback means a fatal error so it shouldn't be ignored */ 3181 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ) 3182 ret = MBEDTLS_ERR_X509_FATAL_ERROR; 3183 3184 if( ret != 0 ) 3185 { 3186 *flags = (uint32_t) -1; 3187 return( ret ); 3188 } 3189 3190 if( *flags != 0 ) 3191 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ); 3192 3193 return( 0 ); 3194 } 3195 3196 3197 /* 3198 * Verify the certificate validity (default profile, not restartable) 3199 */ 3200 int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt, 3201 mbedtls_x509_crt *trust_ca, 3202 mbedtls_x509_crl *ca_crl, 3203 const char *cn, uint32_t *flags, 3204 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), 3205 void *p_vrfy ) 3206 { 3207 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl, 3208 NULL, NULL, 3209 &mbedtls_x509_crt_profile_default, 3210 cn, flags, 3211 f_vrfy, p_vrfy, NULL ) ); 3212 } 3213 3214 /* 3215 * Verify the certificate validity (user-chosen profile, not restartable) 3216 */ 3217 int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt, 3218 mbedtls_x509_crt *trust_ca, 3219 mbedtls_x509_crl *ca_crl, 3220 const mbedtls_x509_crt_profile *profile, 3221 const char *cn, uint32_t *flags, 3222 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), 3223 void *p_vrfy ) 3224 { 3225 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl, 3226 NULL, NULL, 3227 profile, cn, flags, 3228 f_vrfy, p_vrfy, NULL ) ); 3229 } 3230 3231 #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) 3232 /* 3233 * Verify the certificate validity (user-chosen profile, CA callback, 3234 * not restartable). 3235 */ 3236 int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt, 3237 mbedtls_x509_crt_ca_cb_t f_ca_cb, 3238 void *p_ca_cb, 3239 const mbedtls_x509_crt_profile *profile, 3240 const char *cn, uint32_t *flags, 3241 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), 3242 void *p_vrfy ) 3243 { 3244 return( x509_crt_verify_restartable_ca_cb( crt, NULL, NULL, 3245 f_ca_cb, p_ca_cb, 3246 profile, cn, flags, 3247 f_vrfy, p_vrfy, NULL ) ); 3248 } 3249 #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ 3250 3251 int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt, 3252 mbedtls_x509_crt *trust_ca, 3253 mbedtls_x509_crl *ca_crl, 3254 const mbedtls_x509_crt_profile *profile, 3255 const char *cn, uint32_t *flags, 3256 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), 3257 void *p_vrfy, 3258 mbedtls_x509_crt_restart_ctx *rs_ctx ) 3259 { 3260 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl, 3261 NULL, NULL, 3262 profile, cn, flags, 3263 f_vrfy, p_vrfy, rs_ctx ) ); 3264 } 3265 3266 3267 /* 3268 * Initialize a certificate chain 3269 */ 3270 void mbedtls_x509_crt_init( mbedtls_x509_crt *crt ) 3271 { 3272 memset( crt, 0, sizeof(mbedtls_x509_crt) ); 3273 } 3274 3275 /* 3276 * Unallocate all certificate data 3277 */ 3278 void mbedtls_x509_crt_free( mbedtls_x509_crt *crt ) 3279 { 3280 mbedtls_x509_crt *cert_cur = crt; 3281 mbedtls_x509_crt *cert_prv; 3282 mbedtls_x509_name *name_cur; 3283 mbedtls_x509_name *name_prv; 3284 mbedtls_x509_sequence *seq_cur; 3285 mbedtls_x509_sequence *seq_prv; 3286 3287 if( crt == NULL ) 3288 return; 3289 3290 do 3291 { 3292 mbedtls_pk_free( &cert_cur->pk ); 3293 3294 #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) 3295 mbedtls_free( cert_cur->sig_opts ); 3296 #endif 3297 3298 name_cur = cert_cur->issuer.next; 3299 while( name_cur != NULL ) 3300 { 3301 name_prv = name_cur; 3302 name_cur = name_cur->next; 3303 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) ); 3304 mbedtls_free( name_prv ); 3305 } 3306 3307 name_cur = cert_cur->subject.next; 3308 while( name_cur != NULL ) 3309 { 3310 name_prv = name_cur; 3311 name_cur = name_cur->next; 3312 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) ); 3313 mbedtls_free( name_prv ); 3314 } 3315 3316 seq_cur = cert_cur->ext_key_usage.next; 3317 while( seq_cur != NULL ) 3318 { 3319 seq_prv = seq_cur; 3320 seq_cur = seq_cur->next; 3321 mbedtls_platform_zeroize( seq_prv, 3322 sizeof( mbedtls_x509_sequence ) ); 3323 mbedtls_free( seq_prv ); 3324 } 3325 3326 seq_cur = cert_cur->subject_alt_names.next; 3327 while( seq_cur != NULL ) 3328 { 3329 seq_prv = seq_cur; 3330 seq_cur = seq_cur->next; 3331 mbedtls_platform_zeroize( seq_prv, 3332 sizeof( mbedtls_x509_sequence ) ); 3333 mbedtls_free( seq_prv ); 3334 } 3335 3336 seq_cur = cert_cur->certificate_policies.next; 3337 while( seq_cur != NULL ) 3338 { 3339 seq_prv = seq_cur; 3340 seq_cur = seq_cur->next; 3341 mbedtls_platform_zeroize( seq_prv, 3342 sizeof( mbedtls_x509_sequence ) ); 3343 mbedtls_free( seq_prv ); 3344 } 3345 3346 if( cert_cur->raw.p != NULL && cert_cur->own_buffer ) 3347 { 3348 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len ); 3349 mbedtls_free( cert_cur->raw.p ); 3350 } 3351 3352 cert_cur = cert_cur->next; 3353 } 3354 while( cert_cur != NULL ); 3355 3356 cert_cur = crt; 3357 do 3358 { 3359 cert_prv = cert_cur; 3360 cert_cur = cert_cur->next; 3361 3362 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) ); 3363 if( cert_prv != crt ) 3364 mbedtls_free( cert_prv ); 3365 } 3366 while( cert_cur != NULL ); 3367 } 3368 3369 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) 3370 /* 3371 * Initialize a restart context 3372 */ 3373 void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx ) 3374 { 3375 mbedtls_pk_restart_init( &ctx->pk ); 3376 3377 ctx->parent = NULL; 3378 ctx->fallback_parent = NULL; 3379 ctx->fallback_signature_is_good = 0; 3380 3381 ctx->parent_is_trusted = -1; 3382 3383 ctx->in_progress = x509_crt_rs_none; 3384 ctx->self_cnt = 0; 3385 x509_crt_verify_chain_reset( &ctx->ver_chain ); 3386 } 3387 3388 /* 3389 * Free the components of a restart context 3390 */ 3391 void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx ) 3392 { 3393 if( ctx == NULL ) 3394 return; 3395 3396 mbedtls_pk_restart_free( &ctx->pk ); 3397 mbedtls_x509_crt_restart_init( ctx ); 3398 } 3399 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ 3400 3401 #endif /* MBEDTLS_X509_CRT_PARSE_C */ 3402