Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(311)

Side by Side Diff: net/cert/internal/trust_store_mac_unittest.cc

Issue 2585963003: PKI library Mac trust store integration (Closed)
Patch Set: test fixes Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 }
37
38 ::testing::AssertionResult ReadTestCert(
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);
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(
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 }
100
101 } // namespace
102
103 TEST(TrustStoreMacTest, MultiRootNotTrusted) {
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"));
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);
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}));
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));
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 // XXX don't actually need to make a ParsedCertificate here, just need the
236 // der bytes.
237 // XXX NOTE: any parsing options set here also need to be set in
238 // trust_store_mac.cc.
239 ParseCertificateOptions options;
240 // for ...:
241 // Allow duplicate extensions = true;
242 //
243 // for D3EEFBCBBCF49867838626E23BB59CA01E305DB7:
244 options.allow_invalid_serial_numbers = true;
245 scoped_refptr<ParsedCertificate> cert =
246 ParsedCertificate::Create(cert_der, options, &errors);
247 if (!cert) {
248 LOG(WARNING) << "ParseCertificate::Create " << hash_text << " failed:\n"
249 << errors.ToDebugString();
250 continue;
251 }
252
253 X509Certificate::OSCertHandle cert_handle =
254 X509Certificate::CreateOSCertHandleFromBytes(
255 cert->der_cert().AsStringPiece().data(), cert->der_cert().Length());
256 if (!cert_handle) {
257 ADD_FAILURE() << "CreateOSCertHandleFromBytes " << hash_text;
258 continue;
259 }
260 base::ScopedCFTypeRef<SecCertificateRef> scoped_cert_handle(cert_handle);
261 base::ScopedCFTypeRef<CFDataRef> mac_normalized_subject(
262 SecCertificateCopyNormalizedSubjectContent(cert_handle, nullptr));
263 if (!mac_normalized_subject) {
264 ADD_FAILURE() << "SecCertificateCopyNormalizedSubjectContent "
265 << hash_text;
266 continue;
267 }
268
269 // Check if this cert is considered a trust anchor by TrustStoreMac.
270 TrustAnchors trust_anchors;
271 trust_store.FindTrustAnchorsByMacNormalizedSubject(mac_normalized_subject,
272 &trust_anchors);
273 bool is_trust_anchor = false;
274 for (const auto& anchor : trust_anchors) {
275 // TODO(mattm): allow null cert() here.
276 ASSERT_TRUE(anchor->cert());
277 if (anchor->cert()->der_cert() == cert->der_cert())
278 is_trust_anchor = true;
279 }
280
281 // Check if this cert is considered a trust anchor by the OS.
282 base::ScopedCFTypeRef<SecTrustRef> trust;
283 ASSERT_EQ(noErr, SecTrustCreateWithCertificates(cert_handle, sec_policy,
284 trust.InitializeInto()));
285 ASSERT_EQ(noErr,
286 SecTrustSetOptions(trust, kSecTrustOptionLeafIsCA |
287 kSecTrustOptionAllowExpiredRoot));
288
289 SecTrustResultType trust_result;
290 ASSERT_EQ(noErr, SecTrustEvaluate(trust, &trust_result));
291 bool expected_trust_anchor =
292 ((trust_result == kSecTrustResultProceed) ||
293 (trust_result == kSecTrustResultUnspecified)) &&
294 (SecTrustGetCertificateCount(trust) == 1);
295
296 EXPECT_EQ(expected_trust_anchor, is_trust_anchor);
297
298 if (expected_trust_anchor) {
299 if (is_trust_anchor) {
300 true_pos++;
301 } else {
302 false_neg++;
303 }
304 } else {
305 if (is_trust_anchor) {
306 false_pos++;
307 } else {
308 true_neg++;
309 }
310 }
311 }
312
313 LOG(INFO) << "true_pos: " << true_pos;
314 LOG(INFO) << "true_neg: " << true_neg;
315 LOG(INFO) << "false_pos: " << false_pos;
316 LOG(INFO) << "false_neg: " << false_neg;
317 }
318
319 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698