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> | |
11 #include <string> | 8 #include <string> |
12 | 9 |
13 #include "base/bind.h" | 10 #include "base/bind.h" |
14 #include "base/logging.h" | 11 #include "base/logging.h" |
15 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
16 #include "base/test/test_simple_task_runner.h" | 13 #include "base/test/test_simple_task_runner.h" |
17 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
18 #include "base/values.h" | 15 #include "base/values.h" |
| 16 #include "chromeos/network/certificate_helper.h" |
19 #include "chromeos/network/onc/onc_test_utils.h" | 17 #include "chromeos/network/onc/onc_test_utils.h" |
20 #include "components/onc/onc_constants.h" | 18 #include "components/onc/onc_constants.h" |
21 #include "crypto/scoped_test_nss_db.h" | 19 #include "crypto/scoped_test_nss_db.h" |
22 #include "net/base/hash_value.h" | 20 #include "net/base/hash_value.h" |
23 #include "net/cert/cert_type.h" | 21 #include "net/cert/cert_type.h" |
24 #include "net/cert/nss_cert_database_chromeos.h" | 22 #include "net/cert/nss_cert_database_chromeos.h" |
25 #include "net/cert/x509_certificate.h" | 23 #include "net/cert/x509_certificate.h" |
26 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
27 | 25 |
28 namespace chromeos { | 26 namespace chromeos { |
29 namespace onc { | 27 namespace onc { |
30 | 28 |
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 | |
65 class ONCCertificateImporterImplTest : public testing::Test { | 29 class ONCCertificateImporterImplTest : public testing::Test { |
66 public: | 30 public: |
67 ONCCertificateImporterImplTest() {} | 31 ONCCertificateImporterImplTest() {} |
68 ~ONCCertificateImporterImplTest() override {} | 32 ~ONCCertificateImporterImplTest() override {} |
69 | 33 |
70 void SetUp() override { | 34 void SetUp() override { |
71 ASSERT_TRUE(public_nssdb_.is_open()); | 35 ASSERT_TRUE(public_nssdb_.is_open()); |
72 ASSERT_TRUE(private_nssdb_.is_open()); | 36 ASSERT_TRUE(private_nssdb_.is_open()); |
73 | 37 |
74 task_runner_ = new base::TestSimpleTaskRunner(); | 38 task_runner_ = new base::TestSimpleTaskRunner(); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 net::CertType expected_type, | 91 net::CertType expected_type, |
128 std::string* guid) { | 92 std::string* guid) { |
129 std::string guid_temporary; | 93 std::string guid_temporary; |
130 if (!guid) | 94 if (!guid) |
131 guid = &guid_temporary; | 95 guid = &guid_temporary; |
132 | 96 |
133 AddCertificatesFromFile(filename, true); | 97 AddCertificatesFromFile(filename, true); |
134 | 98 |
135 if (expected_type == net::SERVER_CERT || expected_type == net::CA_CERT) { | 99 if (expected_type == net::SERVER_CERT || expected_type == net::CA_CERT) { |
136 ASSERT_EQ(1u, public_list_.size()); | 100 ASSERT_EQ(1u, public_list_.size()); |
137 EXPECT_EQ(expected_type, GetCertType(public_list_[0]->os_cert_handle())); | 101 EXPECT_EQ(expected_type, |
| 102 certificate::GetCertType(public_list_[0]->os_cert_handle())); |
138 EXPECT_TRUE(private_list_.empty()); | 103 EXPECT_TRUE(private_list_.empty()); |
139 } else { // net::USER_CERT | 104 } else { // net::USER_CERT |
140 EXPECT_TRUE(public_list_.empty()); | 105 EXPECT_TRUE(public_list_.empty()); |
141 ASSERT_EQ(1u, private_list_.size()); | 106 ASSERT_EQ(1u, private_list_.size()); |
142 EXPECT_EQ(expected_type, GetCertType(private_list_[0]->os_cert_handle())); | 107 EXPECT_EQ(expected_type, |
| 108 certificate::GetCertType(private_list_[0]->os_cert_handle())); |
143 } | 109 } |
144 | 110 |
145 base::DictionaryValue* certificate = NULL; | 111 base::DictionaryValue* certificate = NULL; |
146 onc_certificates_->GetDictionary(0, &certificate); | 112 onc_certificates_->GetDictionary(0, &certificate); |
147 certificate->GetStringWithoutPathExpansion(::onc::certificate::kGUID, guid); | 113 certificate->GetStringWithoutPathExpansion(::onc::certificate::kGUID, guid); |
148 } | 114 } |
149 | 115 |
150 // Certificates and the NSSCertDatabase depend on these test DBs. Destroy them | 116 // Certificates and the NSSCertDatabase depend on these test DBs. Destroy them |
151 // last. | 117 // last. |
152 crypto::ScopedTestNSSDB public_nssdb_; | 118 crypto::ScopedTestNSSDB public_nssdb_; |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 "certificate-client-update.onc"), | 321 "certificate-client-update.onc"), |
356 CertParam(net::SERVER_CERT, | 322 CertParam(net::SERVER_CERT, |
357 "certificate-server.onc", | 323 "certificate-server.onc", |
358 "certificate-server-update.onc"), | 324 "certificate-server-update.onc"), |
359 CertParam(net::CA_CERT, | 325 CertParam(net::CA_CERT, |
360 "certificate-web-authority.onc", | 326 "certificate-web-authority.onc", |
361 "certificate-web-authority-update.onc"))); | 327 "certificate-web-authority-update.onc"))); |
362 | 328 |
363 } // namespace onc | 329 } // namespace onc |
364 } // namespace chromeos | 330 } // namespace chromeos |
OLD | NEW |