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/trust_store_mac.h" | |
| 6 | |
| 7 #include "base/base_paths.h" | |
| 8 #include "base/files/file_util.h" | |
| 9 #include "base/files/scoped_temp_dir.h" | |
| 10 #include "base/path_service.h" | |
| 11 #include "base/process/launch.h" | |
| 12 #include "base/strings/string_split.h" | |
| 13 #include "net/cert/internal/cert_errors.h" | |
| 14 #include "net/cert/internal/test_helpers.h" | |
| 15 #include "net/cert/pem_tokenizer.h" | |
| 16 #include "net/cert/test_keychain_search_list_mac.h" | |
| 17 #include "net/cert/x509_certificate.h" | |
| 18 #include "net/test/test_data_directory.h" | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | |
| 20 | |
| 21 namespace net { | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 // The PEM block header used for DER certificates | |
| 26 const char kCertificateHeader[] = "CERTIFICATE"; | |
| 27 | |
| 28 ::testing::AssertionResult ReadTestPem(const std::string& file_name, | |
| 29 const std::string& block_name, | |
| 30 std::string* result) { | |
| 31 const PemBlockMapping mappings[] = { | |
| 32 {block_name.c_str(), result}, | |
| 33 }; | |
| 34 | |
| 35 return ReadTestDataFromPemFile(file_name, mappings); | |
| 36 } | |
|
Ryan Sleevi
2017/02/14 19:26:17
Is this helper needed?
mattm
2017/02/16 22:06:09
merged it into the other function
| |
| 37 | |
| 38 ::testing::AssertionResult ReadTestCert( | |
|
Ryan Sleevi
2017/02/14 19:26:17
Perhaps document? :)
mattm
2017/02/16 22:06:09
Done.
| |
| 39 const std::string& file_name, | |
| 40 scoped_refptr<ParsedCertificate>* result) { | |
| 41 std::string der; | |
| 42 ::testing::AssertionResult r = ReadTestPem( | |
| 43 "net/data/ssl/certificates/" + file_name, "CERTIFICATE", &der); | |
|
Ryan Sleevi
2017/02/14 19:26:17
Why not use kCertificateHeader here, like you do e
mattm
2017/02/16 22:06:09
Done.
| |
| 44 if (!r) | |
| 45 return r; | |
| 46 CertErrors errors; | |
| 47 *result = ParsedCertificate::Create(der, {}, &errors); | |
| 48 if (!*result) { | |
| 49 return ::testing::AssertionFailure() | |
| 50 << "ParseCertificate::Create() failed:\n" | |
| 51 << errors.ToDebugString(); | |
| 52 } | |
| 53 return ::testing::AssertionSuccess(); | |
| 54 } | |
| 55 | |
| 56 std::vector<std::string> GetDerCertsFromMatchingItems( | |
|
Ryan Sleevi
2017/02/14 19:26:17
Document?
mattm
2017/02/16 22:06:09
Done.
| |
| 57 CFArrayRef matching_items) { | |
| 58 std::vector<std::string> result; | |
| 59 | |
| 60 for (CFIndex i = 0, item_count = CFArrayGetCount(matching_items); | |
| 61 i < item_count; ++i) { | |
| 62 SecCertificateRef match_cert_handle = reinterpret_cast<SecCertificateRef>( | |
| 63 const_cast<void*>(CFArrayGetValueAtIndex(matching_items, i))); | |
| 64 if (!match_cert_handle) { | |
| 65 ADD_FAILURE() << "null matching_item " << i; | |
| 66 continue; | |
| 67 } | |
| 68 base::ScopedCFTypeRef<CFDataRef> der_data( | |
| 69 SecCertificateCopyData(match_cert_handle)); | |
| 70 if (!der_data) { | |
| 71 ADD_FAILURE() << "SecCertificateCopyData error"; | |
| 72 continue; | |
| 73 } | |
| 74 result.push_back(std::string( | |
| 75 reinterpret_cast<const char*>(CFDataGetBytePtr(der_data.get())), | |
| 76 CFDataGetLength(der_data.get()))); | |
| 77 } | |
| 78 | |
| 79 return result; | |
| 80 } | |
| 81 | |
| 82 bool MatchingItemsMatch(CFArrayRef matching_items, | |
| 83 ParsedCertificateList expected_matches) { | |
| 84 std::vector<std::string> der_result_matches( | |
| 85 GetDerCertsFromMatchingItems(matching_items)); | |
| 86 std::sort(der_result_matches.begin(), der_result_matches.end()); | |
| 87 | |
| 88 std::vector<std::string> der_expected_matches; | |
| 89 for (const auto& it : expected_matches) | |
| 90 der_expected_matches.push_back(it->der_cert().AsString()); | |
| 91 std::sort(der_expected_matches.begin(), der_expected_matches.end()); | |
| 92 | |
| 93 if (der_expected_matches == der_result_matches) | |
| 94 return true; | |
| 95 | |
| 96 // Print some extra information for debugging. | |
| 97 EXPECT_EQ(der_expected_matches, der_result_matches); | |
| 98 return false; | |
| 99 } | |
|
Ryan Sleevi
2017/02/14 19:26:17
A (perhaps convoluted) way in GTest+GMock to handl
mattm
2017/02/16 22:06:09
Done.
| |
| 100 | |
| 101 } // namespace | |
| 102 | |
| 103 TEST(TrustStoreMacTest, MultiRootNotTrusted) { | |
|
Ryan Sleevi
2017/02/14 19:26:17
Document what this is testing :)
mattm
2017/02/16 22:06:09
Done.
| |
| 104 std::unique_ptr<TestKeychainSearchList> test_keychain_search_list( | |
| 105 TestKeychainSearchList::Create()); | |
| 106 ASSERT_TRUE(test_keychain_search_list); | |
| 107 base::FilePath keychain_path( | |
| 108 GetTestCertsDirectory().AppendASCII("multi-root.keychain")); | |
|
Ryan Sleevi
2017/02/14 19:26:17
The thing that makes me nervous from our API incon
mattm
2017/02/16 22:06:09
This is true, but I guess it doesn't worry me too
| |
| 109 // SecKeychainOpen does not fail if the file doesn't exist, so assert it here | |
| 110 // for easier debugging. | |
| 111 ASSERT_TRUE(base::PathExists(keychain_path)); | |
| 112 SecKeychainRef keychain; | |
| 113 OSStatus status = | |
| 114 SecKeychainOpen(keychain_path.MaybeAsASCII().c_str(), &keychain); | |
|
Ryan Sleevi
2017/02/14 19:26:17
.InitializeInto()
mattm
2017/02/16 22:06:09
Done.
| |
| 115 ASSERT_EQ(errSecSuccess, status); | |
| 116 ASSERT_TRUE(keychain); | |
| 117 base::ScopedCFTypeRef<SecKeychainRef> scoped_keychain(keychain); | |
| 118 test_keychain_search_list->AddKeychain(keychain); | |
| 119 | |
| 120 TrustStoreMac trust_store(kSecPolicyAppleSSL); | |
| 121 | |
| 122 scoped_refptr<ParsedCertificate> a_by_b, b_by_c, b_by_f, c_by_d, c_by_e, | |
| 123 f_by_e, d_by_d, e_by_e; | |
| 124 ASSERT_TRUE(ReadTestCert("multi-root-A-by-B.pem", &a_by_b)); | |
| 125 ASSERT_TRUE(ReadTestCert("multi-root-B-by-C.pem", &b_by_c)); | |
| 126 ASSERT_TRUE(ReadTestCert("multi-root-B-by-F.pem", &b_by_f)); | |
| 127 ASSERT_TRUE(ReadTestCert("multi-root-C-by-D.pem", &c_by_d)); | |
| 128 ASSERT_TRUE(ReadTestCert("multi-root-C-by-E.pem", &c_by_e)); | |
| 129 ASSERT_TRUE(ReadTestCert("multi-root-F-by-E.pem", &f_by_e)); | |
| 130 ASSERT_TRUE(ReadTestCert("multi-root-D-by-D.pem", &d_by_d)); | |
| 131 ASSERT_TRUE(ReadTestCert("multi-root-E-by-E.pem", &e_by_e)); | |
| 132 | |
| 133 base::ScopedCFTypeRef<CFDataRef> normalized_name_b = | |
| 134 TrustStoreMac::GetMacNormalizedIssuer(a_by_b); | |
| 135 ASSERT_TRUE(normalized_name_b); | |
| 136 base::ScopedCFTypeRef<CFDataRef> normalized_name_c = | |
| 137 TrustStoreMac::GetMacNormalizedIssuer(b_by_c); | |
| 138 ASSERT_TRUE(normalized_name_c); | |
| 139 base::ScopedCFTypeRef<CFDataRef> normalized_name_f = | |
| 140 TrustStoreMac::GetMacNormalizedIssuer(b_by_f); | |
| 141 ASSERT_TRUE(normalized_name_f); | |
| 142 base::ScopedCFTypeRef<CFDataRef> normalized_name_d = | |
| 143 TrustStoreMac::GetMacNormalizedIssuer(c_by_d); | |
| 144 ASSERT_TRUE(normalized_name_d); | |
| 145 base::ScopedCFTypeRef<CFDataRef> normalized_name_e = | |
| 146 TrustStoreMac::GetMacNormalizedIssuer(f_by_e); | |
| 147 ASSERT_TRUE(normalized_name_e); | |
| 148 | |
| 149 // Test that the matching keychain items are found, even though they aren't | |
| 150 // trusted. | |
| 151 { | |
| 152 base::ScopedCFTypeRef<CFArrayRef> scoped_matching_items = | |
| 153 TrustStoreMac::FindMatchingCertificatesForMacNormalizedSubject( | |
| 154 normalized_name_b.get()); | |
| 155 EXPECT_TRUE(MatchingItemsMatch(scoped_matching_items, {b_by_c, b_by_f})); | |
|
Ryan Sleevi
2017/02/14 19:26:17
It's not clear to me what this segment of things i
mattm
2017/02/16 22:06:09
It independently testing that the SecItemCopyMatch
| |
| 156 } | |
| 157 | |
| 158 { | |
| 159 base::ScopedCFTypeRef<CFArrayRef> scoped_matching_items = | |
| 160 TrustStoreMac::FindMatchingCertificatesForMacNormalizedSubject( | |
| 161 normalized_name_c.get()); | |
| 162 EXPECT_TRUE(MatchingItemsMatch(scoped_matching_items, {c_by_d, c_by_e})); | |
| 163 } | |
| 164 | |
| 165 { | |
| 166 base::ScopedCFTypeRef<CFArrayRef> scoped_matching_items = | |
| 167 TrustStoreMac::FindMatchingCertificatesForMacNormalizedSubject( | |
| 168 normalized_name_f.get()); | |
| 169 EXPECT_TRUE(MatchingItemsMatch(scoped_matching_items, {f_by_e})); | |
| 170 } | |
| 171 | |
| 172 { | |
| 173 base::ScopedCFTypeRef<CFArrayRef> scoped_matching_items = | |
| 174 TrustStoreMac::FindMatchingCertificatesForMacNormalizedSubject( | |
| 175 normalized_name_d.get()); | |
| 176 EXPECT_TRUE(MatchingItemsMatch(scoped_matching_items, {d_by_d})); | |
| 177 } | |
| 178 | |
| 179 { | |
| 180 base::ScopedCFTypeRef<CFArrayRef> scoped_matching_items = | |
| 181 TrustStoreMac::FindMatchingCertificatesForMacNormalizedSubject( | |
| 182 normalized_name_e.get()); | |
| 183 EXPECT_TRUE(MatchingItemsMatch(scoped_matching_items, {e_by_e})); | |
| 184 } | |
| 185 | |
| 186 // None of the certs should return any matching TrustAnchors, since the test | |
| 187 // certs in the keychain aren't trusted (unless someone manually added and | |
| 188 // trusted the test certs on the machine the test is being run on). | |
| 189 for (const auto& cert : | |
| 190 {a_by_b, b_by_c, b_by_f, c_by_d, c_by_e, f_by_e, d_by_d, e_by_e}) { | |
| 191 TrustAnchors matching_anchors; | |
| 192 trust_store.FindTrustAnchorsForCert(cert, &matching_anchors); | |
| 193 EXPECT_EQ(0u, matching_anchors.size()); | |
| 194 } | |
| 195 } | |
| 196 | |
| 197 TEST(TrustStoreMacTest, SystemCerts) { | |
| 198 std::string find_certificate_default_search_list_output; | |
| 199 ASSERT_TRUE( | |
| 200 base::GetAppOutput({"security", "find-certificate", "-a", "-p", "-Z"}, | |
| 201 &find_certificate_default_search_list_output)); | |
| 202 std::string find_certificate_system_roots_output; | |
| 203 ASSERT_TRUE(base::GetAppOutput( | |
| 204 {"security", "find-certificate", "-a", "-p", "-Z", | |
| 205 "/System/Library/Keychains/SystemRootCertificates.keychain"}, | |
| 206 &find_certificate_system_roots_output)); | |
|
Ryan Sleevi
2017/02/14 19:26:17
Document a little more what these calls are doing
mattm
2017/02/16 22:06:09
done.
(It's trust-settings-export that has XML ou
| |
| 207 | |
| 208 TrustStoreMac trust_store(kSecPolicyAppleX509Basic); | |
| 209 | |
| 210 base::ScopedCFTypeRef<SecPolicyRef> sec_policy(SecPolicyCreateBasicX509()); | |
| 211 ASSERT_TRUE(sec_policy); | |
| 212 | |
| 213 int true_pos = 0; | |
| 214 int true_neg = 0; | |
| 215 int false_pos = 0; | |
| 216 int false_neg = 0; | |
| 217 for (const std::string& hash_and_pem : base::SplitStringUsingSubstr( | |
| 218 find_certificate_system_roots_output + | |
| 219 find_certificate_default_search_list_output, | |
| 220 "SHA-1 hash: ", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY)) { | |
| 221 std::string::size_type eol_pos = hash_and_pem.find_first_of("\r\n"); | |
| 222 ASSERT_NE(std::string::npos, eol_pos); | |
| 223 std::string hash_text = hash_and_pem.substr(0, eol_pos); | |
| 224 | |
| 225 SCOPED_TRACE(hash_text); | |
| 226 // TODO(mattm): The same cert might exist in both lists, could de-dupe | |
| 227 // before testing? | |
| 228 | |
| 229 PEMTokenizer pem_tokenizer(hash_and_pem, {kCertificateHeader}); | |
| 230 ASSERT_TRUE(pem_tokenizer.GetNext()); | |
| 231 std::string cert_der(pem_tokenizer.data()); | |
| 232 ASSERT_FALSE(pem_tokenizer.GetNext()); | |
| 233 | |
| 234 CertErrors errors; | |
| 235 // Note: don't actually need to make a ParsedCertificate here, just need | |
| 236 // the der bytes. But parsing it here ensures the test can skip any certs | |
| 237 // that won't be returned due to parsing failures inside TrustStoreMac. | |
| 238 // The parsing options set here need to match the ones used in | |
| 239 // trust_store_mac.cc. | |
| 240 ParseCertificateOptions options; | |
| 241 options.allow_invalid_serial_numbers = true; | |
| 242 scoped_refptr<ParsedCertificate> cert = | |
| 243 ParsedCertificate::Create(cert_der, options, &errors); | |
| 244 if (!cert) { | |
| 245 LOG(WARNING) << "ParseCertificate::Create " << hash_text << " failed:\n" | |
| 246 << errors.ToDebugString(); | |
| 247 continue; | |
| 248 } | |
| 249 | |
| 250 X509Certificate::OSCertHandle cert_handle = | |
| 251 X509Certificate::CreateOSCertHandleFromBytes( | |
| 252 cert->der_cert().AsStringPiece().data(), cert->der_cert().Length()); | |
| 253 if (!cert_handle) { | |
| 254 ADD_FAILURE() << "CreateOSCertHandleFromBytes " << hash_text; | |
| 255 continue; | |
| 256 } | |
| 257 base::ScopedCFTypeRef<SecCertificateRef> scoped_cert_handle(cert_handle); | |
|
Ryan Sleevi
2017/02/14 19:26:17
Why not just force this scoper-wrapper around line
mattm
2017/02/16 22:06:09
Done.
| |
| 258 base::ScopedCFTypeRef<CFDataRef> mac_normalized_subject( | |
| 259 SecCertificateCopyNormalizedSubjectContent(cert_handle, nullptr)); | |
| 260 if (!mac_normalized_subject) { | |
| 261 ADD_FAILURE() << "SecCertificateCopyNormalizedSubjectContent " | |
| 262 << hash_text; | |
| 263 continue; | |
| 264 } | |
| 265 | |
| 266 // Check if this cert is considered a trust anchor by TrustStoreMac. | |
| 267 TrustAnchors trust_anchors; | |
| 268 trust_store.FindTrustAnchorsByMacNormalizedSubject(mac_normalized_subject, | |
| 269 &trust_anchors); | |
| 270 bool is_trust_anchor = false; | |
| 271 for (const auto& anchor : trust_anchors) { | |
| 272 ASSERT_TRUE(anchor->cert()); | |
| 273 if (anchor->cert()->der_cert() == cert->der_cert()) | |
| 274 is_trust_anchor = true; | |
| 275 } | |
| 276 | |
| 277 // Check if this cert is considered a trust anchor by the OS. | |
| 278 base::ScopedCFTypeRef<SecTrustRef> trust; | |
| 279 ASSERT_EQ(noErr, SecTrustCreateWithCertificates(cert_handle, sec_policy, | |
| 280 trust.InitializeInto())); | |
| 281 ASSERT_EQ(noErr, | |
| 282 SecTrustSetOptions(trust, kSecTrustOptionLeafIsCA | | |
| 283 kSecTrustOptionAllowExpiredRoot)); | |
| 284 | |
| 285 SecTrustResultType trust_result; | |
| 286 ASSERT_EQ(noErr, SecTrustEvaluate(trust, &trust_result)); | |
| 287 bool expected_trust_anchor = | |
| 288 ((trust_result == kSecTrustResultProceed) || | |
| 289 (trust_result == kSecTrustResultUnspecified)) && | |
| 290 (SecTrustGetCertificateCount(trust) == 1); | |
| 291 | |
| 292 EXPECT_EQ(expected_trust_anchor, is_trust_anchor); | |
| 293 | |
| 294 if (expected_trust_anchor) { | |
| 295 if (is_trust_anchor) { | |
| 296 true_pos++; | |
| 297 } else { | |
| 298 false_neg++; | |
| 299 } | |
| 300 } else { | |
| 301 if (is_trust_anchor) { | |
| 302 false_pos++; | |
| 303 } else { | |
| 304 true_neg++; | |
| 305 } | |
| 306 } | |
| 307 } | |
| 308 | |
| 309 LOG(INFO) << "true_pos: " << true_pos; | |
| 310 LOG(INFO) << "true_neg: " << true_neg; | |
| 311 LOG(INFO) << "false_pos: " << false_pos; | |
| 312 LOG(INFO) << "false_neg: " << false_neg; | |
| 313 } | |
| 314 | |
| 315 } // namespace net | |
| OLD | NEW |