| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <algorithm> |
| 16 | #include <iterator> |
| 17 |
| 18 | #include <openssl/base.h> |
| 19 | #include <openssl/bytestring.h> |
| 20 |
| 21 | #include "cert_errors.h" |
| 22 | #include "crl.h" |
| 23 | #include "input.h" |
| 24 | #include "parse_values.h" |
| 25 | #include "parser.h" |
| 26 | #include "revocation_util.h" |
| 27 | #include "signature_algorithm.h" |
| 28 | #include "verify_name_match.h" |
| 29 | #include "verify_signed_data.h" |
| 30 |
| 31 | BSSL_NAMESPACE_BEGIN |
| 32 |
| 33 | namespace { |
| 34 |
| 35 | // id-ce-issuingDistributionPoint OBJECT IDENTIFIER ::= { id-ce 28 } |
| 36 | // In dotted notation: 2.5.29.28 |
| 37 | inline constexpr uint8_t kIssuingDistributionPointOid[] = {0x55, 0x1d, 0x1c}; |
| 38 |
| 39 | [[nodiscard]] bool NormalizeNameTLV(der::Input name_tlv, |
| 40 | std::string *out_normalized_name) { |
| 41 | der::Parser parser(name_tlv); |
| 42 | der::Input name_rdn; |
| 43 | bssl::CertErrors unused_errors; |
| 44 | return parser.ReadTag(CBS_ASN1_SEQUENCE, &name_rdn) && |
| 45 | NormalizeName(name_rdn, out_normalized_name, &unused_errors) && |
| 46 | !parser.HasMore(); |
| 47 | } |
| 48 |
| 49 | bool ContainsExactMatchingName(std::vector<std::string_view> a, |
| 50 | std::vector<std::string_view> b) { |
| 51 | std::sort(a.begin(), a.end()); |
| 52 | std::sort(b.begin(), b.end()); |
| 53 | std::vector<std::string_view> names_in_common; |
| 54 | std::set_intersection(a.begin(), a.end(), b.begin(), b.end(), |
| 55 | std::back_inserter(names_in_common)); |
| 56 | return !names_in_common.empty(); |
| 57 | } |
| 58 |
| 59 | } // namespace |
| 60 |
| 61 | bool ParseCrlCertificateList(der::Input crl_tlv, |
| 62 | der::Input *out_tbs_cert_list_tlv, |
| 63 | der::Input *out_signature_algorithm_tlv, |
| 64 | der::BitString *out_signature_value) { |
| 65 | der::Parser parser(crl_tlv); |
| 66 |
| 67 | // CertificateList ::= SEQUENCE { |
| 68 | der::Parser certificate_list_parser; |
| 69 | if (!parser.ReadSequence(&certificate_list_parser)) { |
| 70 | return false; |
| 71 | } |
| 72 |
| 73 | // tbsCertList TBSCertList |
| 74 | if (!certificate_list_parser.ReadRawTLV(out_tbs_cert_list_tlv)) { |
| 75 | return false; |
| 76 | } |
| 77 |
| 78 | // signatureAlgorithm AlgorithmIdentifier, |
| 79 | if (!certificate_list_parser.ReadRawTLV(out_signature_algorithm_tlv)) { |
| 80 | return false; |
| 81 | } |
| 82 |
| 83 | // signatureValue BIT STRING } |
| 84 | std::optional<der::BitString> signature_value = |
| 85 | certificate_list_parser.ReadBitString(); |
| 86 | if (!signature_value) { |
| 87 | return false; |
| 88 | } |
| 89 | *out_signature_value = signature_value.value(); |
| 90 |
| 91 | // There isn't an extension point at the end of CertificateList. |
| 92 | if (certificate_list_parser.HasMore()) { |
| 93 | return false; |
| 94 | } |
| 95 |
| 96 | // By definition the input was a single CertificateList, so there shouldn't be |
| 97 | // unconsumed data. |
| 98 | if (parser.HasMore()) { |
| 99 | return false; |
| 100 | } |
| 101 |
| 102 | return true; |
| 103 | } |
| 104 |
| 105 | bool ParseCrlTbsCertList(der::Input tbs_tlv, ParsedCrlTbsCertList *out) { |
| 106 | der::Parser parser(tbs_tlv); |
| 107 |
| 108 | // TBSCertList ::= SEQUENCE { |
| 109 | der::Parser tbs_parser; |
| 110 | if (!parser.ReadSequence(&tbs_parser)) { |
| 111 | return false; |
| 112 | } |
| 113 |
| 114 | // version Version OPTIONAL, |
| 115 | // -- if present, MUST be v2 |
| 116 | std::optional<der::Input> version_der; |
| 117 | if (!tbs_parser.ReadOptionalTag(CBS_ASN1_INTEGER, &version_der)) { |
| 118 | return false; |
| 119 | } |
| 120 | if (version_der.has_value()) { |
| 121 | uint64_t version64; |
| 122 | if (!der::ParseUint64(*version_der, &version64)) { |
| 123 | return false; |
| 124 | } |
| 125 | // If version is present, it MUST be v2(1). |
| 126 | if (version64 != 1) { |
| 127 | return false; |
| 128 | } |
| 129 | out->version = CrlVersion::V2; |
| 130 | } else { |
| 131 | // Uh, RFC 5280 doesn't actually say it anywhere, but presumably if version |
| 132 | // is not specified, it is V1. |
| 133 | out->version = CrlVersion::V1; |
| 134 | } |
| 135 |
| 136 | // signature AlgorithmIdentifier, |
| 137 | if (!tbs_parser.ReadRawTLV(&out->signature_algorithm_tlv)) { |
| 138 | return false; |
| 139 | } |
| 140 |
| 141 | // issuer Name, |
| 142 | if (!tbs_parser.ReadRawTLV(&out->issuer_tlv)) { |
| 143 | return false; |
| 144 | } |
| 145 |
| 146 | // thisUpdate Time, |
| 147 | if (!ReadUTCOrGeneralizedTime(&tbs_parser, &out->this_update)) { |
| 148 | return false; |
| 149 | } |
| 150 |
| 151 | // nextUpdate Time OPTIONAL, |
| 152 | CBS_ASN1_TAG maybe_next_update_tag; |
| 153 | der::Input unused_next_update_input; |
| 154 | if (tbs_parser.PeekTagAndValue(&maybe_next_update_tag, |
| 155 | &unused_next_update_input) && |
| 156 | (maybe_next_update_tag == CBS_ASN1_UTCTIME || |
| 157 | maybe_next_update_tag == CBS_ASN1_GENERALIZEDTIME)) { |
| 158 | der::GeneralizedTime next_update_time; |
| 159 | if (!ReadUTCOrGeneralizedTime(&tbs_parser, &next_update_time)) { |
| 160 | return false; |
| 161 | } |
| 162 | out->next_update = next_update_time; |
| 163 | } else { |
| 164 | out->next_update = std::nullopt; |
| 165 | } |
| 166 |
| 167 | // revokedCertificates SEQUENCE OF SEQUENCE { ... } OPTIONAL, |
| 168 | der::Input unused_revoked_certificates; |
| 169 | CBS_ASN1_TAG maybe_revoked_certifigates_tag; |
| 170 | if (tbs_parser.PeekTagAndValue(&maybe_revoked_certifigates_tag, |
| 171 | &unused_revoked_certificates) && |
| 172 | maybe_revoked_certifigates_tag == CBS_ASN1_SEQUENCE) { |
| 173 | der::Input revoked_certificates_tlv; |
| 174 | if (!tbs_parser.ReadRawTLV(&revoked_certificates_tlv)) { |
| 175 | return false; |
| 176 | } |
| 177 | out->revoked_certificates_tlv = revoked_certificates_tlv; |
| 178 | } else { |
| 179 | out->revoked_certificates_tlv = std::nullopt; |
| 180 | } |
| 181 |
| 182 | // crlExtensions [0] EXPLICIT Extensions OPTIONAL |
| 183 | // -- if present, version MUST be v2 |
| 184 | if (!tbs_parser.ReadOptionalTag( |
| 185 | CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0, |
| 186 | &out->crl_extensions_tlv)) { |
| 187 | return false; |
| 188 | } |
| 189 | if (out->crl_extensions_tlv.has_value()) { |
| 190 | if (out->version != CrlVersion::V2) { |
| 191 | return false; |
| 192 | } |
| 193 | } |
| 194 |
| 195 | if (tbs_parser.HasMore()) { |
| 196 | // Invalid or extraneous elements. |
| 197 | return false; |
| 198 | } |
| 199 |
| 200 | // By definition the input was a single sequence, so there shouldn't be |
| 201 | // unconsumed data. |
| 202 | if (parser.HasMore()) { |
| 203 | return false; |
| 204 | } |
| 205 |
| 206 | return true; |
| 207 | } |
| 208 |
| 209 | bool ParseIssuingDistributionPoint( |
| 210 | der::Input extension_value, |
| 211 | std::unique_ptr<GeneralNames> *out_distribution_point_names, |
| 212 | ContainedCertsType *out_only_contains_cert_type) { |
| 213 | der::Parser idp_extension_value_parser(extension_value); |
| 214 | // IssuingDistributionPoint ::= SEQUENCE { |
| 215 | der::Parser idp_parser; |
| 216 | if (!idp_extension_value_parser.ReadSequence(&idp_parser)) { |
| 217 | return false; |
| 218 | } |
| 219 |
| 220 | // 5.2.5. Conforming CRLs issuers MUST NOT issue CRLs where the DER |
| 221 | // encoding of the issuing distribution point extension is an empty |
| 222 | // sequence. |
| 223 | if (!idp_parser.HasMore()) { |
| 224 | return false; |
| 225 | } |
| 226 |
| 227 | // distributionPoint [0] DistributionPointName OPTIONAL, |
| 228 | std::optional<der::Input> distribution_point; |
| 229 | if (!idp_parser.ReadOptionalTag( |
| 230 | CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0, |
| 231 | &distribution_point)) { |
| 232 | return false; |
| 233 | } |
| 234 |
| 235 | if (distribution_point.has_value()) { |
| 236 | // DistributionPointName ::= CHOICE { |
| 237 | der::Parser dp_name_parser(*distribution_point); |
| 238 | // fullName [0] GeneralNames, |
| 239 | // nameRelativeToCRLIssuer [1] RelativeDistinguishedName } |
| 240 | std::optional<der::Input> der_full_name; |
| 241 | if (!dp_name_parser.ReadOptionalTag( |
| 242 | CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0, |
| 243 | &der_full_name)) { |
| 244 | return false; |
| 245 | } |
| 246 | if (!der_full_name) { |
| 247 | // Only fullName is supported. |
| 248 | return false; |
| 249 | } |
| 250 | CertErrors errors; |
| 251 | *out_distribution_point_names = |
| 252 | GeneralNames::CreateFromValue(*der_full_name, &errors); |
| 253 | if (!*out_distribution_point_names) { |
| 254 | return false; |
| 255 | } |
| 256 |
| 257 | if (dp_name_parser.HasMore()) { |
| 258 | // CHOICE represents a single value. |
| 259 | return false; |
| 260 | } |
| 261 | } |
| 262 |
| 263 | *out_only_contains_cert_type = ContainedCertsType::ANY_CERTS; |
| 264 |
| 265 | // onlyContainsUserCerts [1] BOOLEAN DEFAULT FALSE, |
| 266 | std::optional<der::Input> only_contains_user_certs; |
| 267 | if (!idp_parser.ReadOptionalTag(CBS_ASN1_CONTEXT_SPECIFIC | 1, |
| 268 | &only_contains_user_certs)) { |
| 269 | return false; |
| 270 | } |
| 271 | if (only_contains_user_certs.has_value()) { |
| 272 | bool bool_value; |
| 273 | if (!der::ParseBool(*only_contains_user_certs, &bool_value)) { |
| 274 | return false; |
| 275 | } |
| 276 | if (!bool_value) { |
| 277 | return false; // DER-encoding requires DEFAULT values be omitted. |
| 278 | } |
| 279 | *out_only_contains_cert_type = ContainedCertsType::USER_CERTS; |
| 280 | } |
| 281 |
| 282 | // onlyContainsCACerts [2] BOOLEAN DEFAULT FALSE, |
| 283 | std::optional<der::Input> only_contains_ca_certs; |
| 284 | if (!idp_parser.ReadOptionalTag(CBS_ASN1_CONTEXT_SPECIFIC | 2, |
| 285 | &only_contains_ca_certs)) { |
| 286 | return false; |
| 287 | } |
| 288 | if (only_contains_ca_certs.has_value()) { |
| 289 | bool bool_value; |
| 290 | if (!der::ParseBool(*only_contains_ca_certs, &bool_value)) { |
| 291 | return false; |
| 292 | } |
| 293 | if (!bool_value) { |
| 294 | return false; // DER-encoding requires DEFAULT values be omitted. |
| 295 | } |
| 296 | if (*out_only_contains_cert_type != ContainedCertsType::ANY_CERTS) { |
| 297 | // 5.2.5. at most one of onlyContainsUserCerts, onlyContainsCACerts, |
| 298 | // and onlyContainsAttributeCerts may be set to TRUE. |
| 299 | return false; |
| 300 | } |
| 301 | *out_only_contains_cert_type = ContainedCertsType::CA_CERTS; |
| 302 | } |
| 303 |
| 304 | // onlySomeReasons [3] ReasonFlags OPTIONAL, |
| 305 | // indirectCRL [4] BOOLEAN DEFAULT FALSE, |
| 306 | // onlyContainsAttributeCerts [5] BOOLEAN DEFAULT FALSE } |
| 307 | // onlySomeReasons, indirectCRL, and onlyContainsAttributeCerts are not |
| 308 | // supported, fail parsing if they are present. |
| 309 | if (idp_parser.HasMore()) { |
| 310 | return false; |
| 311 | } |
| 312 |
| 313 | return true; |
| 314 | } |
| 315 |
| 316 | CRLRevocationStatus GetCRLStatusForCert( |
| 317 | der::Input cert_serial, CrlVersion crl_version, |
| 318 | const std::optional<der::Input> &revoked_certificates_tlv) { |
| 319 | if (!revoked_certificates_tlv.has_value()) { |
| 320 | // RFC 5280 Section 5.1.2.6: "When there are no revoked certificates, the |
| 321 | // revoked certificates list MUST be absent." |
| 322 | // No covered certificates are revoked, therefore the cert is good. |
| 323 | return CRLRevocationStatus::GOOD; |
| 324 | } |
| 325 |
| 326 | der::Parser parser(*revoked_certificates_tlv); |
| 327 |
| 328 | // revokedCertificates SEQUENCE OF SEQUENCE { |
| 329 | der::Parser revoked_certificates_parser; |
| 330 | if (!parser.ReadSequence(&revoked_certificates_parser)) { |
| 331 | return CRLRevocationStatus::UNKNOWN; |
| 332 | } |
| 333 |
| 334 | // RFC 5280 Section 5.1.2.6: "When there are no revoked certificates, the |
| 335 | // revoked certificates list MUST be absent." |
| 336 | if (!revoked_certificates_parser.HasMore()) { |
| 337 | return CRLRevocationStatus::UNKNOWN; |
| 338 | } |
| 339 |
| 340 | // By definition the input was a single Extensions sequence, so there |
| 341 | // shouldn't be unconsumed data. |
| 342 | if (parser.HasMore()) { |
| 343 | return CRLRevocationStatus::UNKNOWN; |
| 344 | } |
| 345 |
| 346 | bool found_matching_serial = false; |
| 347 |
| 348 | while (revoked_certificates_parser.HasMore()) { |
| 349 | // revokedCertificates SEQUENCE OF SEQUENCE { |
| 350 | der::Parser crl_entry_parser; |
| 351 | if (!revoked_certificates_parser.ReadSequence(&crl_entry_parser)) { |
| 352 | return CRLRevocationStatus::UNKNOWN; |
| 353 | } |
| 354 |
| 355 | der::Input revoked_cert_serial_number; |
| 356 | // userCertificate CertificateSerialNumber, |
| 357 | if (!crl_entry_parser.ReadTag(CBS_ASN1_INTEGER, |
| 358 | &revoked_cert_serial_number)) { |
| 359 | return CRLRevocationStatus::UNKNOWN; |
| 360 | } |
| 361 |
| 362 | // revocationDate Time, |
| 363 | der::GeneralizedTime unused_revocation_date; |
| 364 | if (!ReadUTCOrGeneralizedTime(&crl_entry_parser, &unused_revocation_date)) { |
| 365 | return CRLRevocationStatus::UNKNOWN; |
| 366 | } |
| 367 |
| 368 | // crlEntryExtensions Extensions OPTIONAL |
| 369 | if (crl_entry_parser.HasMore()) { |
| 370 | // -- if present, version MUST be v2 |
| 371 | if (crl_version != CrlVersion::V2) { |
| 372 | return CRLRevocationStatus::UNKNOWN; |
| 373 | } |
| 374 |
| 375 | der::Input crl_entry_extensions_tlv; |
| 376 | if (!crl_entry_parser.ReadRawTLV(&crl_entry_extensions_tlv)) { |
| 377 | return CRLRevocationStatus::UNKNOWN; |
| 378 | } |
| 379 |
| 380 | std::map<der::Input, ParsedExtension> extensions; |
| 381 | if (!ParseExtensions(crl_entry_extensions_tlv, &extensions)) { |
| 382 | return CRLRevocationStatus::UNKNOWN; |
| 383 | } |
| 384 |
| 385 | // RFC 5280 Section 5.3: "If a CRL contains a critical CRL entry |
| 386 | // extension that the application cannot process, then the application |
| 387 | // MUST NOT use that CRL to determine the status of any certificates." |
| 388 | for (const auto &ext : extensions) { |
| 389 | if (ext.second.critical) { |
| 390 | return CRLRevocationStatus::UNKNOWN; |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 |
| 395 | if (crl_entry_parser.HasMore()) { |
| 396 | return CRLRevocationStatus::UNKNOWN; |
| 397 | } |
| 398 |
| 399 | if (revoked_cert_serial_number == cert_serial) { |
| 400 | // Cert is revoked, but can't return yet since there might be critical |
| 401 | // extensions on later entries that would prevent use of this CRL. |
| 402 | found_matching_serial = true; |
| 403 | } |
| 404 | } |
| 405 |
| 406 | if (found_matching_serial) { |
| 407 | return CRLRevocationStatus::REVOKED; |
| 408 | } |
| 409 |
| 410 | // |cert| is not present in the revokedCertificates list. |
| 411 | return CRLRevocationStatus::GOOD; |
| 412 | } |
| 413 |
| 414 | ParsedCrlTbsCertList::ParsedCrlTbsCertList() = default; |
| 415 | ParsedCrlTbsCertList::~ParsedCrlTbsCertList() = default; |
| 416 |
| 417 | CRLRevocationStatus CheckCRL(std::string_view raw_crl, |
| 418 | const ParsedCertificateList &valid_chain, |
| 419 | size_t target_cert_index, |
| 420 | const ParsedDistributionPoint &cert_dp, |
| 421 | int64_t verify_time_epoch_seconds, |
| 422 | std::optional<int64_t> max_age_seconds) { |
| 423 | BSSL_CHECK(target_cert_index < valid_chain.size()); |
| 424 |
| 425 | if (cert_dp.reasons) { |
| 426 | // Reason codes are not supported. If the distribution point contains a |
| 427 | // subset of reasons then skip it. We aren't interested in subsets of CRLs |
| 428 | // and the RFC states that there MUST be a CRL that covers all reasons. |
| 429 | return CRLRevocationStatus::UNKNOWN; |
| 430 | } |
| 431 | if (cert_dp.crl_issuer) { |
| 432 | // Indirect CRLs are not supported. |
| 433 | return CRLRevocationStatus::UNKNOWN; |
| 434 | } |
| 435 |
| 436 | const ParsedCertificate *target_cert = valid_chain[target_cert_index].get(); |
| 437 |
| 438 | // 6.3.3 (a) Update the local CRL cache by obtaining a complete CRL, a |
| 439 | // delta CRL, or both, as required. |
| 440 | // |
| 441 | // This implementation only supports complete CRLs and takes the CRL as |
| 442 | // input, it is up to the caller to provide an up to date CRL. |
| 443 |
| 444 | der::Input tbs_cert_list_tlv; |
| 445 | der::Input signature_algorithm_tlv; |
| 446 | der::BitString signature_value; |
| 447 | if (!ParseCrlCertificateList(der::Input(raw_crl), &tbs_cert_list_tlv, |
| 448 | &signature_algorithm_tlv, &signature_value)) { |
| 449 | return CRLRevocationStatus::UNKNOWN; |
| 450 | } |
| 451 |
| 452 | ParsedCrlTbsCertList tbs_cert_list; |
| 453 | if (!ParseCrlTbsCertList(tbs_cert_list_tlv, &tbs_cert_list)) { |
| 454 | return CRLRevocationStatus::UNKNOWN; |
| 455 | } |
| 456 |
| 457 | // 5.1.1.2 signatureAlgorithm |
| 458 | // |
| 459 | // TODO(https://crbug.com/749276): Check the signature algorithm against |
| 460 | // policy. |
| 461 | std::optional<SignatureAlgorithm> signature_algorithm = |
| 462 | ParseSignatureAlgorithm(signature_algorithm_tlv); |
| 463 | if (!signature_algorithm) { |
| 464 | return CRLRevocationStatus::UNKNOWN; |
| 465 | } |
| 466 |
| 467 | // This field MUST contain the same algorithm identifier as the |
| 468 | // signature field in the sequence tbsCertList (Section 5.1.2.2). |
| 469 | std::optional<SignatureAlgorithm> tbs_alg = |
| 470 | ParseSignatureAlgorithm(tbs_cert_list.signature_algorithm_tlv); |
| 471 | if (!tbs_alg || *signature_algorithm != *tbs_alg) { |
| 472 | return CRLRevocationStatus::UNKNOWN; |
| 473 | } |
| 474 |
| 475 | // Check CRL dates. Roughly corresponds to 6.3.3 (a) (1) but does not attempt |
| 476 | // to update the CRL if it is out of date. |
| 477 | if (!CheckRevocationDateValid(tbs_cert_list.this_update, |
| 478 | tbs_cert_list.next_update.has_value() |
| 479 | ? &tbs_cert_list.next_update.value() |
| 480 | : nullptr, |
| 481 | verify_time_epoch_seconds, max_age_seconds)) { |
| 482 | return CRLRevocationStatus::UNKNOWN; |
| 483 | } |
| 484 |
| 485 | // 6.3.3 (a) (2) is skipped: This implementation does not support delta CRLs. |
| 486 |
| 487 | // 6.3.3 (b) Verify the issuer and scope of the complete CRL as follows: |
| 488 | // 6.3.3 (b) (1) If the DP includes cRLIssuer, then verify that the issuer |
| 489 | // field in the complete CRL matches cRLIssuer in the DP and |
| 490 | // that the complete CRL contains an issuing distribution |
| 491 | // point extension with the indirectCRL boolean asserted. |
| 492 | // |
| 493 | // Nothing is done here since distribution points with crlIssuer were skipped |
| 494 | // above. |
| 495 |
| 496 | // 6.3.3 (b) (1) Otherwise, verify that the CRL issuer matches the |
| 497 | // certificate issuer. |
| 498 | // |
| 499 | // Normalization for the name comparison is used although the RFC is not |
| 500 | // clear on this. There are several places that explicitly are called out as |
| 501 | // requiring identical encodings: |
| 502 | // |
| 503 | // 4.2.1.13. CRL Distribution Points (cert extension) says the DP cRLIssuer |
| 504 | // field MUST be exactly the same as the encoding in issuer field of the |
| 505 | // CRL. |
| 506 | // |
| 507 | // 5.2.5. Issuing Distribution Point (crl extension) |
| 508 | // The identical encoding MUST be used in the distributionPoint fields |
| 509 | // of the certificate and the CRL. |
| 510 | // |
| 511 | // 5.3.3. Certificate Issuer (crl entry extension) also says "The encoding of |
| 512 | // the DN MUST be identical to the encoding used in the certificate" |
| 513 | // |
| 514 | // But 6.3.3 (b) (1) just says "matches". Also NIST PKITS includes at least |
| 515 | // one test that requires normalization here. |
| 516 | // TODO(https://crbug.com/749276): could do exact comparison first and only |
| 517 | // fall back to normalizing if that fails. |
| 518 | std::string normalized_crl_issuer; |
| 519 | if (!NormalizeNameTLV(tbs_cert_list.issuer_tlv, &normalized_crl_issuer)) { |
| 520 | return CRLRevocationStatus::UNKNOWN; |
| 521 | } |
| 522 | if (der::Input(normalized_crl_issuer) != target_cert->normalized_issuer()) { |
| 523 | return CRLRevocationStatus::UNKNOWN; |
| 524 | } |
| 525 |
| 526 | if (tbs_cert_list.crl_extensions_tlv.has_value()) { |
| 527 | std::map<der::Input, ParsedExtension> extensions; |
| 528 | if (!ParseExtensions(*tbs_cert_list.crl_extensions_tlv, &extensions)) { |
| 529 | return CRLRevocationStatus::UNKNOWN; |
| 530 | } |
| 531 |
| 532 | // 6.3.3 (b) (2) If the complete CRL includes an issuing distribution point |
| 533 | // (IDP) CRL extension, check the following: |
| 534 | ParsedExtension idp_extension; |
| 535 | if (ConsumeExtension(der::Input(kIssuingDistributionPointOid), &extensions, |
| 536 | &idp_extension)) { |
| 537 | std::unique_ptr<GeneralNames> distribution_point_names; |
| 538 | ContainedCertsType only_contains_cert_type; |
| 539 | if (!ParseIssuingDistributionPoint(idp_extension.value, |
| 540 | &distribution_point_names, |
| 541 | &only_contains_cert_type)) { |
| 542 | return CRLRevocationStatus::UNKNOWN; |
| 543 | } |
| 544 |
| 545 | if (distribution_point_names) { |
| 546 | // 6.3.3. (b) (2) (i) If the distribution point name is present in the |
| 547 | // IDP CRL extension and the distribution field is |
| 548 | // present in the DP, then verify that one of the |
| 549 | // names in the IDP matches one of the names in the |
| 550 | // DP. |
| 551 | // 5.2.5. The identical encoding MUST be used in the distributionPoint |
| 552 | // fields of the certificate and the CRL. |
| 553 | // TODO(https://crbug.com/749276): Check other name types? |
| 554 | if (!cert_dp.distribution_point_fullname || |
| 555 | !ContainsExactMatchingName( |
| 556 | cert_dp.distribution_point_fullname |
| 557 | ->uniform_resource_identifiers, |
| 558 | distribution_point_names->uniform_resource_identifiers)) { |
| 559 | return CRLRevocationStatus::UNKNOWN; |
| 560 | } |
| 561 |
| 562 | // 6.3.3. (b) (2) (i) If the distribution point name is present in the |
| 563 | // IDP CRL extension and the distribution field is |
| 564 | // omitted from the DP, then verify that one of the |
| 565 | // names in the IDP matches one of the names in the |
| 566 | // cRLIssuer field of the DP. |
| 567 | // Indirect CRLs are not supported, if indirectCRL was specified, |
| 568 | // ParseIssuingDistributionPoint would already have failed. |
| 569 | } |
| 570 |
| 571 | switch (only_contains_cert_type) { |
| 572 | case ContainedCertsType::USER_CERTS: |
| 573 | // 6.3.3. (b) (2) (ii) If the onlyContainsUserCerts boolean is |
| 574 | // asserted in the IDP CRL extension, verify |
| 575 | // that the certificate does not include the |
| 576 | // basic constraints extension with the cA |
| 577 | // boolean asserted. |
| 578 | // 5.2.5. If either onlyContainsUserCerts or onlyContainsCACerts is |
| 579 | // set to TRUE, then the scope of the CRL MUST NOT include any |
| 580 | // version 1 or version 2 certificates. |
| 581 | if ((target_cert->has_basic_constraints() && |
| 582 | target_cert->basic_constraints().is_ca) || |
| 583 | target_cert->tbs().version == CertificateVersion::V1 || |
| 584 | target_cert->tbs().version == CertificateVersion::V2) { |
| 585 | return CRLRevocationStatus::UNKNOWN; |
| 586 | } |
| 587 | break; |
| 588 |
| 589 | case ContainedCertsType::CA_CERTS: |
| 590 | // 6.3.3. (b) (2) (iii) If the onlyContainsCACerts boolean is asserted |
| 591 | // in the IDP CRL extension, verify that the |
| 592 | // certificate includes the basic constraints |
| 593 | // extension with the cA boolean asserted. |
| 594 | // The version check is not done here, as the basicConstraints |
| 595 | // extension is required, and could not be present unless it is a V3 |
| 596 | // certificate. |
| 597 | if (!target_cert->has_basic_constraints() || |
| 598 | !target_cert->basic_constraints().is_ca) { |
| 599 | return CRLRevocationStatus::UNKNOWN; |
| 600 | } |
| 601 | break; |
| 602 |
| 603 | case ContainedCertsType::ANY_CERTS: |
| 604 | // (iv) Verify that the onlyContainsAttributeCerts |
| 605 | // boolean is not asserted. |
| 606 | // If onlyContainsAttributeCerts was present, |
| 607 | // ParseIssuingDistributionPoint would already have failed. |
| 608 | break; |
| 609 | } |
| 610 | } |
| 611 |
| 612 | for (const auto &ext : extensions) { |
| 613 | // Fail if any unhandled critical CRL extensions are present. |
| 614 | if (ext.second.critical) { |
| 615 | return CRLRevocationStatus::UNKNOWN; |
| 616 | } |
| 617 | } |
| 618 | } |
| 619 |
| 620 | // 6.3.3 (c-e) skipped: delta CRLs and reason codes are not supported. |
| 621 |
| 622 | // This implementation only supports direct CRLs where the CRL was signed by |
| 623 | // one of the certs in its validated issuer chain. This allows handling some |
| 624 | // cases of key rollover without requiring additional CRL issuer cert |
| 625 | // discovery & path building. |
| 626 | // TODO(https://crbug.com/749276): should this loop start at |
| 627 | // |target_cert_index|? There doesn't seem to be anything in the specs that |
| 628 | // precludes a CRL signed by a self-issued cert from covering itself. On the |
| 629 | // other hand it seems like a pretty weird thing to allow and causes NIST |
| 630 | // PKITS 4.5.3 to pass when it seems like it would not be intended to (since |
| 631 | // issuingDistributionPoint CRL extension is not handled). |
| 632 | for (size_t i = target_cert_index + 1; i < valid_chain.size(); ++i) { |
| 633 | const ParsedCertificate *issuer_cert = valid_chain[i].get(); |
| 634 |
| 635 | // 6.3.3 (f) Obtain and validate the certification path for the issuer of |
| 636 | // the complete CRL. The trust anchor for the certification |
| 637 | // path MUST be the same as the trust anchor used to validate |
| 638 | // the target certificate. |
| 639 | // |
| 640 | // As the |issuer_cert| is from the already validated chain, it is already |
| 641 | // known to chain to the same trust anchor as the target certificate. |
| 642 | if (der::Input(normalized_crl_issuer) != |
| 643 | issuer_cert->normalized_subject()) { |
| 644 | continue; |
| 645 | } |
| 646 |
| 647 | // 6.3.3 (f) If a key usage extension is present in the CRL issuer's |
| 648 | // certificate, verify that the cRLSign bit is set. |
| 649 | if (issuer_cert->has_key_usage() && |
| 650 | !issuer_cert->key_usage().AssertsBit(KEY_USAGE_BIT_CRL_SIGN)) { |
| 651 | continue; |
| 652 | } |
| 653 |
| 654 | // 6.3.3 (g) Validate the signature on the complete CRL using the public |
| 655 | // key validated in step (f). |
| 656 | if (!VerifySignedData(*signature_algorithm, tbs_cert_list_tlv, |
| 657 | signature_value, issuer_cert->tbs().spki_tlv, |
| 658 | /*cache=*/nullptr)) { |
| 659 | continue; |
| 660 | } |
| 661 |
| 662 | // 6.3.3 (h,i) skipped. This implementation does not support delta CRLs. |
| 663 |
| 664 | // 6.3.3 (j) If (cert_status is UNREVOKED), then search for the |
| 665 | // certificate on the complete CRL. If an entry is found that |
| 666 | // matches the certificate issuer and serial number as described |
| 667 | // in Section 5.3.3, then set the cert_status variable to the |
| 668 | // indicated reason as described in step (i). |
| 669 | // |
| 670 | // CRL is valid and covers |target_cert|, check if |target_cert| is present |
| 671 | // in the revokedCertificates sequence. |
| 672 | return GetCRLStatusForCert(target_cert->tbs().serial_number, |
| 673 | tbs_cert_list.version, |
| 674 | tbs_cert_list.revoked_certificates_tlv); |
| 675 |
| 676 | // 6.3.3 (k,l) skipped. This implementation does not support reason codes. |
| 677 | } |
| 678 |
| 679 | // Did not find the issuer & signer of |raw_crl| in |valid_chain|. |
| 680 | return CRLRevocationStatus::UNKNOWN; |
| 681 | } |
| 682 |
| 683 | BSSL_NAMESPACE_END |
| 684 |