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 "chromeos/network/onc/onc_certificate_importer_impl.h" | 5 #include "chromeos/network/onc/onc_certificate_importer_impl.h" |
6 | 6 |
7 #include <cert.h> | 7 #include <cert.h> |
| 8 #include <certdb.h> |
| 9 #include <keyhi.h> |
| 10 #include <pk11pub.h> |
8 #include <string> | 11 #include <string> |
9 | 12 |
10 #include "base/bind.h" | 13 #include "base/bind.h" |
11 #include "base/logging.h" | 14 #include "base/logging.h" |
12 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
13 #include "base/test/test_simple_task_runner.h" | 16 #include "base/test/test_simple_task_runner.h" |
14 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
15 #include "base/values.h" | 18 #include "base/values.h" |
16 #include "chromeos/network/certificate_helper.h" | |
17 #include "chromeos/network/onc/onc_test_utils.h" | 19 #include "chromeos/network/onc/onc_test_utils.h" |
18 #include "components/onc/onc_constants.h" | 20 #include "components/onc/onc_constants.h" |
19 #include "crypto/scoped_test_nss_db.h" | 21 #include "crypto/scoped_test_nss_db.h" |
20 #include "net/base/hash_value.h" | 22 #include "net/base/hash_value.h" |
21 #include "net/cert/cert_type.h" | 23 #include "net/cert/cert_type.h" |
22 #include "net/cert/nss_cert_database_chromeos.h" | 24 #include "net/cert/nss_cert_database_chromeos.h" |
23 #include "net/cert/x509_certificate.h" | 25 #include "net/cert/x509_certificate.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
25 | 27 |
26 namespace chromeos { | 28 namespace chromeos { |
27 namespace onc { | 29 namespace onc { |
28 | 30 |
| 31 namespace { |
| 32 |
| 33 #if defined(USE_NSS_CERTS) |
| 34 // In NSS 3.13, CERTDB_VALID_PEER was renamed CERTDB_TERMINAL_RECORD. So we use |
| 35 // the new name of the macro. |
| 36 #if !defined(CERTDB_TERMINAL_RECORD) |
| 37 #define CERTDB_TERMINAL_RECORD CERTDB_VALID_PEER |
| 38 #endif |
| 39 |
| 40 net::CertType GetCertType(net::X509Certificate::OSCertHandle cert) { |
| 41 CERTCertTrust trust = {0}; |
| 42 CERT_GetCertTrust(cert, &trust); |
| 43 |
| 44 unsigned all_flags = trust.sslFlags | trust.emailFlags | |
| 45 trust.objectSigningFlags; |
| 46 |
| 47 if (cert->nickname && (all_flags & CERTDB_USER)) |
| 48 return net::USER_CERT; |
| 49 if ((all_flags & CERTDB_VALID_CA) || CERT_IsCACert(cert, NULL)) |
| 50 return net::CA_CERT; |
| 51 // TODO(mattm): http://crbug.com/128633. |
| 52 if (trust.sslFlags & CERTDB_TERMINAL_RECORD) |
| 53 return net::SERVER_CERT; |
| 54 return net::OTHER_CERT; |
| 55 } |
| 56 #else |
| 57 net::CertType GetCertType(net::X509Certificate::OSCertHandle cert) { |
| 58 NOTIMPLEMENTED(); |
| 59 return net::OTHER_CERT; |
| 60 } |
| 61 #endif // USE_NSS_CERTS |
| 62 |
| 63 } // namespace |
| 64 |
29 class ONCCertificateImporterImplTest : public testing::Test { | 65 class ONCCertificateImporterImplTest : public testing::Test { |
30 public: | 66 public: |
31 ONCCertificateImporterImplTest() {} | 67 ONCCertificateImporterImplTest() {} |
32 ~ONCCertificateImporterImplTest() override {} | 68 ~ONCCertificateImporterImplTest() override {} |
33 | 69 |
34 void SetUp() override { | 70 void SetUp() override { |
35 ASSERT_TRUE(public_nssdb_.is_open()); | 71 ASSERT_TRUE(public_nssdb_.is_open()); |
36 ASSERT_TRUE(private_nssdb_.is_open()); | 72 ASSERT_TRUE(private_nssdb_.is_open()); |
37 | 73 |
38 task_runner_ = new base::TestSimpleTaskRunner(); | 74 task_runner_ = new base::TestSimpleTaskRunner(); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 net::CertType expected_type, | 127 net::CertType expected_type, |
92 std::string* guid) { | 128 std::string* guid) { |
93 std::string guid_temporary; | 129 std::string guid_temporary; |
94 if (!guid) | 130 if (!guid) |
95 guid = &guid_temporary; | 131 guid = &guid_temporary; |
96 | 132 |
97 AddCertificatesFromFile(filename, true); | 133 AddCertificatesFromFile(filename, true); |
98 | 134 |
99 if (expected_type == net::SERVER_CERT || expected_type == net::CA_CERT) { | 135 if (expected_type == net::SERVER_CERT || expected_type == net::CA_CERT) { |
100 ASSERT_EQ(1u, public_list_.size()); | 136 ASSERT_EQ(1u, public_list_.size()); |
101 EXPECT_EQ(expected_type, | 137 EXPECT_EQ(expected_type, GetCertType(public_list_[0]->os_cert_handle())); |
102 certificate::GetCertType(public_list_[0]->os_cert_handle())); | |
103 EXPECT_TRUE(private_list_.empty()); | 138 EXPECT_TRUE(private_list_.empty()); |
104 } else { // net::USER_CERT | 139 } else { // net::USER_CERT |
105 EXPECT_TRUE(public_list_.empty()); | 140 EXPECT_TRUE(public_list_.empty()); |
106 ASSERT_EQ(1u, private_list_.size()); | 141 ASSERT_EQ(1u, private_list_.size()); |
107 EXPECT_EQ(expected_type, | 142 EXPECT_EQ(expected_type, GetCertType(private_list_[0]->os_cert_handle())); |
108 certificate::GetCertType(private_list_[0]->os_cert_handle())); | |
109 } | 143 } |
110 | 144 |
111 base::DictionaryValue* certificate = NULL; | 145 base::DictionaryValue* certificate = NULL; |
112 onc_certificates_->GetDictionary(0, &certificate); | 146 onc_certificates_->GetDictionary(0, &certificate); |
113 certificate->GetStringWithoutPathExpansion(::onc::certificate::kGUID, guid); | 147 certificate->GetStringWithoutPathExpansion(::onc::certificate::kGUID, guid); |
114 } | 148 } |
115 | 149 |
116 // Certificates and the NSSCertDatabase depend on these test DBs. Destroy them | 150 // Certificates and the NSSCertDatabase depend on these test DBs. Destroy them |
117 // last. | 151 // last. |
118 crypto::ScopedTestNSSDB public_nssdb_; | 152 crypto::ScopedTestNSSDB public_nssdb_; |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 "certificate-client-update.onc"), | 355 "certificate-client-update.onc"), |
322 CertParam(net::SERVER_CERT, | 356 CertParam(net::SERVER_CERT, |
323 "certificate-server.onc", | 357 "certificate-server.onc", |
324 "certificate-server-update.onc"), | 358 "certificate-server-update.onc"), |
325 CertParam(net::CA_CERT, | 359 CertParam(net::CA_CERT, |
326 "certificate-web-authority.onc", | 360 "certificate-web-authority.onc", |
327 "certificate-web-authority-update.onc"))); | 361 "certificate-web-authority-update.onc"))); |
328 | 362 |
329 } // namespace onc | 363 } // namespace onc |
330 } // namespace chromeos | 364 } // namespace chromeos |
OLD | NEW |