Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "net/cert/internal/parse_ocsp.h" | |
| 6 | |
| 7 #include "base/files/file_path.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "net/base/test_data_directory.h" | |
| 10 #include "net/cert/internal/test_helpers.h" | |
| 11 #include "net/cert/x509_certificate.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 namespace net { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 std::string GetFilePath(const std::string& file_name) { | |
| 19 return std::string("net/data/parse_ocsp_unittest/") + file_name; | |
| 20 } | |
| 21 | |
| 22 void ReadOCSPFromFile(const std::string& file_name, | |
|
eroman
2016/03/22 21:57:02
This function is only used in one place, I imagine
svaldez
2016/03/23 15:10:12
Done.
| |
| 23 std::string* ocsp_data, | |
| 24 std::string* ca_data, | |
| 25 std::string* cert_data) { | |
| 26 const PemBlockMapping mappings[] = { | |
| 27 {"OCSP RESPONSE", ocsp_data}, | |
| 28 {"CA CERTIFICATE", ca_data}, | |
| 29 {"CERTIFICATE", cert_data}, | |
| 30 }; | |
| 31 | |
| 32 ASSERT_TRUE(ReadTestDataFromPemFile(GetFilePath(file_name), mappings)); | |
| 33 } | |
| 34 | |
| 35 enum OCSPFailure { | |
| 36 PARSE_CERT, | |
| 37 PARSE_OCSP, | |
| 38 OCSP_NOT_SUCCESSFUL, | |
| 39 PARSE_OCSP_DATA, | |
| 40 PARSE_OCSP_SINGLE_RESPONSE, | |
| 41 VERIFY_OCSP, | |
| 42 OCSP_SUCCESS, | |
|
eroman
2016/03/22 21:57:02
nit: I suggest moving this to the top, since the r
svaldez
2016/03/23 15:10:12
Done.
| |
| 43 OCSP_SUCCESS_REVOKED, | |
| 44 OCSP_SUCCESS_UNKNOWN, | |
| 45 }; | |
| 46 | |
| 47 OCSPFailure ParseOCSP(const std::string& file_name) { | |
| 48 std::string ocsp_data; | |
| 49 std::string ca_data; | |
| 50 std::string cert_data; | |
| 51 ReadOCSPFromFile(file_name, &ocsp_data, &ca_data, &cert_data); | |
| 52 der::Input ocsp_input(&ocsp_data); | |
| 53 der::Input ca_input(&ca_data); | |
| 54 der::Input cert_input(&cert_data); | |
| 55 | |
| 56 ParsedCertificate issuer; | |
| 57 ParsedCertificate cert; | |
| 58 if (!ParseCertificate(ca_input, &issuer)) | |
| 59 return PARSE_CERT; | |
| 60 if (!ParseCertificate(cert_input, &cert)) | |
| 61 return PARSE_CERT; | |
| 62 OCSPResponse parsed_ocsp; | |
| 63 OCSPResponseData parsed_ocsp_data; | |
| 64 if (!ParseOCSPResponse(ocsp_input, &parsed_ocsp)) | |
| 65 return PARSE_OCSP; | |
| 66 if (parsed_ocsp.status != OCSPResponse::ResponseStatus::SUCCESSFUL) | |
| 67 return OCSP_NOT_SUCCESSFUL; | |
| 68 if (!ParseOCSPResponseData(parsed_ocsp.data, &parsed_ocsp_data)) | |
| 69 return PARSE_OCSP_DATA; | |
| 70 | |
| 71 OCSPCertStatus status; | |
| 72 | |
| 73 if (!GetOCSPCertStatus(parsed_ocsp_data, issuer, cert, &status)) | |
| 74 return PARSE_OCSP_SINGLE_RESPONSE; | |
| 75 | |
| 76 switch (status.status) { | |
| 77 case OCSPCertStatus::Status::GOOD: | |
| 78 return OCSP_SUCCESS; | |
| 79 case OCSPCertStatus::Status::REVOKED: | |
| 80 return OCSP_SUCCESS_REVOKED; | |
| 81 case OCSPCertStatus::Status::UNKNOWN: | |
| 82 return OCSP_SUCCESS_UNKNOWN; | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 } // namespace | |
| 87 | |
| 88 TEST(ParseOCSPTest, OCSPGoodResponse) { | |
| 89 ASSERT_EQ(OCSP_SUCCESS, ParseOCSP("good_response.pem")); | |
| 90 } | |
| 91 | |
| 92 TEST(ParseOCSPTest, OCSPNoResponse) { | |
| 93 ASSERT_EQ(PARSE_OCSP_SINGLE_RESPONSE, ParseOCSP("no_response.pem")); | |
| 94 } | |
| 95 | |
| 96 TEST(ParseOCSPTest, OCSPMalformedStatus) { | |
| 97 ASSERT_EQ(OCSP_NOT_SUCCESSFUL, ParseOCSP("malformed_status.pem")); | |
| 98 } | |
| 99 | |
| 100 TEST(ParseOCSPTest, OCSPBadStatus) { | |
| 101 ASSERT_EQ(PARSE_OCSP, ParseOCSP("bad_status.pem")); | |
| 102 } | |
| 103 | |
| 104 TEST(ParseOCSPTest, OCSPInvalidOCSPOid) { | |
| 105 ASSERT_EQ(PARSE_OCSP, ParseOCSP("bad_ocsp_type.pem")); | |
| 106 } | |
| 107 | |
| 108 TEST(ParseOCSPTest, OCSPBadSignature) { | |
| 109 ASSERT_EQ(OCSP_SUCCESS, ParseOCSP("bad_signature.pem")); | |
| 110 } | |
| 111 | |
| 112 TEST(ParseOCSPTest, OCSPDirectSignature) { | |
| 113 ASSERT_EQ(OCSP_SUCCESS, ParseOCSP("ocsp_sign_direct.pem")); | |
| 114 } | |
| 115 | |
| 116 TEST(ParseOCSPTest, OCSPIndirectSignature) { | |
| 117 ASSERT_EQ(OCSP_SUCCESS, ParseOCSP("ocsp_sign_indirect.pem")); | |
| 118 } | |
| 119 | |
| 120 TEST(ParseOCSPTest, OCSPMissingIndirectSignature) { | |
| 121 ASSERT_EQ(OCSP_SUCCESS, ParseOCSP("ocsp_sign_indirect_missing.pem")); | |
| 122 } | |
| 123 | |
| 124 TEST(ParseOCSPTest, OCSPInvalidSignature) { | |
| 125 ASSERT_EQ(OCSP_SUCCESS, ParseOCSP("ocsp_sign_bad_indirect.pem")); | |
| 126 } | |
| 127 | |
| 128 TEST(ParseOCSPTest, OCSPExtraCerts) { | |
| 129 ASSERT_EQ(OCSP_SUCCESS, ParseOCSP("ocsp_extra_certs.pem")); | |
| 130 } | |
| 131 | |
| 132 TEST(ParseOCSPTest, OCSPIncludesVersion) { | |
| 133 ASSERT_EQ(OCSP_SUCCESS, ParseOCSP("has_version.pem")); | |
| 134 } | |
| 135 | |
| 136 TEST(ParseOCSPTest, OCSPResponderName) { | |
| 137 ASSERT_EQ(OCSP_SUCCESS, ParseOCSP("responder_name.pem")); | |
| 138 } | |
| 139 | |
| 140 TEST(ParseOCSPTest, OCSPResponderKeyHash) { | |
| 141 ASSERT_EQ(OCSP_SUCCESS, ParseOCSP("responder_id.pem")); | |
| 142 } | |
| 143 | |
| 144 TEST(ParseOCSPTest, OCSPOCSPExtension) { | |
| 145 ASSERT_EQ(OCSP_SUCCESS, ParseOCSP("has_extension.pem")); | |
| 146 } | |
| 147 | |
| 148 TEST(ParseOCSPTest, OCSPIncludeNextUpdate) { | |
| 149 ASSERT_EQ(OCSP_SUCCESS, ParseOCSP("good_response_next_update.pem")); | |
| 150 } | |
| 151 | |
| 152 TEST(ParseOCSPTest, OCSPRevokedResponse) { | |
| 153 ASSERT_EQ(OCSP_SUCCESS_REVOKED, ParseOCSP("revoke_response.pem")); | |
| 154 } | |
| 155 | |
| 156 TEST(ParseOCSPTest, OCSPRevokedResponseWithReason) { | |
| 157 ASSERT_EQ(OCSP_SUCCESS_REVOKED, ParseOCSP("revoke_response_reason.pem")); | |
| 158 } | |
| 159 | |
| 160 TEST(ParseOCSPTest, OCSPUnknownCertStatus) { | |
| 161 ASSERT_EQ(OCSP_SUCCESS_UNKNOWN, ParseOCSP("unknown_response.pem")); | |
| 162 } | |
| 163 | |
| 164 TEST(ParseOCSPTest, OCSPMultipleCertStatus) { | |
| 165 ASSERT_EQ(OCSP_SUCCESS_UNKNOWN, ParseOCSP("multiple_response.pem")); | |
| 166 } | |
| 167 | |
| 168 TEST(ParseOCSPTest, OCSPWrongCertResponse) { | |
| 169 ASSERT_EQ(PARSE_OCSP_SINGLE_RESPONSE, ParseOCSP("other_response.pem")); | |
| 170 } | |
| 171 | |
| 172 TEST(ParseOCSPTest, OCSPOCSPSingleExtension) { | |
| 173 ASSERT_EQ(OCSP_SUCCESS, ParseOCSP("has_single_extension.pem")); | |
| 174 } | |
| 175 | |
| 176 TEST(ParseOCSPTest, OCSPMissingResponse) { | |
| 177 ASSERT_EQ(PARSE_OCSP_SINGLE_RESPONSE, ParseOCSP("missing_response.pem")); | |
| 178 } | |
| 179 | |
| 180 } // namespace net | |
| OLD | NEW |