| 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 "ocsp.h" |
| 16 |
| 17 | #include <gtest/gtest.h> |
| 18 | #include <openssl/base64.h> |
| 19 | #include <openssl/pool.h> |
| 20 | #include "encode_values.h" |
| 21 | #include "string_util.h" |
| 22 | #include "test_helpers.h" |
| 23 |
| 24 | BSSL_NAMESPACE_BEGIN |
| 25 |
| 26 | namespace { |
| 27 |
| 28 | constexpr int64_t kOCSPAgeOneWeek = 7 * 24 * 60 * 60; |
| 29 |
| 30 | std::string GetFilePath(const std::string &file_name) { |
| 31 | return std::string("testdata/ocsp_unittest/") + file_name; |
| 32 | } |
| 33 |
| 34 | std::shared_ptr<const ParsedCertificate> ParseCertificate( |
| 35 | std::string_view data) { |
| 36 | CertErrors errors; |
| 37 | return ParsedCertificate::Create( |
| 38 | bssl::UniquePtr<CRYPTO_BUFFER>( |
| 39 | CRYPTO_BUFFER_new(reinterpret_cast<const uint8_t *>(data.data()), |
| 40 | data.size(), nullptr)), |
| 41 | {}, &errors); |
| 42 | } |
| 43 |
| 44 | struct TestParams { |
| 45 | const char *file_name; |
| 46 | OCSPRevocationStatus expected_revocation_status; |
| 47 | OCSPVerifyResult::ResponseStatus expected_response_status; |
| 48 | }; |
| 49 |
| 50 | class CheckOCSPTest : public ::testing::TestWithParam<TestParams> {}; |
| 51 |
| 52 | const TestParams kTestParams[] = { |
| 53 | {"good_response.pem", OCSPRevocationStatus::GOOD, |
| 54 | OCSPVerifyResult::PROVIDED}, |
| 55 |
| 56 | {"good_response_sha256.pem", OCSPRevocationStatus::GOOD, |
| 57 | OCSPVerifyResult::PROVIDED}, |
| 58 |
| 59 | {"no_response.pem", OCSPRevocationStatus::UNKNOWN, |
| 60 | OCSPVerifyResult::NO_MATCHING_RESPONSE}, |
| 61 |
| 62 | {"malformed_request.pem", OCSPRevocationStatus::UNKNOWN, |
| 63 | OCSPVerifyResult::ERROR_RESPONSE}, |
| 64 |
| 65 | {"bad_status.pem", OCSPRevocationStatus::UNKNOWN, |
| 66 | OCSPVerifyResult::PARSE_RESPONSE_ERROR}, |
| 67 |
| 68 | {"bad_ocsp_type.pem", OCSPRevocationStatus::UNKNOWN, |
| 69 | OCSPVerifyResult::PARSE_RESPONSE_ERROR}, |
| 70 |
| 71 | {"bad_signature.pem", OCSPRevocationStatus::UNKNOWN, |
| 72 | OCSPVerifyResult::PROVIDED}, |
| 73 |
| 74 | {"ocsp_sign_direct.pem", OCSPRevocationStatus::GOOD, |
| 75 | OCSPVerifyResult::PROVIDED}, |
| 76 |
| 77 | {"ocsp_sign_indirect.pem", OCSPRevocationStatus::GOOD, |
| 78 | OCSPVerifyResult::PROVIDED}, |
| 79 |
| 80 | {"ocsp_sign_indirect_missing.pem", OCSPRevocationStatus::UNKNOWN, |
| 81 | OCSPVerifyResult::PROVIDED}, |
| 82 |
| 83 | {"ocsp_sign_bad_indirect.pem", OCSPRevocationStatus::UNKNOWN, |
| 84 | OCSPVerifyResult::PROVIDED}, |
| 85 |
| 86 | {"ocsp_extra_certs.pem", OCSPRevocationStatus::GOOD, |
| 87 | OCSPVerifyResult::PROVIDED}, |
| 88 |
| 89 | {"has_version.pem", OCSPRevocationStatus::GOOD, OCSPVerifyResult::PROVIDED}, |
| 90 |
| 91 | {"responder_name.pem", OCSPRevocationStatus::GOOD, |
| 92 | OCSPVerifyResult::PROVIDED}, |
| 93 |
| 94 | {"responder_id.pem", OCSPRevocationStatus::GOOD, |
| 95 | OCSPVerifyResult::PROVIDED}, |
| 96 |
| 97 | {"has_extension.pem", OCSPRevocationStatus::GOOD, |
| 98 | OCSPVerifyResult::PROVIDED}, |
| 99 |
| 100 | {"good_response_next_update.pem", OCSPRevocationStatus::GOOD, |
| 101 | OCSPVerifyResult::PROVIDED}, |
| 102 |
| 103 | {"revoke_response.pem", OCSPRevocationStatus::REVOKED, |
| 104 | OCSPVerifyResult::PROVIDED}, |
| 105 |
| 106 | {"revoke_response_reason.pem", OCSPRevocationStatus::REVOKED, |
| 107 | OCSPVerifyResult::PROVIDED}, |
| 108 |
| 109 | {"unknown_response.pem", OCSPRevocationStatus::UNKNOWN, |
| 110 | OCSPVerifyResult::PROVIDED}, |
| 111 |
| 112 | {"multiple_response.pem", OCSPRevocationStatus::UNKNOWN, |
| 113 | OCSPVerifyResult::PROVIDED}, |
| 114 |
| 115 | {"other_response.pem", OCSPRevocationStatus::UNKNOWN, |
| 116 | OCSPVerifyResult::NO_MATCHING_RESPONSE}, |
| 117 |
| 118 | {"has_single_extension.pem", OCSPRevocationStatus::GOOD, |
| 119 | OCSPVerifyResult::PROVIDED}, |
| 120 |
| 121 | {"has_critical_single_extension.pem", OCSPRevocationStatus::UNKNOWN, |
| 122 | OCSPVerifyResult::UNHANDLED_CRITICAL_EXTENSION}, |
| 123 |
| 124 | {"has_critical_response_extension.pem", OCSPRevocationStatus::UNKNOWN, |
| 125 | OCSPVerifyResult::UNHANDLED_CRITICAL_EXTENSION}, |
| 126 |
| 127 | {"has_critical_ct_extension.pem", OCSPRevocationStatus::GOOD, |
| 128 | OCSPVerifyResult::PROVIDED}, |
| 129 |
| 130 | {"missing_response.pem", OCSPRevocationStatus::UNKNOWN, |
| 131 | OCSPVerifyResult::NO_MATCHING_RESPONSE}, |
| 132 | }; |
| 133 |
| 134 | // Parameterised test name generator for tests depending on RenderTextBackend. |
| 135 | struct PrintTestName { |
| 136 | std::string operator()(const testing::TestParamInfo<TestParams> &info) const { |
| 137 | std::string_view name(info.param.file_name); |
| 138 | // Strip ".pem" from the end as GTest names cannot contain period. |
| 139 | name.remove_suffix(4); |
| 140 | return std::string(name); |
| 141 | } |
| 142 | }; |
| 143 |
| 144 | INSTANTIATE_TEST_SUITE_P(All, CheckOCSPTest, ::testing::ValuesIn(kTestParams), |
| 145 | PrintTestName()); |
| 146 |
| 147 | TEST_P(CheckOCSPTest, FromFile) { |
| 148 | const TestParams ¶ms = GetParam(); |
| 149 |
| 150 | std::string ocsp_data; |
| 151 | std::string ca_data; |
| 152 | std::string cert_data; |
| 153 | std::string request_data; |
| 154 | const PemBlockMapping mappings[] = { |
| 155 | {"OCSP RESPONSE", &ocsp_data}, |
| 156 | {"CA CERTIFICATE", &ca_data}, |
| 157 | {"CERTIFICATE", &cert_data}, |
| 158 | {"OCSP REQUEST", &request_data}, |
| 159 | }; |
| 160 |
| 161 | ASSERT_TRUE(ReadTestDataFromPemFile(GetFilePath(params.file_name), mappings)); |
| 162 |
| 163 | // Mar 5 00:00:00 2017 GMT |
| 164 | int64_t kVerifyTime = 1488672000; |
| 165 |
| 166 | // Test that CheckOCSP() works. |
| 167 | OCSPVerifyResult::ResponseStatus response_status; |
| 168 | OCSPRevocationStatus revocation_status = |
| 169 | CheckOCSP(ocsp_data, cert_data, ca_data, kVerifyTime, kOCSPAgeOneWeek, |
| 170 | &response_status); |
| 171 |
| 172 | EXPECT_EQ(params.expected_revocation_status, revocation_status); |
| 173 | EXPECT_EQ(params.expected_response_status, response_status); |
| 174 |
| 175 | // Check that CreateOCSPRequest() works. |
| 176 | std::shared_ptr<const ParsedCertificate> cert = ParseCertificate(cert_data); |
| 177 | ASSERT_TRUE(cert); |
| 178 |
| 179 | std::shared_ptr<const ParsedCertificate> issuer = ParseCertificate(ca_data); |
| 180 | ASSERT_TRUE(issuer); |
| 181 |
| 182 | std::vector<uint8_t> encoded_request; |
| 183 | ASSERT_TRUE(CreateOCSPRequest(cert.get(), issuer.get(), &encoded_request)); |
| 184 |
| 185 | EXPECT_EQ(der::Input(encoded_request), der::Input(request_data)); |
| 186 | } |
| 187 |
| 188 | std::string_view kGetURLTestParams[] = { |
| 189 | "http://www.example.com/", |
| 190 | "http://www.example.com/path/", |
| 191 | "http://www.example.com/path", |
| 192 | "http://www.example.com/path?query" |
| 193 | "http://user:pass@www.example.com/path?query", |
| 194 | }; |
| 195 |
| 196 | class CreateOCSPGetURLTest : public ::testing::TestWithParam<std::string_view> { |
| 197 | }; |
| 198 |
| 199 | INSTANTIATE_TEST_SUITE_P(All, CreateOCSPGetURLTest, |
| 200 | ::testing::ValuesIn(kGetURLTestParams)); |
| 201 |
| 202 | TEST_P(CreateOCSPGetURLTest, Basic) { |
| 203 | std::string ca_data; |
| 204 | std::string cert_data; |
| 205 | std::string request_data; |
| 206 | const PemBlockMapping mappings[] = { |
| 207 | {"CA CERTIFICATE", &ca_data}, |
| 208 | {"CERTIFICATE", &cert_data}, |
| 209 | {"OCSP REQUEST", &request_data}, |
| 210 | }; |
| 211 |
| 212 | // Load one of the test files. (Doesn't really matter which one as |
| 213 | // constructing the DER is tested elsewhere). |
| 214 | ASSERT_TRUE( |
| 215 | ReadTestDataFromPemFile(GetFilePath("good_response.pem"), mappings)); |
| 216 |
| 217 | std::shared_ptr<const ParsedCertificate> cert = ParseCertificate(cert_data); |
| 218 | ASSERT_TRUE(cert); |
| 219 |
| 220 | std::shared_ptr<const ParsedCertificate> issuer = ParseCertificate(ca_data); |
| 221 | ASSERT_TRUE(issuer); |
| 222 |
| 223 | std::optional<std::string> url = |
| 224 | CreateOCSPGetURL(cert.get(), issuer.get(), GetParam()); |
| 225 | ASSERT_TRUE(url); |
| 226 |
| 227 | // Try to extract the encoded data and compare against |request_data|. |
| 228 | // |
| 229 | // A known answer output test would be better as this just reverses the logic |
| 230 | // from the implementation file. |
| 231 | std::string b64 = url->substr(GetParam().size() + 1); |
| 232 |
| 233 | // Hex un-escape the data. |
| 234 | b64 = bssl::string_util::FindAndReplace(b64, "%2B", "+"); |
| 235 | b64 = bssl::string_util::FindAndReplace(b64, "%2F", "/"); |
| 236 | b64 = bssl::string_util::FindAndReplace(b64, "%3D", "="); |
| 237 |
| 238 | // Base64 decode the data. |
| 239 | size_t len; |
| 240 | EXPECT_TRUE(EVP_DecodedLength(&len, b64.size())); |
| 241 | std::vector<uint8_t> decoded(len); |
| 242 | EXPECT_TRUE(EVP_DecodeBase64(decoded.data(), &len, len, |
| 243 | reinterpret_cast<const uint8_t *>(b64.data()), |
| 244 | b64.size())); |
| 245 | std::string decoded_string(decoded.begin(), decoded.begin() + len); |
| 246 |
| 247 | EXPECT_EQ(request_data, decoded_string); |
| 248 | } |
| 249 |
| 250 | } // namespace |
| 251 |
| 252 | BSSL_NAMESPACE_END |
| 253 |