| 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 "verify_certificate_chain.h" |
| 16 |
| 17 | #include <algorithm> |
| 18 | #include <cassert> |
| 19 |
| 20 | #include <openssl/base.h> |
| 21 | #include "cert_error_params.h" |
| 22 | #include "cert_errors.h" |
| 23 | #include "common_cert_errors.h" |
| 24 | #include "extended_key_usage.h" |
| 25 | #include "input.h" |
| 26 | #include "name_constraints.h" |
| 27 | #include "parse_certificate.h" |
| 28 | #include "signature_algorithm.h" |
| 29 | #include "trust_store.h" |
| 30 | #include "verify_signed_data.h" |
| 31 |
| 32 | BSSL_NAMESPACE_BEGIN |
| 33 |
| 34 | namespace { |
| 35 |
| 36 | bool IsHandledCriticalExtension(const ParsedExtension &extension) { |
| 37 | if (extension.oid == der::Input(kBasicConstraintsOid)) { |
| 38 | return true; |
| 39 | } |
| 40 | // Key Usage is NOT processed for end-entity certificates (this is the |
| 41 | // responsibility of callers), however it is considered "handled" here in |
| 42 | // order to allow being marked as critical. |
| 43 | if (extension.oid == der::Input(kKeyUsageOid)) { |
| 44 | return true; |
| 45 | } |
| 46 | if (extension.oid == der::Input(kExtKeyUsageOid)) { |
| 47 | return true; |
| 48 | } |
| 49 | if (extension.oid == der::Input(kNameConstraintsOid)) { |
| 50 | return true; |
| 51 | } |
| 52 | if (extension.oid == der::Input(kSubjectAltNameOid)) { |
| 53 | return true; |
| 54 | } |
| 55 | if (extension.oid == der::Input(kCertificatePoliciesOid)) { |
| 56 | // Policy qualifiers are skipped during processing, so if the |
| 57 | // extension is marked critical need to ensure there weren't any |
| 58 | // qualifiers other than User Notice / CPS. |
| 59 | // |
| 60 | // This follows from RFC 5280 section 4.2.1.4: |
| 61 | // |
| 62 | // If this extension is critical, the path validation software MUST |
| 63 | // be able to interpret this extension (including the optional |
| 64 | // qualifier), or MUST reject the certificate. |
| 65 | std::vector<der::Input> unused_policies; |
| 66 | CertErrors unused_errors; |
| 67 | return ParseCertificatePoliciesExtensionOids( |
| 68 | extension.value, true /*fail_parsing_unknown_qualifier_oids*/, |
| 69 | &unused_policies, &unused_errors); |
| 70 |
| 71 | // TODO(eroman): Give a better error message. |
| 72 | } |
| 73 | if (extension.oid == der::Input(kPolicyMappingsOid)) { |
| 74 | return true; |
| 75 | } |
| 76 | if (extension.oid == der::Input(kPolicyConstraintsOid)) { |
| 77 | return true; |
| 78 | } |
| 79 | if (extension.oid == der::Input(kInhibitAnyPolicyOid)) { |
| 80 | return true; |
| 81 | } |
| 82 |
| 83 | return false; |
| 84 | } |
| 85 |
| 86 | // Adds errors to |errors| if the certificate contains unconsumed _critical_ |
| 87 | // extensions. |
| 88 | void VerifyNoUnconsumedCriticalExtensions(const ParsedCertificate &cert, |
| 89 | CertErrors *errors, |
| 90 | bool allow_precertificate, |
| 91 | KeyPurpose key_purpose) { |
| 92 | for (const auto &it : cert.extensions()) { |
| 93 | const ParsedExtension &extension = it.second; |
| 94 | if (extension.critical) { |
| 95 | if (key_purpose == KeyPurpose::RCS_MLS_CLIENT_AUTH) { |
| 96 | if (extension.oid == der::Input(kRcsMlsParticipantInformation) || |
| 97 | extension.oid == der::Input(kRcsMlsAcsParticipantInformation)) { |
| 98 | continue; |
| 99 | } |
| 100 | } |
| 101 | if (allow_precertificate && extension.oid == der::Input(kCtPoisonOid)) { |
| 102 | continue; |
| 103 | } |
| 104 | if (extension.oid == der::Input(kMSApplicationPoliciesOid) && |
| 105 | cert.has_extended_key_usage()) { |
| 106 | // Per https://crbug.com/1439638 and |
| 107 | // https://learn.microsoft.com/en-us/windows/win32/seccertenroll/supported-extensions#msapplicationpolicies |
| 108 | // The MSApplicationPolicies extension may be ignored if the |
| 109 | // extendedKeyUsage extension is also present. |
| 110 | continue; |
| 111 | } |
| 112 | if (!IsHandledCriticalExtension(extension)) { |
| 113 | errors->AddError(cert_errors::kUnconsumedCriticalExtension, |
| 114 | CreateCertErrorParams2Der("oid", extension.oid, |
| 115 | "value", extension.value)); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 |
| 121 | // Returns true if |cert| was self-issued. The definition of self-issuance |
| 122 | // comes from RFC 5280 section 6.1: |
| 123 | // |
| 124 | // A certificate is self-issued if the same DN appears in the subject |
| 125 | // and issuer fields (the two DNs are the same if they match according |
| 126 | // to the rules specified in Section 7.1). In general, the issuer and |
| 127 | // subject of the certificates that make up a path are different for |
| 128 | // each certificate. However, a CA may issue a certificate to itself to |
| 129 | // support key rollover or changes in certificate policies. These |
| 130 | // self-issued certificates are not counted when evaluating path length |
| 131 | // or name constraints. |
| 132 | [[nodiscard]] bool IsSelfIssued(const ParsedCertificate &cert) { |
| 133 | return cert.normalized_subject() == cert.normalized_issuer(); |
| 134 | } |
| 135 |
| 136 | // Adds errors to |errors| if |cert| is not valid at time |time|. |
| 137 | // |
| 138 | // The certificate's validity requirements are described by RFC 5280 section |
| 139 | // 4.1.2.5: |
| 140 | // |
| 141 | // The validity period for a certificate is the period of time from |
| 142 | // notBefore through notAfter, inclusive. |
| 143 | void VerifyTimeValidity(const ParsedCertificate &cert, |
| 144 | const der::GeneralizedTime &time, CertErrors *errors) { |
| 145 | if (time < cert.tbs().validity_not_before) { |
| 146 | errors->AddError(cert_errors::kValidityFailedNotBefore); |
| 147 | } |
| 148 |
| 149 | if (cert.tbs().validity_not_after < time) { |
| 150 | errors->AddError(cert_errors::kValidityFailedNotAfter); |
| 151 | } |
| 152 | } |
| 153 |
| 154 | // Adds errors to |errors| if |cert| has internally inconsistent signature |
| 155 | // algorithms. |
| 156 | // |
| 157 | // X.509 certificates contain two different signature algorithms: |
| 158 | // (1) The signatureAlgorithm field of Certificate |
| 159 | // (2) The signature field of TBSCertificate |
| 160 | // |
| 161 | // According to RFC 5280 section 4.1.1.2 and 4.1.2.3 these two fields must be |
| 162 | // equal: |
| 163 | // |
| 164 | // This field MUST contain the same algorithm identifier as the |
| 165 | // signature field in the sequence tbsCertificate (Section 4.1.2.3). |
| 166 | // |
| 167 | // The spec is not explicit about what "the same algorithm identifier" means. |
| 168 | // Our interpretation is that the two DER-encoded fields must be byte-for-byte |
| 169 | // identical. |
| 170 | // |
| 171 | // In practice however there are certificates which use different encodings for |
| 172 | // specifying RSA with SHA1 (different OIDs). This is special-cased for |
| 173 | // compatibility sake. |
| 174 | bool VerifySignatureAlgorithmsMatch(const ParsedCertificate &cert, |
| 175 | CertErrors *errors) { |
| 176 | der::Input alg1_tlv = cert.signature_algorithm_tlv(); |
| 177 | der::Input alg2_tlv = cert.tbs().signature_algorithm_tlv; |
| 178 |
| 179 | // Ensure that the two DER-encoded signature algorithms are byte-for-byte |
| 180 | // equal. |
| 181 | if (alg1_tlv == alg2_tlv) { |
| 182 | return true; |
| 183 | } |
| 184 |
| 185 | // But make a compatibility concession if alternate encodings are used |
| 186 | // TODO(eroman): Turn this warning into an error. |
| 187 | // TODO(eroman): Add a unit-test that exercises this case. |
| 188 | std::optional<SignatureAlgorithm> alg1 = ParseSignatureAlgorithm(alg1_tlv); |
| 189 | if (!alg1) { |
| 190 | errors->AddError(cert_errors::kUnacceptableSignatureAlgorithm); |
| 191 | return false; |
| 192 | } |
| 193 | std::optional<SignatureAlgorithm> alg2 = ParseSignatureAlgorithm(alg2_tlv); |
| 194 | if (!alg2) { |
| 195 | errors->AddError(cert_errors::kUnacceptableSignatureAlgorithm); |
| 196 | return false; |
| 197 | } |
| 198 |
| 199 | if (*alg1 == *alg2) { |
| 200 | errors->AddWarning( |
| 201 | cert_errors::kSignatureAlgorithmsDifferentEncoding, |
| 202 | CreateCertErrorParams2Der("Certificate.algorithm", alg1_tlv, |
| 203 | "TBSCertificate.signature", alg2_tlv)); |
| 204 | return true; |
| 205 | } |
| 206 |
| 207 | errors->AddError( |
| 208 | cert_errors::kSignatureAlgorithmMismatch, |
| 209 | CreateCertErrorParams2Der("Certificate.algorithm", alg1_tlv, |
| 210 | "TBSCertificate.signature", alg2_tlv)); |
| 211 | return false; |
| 212 | } |
| 213 |
| 214 | // Verify that |cert| can be used for |required_key_purpose|. |
| 215 | void VerifyExtendedKeyUsage(const ParsedCertificate &cert, |
| 216 | KeyPurpose required_key_purpose, CertErrors *errors, |
| 217 | bool is_target_cert, bool is_target_cert_issuer) { |
| 218 | // We treat a required KeyPurpose of ANY_EKU to mean "Do not check EKU" |
| 219 | if (required_key_purpose == KeyPurpose::ANY_EKU) { |
| 220 | return; |
| 221 | } |
| 222 | bool has_any_eku = false; |
| 223 | bool has_server_auth_eku = false; |
| 224 | bool has_client_auth_eku = false; |
| 225 | bool has_code_signing_eku = false; |
| 226 | bool has_time_stamping_eku = false; |
| 227 | bool has_ocsp_signing_eku = false; |
| 228 | bool has_rcs_mls_client_eku = false; |
| 229 | size_t eku_oid_count = 0; |
| 230 | if (cert.has_extended_key_usage()) { |
| 231 | for (const auto &key_purpose_oid : cert.extended_key_usage()) { |
| 232 | eku_oid_count++; |
| 233 | if (key_purpose_oid == der::Input(kAnyEKU)) { |
| 234 | has_any_eku = true; |
| 235 | } |
| 236 | if (key_purpose_oid == der::Input(kServerAuth)) { |
| 237 | has_server_auth_eku = true; |
| 238 | } |
| 239 | if (key_purpose_oid == der::Input(kClientAuth)) { |
| 240 | has_client_auth_eku = true; |
| 241 | } |
| 242 | if (key_purpose_oid == der::Input(kCodeSigning)) { |
| 243 | has_code_signing_eku = true; |
| 244 | } |
| 245 | if (key_purpose_oid == der::Input(kTimeStamping)) { |
| 246 | has_time_stamping_eku = true; |
| 247 | } |
| 248 | if (key_purpose_oid == der::Input(kOCSPSigning)) { |
| 249 | has_ocsp_signing_eku = true; |
| 250 | } |
| 251 | if (key_purpose_oid == der::Input(kRcsMlsClient)) { |
| 252 | has_rcs_mls_client_eku = true; |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 |
| 257 | if (required_key_purpose == KeyPurpose::RCS_MLS_CLIENT_AUTH) { |
| 258 | // Enforce the key usage restriction for a leaf from section A.3.8.3 here |
| 259 | // as well. |
| 260 | if (is_target_cert && |
| 261 | (!cert.has_key_usage() || |
| 262 | // This works to enforce that digital signature is the only bit because |
| 263 | // digital signature is bit 0. |
| 264 | !cert.key_usage().AssertsBit(KEY_USAGE_BIT_DIGITAL_SIGNATURE) || |
| 265 | cert.key_usage().bytes().size() != 1 || |
| 266 | cert.key_usage().unused_bits() != 7)) { |
| 267 | errors->AddError(cert_errors::kKeyUsageIncorrectForRcsMlsClient); |
| 268 | } |
| 269 | // Rules for MLS client auth. For the leaf and all intermediates, EKU must |
| 270 | // be present and have exactly one EKU which is rcsMlsClient. |
| 271 | if (!cert.has_extended_key_usage()) { |
| 272 | errors->AddError(cert_errors::kEkuNotPresent); |
| 273 | } else if (eku_oid_count != 1 || !has_rcs_mls_client_eku) { |
| 274 | errors->AddError(cert_errors::kEkuIncorrectForRcsMlsClient); |
| 275 | } |
| 276 | return; |
| 277 | } |
| 278 |
| 279 | // Rules TLS client and server authentication variants. |
| 280 |
| 281 | // Apply strict only to leaf certificates in these cases. |
| 282 | if (required_key_purpose == KeyPurpose::CLIENT_AUTH_STRICT_LEAF) { |
| 283 | if (!is_target_cert) { |
| 284 | required_key_purpose = KeyPurpose::CLIENT_AUTH; |
| 285 | } else { |
| 286 | required_key_purpose = KeyPurpose::CLIENT_AUTH_STRICT; |
| 287 | } |
| 288 | } |
| 289 |
| 290 | if (required_key_purpose == KeyPurpose::SERVER_AUTH_STRICT_LEAF) { |
| 291 | if (!is_target_cert) { |
| 292 | required_key_purpose = KeyPurpose::SERVER_AUTH; |
| 293 | } else { |
| 294 | required_key_purpose = KeyPurpose::SERVER_AUTH_STRICT; |
| 295 | } |
| 296 | } |
| 297 |
| 298 | auto add_error_if_strict = [&](CertErrorId id) { |
| 299 | if (required_key_purpose == KeyPurpose::SERVER_AUTH_STRICT || |
| 300 | required_key_purpose == KeyPurpose::CLIENT_AUTH_STRICT) { |
| 301 | errors->AddError(id); |
| 302 | } else { |
| 303 | errors->AddWarning(id); |
| 304 | } |
| 305 | }; |
| 306 | if (is_target_cert) { |
| 307 | // Loosely based upon CABF BR version 1.8.4, 7.1.2.3(f). We are more |
| 308 | // permissive in that we still allow EKU any to be present in a leaf |
| 309 | // certificate, but we ignore it for purposes of server or client auth. We |
| 310 | // are less permissive in that we prohibit Code Signing, OCSP Signing, and |
| 311 | // Time Stamping which are currently only a SHOULD NOT. The BR does |
| 312 | // explicitly allow Email authentication to be present, as this still exists |
| 313 | // in the wild (2022), so we do not prohibit Email authentication here (and |
| 314 | // by extension must allow it to be present in the signer, below). |
| 315 | if (!cert.has_extended_key_usage()) { |
| 316 | // This is added as a warning, an error will be added in STRICT modes |
| 317 | // if we then lack client or server auth due to this not being present. |
| 318 | errors->AddWarning(cert_errors::kEkuNotPresent); |
| 319 | } else { |
| 320 | if (has_code_signing_eku) { |
| 321 | add_error_if_strict(cert_errors::kEkuHasProhibitedCodeSigning); |
| 322 | } |
| 323 | if (has_ocsp_signing_eku) { |
| 324 | add_error_if_strict(cert_errors::kEkuHasProhibitedOCSPSigning); |
| 325 | } |
| 326 | if (has_time_stamping_eku) { |
| 327 | add_error_if_strict(cert_errors::kEkuHasProhibitedTimeStamping); |
| 328 | } |
| 329 | } |
| 330 | } else if (is_target_cert_issuer) { |
| 331 | // Handle the decision to overload EKU as a constraint on issuers. |
| 332 | // |
| 333 | // CABF BR version 1.8.4, 7.1.2.2(g) pertains to the case of "Certs used to |
| 334 | // issue TLS certificates", While the BR refers to the entire chain of |
| 335 | // intermediates, there are a number of exceptions regarding CA ownership |
| 336 | // and cross signing which are impossible for us to know or enforce here. |
| 337 | // Therefore, we can only enforce at the level of the intermediate that |
| 338 | // issued our target certificate. This means we we differ in the following |
| 339 | // ways: |
| 340 | // - We only enforce at the issuer of the TLS certificate. |
| 341 | // - We allow email protection to exist in the issuer, since without |
| 342 | // this it can not be allowed in the client (other than via EKU any)) |
| 343 | // - As in the leaf certificate case, we allow EKU any to be present, but |
| 344 | // we ignore it for the purposes of server or client auth. |
| 345 | // |
| 346 | // At this time (until at least 2023) some intermediates are lacking EKU in |
| 347 | // the world at large from common CA's, so we allow the noEKU case to permit |
| 348 | // everything. |
| 349 | // TODO(bbe): enforce requiring EKU in the issuer when we can manage it. |
| 350 | if (cert.has_extended_key_usage()) { |
| 351 | if (has_code_signing_eku) { |
| 352 | add_error_if_strict(cert_errors::kEkuHasProhibitedCodeSigning); |
| 353 | } |
| 354 | if (has_time_stamping_eku) { |
| 355 | add_error_if_strict(cert_errors::kEkuHasProhibitedTimeStamping); |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | // Otherwise, we are a parent of an issuer of a TLS certificate. The CABF |
| 360 | // BR version 1.8.4, 7.1.2.2(g) goes as far as permitting EKU any in certain |
| 361 | // cases of Cross Signing and CA Ownership, having permitted cases where EKU |
| 362 | // is permitted to not be present at all. These cases are not practical to |
| 363 | // differentiate here and therefore we don't attempt to enforce any further |
| 364 | // EKU "constraints" on such certificates. Unlike the above cases we also |
| 365 | // allow the use of EKU any for client or server auth constraint purposes. |
| 366 |
| 367 | switch (required_key_purpose) { |
| 368 | case KeyPurpose::ANY_EKU: |
| 369 | case KeyPurpose::CLIENT_AUTH_STRICT_LEAF: |
| 370 | case KeyPurpose::SERVER_AUTH_STRICT_LEAF: |
| 371 | case KeyPurpose::RCS_MLS_CLIENT_AUTH: |
| 372 | assert(0); // NOTREACHED |
| 373 | return; |
| 374 | case KeyPurpose::SERVER_AUTH: |
| 375 | case KeyPurpose::SERVER_AUTH_STRICT: { |
| 376 | if (has_any_eku && !has_server_auth_eku) { |
| 377 | if (is_target_cert || is_target_cert_issuer) { |
| 378 | errors->AddWarning(cert_errors::kEkuLacksServerAuthButHasAnyEKU); |
| 379 | } else { |
| 380 | // Accept anyEKU for server auth below target issuer. |
| 381 | has_server_auth_eku = true; |
| 382 | } |
| 383 | } |
| 384 | if (is_target_cert_issuer && !cert.has_extended_key_usage()) { |
| 385 | // Accept noEKU for server auth in target issuer. |
| 386 | // TODO(bbe): remove this once BR requirements catch up with CA's. |
| 387 | has_server_auth_eku = true; |
| 388 | } |
| 389 | if (required_key_purpose == KeyPurpose::SERVER_AUTH) { |
| 390 | // Legacy compatible. |
| 391 | if (cert.has_extended_key_usage() && !has_server_auth_eku && |
| 392 | !has_any_eku) { |
| 393 | errors->AddError(cert_errors::kEkuLacksServerAuth); |
| 394 | } |
| 395 | } else { |
| 396 | if (!has_server_auth_eku) { |
| 397 | errors->AddError(cert_errors::kEkuLacksServerAuth); |
| 398 | } |
| 399 | } |
| 400 | break; |
| 401 | } |
| 402 | case KeyPurpose::CLIENT_AUTH: |
| 403 | case KeyPurpose::CLIENT_AUTH_STRICT: { |
| 404 | if (has_any_eku && !has_client_auth_eku) { |
| 405 | if (is_target_cert || is_target_cert_issuer) { |
| 406 | errors->AddWarning(cert_errors::kEkuLacksClientAuthButHasAnyEKU); |
| 407 | } else { |
| 408 | // accept anyEKU for client auth. |
| 409 | has_client_auth_eku = true; |
| 410 | } |
| 411 | } |
| 412 | if (required_key_purpose == KeyPurpose::CLIENT_AUTH) { |
| 413 | // Legacy-compatible. |
| 414 | if (cert.has_extended_key_usage() && !has_client_auth_eku && |
| 415 | !has_any_eku) { |
| 416 | errors->AddError(cert_errors::kEkuLacksClientAuth); |
| 417 | } |
| 418 | } else { |
| 419 | if (!has_client_auth_eku) { |
| 420 | errors->AddError(cert_errors::kEkuLacksClientAuth); |
| 421 | } |
| 422 | } |
| 423 | break; |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 |
| 428 | // Representation of RFC 5280's "valid_policy_tree", used to keep track of the |
| 429 | // valid policies and policy re-mappings. This structure is defined in |
| 430 | // section 6.1.2. |
| 431 | // |
| 432 | // ValidPolicyGraph differs from RFC 5280's description in that: |
| 433 | // |
| 434 | // (1) It does not track "qualifier_set". This is not needed as it is not |
| 435 | // output by this implementation. |
| 436 | // |
| 437 | // (2) It builds a directed acyclic graph, rather than a tree. When a given |
| 438 | // policy matches multiple parents, RFC 5280 makes a separate node for |
| 439 | // each parent. This representation condenses them into one node with |
| 440 | // multiple parents. |
| 441 | // |
| 442 | // (3) It does not track "expected_policy_set" or anyPolicy nodes directly. |
| 443 | // Rather it maintains, only for the most recent level, whether there is an |
| 444 | // anyPolicy node and an inverted map of all "expected_policy_set" values. |
| 445 | // |
| 446 | // (4) Some pruning steps are deferred to when policies are evaluated, as a |
| 447 | // reachability pass. |
| 448 | class ValidPolicyGraph { |
| 449 | public: |
| 450 | ValidPolicyGraph() = default; |
| 451 |
| 452 | ValidPolicyGraph(const ValidPolicyGraph &) = delete; |
| 453 | ValidPolicyGraph &operator=(const ValidPolicyGraph &) = delete; |
| 454 |
| 455 | // A Node is an entry in the policy graph. It contains information about some |
| 456 | // policy asserted by a certificate in the chain. The policy OID itself is |
| 457 | // omitted because it is the key in the Level map. |
| 458 | struct Node { |
| 459 | // The list of "valid_policy" values for all nodes which are a parent of |
| 460 | // this node, other than anyPolicy. If empty, this node has a single parent, |
| 461 | // anyPolicy. |
| 462 | // |
| 463 | // Nodes whose parent is anyPolicy are root policies, and may be returned |
| 464 | // in the authorities-constrained-policy-set. Nodes with a concrete policy |
| 465 | // as a parent are derived from that policy in the issuer certificate, |
| 466 | // possibly with a policy mapping applied. |
| 467 | // |
| 468 | // Note it is not possible for a policy to have both anyPolicy and a |
| 469 | // concrete policy as a parent. Section 6.1.3, step d.1.ii only runs if |
| 470 | // there was no match in step d.1.i. |
| 471 | std::vector<der::Input> parent_policies; |
| 472 |
| 473 | // Whether this node matches a policy mapping in the certificate. If true, |
| 474 | // its "expected_policy_set" comes from the policy mappings extension. If |
| 475 | // false, its "expected_policy_set" is itself. |
| 476 | bool mapped = false; |
| 477 |
| 478 | // Whether this node is reachable from some valid policy in the end-entity |
| 479 | // certificate. Computed during GetValidRootPolicySet(). |
| 480 | bool reachable = false; |
| 481 | }; |
| 482 |
| 483 | // The policy graph is organized into "levels", each corresponding to a |
| 484 | // certificate in the chain. We maintain a map from "valid_policy" to the |
| 485 | // corresponding Node. This is the set of policies asserted by this |
| 486 | // certificate. The special anyPolicy OID is handled separately below. |
| 487 | using Level = std::map<der::Input, Node>; |
| 488 |
| 489 | // Additional per-level information that only needs to be maintained for the |
| 490 | // bottom-most level. |
| 491 | struct LevelDetails { |
| 492 | // Maintains the "expected_policy_set" values for nodes in a level of the |
| 493 | // graph, but the map is inverted from RFC 5280's formulation. For a given |
| 494 | // policy OID P, other than anyPolicy, this map gives the set of nodes where |
| 495 | // P appears in the node's "expected_policy_set". anyPolicy is handled |
| 496 | // separately below. |
| 497 | std::map<der::Input, std::vector<der::Input>> expected_policy_map; |
| 498 |
| 499 | // Whether there is a node at this level whose "valid_policy" is anyPolicy. |
| 500 | // |
| 501 | // Note anyPolicy's "expected_policy_set" always {anyPolicy}, and anyPolicy |
| 502 | // will never appear in the "expected_policy_set" of any other policy. That |
| 503 | // means this field also captures how anyPolicy appears in |
| 504 | // "expected_policy_set". |
| 505 | bool has_any_policy = false; |
| 506 | }; |
| 507 |
| 508 | // Initializes the ValidPolicyGraph. |
| 509 | void Init() { |
| 510 | SetNull(); |
| 511 | StartLevel(); |
| 512 | AddAnyPolicyNode(); |
| 513 | } |
| 514 |
| 515 | // In RFC 5280 valid_policy_tree may be set to null. That is represented here |
| 516 | // by emptiness. |
| 517 | bool IsNull() const { |
| 518 | return !current_level_.has_any_policy && |
| 519 | (levels_.empty() || levels_.back().empty()); |
| 520 | } |
| 521 | void SetNull() { |
| 522 | levels_.clear(); |
| 523 | current_level_ = LevelDetails{}; |
| 524 | } |
| 525 |
| 526 | // Completes the previous level, returning a corresponding LevelDetails |
| 527 | // structure, and starts a new level. |
| 528 | LevelDetails StartLevel() { |
| 529 | // Finish building expected_policy_map for the previous level. |
| 530 | if (!levels_.empty()) { |
| 531 | for (const auto &[policy, node] : levels_.back()) { |
| 532 | if (!node.mapped) { |
| 533 | current_level_.expected_policy_map[policy].push_back(policy); |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 |
| 538 | LevelDetails prev_level = std::move(current_level_); |
| 539 | levels_.emplace_back(); |
| 540 | current_level_ = LevelDetails{}; |
| 541 | return prev_level; |
| 542 | } |
| 543 |
| 544 | // Gets the set of policies (in terms of root authority's policy domain) that |
| 545 | // are valid at the bottom level of the policy graph, intersected with |
| 546 | // |user_initial_policy_set|. This is what X.509 calls |
| 547 | // "user-constrained-policy-set". |
| 548 | // |
| 549 | // This method may only be called once, after the policy graph is constructed. |
| 550 | std::set<der::Input> GetUserConstrainedPolicySet( |
| 551 | const std::set<der::Input> &user_initial_policy_set) { |
| 552 | if (levels_.empty()) { |
| 553 | return {}; |
| 554 | } |
| 555 |
| 556 | bool user_has_any_policy = |
| 557 | user_initial_policy_set.count(der::Input(kAnyPolicyOid)) != 0; |
| 558 | if (current_level_.has_any_policy) { |
| 559 | if (user_has_any_policy) { |
| 560 | return {der::Input(kAnyPolicyOid)}; |
| 561 | } |
| 562 | return user_initial_policy_set; |
| 563 | } |
| 564 |
| 565 | // The root's policy domain is determined by nodes with anyPolicy as a |
| 566 | // parent. However, we must limit to those which are reachable from the |
| 567 | // end-entity certificate because we defer some pruning steps. |
| 568 | for (auto &[policy, node] : levels_.back()) { |
| 569 | // GCC before 8.1 tracks individual unused bindings and does not support |
| 570 | // marking them [[maybe_unused]]. |
| 571 | (void)policy; |
| 572 | node.reachable = true; |
| 573 | } |
| 574 | std::set<der::Input> policy_set; |
| 575 | for (size_t i = levels_.size() - 1; i < levels_.size(); i--) { |
| 576 | for (auto &[policy, node] : levels_[i]) { |
| 577 | if (!node.reachable) { |
| 578 | continue; |
| 579 | } |
| 580 | if (node.parent_policies.empty()) { |
| 581 | // |node|'s parent is anyPolicy, so this is in the root policy domain. |
| 582 | // Add it to the set if it is also in user's list. |
| 583 | if (user_has_any_policy || |
| 584 | user_initial_policy_set.count(policy) > 0) { |
| 585 | policy_set.insert(policy); |
| 586 | } |
| 587 | } else if (i > 0) { |
| 588 | // Otherwise, continue searching the previous level. |
| 589 | for (der::Input parent : node.parent_policies) { |
| 590 | auto iter = levels_[i - 1].find(parent); |
| 591 | if (iter != levels_[i - 1].end()) { |
| 592 | iter->second.reachable = true; |
| 593 | } |
| 594 | } |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | return policy_set; |
| 599 | } |
| 600 |
| 601 | // Adds a node with policy anyPolicy to the current level. |
| 602 | void AddAnyPolicyNode() { |
| 603 | assert(!levels_.empty()); |
| 604 | current_level_.has_any_policy = true; |
| 605 | } |
| 606 |
| 607 | // Adds a node to the current level which is a child of |parent_policies| with |
| 608 | // the specified policy. |
| 609 | void AddNode(der::Input policy, std::vector<der::Input> parent_policies) { |
| 610 | assert(policy != der::Input(kAnyPolicyOid)); |
| 611 | AddNodeReturningIterator(policy, std::move(parent_policies)); |
| 612 | } |
| 613 |
| 614 | // Adds a node to the current level which is a child of anyPolicy with the |
| 615 | // specified policy. |
| 616 | void AddNodeWithParentAnyPolicy(der::Input policy) { |
| 617 | // An empty parent set represents a node parented by anyPolicy. |
| 618 | AddNode(policy, {}); |
| 619 | } |
| 620 |
| 621 | // Maps |issuer_policy| to |subject_policy|, as in RFC 5280, section 6.1.4, |
| 622 | // step b.1. |
| 623 | void AddPolicyMapping(der::Input issuer_policy, der::Input subject_policy) { |
| 624 | assert(issuer_policy != der::Input(kAnyPolicyOid)); |
| 625 | assert(subject_policy != der::Input(kAnyPolicyOid)); |
| 626 | if (levels_.empty()) { |
| 627 | return; |
| 628 | } |
| 629 |
| 630 | // The mapping only applies if |issuer_policy| exists in the current level. |
| 631 | auto issuer_policy_iter = levels_.back().find(issuer_policy); |
| 632 | if (issuer_policy_iter == levels_.back().end()) { |
| 633 | // If there is no match, it can instead match anyPolicy. |
| 634 | if (!current_level_.has_any_policy) { |
| 635 | return; |
| 636 | } |
| 637 |
| 638 | // From RFC 5280, section 6.1.4, step b.1: |
| 639 | // |
| 640 | // If no node of depth i in the valid_policy_tree has a |
| 641 | // valid_policy of ID-P but there is a node of depth i with a |
| 642 | // valid_policy of anyPolicy, then generate a child node of |
| 643 | // the node of depth i-1 that has a valid_policy of anyPolicy |
| 644 | // as follows: [...] |
| 645 | // |
| 646 | // The anyPolicy node of depth i-1 is referring to the parent of the |
| 647 | // anyPolicy node of depth i. The parent of anyPolicy is always anyPolicy. |
| 648 | issuer_policy_iter = AddNodeReturningIterator(issuer_policy, {}); |
| 649 | } |
| 650 |
| 651 | // Unmapped nodes have a singleton "expected_policy_set" containing their |
| 652 | // valid_policy. Track whether nodes have been mapped so this can be filled |
| 653 | // in at StartLevel(). |
| 654 | issuer_policy_iter->second.mapped = true; |
| 655 |
| 656 | // Add |subject_policy| to |issuer_policy|'s "expected_policy_set". |
| 657 | current_level_.expected_policy_map[subject_policy].push_back(issuer_policy); |
| 658 | } |
| 659 |
| 660 | // Removes the node with the specified policy from the current level. |
| 661 | void DeleteNode(der::Input policy) { |
| 662 | if (!levels_.empty()) { |
| 663 | levels_.back().erase(policy); |
| 664 | } |
| 665 | } |
| 666 |
| 667 | private: |
| 668 | Level::iterator AddNodeReturningIterator( |
| 669 | der::Input policy, std::vector<der::Input> parent_policies) { |
| 670 | assert(policy != der::Input(kAnyPolicyOid)); |
| 671 | auto [iter, inserted] = levels_.back().insert( |
| 672 | std::pair{policy, Node{std::move(parent_policies)}}); |
| 673 | // GCC before 8.1 tracks individual unused bindings and does not support |
| 674 | // marking them [[maybe_unused]]. |
| 675 | (void)inserted; |
| 676 | assert(inserted); |
| 677 | return iter; |
| 678 | } |
| 679 |
| 680 | // The list of levels, starting from the root. |
| 681 | std::vector<Level> levels_; |
| 682 | // Additional information about the current level. |
| 683 | LevelDetails current_level_; |
| 684 | }; |
| 685 |
| 686 | // Class that encapsulates the state variables used by certificate path |
| 687 | // validation. |
| 688 | class PathVerifier { |
| 689 | public: |
| 690 | // Same parameters and meaning as VerifyCertificateChain(). |
| 691 | void Run(const ParsedCertificateList &certs, |
| 692 | const CertificateTrust &last_cert_trust, |
| 693 | VerifyCertificateChainDelegate *delegate, |
| 694 | const der::GeneralizedTime &time, KeyPurpose required_key_purpose, |
| 695 | InitialExplicitPolicy initial_explicit_policy, |
| 696 | const std::set<der::Input> &user_initial_policy_set, |
| 697 | InitialPolicyMappingInhibit initial_policy_mapping_inhibit, |
| 698 | InitialAnyPolicyInhibit initial_any_policy_inhibit, |
| 699 | std::set<der::Input> *user_constrained_policy_set, |
| 700 | CertPathErrors *errors); |
| 701 |
| 702 | private: |
| 703 | // Verifies and updates the valid policies. This corresponds with RFC 5280 |
| 704 | // section 6.1.3 steps d-f. |
| 705 | void VerifyPolicies(const ParsedCertificate &cert, bool is_target_cert, |
| 706 | CertErrors *errors); |
| 707 |
| 708 | // Applies the policy mappings. This corresponds with RFC 5280 section 6.1.4 |
| 709 | // steps a-b. |
| 710 | void VerifyPolicyMappings(const ParsedCertificate &cert, CertErrors *errors); |
| 711 |
| 712 | // Applies policyConstraints and inhibitAnyPolicy. This corresponds with RFC |
| 713 | // 5280 section 6.1.4 steps i-j. |
| 714 | void ApplyPolicyConstraints(const ParsedCertificate &cert); |
| 715 |
| 716 | // This function corresponds to RFC 5280 section 6.1.3's "Basic Certificate |
| 717 | // Processing" procedure. |
| 718 | void BasicCertificateProcessing(const ParsedCertificate &cert, |
| 719 | bool is_target_cert, |
| 720 | bool is_target_cert_issuer, |
| 721 | const der::GeneralizedTime &time, |
| 722 | KeyPurpose required_key_purpose, |
| 723 | CertErrors *errors, |
| 724 | bool *shortcircuit_chain_validation); |
| 725 |
| 726 | // This function corresponds to RFC 5280 section 6.1.4's "Preparation for |
| 727 | // Certificate i+1" procedure. |cert| is expected to be an intermediate. |
| 728 | void PrepareForNextCertificate(const ParsedCertificate &cert, |
| 729 | KeyPurpose key_purpose, CertErrors *errors); |
| 730 |
| 731 | // This function corresponds with RFC 5280 section 6.1.5's "Wrap-Up |
| 732 | // Procedure". It does processing for the final certificate (the target cert). |
| 733 | void WrapUp(const ParsedCertificate &cert, KeyPurpose required_key_purpose, |
| 734 | const std::set<der::Input> &user_initial_policy_set, |
| 735 | bool allow_precertificate, CertErrors *errors); |
| 736 |
| 737 | // Enforces trust anchor constraints compatibile with RFC 5937. |
| 738 | // |
| 739 | // Note that the anchor constraints are encoded via the attached certificate |
| 740 | // itself. |
| 741 | void ApplyTrustAnchorConstraints(const ParsedCertificate &cert, |
| 742 | KeyPurpose required_key_purpose, |
| 743 | CertErrors *errors); |
| 744 |
| 745 | // Initializes the path validation algorithm given anchor constraints. This |
| 746 | // follows the description in RFC 5937 |
| 747 | void ProcessRootCertificate(const ParsedCertificate &cert, |
| 748 | const CertificateTrust &trust, |
| 749 | const der::GeneralizedTime &time, |
| 750 | KeyPurpose required_key_purpose, |
| 751 | CertErrors *errors, |
| 752 | bool *shortcircuit_chain_validation); |
| 753 |
| 754 | // Processes verification when the input is a single certificate. This is not |
| 755 | // defined by any standard. We attempt to match the de-facto behaviour of |
| 756 | // Operating System verifiers. |
| 757 | void ProcessSingleCertChain(const ParsedCertificate &cert, |
| 758 | const CertificateTrust &trust, |
| 759 | const der::GeneralizedTime &time, |
| 760 | KeyPurpose required_key_purpose, |
| 761 | CertErrors *errors); |
| 762 |
| 763 | // Parses |spki| to an EVP_PKEY and checks whether the public key is accepted |
| 764 | // by |delegate_|. On failure parsing returns nullptr. If either parsing the |
| 765 | // key or key policy failed, adds a high-severity error to |errors|. |
| 766 | bssl::UniquePtr<EVP_PKEY> ParseAndCheckPublicKey(der::Input spki, |
| 767 | CertErrors *errors); |
| 768 |
| 769 | ValidPolicyGraph valid_policy_graph_; |
| 770 |
| 771 | std::set<der::Input> user_constrained_policy_set_; |
| 772 |
| 773 | // Will contain a NameConstraints for each previous cert in the chain which |
| 774 | // had nameConstraints. This corresponds to the permitted_subtrees and |
| 775 | // excluded_subtrees state variables from RFC 5280. |
| 776 | std::vector<const NameConstraints *> name_constraints_list_; |
| 777 |
| 778 | // |explicit_policy_| corresponds with the same named variable from RFC 5280 |
| 779 | // section 6.1.2: |
| 780 | // |
| 781 | // explicit_policy: an integer that indicates if a non-NULL |
| 782 | // valid_policy_tree is required. The integer indicates the |
| 783 | // number of non-self-issued certificates to be processed before |
| 784 | // this requirement is imposed. Once set, this variable may be |
| 785 | // decreased, but may not be increased. That is, if a certificate in the |
| 786 | // path requires a non-NULL valid_policy_tree, a later certificate cannot |
| 787 | // remove this requirement. If initial-explicit-policy is set, then the |
| 788 | // initial value is 0, otherwise the initial value is n+1. |
| 789 | size_t explicit_policy_; |
| 790 |
| 791 | // |inhibit_any_policy_| corresponds with the same named variable from RFC |
| 792 | // 5280 section 6.1.2: |
| 793 | // |
| 794 | // inhibit_anyPolicy: an integer that indicates whether the |
| 795 | // anyPolicy policy identifier is considered a match. The |
| 796 | // integer indicates the number of non-self-issued certificates |
| 797 | // to be processed before the anyPolicy OID, if asserted in a |
| 798 | // certificate other than an intermediate self-issued |
| 799 | // certificate, is ignored. Once set, this variable may be |
| 800 | // decreased, but may not be increased. That is, if a |
| 801 | // certificate in the path inhibits processing of anyPolicy, a |
| 802 | // later certificate cannot permit it. If initial-any-policy- |
| 803 | // inhibit is set, then the initial value is 0, otherwise the |
| 804 | // initial value is n+1. |
| 805 | size_t inhibit_any_policy_; |
| 806 |
| 807 | // |policy_mapping_| corresponds with the same named variable from RFC 5280 |
| 808 | // section 6.1.2: |
| 809 | // |
| 810 | // policy_mapping: an integer that indicates if policy mapping |
| 811 | // is permitted. The integer indicates the number of non-self- |
| 812 | // issued certificates to be processed before policy mapping is |
| 813 | // inhibited. Once set, this variable may be decreased, but may |
| 814 | // not be increased. That is, if a certificate in the path |
| 815 | // specifies that policy mapping is not permitted, it cannot be |
| 816 | // overridden by a later certificate. If initial-policy- |
| 817 | // mapping-inhibit is set, then the initial value is 0, |
| 818 | // otherwise the initial value is n+1. |
| 819 | size_t policy_mapping_; |
| 820 |
| 821 | // |working_public_key_| is an amalgamation of 3 separate variables from RFC |
| 822 | // 5280: |
| 823 | // * working_public_key |
| 824 | // * working_public_key_algorithm |
| 825 | // * working_public_key_parameters |
| 826 | // |
| 827 | // They are combined for simplicity since the signature verification takes an |
| 828 | // EVP_PKEY, and the parameter inheritence is not applicable for the supported |
| 829 | // key types. |working_public_key_| may be null if parsing failed. |
| 830 | // |
| 831 | // An approximate explanation of |working_public_key_| is this description |
| 832 | // from RFC 5280 section 6.1.2: |
| 833 | // |
| 834 | // working_public_key: the public key used to verify the |
| 835 | // signature of a certificate. |
| 836 | bssl::UniquePtr<EVP_PKEY> working_public_key_; |
| 837 |
| 838 | // |working_normalized_issuer_name_| is the normalized value of the |
| 839 | // working_issuer_name variable in RFC 5280 section 6.1.2: |
| 840 | // |
| 841 | // working_issuer_name: the issuer distinguished name expected |
| 842 | // in the next certificate in the chain. |
| 843 | der::Input working_normalized_issuer_name_; |
| 844 |
| 845 | // |max_path_length_| corresponds with the same named variable in RFC 5280 |
| 846 | // section 6.1.2. |
| 847 | // |
| 848 | // max_path_length: this integer is initialized to n, is |
| 849 | // decremented for each non-self-issued certificate in the path, |
| 850 | // and may be reduced to the value in the path length constraint |
| 851 | // field within the basic constraints extension of a CA |
| 852 | // certificate. |
| 853 | size_t max_path_length_; |
| 854 |
| 855 | VerifyCertificateChainDelegate *delegate_; |
| 856 | }; |
| 857 |
| 858 | void PathVerifier::VerifyPolicies(const ParsedCertificate &cert, |
| 859 | bool is_target_cert, CertErrors *errors) { |
| 860 | // From RFC 5280 section 6.1.3: |
| 861 | // |
| 862 | // (d) If the certificate policies extension is present in the |
| 863 | // certificate and the valid_policy_tree is not NULL, process |
| 864 | // the policy information by performing the following steps in |
| 865 | // order: |
| 866 | if (cert.has_policy_oids() && !valid_policy_graph_.IsNull()) { |
| 867 | ValidPolicyGraph::LevelDetails previous_level = |
| 868 | valid_policy_graph_.StartLevel(); |
| 869 |
| 870 | // (1) For each policy P not equal to anyPolicy in the |
| 871 | // certificate policies extension, let P-OID denote the OID |
| 872 | // for policy P and P-Q denote the qualifier set for policy |
| 873 | // P. Perform the following steps in order: |
| 874 | bool cert_has_any_policy = false; |
| 875 | for (der::Input p_oid : cert.policy_oids()) { |
| 876 | if (p_oid == der::Input(kAnyPolicyOid)) { |
| 877 | cert_has_any_policy = true; |
| 878 | continue; |
| 879 | } |
| 880 |
| 881 | // (i) For each node of depth i-1 in the valid_policy_tree |
| 882 | // where P-OID is in the expected_policy_set, create a |
| 883 | // child node as follows: set the valid_policy to P-OID, |
| 884 | // set the qualifier_set to P-Q, and set the |
| 885 | // expected_policy_set to {P-OID}. |
| 886 | auto iter = previous_level.expected_policy_map.find(p_oid); |
| 887 | if (iter != previous_level.expected_policy_map.end()) { |
| 888 | valid_policy_graph_.AddNode( |
| 889 | p_oid, /*parent_policies=*/std::move(iter->second)); |
| 890 | previous_level.expected_policy_map.erase(iter); |
| 891 | } else if (previous_level.has_any_policy) { |
| 892 | // (ii) If there was no match in step (i) and the |
| 893 | // valid_policy_tree includes a node of depth i-1 with |
| 894 | // the valid_policy anyPolicy, generate a child node with |
| 895 | // the following values: set the valid_policy to P-OID, |
| 896 | // set the qualifier_set to P-Q, and set the |
| 897 | // expected_policy_set to {P-OID}. |
| 898 | valid_policy_graph_.AddNodeWithParentAnyPolicy(p_oid); |
| 899 | } |
| 900 | } |
| 901 |
| 902 | // (2) If the certificate policies extension includes the policy |
| 903 | // anyPolicy with the qualifier set AP-Q and either (a) |
| 904 | // inhibit_anyPolicy is greater than 0 or (b) i<n and the |
| 905 | // certificate is self-issued, then: |
| 906 | // |
| 907 | // For each node in the valid_policy_tree of depth i-1, for |
| 908 | // each value in the expected_policy_set (including |
| 909 | // anyPolicy) that does not appear in a child node, create a |
| 910 | // child node with the following values: set the valid_policy |
| 911 | // to the value from the expected_policy_set in the parent |
| 912 | // node, set the qualifier_set to AP-Q, and set the |
| 913 | // expected_policy_set to the value in the valid_policy from |
| 914 | // this node. |
| 915 | if (cert_has_any_policy && ((inhibit_any_policy_ > 0) || |
| 916 | (!is_target_cert && IsSelfIssued(cert)))) { |
| 917 | for (auto &[p_oid, parent_policies] : |
| 918 | previous_level.expected_policy_map) { |
| 919 | valid_policy_graph_.AddNode(p_oid, std::move(parent_policies)); |
| 920 | } |
| 921 | if (previous_level.has_any_policy) { |
| 922 | valid_policy_graph_.AddAnyPolicyNode(); |
| 923 | } |
| 924 | } |
| 925 |
| 926 | // (3) If there is a node in the valid_policy_tree of depth i-1 |
| 927 | // or less without any child nodes, delete that node. Repeat |
| 928 | // this step until there are no nodes of depth i-1 or less |
| 929 | // without children. |
| 930 | // |
| 931 | // This implementation does this as part of GetUserConstrainedPolicySet(). |
| 932 | // Only the current level needs to be pruned to compute the policy graph. |
| 933 | } |
| 934 |
| 935 | // (e) If the certificate policies extension is not present, set the |
| 936 | // valid_policy_tree to NULL. |
| 937 | if (!cert.has_policy_oids()) { |
| 938 | valid_policy_graph_.SetNull(); |
| 939 | } |
| 940 |
| 941 | // (f) Verify that either explicit_policy is greater than 0 or the |
| 942 | // valid_policy_tree is not equal to NULL; |
| 943 | if (!((explicit_policy_ > 0) || !valid_policy_graph_.IsNull())) { |
| 944 | errors->AddError(cert_errors::kNoValidPolicy); |
| 945 | } |
| 946 | } |
| 947 |
| 948 | void PathVerifier::VerifyPolicyMappings(const ParsedCertificate &cert, |
| 949 | CertErrors *errors) { |
| 950 | if (!cert.has_policy_mappings()) { |
| 951 | return; |
| 952 | } |
| 953 |
| 954 | // From RFC 5280 section 6.1.4: |
| 955 | // |
| 956 | // (a) If a policy mappings extension is present, verify that the |
| 957 | // special value anyPolicy does not appear as an |
| 958 | // issuerDomainPolicy or a subjectDomainPolicy. |
| 959 | for (const ParsedPolicyMapping &mapping : cert.policy_mappings()) { |
| 960 | if (mapping.issuer_domain_policy == der::Input(kAnyPolicyOid) || |
| 961 | mapping.subject_domain_policy == der::Input(kAnyPolicyOid)) { |
| 962 | // Because this implementation continues processing certificates after |
| 963 | // this error, clear the valid policy graph to ensure the |
| 964 | // "user_constrained_policy_set" output upon failure is empty. |
| 965 | valid_policy_graph_.SetNull(); |
| 966 | errors->AddError(cert_errors::kPolicyMappingAnyPolicy); |
| 967 | return; |
| 968 | } |
| 969 | } |
| 970 |
| 971 | // (b) If a policy mappings extension is present, then for each |
| 972 | // issuerDomainPolicy ID-P in the policy mappings extension: |
| 973 | // |
| 974 | // (1) If the policy_mapping variable is greater than 0, for each |
| 975 | // node in the valid_policy_tree of depth i where ID-P is the |
| 976 | // valid_policy, set expected_policy_set to the set of |
| 977 | // subjectDomainPolicy values that are specified as |
| 978 | // equivalent to ID-P by the policy mappings extension. |
| 979 | // |
| 980 | // If no node of depth i in the valid_policy_tree has a |
| 981 | // valid_policy of ID-P but there is a node of depth i with a |
| 982 | // valid_policy of anyPolicy, then generate a child node of |
| 983 | // the node of depth i-1 that has a valid_policy of anyPolicy |
| 984 | // as follows: |
| 985 | // |
| 986 | // (i) set the valid_policy to ID-P; |
| 987 | // |
| 988 | // (ii) set the qualifier_set to the qualifier set of the |
| 989 | // policy anyPolicy in the certificate policies |
| 990 | // extension of certificate i; and |
| 991 | // |
| 992 | // (iii) set the expected_policy_set to the set of |
| 993 | // subjectDomainPolicy values that are specified as |
| 994 | // equivalent to ID-P by the policy mappings extension. |
| 995 | // |
| 996 | if (policy_mapping_ > 0) { |
| 997 | for (const ParsedPolicyMapping &mapping : cert.policy_mappings()) { |
| 998 | valid_policy_graph_.AddPolicyMapping(mapping.issuer_domain_policy, |
| 999 | mapping.subject_domain_policy); |
| 1000 | } |
| 1001 | } |
| 1002 |
| 1003 | // (b) If a policy mappings extension is present, then for each |
| 1004 | // issuerDomainPolicy ID-P in the policy mappings extension: |
| 1005 | // |
| 1006 | // ... |
| 1007 | // |
| 1008 | // (2) If the policy_mapping variable is equal to 0: |
| 1009 | // |
| 1010 | // (i) delete each node of depth i in the valid_policy_tree |
| 1011 | // where ID-P is the valid_policy. |
| 1012 | // |
| 1013 | // (ii) If there is a node in the valid_policy_tree of depth |
| 1014 | // i-1 or less without any child nodes, delete that |
| 1015 | // node. Repeat this step until there are no nodes of |
| 1016 | // depth i-1 or less without children. |
| 1017 | // |
| 1018 | // Step (ii) is deferred to part of GetUserConstrainedPolicySet(). |
| 1019 | if (policy_mapping_ == 0) { |
| 1020 | for (const ParsedPolicyMapping &mapping : cert.policy_mappings()) { |
| 1021 | valid_policy_graph_.DeleteNode(mapping.issuer_domain_policy); |
| 1022 | } |
| 1023 | } |
| 1024 | } |
| 1025 |
| 1026 | void PathVerifier::ApplyPolicyConstraints(const ParsedCertificate &cert) { |
| 1027 | // RFC 5280 section 6.1.4 step i-j: |
| 1028 | // (i) If a policy constraints extension is included in the |
| 1029 | // certificate, modify the explicit_policy and policy_mapping |
| 1030 | // state variables as follows: |
| 1031 | if (cert.has_policy_constraints()) { |
| 1032 | // (1) If requireExplicitPolicy is present and is less than |
| 1033 | // explicit_policy, set explicit_policy to the value of |
| 1034 | // requireExplicitPolicy. |
| 1035 | if (cert.policy_constraints().require_explicit_policy && |
| 1036 | cert.policy_constraints().require_explicit_policy.value() < |
| 1037 | explicit_policy_) { |
| 1038 | explicit_policy_ = |
| 1039 | cert.policy_constraints().require_explicit_policy.value(); |
| 1040 | } |
| 1041 |
| 1042 | // (2) If inhibitPolicyMapping is present and is less than |
| 1043 | // policy_mapping, set policy_mapping to the value of |
| 1044 | // inhibitPolicyMapping. |
| 1045 | if (cert.policy_constraints().inhibit_policy_mapping && |
| 1046 | cert.policy_constraints().inhibit_policy_mapping.value() < |
| 1047 | policy_mapping_) { |
| 1048 | policy_mapping_ = |
| 1049 | cert.policy_constraints().inhibit_policy_mapping.value(); |
| 1050 | } |
| 1051 | } |
| 1052 |
| 1053 | // (j) If the inhibitAnyPolicy extension is included in the |
| 1054 | // certificate and is less than inhibit_anyPolicy, set |
| 1055 | // inhibit_anyPolicy to the value of inhibitAnyPolicy. |
| 1056 | if (cert.inhibit_any_policy() && |
| 1057 | cert.inhibit_any_policy().value() < inhibit_any_policy_) { |
| 1058 | inhibit_any_policy_ = cert.inhibit_any_policy().value(); |
| 1059 | } |
| 1060 | } |
| 1061 |
| 1062 | void PathVerifier::BasicCertificateProcessing( |
| 1063 | const ParsedCertificate &cert, bool is_target_cert, |
| 1064 | bool is_target_cert_issuer, const der::GeneralizedTime &time, |
| 1065 | KeyPurpose required_key_purpose, CertErrors *errors, |
| 1066 | bool *shortcircuit_chain_validation) { |
| 1067 | *shortcircuit_chain_validation = false; |
| 1068 | // Check that the signature algorithms in Certificate vs TBSCertificate |
| 1069 | // match. This isn't part of RFC 5280 section 6.1.3, but is mandated by |
| 1070 | // sections 4.1.1.2 and 4.1.2.3. |
| 1071 | if (!VerifySignatureAlgorithmsMatch(cert, errors)) { |
| 1072 | BSSL_CHECK(errors->ContainsAnyErrorWithSeverity(CertError::SEVERITY_HIGH)); |
| 1073 | *shortcircuit_chain_validation = true; |
| 1074 | } |
| 1075 |
| 1076 | // Check whether this signature algorithm is allowed. |
| 1077 | if (!cert.signature_algorithm().has_value() || |
| 1078 | !delegate_->IsSignatureAlgorithmAcceptable(*cert.signature_algorithm(), |
| 1079 | errors)) { |
| 1080 | *shortcircuit_chain_validation = true; |
| 1081 | errors->AddError(cert_errors::kUnacceptableSignatureAlgorithm); |
| 1082 | return; |
| 1083 | } |
| 1084 |
| 1085 | if (working_public_key_) { |
| 1086 | // Verify the digital signature using the previous certificate's key (RFC |
| 1087 | // 5280 section 6.1.3 step a.1). |
| 1088 | if (!VerifySignedData(*cert.signature_algorithm(), |
| 1089 | cert.tbs_certificate_tlv(), cert.signature_value(), |
| 1090 | working_public_key_.get(), |
| 1091 | delegate_->GetVerifyCache())) { |
| 1092 | *shortcircuit_chain_validation = true; |
| 1093 | errors->AddError(cert_errors::kVerifySignedDataFailed); |
| 1094 | } |
| 1095 | } else { |
| 1096 | // If `working_public_key_` is null, that indicates the SPKI of the issuer |
| 1097 | // could not be parsed. Handle this the same way as an invalid signature by |
| 1098 | // shortcircuiting the rest of verification. |
| 1099 | // An error should already have been added by ParseAndCheckPublicKey, but |
| 1100 | // it's added on the CertErrors for the issuer, so we can't BSSL_CHECK |
| 1101 | // errors->ContainsAnyErrorWithSeverity here. (It will be BSSL_CHECKed when |
| 1102 | // the shortcircuit_chain_validation is acted on in PathVerifier::Run.) |
| 1103 | *shortcircuit_chain_validation = true; |
| 1104 | } |
| 1105 | if (*shortcircuit_chain_validation) { |
| 1106 | return; |
| 1107 | } |
| 1108 |
| 1109 | // Check the time range for the certificate's validity, ensuring it is valid |
| 1110 | // at |time|. |
| 1111 | // (RFC 5280 section 6.1.3 step a.2) |
| 1112 | VerifyTimeValidity(cert, time, errors); |
| 1113 |
| 1114 | // RFC 5280 section 6.1.3 step a.3 calls for checking the certificate's |
| 1115 | // revocation status here. In this implementation revocation checking is |
| 1116 | // implemented separately from path validation. |
| 1117 |
| 1118 | // Verify the certificate's issuer name matches the issuing certificate's |
| 1119 | // subject name. (RFC 5280 section 6.1.3 step a.4) |
| 1120 | if (cert.normalized_issuer() != working_normalized_issuer_name_) { |
| 1121 | errors->AddError(cert_errors::kSubjectDoesNotMatchIssuer); |
| 1122 | } |
| 1123 |
| 1124 | // Name constraints (RFC 5280 section 6.1.3 step b & c) |
| 1125 | // If certificate i is self-issued and it is not the final certificate in the |
| 1126 | // path, skip this step for certificate i. |
| 1127 | if (!name_constraints_list_.empty() && |
| 1128 | (!IsSelfIssued(cert) || is_target_cert)) { |
| 1129 | for (const NameConstraints *nc : name_constraints_list_) { |
| 1130 | nc->IsPermittedCert(cert.normalized_subject(), cert.subject_alt_names(), |
| 1131 | errors); |
| 1132 | } |
| 1133 | } |
| 1134 |
| 1135 | // RFC 5280 section 6.1.3 step d - f. |
| 1136 | VerifyPolicies(cert, is_target_cert, errors); |
| 1137 |
| 1138 | // The key purpose is checked not just for the end-entity certificate, but |
| 1139 | // also interpreted as a constraint when it appears in intermediates. This |
| 1140 | // goes beyond what RFC 5280 describes, but is the de-facto standard. See |
| 1141 | // https://wiki.mozilla.org/CA/CertificatePolicyV2.1#Frequently_Asked_Questions |
| 1142 | VerifyExtendedKeyUsage(cert, required_key_purpose, errors, is_target_cert, |
| 1143 | is_target_cert_issuer); |
| 1144 | } |
| 1145 |
| 1146 | void PathVerifier::PrepareForNextCertificate(const ParsedCertificate &cert, |
| 1147 | KeyPurpose key_purpose, |
| 1148 | CertErrors *errors) { |
| 1149 | // RFC 5280 section 6.1.4 step a-b |
| 1150 | VerifyPolicyMappings(cert, errors); |
| 1151 |
| 1152 | // From RFC 5280 section 6.1.4 step c: |
| 1153 | // |
| 1154 | // Assign the certificate subject name to working_normalized_issuer_name. |
| 1155 | working_normalized_issuer_name_ = cert.normalized_subject(); |
| 1156 |
| 1157 | // From RFC 5280 section 6.1.4 step d: |
| 1158 | // |
| 1159 | // Assign the certificate subjectPublicKey to working_public_key. |
| 1160 | working_public_key_ = ParseAndCheckPublicKey(cert.tbs().spki_tlv, errors); |
| 1161 |
| 1162 | // Note that steps e and f are omitted as they are handled by |
| 1163 | // the assignment to |working_spki| above. See the definition |
| 1164 | // of |working_spki|. |
| 1165 |
| 1166 | // From RFC 5280 section 6.1.4 step g: |
| 1167 | if (cert.has_name_constraints()) { |
| 1168 | name_constraints_list_.push_back(&cert.name_constraints()); |
| 1169 | } |
| 1170 |
| 1171 | // (h) If certificate i is not self-issued: |
| 1172 | if (!IsSelfIssued(cert)) { |
| 1173 | // (1) If explicit_policy is not 0, decrement explicit_policy by |
| 1174 | // 1. |
| 1175 | if (explicit_policy_ > 0) { |
| 1176 | explicit_policy_ -= 1; |
| 1177 | } |
| 1178 |
| 1179 | // (2) If policy_mapping is not 0, decrement policy_mapping by 1. |
| 1180 | if (policy_mapping_ > 0) { |
| 1181 | policy_mapping_ -= 1; |
| 1182 | } |
| 1183 |
| 1184 | // (3) If inhibit_anyPolicy is not 0, decrement inhibit_anyPolicy |
| 1185 | // by 1. |
| 1186 | if (inhibit_any_policy_ > 0) { |
| 1187 | inhibit_any_policy_ -= 1; |
| 1188 | } |
| 1189 | } |
| 1190 |
| 1191 | // RFC 5280 section 6.1.4 step i-j: |
| 1192 | ApplyPolicyConstraints(cert); |
| 1193 |
| 1194 | // From RFC 5280 section 6.1.4 step k: |
| 1195 | // |
| 1196 | // If certificate i is a version 3 certificate, verify that the |
| 1197 | // basicConstraints extension is present and that cA is set to |
| 1198 | // TRUE. (If certificate i is a version 1 or version 2 |
| 1199 | // certificate, then the application MUST either verify that |
| 1200 | // certificate i is a CA certificate through out-of-band means |
| 1201 | // or reject the certificate. Conforming implementations may |
| 1202 | // choose to reject all version 1 and version 2 intermediate |
| 1203 | // certificates.) |
| 1204 | // |
| 1205 | // This code implicitly rejects non version 3 intermediates, since they |
| 1206 | // can't contain a BasicConstraints extension. |
| 1207 | if (!cert.has_basic_constraints()) { |
| 1208 | errors->AddError(cert_errors::kMissingBasicConstraints); |
| 1209 | } else if (!cert.basic_constraints().is_ca) { |
| 1210 | errors->AddError(cert_errors::kBasicConstraintsIndicatesNotCa); |
| 1211 | } |
| 1212 |
| 1213 | // From RFC 5280 section 6.1.4 step l: |
| 1214 | // |
| 1215 | // If the certificate was not self-issued, verify that |
| 1216 | // max_path_length is greater than zero and decrement |
| 1217 | // max_path_length by 1. |
| 1218 | if (!IsSelfIssued(cert)) { |
| 1219 | if (max_path_length_ == 0) { |
| 1220 | errors->AddError(cert_errors::kMaxPathLengthViolated); |
| 1221 | } else { |
| 1222 | --max_path_length_; |
| 1223 | } |
| 1224 | } |
| 1225 |
| 1226 | // From RFC 5280 section 6.1.4 step m: |
| 1227 | // |
| 1228 | // If pathLenConstraint is present in the certificate and is |
| 1229 | // less than max_path_length, set max_path_length to the value |
| 1230 | // of pathLenConstraint. |
| 1231 | if (cert.has_basic_constraints() && cert.basic_constraints().has_path_len && |
| 1232 | cert.basic_constraints().path_len < max_path_length_) { |
| 1233 | max_path_length_ = cert.basic_constraints().path_len; |
| 1234 | } |
| 1235 |
| 1236 | // From RFC 5280 section 6.1.4 step n: |
| 1237 | // |
| 1238 | // If a key usage extension is present, verify that the |
| 1239 | // keyCertSign bit is set. |
| 1240 | if (cert.has_key_usage() && |
| 1241 | !cert.key_usage().AssertsBit(KEY_USAGE_BIT_KEY_CERT_SIGN)) { |
| 1242 | errors->AddError(cert_errors::kKeyCertSignBitNotSet); |
| 1243 | } |
| 1244 |
| 1245 | // From RFC 5280 section 6.1.4 step o: |
| 1246 | // |
| 1247 | // Recognize and process any other critical extension present in |
| 1248 | // the certificate. Process any other recognized non-critical |
| 1249 | // extension present in the certificate that is relevant to path |
| 1250 | // processing. |
| 1251 | VerifyNoUnconsumedCriticalExtensions( |
| 1252 | cert, errors, delegate_->AcceptPreCertificates(), key_purpose); |
| 1253 | } |
| 1254 |
| 1255 | // Checks if the target certificate has the CA bit set. If it does, add |
| 1256 | // the appropriate error or warning to |errors|. |
| 1257 | void VerifyTargetCertIsNotCA(const ParsedCertificate &cert, |
| 1258 | KeyPurpose required_key_purpose, |
| 1259 | CertErrors *errors) { |
| 1260 | if (cert.has_basic_constraints() && cert.basic_constraints().is_ca) { |
| 1261 | // In spite of RFC 5280 4.2.1.9 which says the CA properties MAY exist in |
| 1262 | // an end entity certificate, the CABF Baseline Requirements version |
| 1263 | // 1.8.4, 7.1.2.3(d) prohibit the CA bit being set in an end entity |
| 1264 | // certificate. |
| 1265 | switch (required_key_purpose) { |
| 1266 | case KeyPurpose::ANY_EKU: |
| 1267 | break; |
| 1268 | case KeyPurpose::SERVER_AUTH: |
| 1269 | case KeyPurpose::CLIENT_AUTH: |
| 1270 | errors->AddWarning(cert_errors::kTargetCertShouldNotBeCa); |
| 1271 | break; |
| 1272 | case KeyPurpose::SERVER_AUTH_STRICT: |
| 1273 | case KeyPurpose::CLIENT_AUTH_STRICT: |
| 1274 | case KeyPurpose::CLIENT_AUTH_STRICT_LEAF: |
| 1275 | case KeyPurpose::SERVER_AUTH_STRICT_LEAF: |
| 1276 | case KeyPurpose::RCS_MLS_CLIENT_AUTH: |
| 1277 | errors->AddError(cert_errors::kTargetCertShouldNotBeCa); |
| 1278 | break; |
| 1279 | } |
| 1280 | } |
| 1281 | } |
| 1282 |
| 1283 | void PathVerifier::WrapUp(const ParsedCertificate &cert, |
| 1284 | KeyPurpose required_key_purpose, |
| 1285 | const std::set<der::Input> &user_initial_policy_set, |
| 1286 | bool allow_precertificate, |
| 1287 | CertErrors * errors) { |
| 1288 | // From RFC 5280 section 6.1.5: |
| 1289 | // (a) If explicit_policy is not 0, decrement explicit_policy by 1. |
| 1290 | if (explicit_policy_ > 0) { |
| 1291 | explicit_policy_ -= 1; |
| 1292 | } |
| 1293 |
| 1294 | // (b) If a policy constraints extension is included in the |
| 1295 | // certificate and requireExplicitPolicy is present and has a |
| 1296 | // value of 0, set the explicit_policy state variable to 0. |
| 1297 | if (cert.has_policy_constraints() && |
| 1298 | cert.policy_constraints().require_explicit_policy.has_value() && |
| 1299 | cert.policy_constraints().require_explicit_policy == 0) { |
| 1300 | explicit_policy_ = 0; |
| 1301 | } |
| 1302 |
| 1303 | // Note step c-e are omitted as the verification function does |
| 1304 | // not output the working public key. |
| 1305 |
| 1306 | // From RFC 5280 section 6.1.5 step f: |
| 1307 | // |
| 1308 | // Recognize and process any other critical extension present in |
| 1309 | // the certificate n. Process any other recognized non-critical |
| 1310 | // extension present in certificate n that is relevant to path |
| 1311 | // processing. |
| 1312 | // |
| 1313 | // Note that this is duplicated by PrepareForNextCertificate() so as to |
| 1314 | // directly match the procedures in RFC 5280's section 6.1. |
| 1315 | VerifyNoUnconsumedCriticalExtensions(cert, errors, allow_precertificate, |
| 1316 | required_key_purpose); |
| 1317 |
| 1318 | // This calculates the intersection from RFC 5280 section 6.1.5 step g, as |
| 1319 | // well as applying the deferred recursive node that were skipped earlier in |
| 1320 | // the process. |
| 1321 | user_constrained_policy_set_ = |
| 1322 | valid_policy_graph_.GetUserConstrainedPolicySet(user_initial_policy_set); |
| 1323 |
| 1324 | // From RFC 5280 section 6.1.5 step g: |
| 1325 | // |
| 1326 | // If either (1) the value of explicit_policy variable is greater than |
| 1327 | // zero or (2) the valid_policy_tree is not NULL, then path processing |
| 1328 | // has succeeded. |
| 1329 | if (explicit_policy_ == 0 && user_constrained_policy_set_.empty()) { |
| 1330 | errors->AddError(cert_errors::kNoValidPolicy); |
| 1331 | } |
| 1332 |
| 1333 | // The following check is NOT part of RFC 5280 6.1.5's "Wrap-Up Procedure", |
| 1334 | // however is implied by RFC 5280 section 4.2.1.9, as well as CABF Base |
| 1335 | // Requirements. |
| 1336 | VerifyTargetCertIsNotCA(cert, required_key_purpose, errors); |
| 1337 |
| 1338 | // Check the public key for the target certificate. The public key for the |
| 1339 | // other certificates is already checked by PrepareForNextCertificate(). |
| 1340 | // Note that this step is not part of RFC 5280 6.1.5. |
| 1341 | ParseAndCheckPublicKey(cert.tbs().spki_tlv, errors); |
| 1342 | } |
| 1343 |
| 1344 | void PathVerifier::ApplyTrustAnchorConstraints(const ParsedCertificate &cert, |
| 1345 | KeyPurpose required_key_purpose, |
| 1346 | CertErrors *errors) { |
| 1347 | // If certificatePolicies is present, process the policies. This matches the |
| 1348 | // handling for intermediates from RFC 5280 section 6.1.3.d (except that for |
| 1349 | // intermediates it is non-optional). It intentionally deviates from RFC 5937 |
| 1350 | // section 3.2 which says to intersect with user-initial-policy-set, since |
| 1351 | // processing as part of user-initial-policy-set has subtly different |
| 1352 | // semantics from being handled as part of the chain processing (see |
| 1353 | // https://crbug.com/1403258). |
| 1354 | if (cert.has_policy_oids()) { |
| 1355 | VerifyPolicies(cert, /*is_target_cert=*/false, errors); |
| 1356 | } |
| 1357 |
| 1358 | // Process policyMappings, if present. This matches the handling for |
| 1359 | // intermediates from RFC 5280 section 6.1.4 step a-b. |
| 1360 | VerifyPolicyMappings(cert, errors); |
| 1361 |
| 1362 | // Process policyConstraints and inhibitAnyPolicy. This matches the |
| 1363 | // handling for intermediates from RFC 5280 section 6.1.4 step i-j. |
| 1364 | // This intentionally deviates from RFC 5937 section 3.2 which says to |
| 1365 | // initialize the initial-any-policy-inhibit, initial-explicit-policy, and/or |
| 1366 | // initial-policy-mapping-inhibit inputs to verification. Those are all |
| 1367 | // bools, so they cannot properly represent the constraints encoded in the |
| 1368 | // policyConstraints and inhibitAnyPolicy extensions. |
| 1369 | ApplyPolicyConstraints(cert); |
| 1370 |
| 1371 | // If keyUsage is present, verify that |cert| has correct keyUsage bits for a |
| 1372 | // CA. This matches the handling for intermediates from RFC 5280 section |
| 1373 | // 6.1.4 step n. |
| 1374 | if (cert.has_key_usage() && |
| 1375 | !cert.key_usage().AssertsBit(KEY_USAGE_BIT_KEY_CERT_SIGN)) { |
| 1376 | errors->AddError(cert_errors::kKeyCertSignBitNotSet); |
| 1377 | } |
| 1378 |
| 1379 | // This is not part of RFC 5937 nor RFC 5280, but matches the EKU handling |
| 1380 | // done for intermediates (described in Web PKI's Baseline Requirements). |
| 1381 | VerifyExtendedKeyUsage(cert, required_key_purpose, errors, |
| 1382 | /*is_target_cert=*/false, |
| 1383 | /*is_target_cert_issuer=*/false); |
| 1384 |
| 1385 | // The following enforcements follow from RFC 5937 (primarily section 3.2): |
| 1386 |
| 1387 | // Initialize name constraints initial-permitted/excluded-subtrees. |
| 1388 | if (cert.has_name_constraints()) { |
| 1389 | name_constraints_list_.push_back(&cert.name_constraints()); |
| 1390 | } |
| 1391 |
| 1392 | if (cert.has_basic_constraints()) { |
| 1393 | // Enforce CA=true if basicConstraints is present. This matches behavior of |
| 1394 | // other verifiers, and seems like a good thing to do to avoid a |
| 1395 | // certificate being used in the wrong context if it was specifically |
| 1396 | // marked as not being a CA. |
| 1397 | if (!cert.basic_constraints().is_ca) { |
| 1398 | errors->AddError(cert_errors::kBasicConstraintsIndicatesNotCa); |
| 1399 | } |
| 1400 | // From RFC 5937 section 3.2: |
| 1401 | // |
| 1402 | // If a basic constraints extension is associated with the trust |
| 1403 | // anchor and contains a pathLenConstraint value, set the |
| 1404 | // max_path_length state variable equal to the pathLenConstraint |
| 1405 | // value from the basic constraints extension. |
| 1406 | // |
| 1407 | if (cert.basic_constraints().has_path_len) { |
| 1408 | max_path_length_ = cert.basic_constraints().path_len; |
| 1409 | } |
| 1410 | } |
| 1411 |
| 1412 | // From RFC 5937 section 2: |
| 1413 | // |
| 1414 | // Extensions may be marked critical or not critical. When trust anchor |
| 1415 | // constraints are enforced, clients MUST reject certification paths |
| 1416 | // containing a trust anchor with unrecognized critical extensions. |
| 1417 | VerifyNoUnconsumedCriticalExtensions(cert, errors, |
| 1418 | /*allow_precertificate=*/false, |
| 1419 | required_key_purpose); |
| 1420 | } |
| 1421 |
| 1422 | void PathVerifier::ProcessRootCertificate(const ParsedCertificate &cert, |
| 1423 | const CertificateTrust &trust, |
| 1424 | const der::GeneralizedTime &time, |
| 1425 | KeyPurpose required_key_purpose, |
| 1426 | CertErrors *errors, |
| 1427 | bool *shortcircuit_chain_validation) { |
| 1428 | *shortcircuit_chain_validation = false; |
| 1429 | switch (trust.type) { |
| 1430 | case CertificateTrustType::UNSPECIFIED: |
| 1431 | case CertificateTrustType::TRUSTED_LEAF: |
| 1432 | // Doesn't chain to a trust anchor - implicitly distrusted |
| 1433 | errors->AddError(cert_errors::kCertIsNotTrustAnchor); |
| 1434 | *shortcircuit_chain_validation = true; |
| 1435 | break; |
| 1436 | case CertificateTrustType::DISTRUSTED: |
| 1437 | // Chains to an actively distrusted certificate. |
| 1438 | errors->AddError(cert_errors::kDistrustedByTrustStore); |
| 1439 | *shortcircuit_chain_validation = true; |
| 1440 | break; |
| 1441 | case CertificateTrustType::TRUSTED_ANCHOR: |
| 1442 | case CertificateTrustType::TRUSTED_ANCHOR_OR_LEAF: |
| 1443 | break; |
| 1444 | } |
| 1445 | if (*shortcircuit_chain_validation) { |
| 1446 | return; |
| 1447 | } |
| 1448 |
| 1449 | if (trust.enforce_anchor_expiry) { |
| 1450 | VerifyTimeValidity(cert, time, errors); |
| 1451 | } |
| 1452 | if (trust.enforce_anchor_constraints) { |
| 1453 | if (trust.require_anchor_basic_constraints && |
| 1454 | !cert.has_basic_constraints()) { |
| 1455 | switch (cert.tbs().version) { |
| 1456 | case CertificateVersion::V1: |
| 1457 | case CertificateVersion::V2: |
| 1458 | break; |
| 1459 | case CertificateVersion::V3: |
| 1460 | errors->AddError(cert_errors::kMissingBasicConstraints); |
| 1461 | break; |
| 1462 | } |
| 1463 | } |
| 1464 | ApplyTrustAnchorConstraints(cert, required_key_purpose, errors); |
| 1465 | } |
| 1466 |
| 1467 | // Use the certificate's SPKI and subject when verifying the next certificate. |
| 1468 | working_public_key_ = ParseAndCheckPublicKey(cert.tbs().spki_tlv, errors); |
| 1469 | working_normalized_issuer_name_ = cert.normalized_subject(); |
| 1470 | } |
| 1471 |
| 1472 | void PathVerifier::ProcessSingleCertChain(const ParsedCertificate &cert, |
| 1473 | const CertificateTrust &trust, |
| 1474 | const der::GeneralizedTime &time, |
| 1475 | KeyPurpose required_key_purpose, |
| 1476 | CertErrors *errors) { |
| 1477 | switch (trust.type) { |
| 1478 | case CertificateTrustType::UNSPECIFIED: |
| 1479 | case CertificateTrustType::TRUSTED_ANCHOR: |
| 1480 | // Target doesn't have a chain and isn't a directly trusted leaf - |
| 1481 | // implicitly distrusted. |
| 1482 | errors->AddError(cert_errors::kCertIsNotTrustAnchor); |
| 1483 | return; |
| 1484 | case CertificateTrustType::DISTRUSTED: |
| 1485 | // Target is directly distrusted. |
| 1486 | errors->AddError(cert_errors::kDistrustedByTrustStore); |
| 1487 | return; |
| 1488 | case CertificateTrustType::TRUSTED_LEAF: |
| 1489 | case CertificateTrustType::TRUSTED_ANCHOR_OR_LEAF: |
| 1490 | break; |
| 1491 | } |
| 1492 |
| 1493 | // Check the public key for the target certificate regardless of whether |
| 1494 | // `require_leaf_selfsigned` is true. This matches the check in WrapUp and |
| 1495 | // fulfills the documented behavior of the IsPublicKeyAcceptable delegate. |
| 1496 | ParseAndCheckPublicKey(cert.tbs().spki_tlv, errors); |
| 1497 |
| 1498 | if (trust.require_leaf_selfsigned) { |
| 1499 | if (!VerifyCertificateIsSelfSigned(cert, delegate_->GetVerifyCache(), |
| 1500 | errors)) { |
| 1501 | // VerifyCertificateIsSelfSigned should have added an error, but just |
| 1502 | // double check to be safe. |
| 1503 | if (!errors->ContainsAnyErrorWithSeverity(CertError::SEVERITY_HIGH)) { |
| 1504 | errors->AddError(cert_errors::kInternalError); |
| 1505 | } |
| 1506 | return; |
| 1507 | } |
| 1508 | } |
| 1509 |
| 1510 | // There is no standard for what it means to verify a directly trusted leaf |
| 1511 | // certificate, so this is basically just checking common sense things that |
| 1512 | // also mirror what we observed to be enforced with the Operating System |
| 1513 | // native verifiers. |
| 1514 | VerifyTimeValidity(cert, time, errors); |
| 1515 | VerifyExtendedKeyUsage(cert, required_key_purpose, errors, |
| 1516 | /*is_target_cert=*/true, |
| 1517 | /*is_target_cert_issuer=*/false); |
| 1518 |
| 1519 | // Checking for unknown critical extensions matches Windows, but is stricter |
| 1520 | // than the Mac verifier. |
| 1521 | VerifyNoUnconsumedCriticalExtensions(cert, errors, |
| 1522 | /*allow_precertificate=*/false, |
| 1523 | required_key_purpose); |
| 1524 | } |
| 1525 |
| 1526 | bssl::UniquePtr<EVP_PKEY> PathVerifier::ParseAndCheckPublicKey( |
| 1527 | der::Input spki, CertErrors *errors) { |
| 1528 | // Parse the public key. |
| 1529 | bssl::UniquePtr<EVP_PKEY> pkey; |
| 1530 | if (!ParsePublicKey(spki, &pkey)) { |
| 1531 | errors->AddError(cert_errors::kFailedParsingSpki); |
| 1532 | return nullptr; |
| 1533 | } |
| 1534 |
| 1535 | // Check if the key is acceptable by the delegate. |
| 1536 | if (!delegate_->IsPublicKeyAcceptable(pkey.get(), errors)) { |
| 1537 | errors->AddError(cert_errors::kUnacceptablePublicKey); |
| 1538 | } |
| 1539 |
| 1540 | return pkey; |
| 1541 | } |
| 1542 |
| 1543 | void PathVerifier::Run( |
| 1544 | const ParsedCertificateList &certs, const CertificateTrust &last_cert_trust, |
| 1545 | VerifyCertificateChainDelegate *delegate, const der::GeneralizedTime &time, |
| 1546 | KeyPurpose required_key_purpose, |
| 1547 | InitialExplicitPolicy initial_explicit_policy, |
| 1548 | const std::set<der::Input> &user_initial_policy_set, |
| 1549 | InitialPolicyMappingInhibit initial_policy_mapping_inhibit, |
| 1550 | InitialAnyPolicyInhibit initial_any_policy_inhibit, |
| 1551 | std::set<der::Input> *user_constrained_policy_set, CertPathErrors *errors) { |
| 1552 | // This implementation is structured to mimic the description of certificate |
| 1553 | // path verification given by RFC 5280 section 6.1. |
| 1554 | BSSL_CHECK(delegate); |
| 1555 | BSSL_CHECK(errors); |
| 1556 |
| 1557 | delegate_ = delegate; |
| 1558 |
| 1559 | // An empty chain is necessarily invalid. |
| 1560 | if (certs.empty()) { |
| 1561 | errors->GetOtherErrors()->AddError(cert_errors::kChainIsEmpty); |
| 1562 | return; |
| 1563 | } |
| 1564 |
| 1565 | // Verifying a trusted leaf certificate isn't a well-specified operation, so |
| 1566 | // it's handled separately from the RFC 5280 defined verification process. |
| 1567 | if (certs.size() == 1) { |
| 1568 | ProcessSingleCertChain(*certs.front(), last_cert_trust, time, |
| 1569 | required_key_purpose, errors->GetErrorsForCert(0)); |
| 1570 | return; |
| 1571 | } |
| 1572 |
| 1573 | // RFC 5280's "n" variable is the length of the path, which does not count |
| 1574 | // the trust anchor. (Although in practice it doesn't really change behaviors |
| 1575 | // if n is used in place of n+1). |
| 1576 | const size_t n = certs.size() - 1; |
| 1577 |
| 1578 | valid_policy_graph_.Init(); |
| 1579 |
| 1580 | // RFC 5280 section section 6.1.2: |
| 1581 | // |
| 1582 | // If initial-explicit-policy is set, then the initial value |
| 1583 | // [of explicit_policy] is 0, otherwise the initial value is n+1. |
| 1584 | explicit_policy_ = |
| 1585 | initial_explicit_policy == InitialExplicitPolicy::kTrue ? 0 : n + 1; |
| 1586 |
| 1587 | // RFC 5280 section section 6.1.2: |
| 1588 | // |
| 1589 | // If initial-any-policy-inhibit is set, then the initial value |
| 1590 | // [of inhibit_anyPolicy] is 0, otherwise the initial value is n+1. |
| 1591 | inhibit_any_policy_ = |
| 1592 | initial_any_policy_inhibit == InitialAnyPolicyInhibit::kTrue ? 0 : n + 1; |
| 1593 |
| 1594 | // RFC 5280 section section 6.1.2: |
| 1595 | // |
| 1596 | // If initial-policy-mapping-inhibit is set, then the initial value |
| 1597 | // [of policy_mapping] is 0, otherwise the initial value is n+1. |
| 1598 | policy_mapping_ = |
| 1599 | initial_policy_mapping_inhibit == InitialPolicyMappingInhibit::kTrue |
| 1600 | ? 0 |
| 1601 | : n + 1; |
| 1602 |
| 1603 | // RFC 5280 section section 6.1.2: |
| 1604 | // |
| 1605 | // max_path_length: this integer is initialized to n, ... |
| 1606 | max_path_length_ = n; |
| 1607 |
| 1608 | // Iterate over all the certificates in the reverse direction: starting from |
| 1609 | // the root certificate and progressing towards the target certificate. |
| 1610 | // |
| 1611 | // * i=0 : Root certificate (i.e. trust anchor) |
| 1612 | // * i=1 : Certificate issued by root |
| 1613 | // * i=x : Certificate i=x is issued by certificate i=x-1 |
| 1614 | // * i=n : Target certificate. |
| 1615 | for (size_t i = 0; i < certs.size(); ++i) { |
| 1616 | const size_t index_into_certs = certs.size() - i - 1; |
| 1617 |
| 1618 | // |is_target_cert| is true if the current certificate is the target |
| 1619 | // certificate being verified. The target certificate isn't necessarily an |
| 1620 | // end-entity certificate. |
| 1621 | const bool is_target_cert = index_into_certs == 0; |
| 1622 | const bool is_target_cert_issuer = index_into_certs == 1; |
| 1623 | const bool is_root_cert = i == 0; |
| 1624 |
| 1625 | const ParsedCertificate &cert = *certs[index_into_certs]; |
| 1626 |
| 1627 | // Output errors for the current certificate into an error bucket that is |
| 1628 | // associated with that certificate. |
| 1629 | CertErrors *cert_errors = errors->GetErrorsForCert(index_into_certs); |
| 1630 |
| 1631 | if (is_root_cert) { |
| 1632 | bool shortcircuit_chain_validation = false; |
| 1633 | ProcessRootCertificate(cert, last_cert_trust, time, required_key_purpose, |
| 1634 | cert_errors, &shortcircuit_chain_validation); |
| 1635 | if (shortcircuit_chain_validation) { |
| 1636 | // Chains that don't start from a trusted root should short-circuit the |
| 1637 | // rest of the verification, as accumulating more errors from untrusted |
| 1638 | // certificates would not be meaningful. |
| 1639 | BSSL_CHECK(cert_errors->ContainsAnyErrorWithSeverity( |
| 1640 | CertError::SEVERITY_HIGH)); |
| 1641 | return; |
| 1642 | } |
| 1643 |
| 1644 | // Don't do any other checks for root certificates. |
| 1645 | continue; |
| 1646 | } |
| 1647 |
| 1648 | bool shortcircuit_chain_validation = false; |
| 1649 | // Per RFC 5280 section 6.1: |
| 1650 | // * Do basic processing for each certificate |
| 1651 | // * If it is the last certificate in the path (target certificate) |
| 1652 | // - Then run "Wrap up" |
| 1653 | // - Otherwise run "Prepare for Next cert" |
| 1654 | BasicCertificateProcessing(cert, is_target_cert, is_target_cert_issuer, |
| 1655 | time, required_key_purpose, cert_errors, |
| 1656 | &shortcircuit_chain_validation); |
| 1657 | if (shortcircuit_chain_validation) { |
| 1658 | // Signature errors or unparsable SPKIs should short-circuit the rest of |
| 1659 | // the verification, as accumulating more errors from untrusted |
| 1660 | // certificates would not be meaningful. |
| 1661 | BSSL_CHECK( |
| 1662 | errors->ContainsAnyErrorWithSeverity(CertError::SEVERITY_HIGH)); |
| 1663 | return; |
| 1664 | } |
| 1665 | if (!is_target_cert) { |
| 1666 | PrepareForNextCertificate(cert, required_key_purpose, cert_errors); |
| 1667 | } else { |
| 1668 | WrapUp(cert, required_key_purpose, user_initial_policy_set, |
| 1669 | delegate->AcceptPreCertificates(), cert_errors); |
| 1670 | } |
| 1671 | } |
| 1672 |
| 1673 | if (user_constrained_policy_set) { |
| 1674 | *user_constrained_policy_set = user_constrained_policy_set_; |
| 1675 | } |
| 1676 |
| 1677 | // TODO(eroman): RFC 5280 forbids duplicate certificates per section 6.1: |
| 1678 | // |
| 1679 | // A certificate MUST NOT appear more than once in a prospective |
| 1680 | // certification path. |
| 1681 | } |
| 1682 |
| 1683 | } // namespace |
| 1684 |
| 1685 | VerifyCertificateChainDelegate::~VerifyCertificateChainDelegate() = default; |
| 1686 |
| 1687 | void VerifyCertificateChain( |
| 1688 | const ParsedCertificateList &certs, const CertificateTrust &last_cert_trust, |
| 1689 | VerifyCertificateChainDelegate *delegate, const der::GeneralizedTime &time, |
| 1690 | KeyPurpose required_key_purpose, |
| 1691 | InitialExplicitPolicy initial_explicit_policy, |
| 1692 | const std::set<der::Input> &user_initial_policy_set, |
| 1693 | InitialPolicyMappingInhibit initial_policy_mapping_inhibit, |
| 1694 | InitialAnyPolicyInhibit initial_any_policy_inhibit, |
| 1695 | std::set<der::Input> *user_constrained_policy_set, CertPathErrors *errors) { |
| 1696 | PathVerifier verifier; |
| 1697 | verifier.Run(certs, last_cert_trust, delegate, time, required_key_purpose, |
| 1698 | initial_explicit_policy, user_initial_policy_set, |
| 1699 | initial_policy_mapping_inhibit, initial_any_policy_inhibit, |
| 1700 | user_constrained_policy_set, errors); |
| 1701 | } |
| 1702 |
| 1703 | bool VerifyCertificateIsSelfSigned(const ParsedCertificate &cert, |
| 1704 | SignatureVerifyCache *cache, |
| 1705 | CertErrors *errors) { |
| 1706 | if (cert.normalized_subject() != cert.normalized_issuer()) { |
| 1707 | if (errors) { |
| 1708 | errors->AddError(cert_errors::kSubjectDoesNotMatchIssuer); |
| 1709 | } |
| 1710 | return false; |
| 1711 | } |
| 1712 |
| 1713 | // Note that we do not restrict the available algorithms when determining if |
| 1714 | // something is a self-signed cert. The signature isn't very important on a |
| 1715 | // self-signed cert so just allow any supported algorithm here, to avoid |
| 1716 | // breakage. |
| 1717 | if (!cert.signature_algorithm().has_value()) { |
| 1718 | if (errors) { |
| 1719 | errors->AddError(cert_errors::kUnacceptableSignatureAlgorithm); |
| 1720 | } |
| 1721 | return false; |
| 1722 | } |
| 1723 |
| 1724 | if (!VerifySignedData(*cert.signature_algorithm(), cert.tbs_certificate_tlv(), |
| 1725 | cert.signature_value(), cert.tbs().spki_tlv, cache)) { |
| 1726 | if (errors) { |
| 1727 | errors->AddError(cert_errors::kVerifySignedDataFailed); |
| 1728 | } |
| 1729 | return false; |
| 1730 | } |
| 1731 |
| 1732 | return true; |
| 1733 | } |
| 1734 |
| 1735 | BSSL_NAMESPACE_END |
| 1736 |