| 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 "crl.h" |
| 16 |
| 17 | #include <string_view> |
| 18 |
| 19 | #include <gtest/gtest.h> |
| 20 | #include <openssl/pool.h> |
| 21 | #include "cert_errors.h" |
| 22 | #include "parsed_certificate.h" |
| 23 | #include "string_util.h" |
| 24 | #include "test_helpers.h" |
| 25 |
| 26 | BSSL_NAMESPACE_BEGIN |
| 27 |
| 28 | namespace { |
| 29 |
| 30 | constexpr int64_t kAgeOneWeek = 7 * 24 * 60 * 60; |
| 31 |
| 32 | std::string GetFilePath(std::string_view file_name) { |
| 33 | return std::string("testdata/crl_unittest/") + std::string(file_name); |
| 34 | } |
| 35 |
| 36 | std::shared_ptr<const ParsedCertificate> ParseCertificate( |
| 37 | std::string_view data) { |
| 38 | CertErrors errors; |
| 39 | return ParsedCertificate::Create( |
| 40 | bssl::UniquePtr<CRYPTO_BUFFER>( |
| 41 | CRYPTO_BUFFER_new(reinterpret_cast<const uint8_t *>(data.data()), |
| 42 | data.size(), nullptr)), |
| 43 | {}, &errors); |
| 44 | } |
| 45 |
| 46 | class CheckCRLTest : public ::testing::TestWithParam<const char *> {}; |
| 47 |
| 48 | // Test prefix naming scheme: |
| 49 | // good = valid CRL, cert affirmatively not revoked |
| 50 | // revoked = valid CRL, cert affirmatively revoked |
| 51 | // bad = valid CRL, but cert status is unknown (cases like unhandled features, |
| 52 | // mismatching issuer or signature, etc) |
| 53 | // invalid = corrupt or violates some spec requirement |
| 54 | constexpr char const *kTestParams[] = { |
| 55 | "good.pem", |
| 56 | "good_issuer_name_normalization.pem", |
| 57 | "good_issuer_no_keyusage.pem", |
| 58 | "good_no_nextupdate.pem", |
| 59 | "good_fake_extension.pem", |
| 60 | "good_fake_extension_no_nextupdate.pem", |
| 61 | "good_generalizedtime.pem", |
| 62 | "good_no_version.pem", |
| 63 | "good_no_crldp.pem", |
| 64 | "good_key_rollover.pem", |
| 65 | "good_idp_contains_uri.pem", |
| 66 | "good_idp_onlycontainsusercerts.pem", |
| 67 | "good_idp_onlycontainsusercerts_no_basic_constraints.pem", |
| 68 | "good_idp_onlycontainscacerts.pem", |
| 69 | "good_idp_uri_and_onlycontainsusercerts.pem", |
| 70 | "good_idp_uri_and_onlycontainscacerts.pem", |
| 71 | "revoked.pem", |
| 72 | "revoked_no_nextupdate.pem", |
| 73 | "revoked_fake_crlentryextension.pem", |
| 74 | "revoked_generalized_revocationdate.pem", |
| 75 | "revoked_key_rollover.pem", |
| 76 | "bad_crldp_has_crlissuer.pem", |
| 77 | "bad_fake_critical_extension.pem", |
| 78 | "bad_fake_critical_crlentryextension.pem", |
| 79 | "bad_signature.pem", |
| 80 | "bad_thisupdate_in_future.pem", |
| 81 | "bad_thisupdate_too_old.pem", |
| 82 | "bad_nextupdate_too_old.pem", |
| 83 | "bad_wrong_issuer.pem", |
| 84 | "bad_key_rollover_signature.pem", |
| 85 | "bad_idp_contains_wrong_uri.pem", |
| 86 | "bad_idp_indirectcrl.pem", |
| 87 | "bad_idp_onlycontainsusercerts.pem", |
| 88 | "bad_idp_onlycontainscacerts.pem", |
| 89 | "bad_idp_onlycontainscacerts_no_basic_constraints.pem", |
| 90 | "bad_idp_uri_and_onlycontainsusercerts.pem", |
| 91 | "bad_idp_uri_and_onlycontainscacerts.pem", |
| 92 | "invalid_mismatched_signature_algorithm.pem", |
| 93 | "invalid_revoked_empty_sequence.pem", |
| 94 | "invalid_v1_with_extension.pem", |
| 95 | "invalid_v1_with_crlentryextension.pem", |
| 96 | "invalid_v1_explicit.pem", |
| 97 | "invalid_v3.pem", |
| 98 | "invalid_issuer_keyusage_no_crlsign.pem", |
| 99 | "invalid_key_rollover_issuer_keyusage_no_crlsign.pem", |
| 100 | "invalid_garbage_version.pem", |
| 101 | "invalid_garbage_tbs_signature_algorithm.pem", |
| 102 | "invalid_garbage_issuer_name.pem", |
| 103 | "invalid_garbage_thisupdate.pem", |
| 104 | "invalid_garbage_after_thisupdate.pem", |
| 105 | "invalid_garbage_after_nextupdate.pem", |
| 106 | "invalid_garbage_after_revokedcerts.pem", |
| 107 | "invalid_garbage_after_extensions.pem", |
| 108 | "invalid_garbage_tbscertlist.pem", |
| 109 | "invalid_garbage_signaturealgorithm.pem", |
| 110 | "invalid_garbage_signaturevalue.pem", |
| 111 | "invalid_garbage_after_signaturevalue.pem", |
| 112 | "invalid_garbage_revoked_serial_number.pem", |
| 113 | "invalid_garbage_revocationdate.pem", |
| 114 | "invalid_garbage_after_revocationdate.pem", |
| 115 | "invalid_garbage_after_crlentryextensions.pem", |
| 116 | "invalid_garbage_crlentry.pem", |
| 117 | "invalid_idp_dpname_choice_extra_data.pem", |
| 118 | "invalid_idp_empty_sequence.pem", |
| 119 | "invalid_idp_onlycontains_user_and_ca_certs.pem", |
| 120 | "invalid_idp_onlycontainsusercerts_v1_leaf.pem", |
| 121 | }; |
| 122 |
| 123 | struct PrintTestName { |
| 124 | std::string operator()( |
| 125 | const testing::TestParamInfo<const char *> &info) const { |
| 126 | std::string_view name(info.param); |
| 127 | // Strip ".pem" from the end as GTest names cannot contain period. |
| 128 | name.remove_suffix(4); |
| 129 | return std::string(name); |
| 130 | } |
| 131 | }; |
| 132 |
| 133 | INSTANTIATE_TEST_SUITE_P(All, CheckCRLTest, ::testing::ValuesIn(kTestParams), |
| 134 | PrintTestName()); |
| 135 |
| 136 | TEST_P(CheckCRLTest, FromFile) { |
| 137 | std::string_view file_name(GetParam()); |
| 138 |
| 139 | std::string crl_data; |
| 140 | std::string ca_data_2; |
| 141 | std::string ca_data; |
| 142 | std::string cert_data; |
| 143 | const PemBlockMapping mappings[] = { |
| 144 | {"CRL", &crl_data}, |
| 145 | {"CA CERTIFICATE 2", &ca_data_2, /*optional=*/true}, |
| 146 | {"CA CERTIFICATE", &ca_data}, |
| 147 | {"CERTIFICATE", &cert_data}, |
| 148 | }; |
| 149 |
| 150 | ASSERT_TRUE(ReadTestDataFromPemFile(GetFilePath(file_name), mappings)); |
| 151 |
| 152 | std::shared_ptr<const ParsedCertificate> cert = ParseCertificate(cert_data); |
| 153 | ASSERT_TRUE(cert); |
| 154 | std::shared_ptr<const ParsedCertificate> issuer_cert = |
| 155 | ParseCertificate(ca_data); |
| 156 | ASSERT_TRUE(issuer_cert); |
| 157 | ParsedCertificateList certs = {cert, issuer_cert}; |
| 158 | if (!ca_data_2.empty()) { |
| 159 | std::shared_ptr<const ParsedCertificate> issuer_cert_2 = |
| 160 | ParseCertificate(ca_data_2); |
| 161 | ASSERT_TRUE(issuer_cert_2); |
| 162 | certs.push_back(issuer_cert_2); |
| 163 | } |
| 164 |
| 165 | // Assumes that all the target certs in the test data certs have at most 1 |
| 166 | // CRL distributionPoint. If the cert has a CRL distributionPoint, it is |
| 167 | // used for verifying the CRL, otherwise the CRL is verified with a |
| 168 | // synthesized distributionPoint. This is allowed since there are some |
| 169 | // conditions that require a V1 certificate to test, which cannot have a |
| 170 | // crlDistributionPoints extension. |
| 171 | // TODO(https://crbug.com/749276): This seems slightly hacky. Maybe the |
| 172 | // distribution point to use should be specified separately in the test PEM? |
| 173 | ParsedDistributionPoint fake_cert_dp; |
| 174 | ParsedDistributionPoint *cert_dp = &fake_cert_dp; |
| 175 | std::vector<ParsedDistributionPoint> distribution_points; |
| 176 | ParsedExtension crl_dp_extension; |
| 177 | if (cert->GetExtension(der::Input(kCrlDistributionPointsOid), |
| 178 | &crl_dp_extension)) { |
| 179 | ASSERT_TRUE(ParseCrlDistributionPoints(crl_dp_extension.value, |
| 180 | &distribution_points)); |
| 181 | ASSERT_LE(distribution_points.size(), 1U); |
| 182 | if (!distribution_points.empty()) { |
| 183 | cert_dp = &distribution_points[0]; |
| 184 | } |
| 185 | } |
| 186 | ASSERT_TRUE(cert_dp); |
| 187 |
| 188 | // Mar 9 00:00:00 2017 GMT |
| 189 | int64_t kVerifyTime = 1489017600; |
| 190 |
| 191 | CRLRevocationStatus expected_revocation_status = CRLRevocationStatus::UNKNOWN; |
| 192 | if (string_util::StartsWith(file_name, "good")) { |
| 193 | expected_revocation_status = CRLRevocationStatus::GOOD; |
| 194 | } else if (string_util::StartsWith(file_name, "revoked")) { |
| 195 | expected_revocation_status = CRLRevocationStatus::REVOKED; |
| 196 | } |
| 197 |
| 198 | CRLRevocationStatus revocation_status = |
| 199 | CheckCRL(crl_data, certs, /*target_cert_index=*/0, *cert_dp, kVerifyTime, |
| 200 | kAgeOneWeek); |
| 201 | EXPECT_EQ(expected_revocation_status, revocation_status); |
| 202 |
| 203 | // Test with a random cert added to the front of the chain and |
| 204 | // |target_cert_index=1|. This is a hacky way to verify that |
| 205 | // target_cert_index is actually being honored. |
| 206 | ParsedCertificateList other_certs; |
| 207 | ASSERT_TRUE(ReadCertChainFromFile( |
| 208 | "testdata/parse_certificate_unittest/cert_version3.pem", &other_certs)); |
| 209 | ASSERT_FALSE(other_certs.empty()); |
| 210 | certs.insert(certs.begin(), other_certs[0]); |
| 211 | revocation_status = CheckCRL(crl_data, certs, /*target_cert_index=*/1, |
| 212 | *cert_dp, kVerifyTime, kAgeOneWeek); |
| 213 | EXPECT_EQ(expected_revocation_status, revocation_status); |
| 214 | } |
| 215 |
| 216 | } // namespace |
| 217 |
| 218 | BSSL_NAMESPACE_END |
| 219 |