OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/cert/nss_cert_database_chromeos.h" | 5 #include "net/cert/nss_cert_database_chromeos.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "crypto/nss_util.h" | 10 #include "crypto/nss_util.h" |
11 #include "crypto/nss_util_internal.h" | 11 #include "crypto/nss_util_internal.h" |
| 12 #include "net/base/net_errors.h" |
12 #include "net/base/test_data_directory.h" | 13 #include "net/base/test_data_directory.h" |
13 #include "net/cert/cert_database.h" | 14 #include "net/cert/cert_database.h" |
| 15 #include "net/cert/cert_verify_proc.h" |
| 16 #include "net/cert/cert_verify_proc_chromeos.h" |
| 17 #include "net/cert/cert_verify_result.h" |
14 #include "net/test/cert_test_util.h" | 18 #include "net/test/cert_test_util.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
16 | 20 |
17 namespace net { | 21 namespace net { |
18 | 22 |
19 namespace { | 23 namespace { |
20 | 24 |
21 bool IsCertInCertificateList(const X509Certificate* cert, | 25 bool IsCertInCertificateList(const X509Certificate* cert, |
22 const CertificateList& cert_list) { | 26 const CertificateList& cert_list) { |
23 for (CertificateList::const_iterator it = cert_list.begin(); | 27 for (CertificateList::const_iterator it = cert_list.begin(); |
(...skipping 26 matching lines...) Expand all Loading... |
50 crypto::GetPublicSlotForChromeOSUser(user_1_.username_hash()), | 54 crypto::GetPublicSlotForChromeOSUser(user_1_.username_hash()), |
51 crypto::GetPrivateSlotForChromeOSUser( | 55 crypto::GetPrivateSlotForChromeOSUser( |
52 user_1_.username_hash(), | 56 user_1_.username_hash(), |
53 base::Callback<void(crypto::ScopedPK11Slot)>()))); | 57 base::Callback<void(crypto::ScopedPK11Slot)>()))); |
54 db_2_.reset(new NSSCertDatabaseChromeOS( | 58 db_2_.reset(new NSSCertDatabaseChromeOS( |
55 crypto::GetPublicSlotForChromeOSUser(user_2_.username_hash()), | 59 crypto::GetPublicSlotForChromeOSUser(user_2_.username_hash()), |
56 crypto::GetPrivateSlotForChromeOSUser( | 60 crypto::GetPrivateSlotForChromeOSUser( |
57 user_2_.username_hash(), | 61 user_2_.username_hash(), |
58 base::Callback<void(crypto::ScopedPK11Slot)>()))); | 62 base::Callback<void(crypto::ScopedPK11Slot)>()))); |
59 | 63 |
| 64 verify_proc_default_ = CertVerifyProc::CreateDefault(); |
| 65 verify_proc_1_ = new CertVerifyProcChromeOS( |
| 66 crypto::GetPublicSlotForChromeOSUser(user_1_.username_hash()), |
| 67 crypto::GetPrivateSlotForChromeOSUser( |
| 68 user_1_.username_hash(), |
| 69 base::Callback<void(crypto::ScopedPK11Slot)>())); |
| 70 verify_proc_2_ = new CertVerifyProcChromeOS( |
| 71 crypto::GetPublicSlotForChromeOSUser(user_2_.username_hash()), |
| 72 crypto::GetPrivateSlotForChromeOSUser( |
| 73 user_2_.username_hash(), |
| 74 base::Callback<void(crypto::ScopedPK11Slot)>())); |
| 75 |
60 // Add observer to CertDatabase for checking that notifications from | 76 // Add observer to CertDatabase for checking that notifications from |
61 // NSSCertDatabaseChromeOS are proxied to the CertDatabase. | 77 // NSSCertDatabaseChromeOS are proxied to the CertDatabase. |
62 CertDatabase::GetInstance()->AddObserver(this); | 78 CertDatabase::GetInstance()->AddObserver(this); |
63 observer_added_ = true; | 79 observer_added_ = true; |
64 } | 80 } |
65 | 81 |
66 virtual void TearDown() OVERRIDE { | 82 virtual void TearDown() OVERRIDE { |
67 if (observer_added_) | 83 if (observer_added_) |
68 CertDatabase::GetInstance()->RemoveObserver(this); | 84 CertDatabase::GetInstance()->RemoveObserver(this); |
69 } | 85 } |
70 | 86 |
71 // CertDatabase::Observer: | 87 // CertDatabase::Observer: |
72 virtual void OnCertAdded(const X509Certificate* cert) OVERRIDE { | 88 virtual void OnCertAdded(const X509Certificate* cert) OVERRIDE { |
73 added_.push_back(cert ? cert->os_cert_handle() : NULL); | 89 added_.push_back(cert ? cert->os_cert_handle() : NULL); |
74 } | 90 } |
75 | 91 |
76 virtual void OnCertRemoved(const X509Certificate* cert) OVERRIDE {} | 92 virtual void OnCertRemoved(const X509Certificate* cert) OVERRIDE {} |
77 | 93 |
78 virtual void OnCACertChanged(const X509Certificate* cert) OVERRIDE { | 94 virtual void OnCACertChanged(const X509Certificate* cert) OVERRIDE { |
79 added_ca_.push_back(cert ? cert->os_cert_handle() : NULL); | 95 added_ca_.push_back(cert ? cert->os_cert_handle() : NULL); |
80 } | 96 } |
81 | 97 |
| 98 int Verify(CertVerifyProc* verify_proc, |
| 99 X509Certificate* cert, |
| 100 const std::string& hostname) { |
| 101 int flags = 0; |
| 102 CertVerifyResult verify_result; |
| 103 CertificateList additional_trust_anchors; |
| 104 int error = verify_proc->Verify(cert, |
| 105 hostname, |
| 106 flags, |
| 107 NULL, |
| 108 additional_trust_anchors, |
| 109 &verify_result); |
| 110 return error; |
| 111 } |
| 112 |
82 protected: | 113 protected: |
83 bool observer_added_; | 114 bool observer_added_; |
84 // Certificates that were passed to the CertDatabase observers. | 115 // Certificates that were passed to the CertDatabase observers. |
85 std::vector<CERTCertificate*> added_ca_; | 116 std::vector<CERTCertificate*> added_ca_; |
86 std::vector<CERTCertificate*> added_; | 117 std::vector<CERTCertificate*> added_; |
87 | 118 |
88 crypto::ScopedTestNSSChromeOSUser user_1_; | 119 crypto::ScopedTestNSSChromeOSUser user_1_; |
89 crypto::ScopedTestNSSChromeOSUser user_2_; | 120 crypto::ScopedTestNSSChromeOSUser user_2_; |
90 scoped_ptr<NSSCertDatabaseChromeOS> db_1_; | 121 scoped_ptr<NSSCertDatabaseChromeOS> db_1_; |
91 scoped_ptr<NSSCertDatabaseChromeOS> db_2_; | 122 scoped_ptr<NSSCertDatabaseChromeOS> db_2_; |
| 123 scoped_refptr<CertVerifyProc> verify_proc_default_; |
| 124 scoped_refptr<CertVerifyProc> verify_proc_1_; |
| 125 scoped_refptr<CertVerifyProc> verify_proc_2_; |
92 }; | 126 }; |
93 | 127 |
94 // Test that ListModules() on each user includes that user's NSS software slot, | 128 // Test that ListModules() on each user includes that user's NSS software slot, |
95 // and does not include the software slot of the other user. (Does not check the | 129 // and does not include the software slot of the other user. (Does not check the |
96 // private slot, since it is the same as the public slot in tests.) | 130 // private slot, since it is the same as the public slot in tests.) |
97 TEST_F(NSSCertDatabaseChromeOSTest, ListModules) { | 131 TEST_F(NSSCertDatabaseChromeOSTest, ListModules) { |
98 CryptoModuleList modules_1; | 132 CryptoModuleList modules_1; |
99 CryptoModuleList modules_2; | 133 CryptoModuleList modules_2; |
100 | 134 |
101 db_1_->ListModules(&modules_1, false /* need_rw */); | 135 db_1_->ListModules(&modules_1, false /* need_rw */); |
(...skipping 24 matching lines...) Expand all Loading... |
126 TEST_F(NSSCertDatabaseChromeOSTest, ImportCACerts) { | 160 TEST_F(NSSCertDatabaseChromeOSTest, ImportCACerts) { |
127 // Load test certs from disk. | 161 // Load test certs from disk. |
128 CertificateList certs_1 = | 162 CertificateList certs_1 = |
129 CreateCertificateListFromFile(GetTestCertsDirectory(), | 163 CreateCertificateListFromFile(GetTestCertsDirectory(), |
130 "root_ca_cert.pem", | 164 "root_ca_cert.pem", |
131 X509Certificate::FORMAT_AUTO); | 165 X509Certificate::FORMAT_AUTO); |
132 ASSERT_EQ(1U, certs_1.size()); | 166 ASSERT_EQ(1U, certs_1.size()); |
133 | 167 |
134 CertificateList certs_2 = | 168 CertificateList certs_2 = |
135 CreateCertificateListFromFile(GetTestCertsDirectory(), | 169 CreateCertificateListFromFile(GetTestCertsDirectory(), |
136 "2048-rsa-root.pem", | 170 "2048-rsa-intermediate.pem", |
137 X509Certificate::FORMAT_AUTO); | 171 X509Certificate::FORMAT_AUTO); |
138 ASSERT_EQ(1U, certs_2.size()); | 172 ASSERT_EQ(1U, certs_2.size()); |
139 | 173 |
140 // Import one cert for each user. | 174 // Import one cert for each user. |
141 NSSCertDatabase::ImportCertFailureList failed; | 175 NSSCertDatabase::ImportCertFailureList failed; |
142 EXPECT_TRUE( | 176 EXPECT_TRUE( |
143 db_1_->ImportCACerts(certs_1, NSSCertDatabase::TRUSTED_SSL, &failed)); | 177 db_1_->ImportCACerts(certs_1, NSSCertDatabase::TRUSTED_SSL, &failed)); |
144 EXPECT_EQ(0U, failed.size()); | 178 EXPECT_EQ(0U, failed.size()); |
145 failed.clear(); | 179 failed.clear(); |
146 EXPECT_TRUE( | 180 EXPECT_TRUE( |
147 db_2_->ImportCACerts(certs_2, NSSCertDatabase::TRUSTED_SSL, &failed)); | 181 db_2_->ImportCACerts(certs_2, NSSCertDatabase::TRUSTED_SSL, &failed)); |
148 EXPECT_EQ(0U, failed.size()); | 182 EXPECT_EQ(0U, failed.size()); |
149 | 183 |
150 // Get cert list for each user. | 184 // Get cert list for each user. |
151 CertificateList user_1_certlist; | 185 CertificateList user_1_certlist; |
152 CertificateList user_2_certlist; | 186 CertificateList user_2_certlist; |
153 db_1_->ListCerts(&user_1_certlist); | 187 db_1_->ListCerts(&user_1_certlist); |
154 db_2_->ListCerts(&user_2_certlist); | 188 db_2_->ListCerts(&user_2_certlist); |
155 | 189 |
156 // Check that the imported certs only shows up in the list for the user that | 190 // Check that the imported certs only shows up in the list for the user that |
157 // imported them. | 191 // imported them. |
158 EXPECT_TRUE(IsCertInCertificateList(certs_1[0], user_1_certlist)); | 192 EXPECT_TRUE(IsCertInCertificateList(certs_1[0], user_1_certlist)); |
159 EXPECT_FALSE(IsCertInCertificateList(certs_1[0], user_2_certlist)); | 193 EXPECT_FALSE(IsCertInCertificateList(certs_1[0], user_2_certlist)); |
160 | 194 |
161 EXPECT_TRUE(IsCertInCertificateList(certs_2[0], user_2_certlist)); | 195 EXPECT_TRUE(IsCertInCertificateList(certs_2[0], user_2_certlist)); |
162 EXPECT_FALSE(IsCertInCertificateList(certs_2[0], user_1_certlist)); | 196 EXPECT_FALSE(IsCertInCertificateList(certs_2[0], user_1_certlist)); |
163 | 197 |
| 198 // Load matching server certs for testing trust. |
| 199 CertificateList server_cert_1 = CreateCertificateListFromFile( |
| 200 GetTestCertsDirectory(), "ok_cert.pem", |
| 201 X509Certificate::FORMAT_AUTO); |
| 202 ASSERT_EQ(1U, server_cert_1.size()); |
| 203 CertificateList server_cert_2 = CreateCertificateListFromFile( |
| 204 GetTestCertsDirectory(), "2048-rsa-ee-by-2048-rsa-intermediate.pem", |
| 205 X509Certificate::FORMAT_AUTO); |
| 206 ASSERT_EQ(1U, server_cert_1.size()); |
| 207 |
| 208 // Imported CA certs are not trusted by default verifier. |
| 209 EXPECT_EQ( |
| 210 ERR_CERT_REVOKED, |
| 211 Verify(verify_proc_default_.get(), server_cert_1[0].get(), "127.0.0.1")); |
| 212 EXPECT_EQ( |
| 213 ERR_CERT_REVOKED, |
| 214 Verify(verify_proc_default_.get(), server_cert_2[0].get(), "127.0.0.1")); |
| 215 |
| 216 // Trust applies only to the user that imported the CA. |
| 217 EXPECT_EQ(OK, |
| 218 Verify(verify_proc_1_.get(), server_cert_1[0].get(), "127.0.0.1")); |
| 219 EXPECT_EQ(ERR_CERT_REVOKED, |
| 220 Verify(verify_proc_1_.get(), server_cert_2[0].get(), "127.0.0.1")); |
| 221 |
| 222 EXPECT_EQ(ERR_CERT_REVOKED, |
| 223 Verify(verify_proc_2_.get(), server_cert_1[0].get(), "127.0.0.1")); |
| 224 EXPECT_EQ(OK, |
| 225 Verify(verify_proc_2_.get(), server_cert_2[0].get(), "127.0.0.1")); |
| 226 |
164 // Run the message loop so the observer notifications get processed. | 227 // Run the message loop so the observer notifications get processed. |
165 base::RunLoop().RunUntilIdle(); | 228 base::RunLoop().RunUntilIdle(); |
166 // Should have gotten two OnCACertChanged notifications. | 229 // Should have gotten two OnCACertChanged notifications. |
167 ASSERT_EQ(2U, added_ca_.size()); | 230 ASSERT_EQ(2U, added_ca_.size()); |
168 // TODO(mattm): make NSSCertDatabase actually pass the cert to the callback, | 231 // TODO(mattm): make NSSCertDatabase actually pass the cert to the callback, |
169 // and enable these checks: | 232 // and enable these checks: |
170 // EXPECT_EQ(certs_1[0]->os_cert_handle(), added_ca_[0]); | 233 // EXPECT_EQ(certs_1[0]->os_cert_handle(), added_ca_[0]); |
171 // EXPECT_EQ(certs_2[0]->os_cert_handle(), added_ca_[1]); | 234 // EXPECT_EQ(certs_2[0]->os_cert_handle(), added_ca_[1]); |
172 EXPECT_EQ(0U, added_.size()); | 235 EXPECT_EQ(0U, added_.size()); |
173 } | 236 } |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 | 276 |
214 // Run the message loop so the observer notifications get processed. | 277 // Run the message loop so the observer notifications get processed. |
215 base::RunLoop().RunUntilIdle(); | 278 base::RunLoop().RunUntilIdle(); |
216 // TODO(mattm): ImportServerCert doesn't actually cause any observers to | 279 // TODO(mattm): ImportServerCert doesn't actually cause any observers to |
217 // fire. Is that correct? | 280 // fire. Is that correct? |
218 EXPECT_EQ(0U, added_ca_.size()); | 281 EXPECT_EQ(0U, added_ca_.size()); |
219 EXPECT_EQ(0U, added_.size()); | 282 EXPECT_EQ(0U, added_.size()); |
220 } | 283 } |
221 | 284 |
222 } // namespace net | 285 } // namespace net |
OLD | NEW |