Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/chromeos/network_settings/onc_certificate_importer.h" | 5 #include "chromeos/network/onc/onc_certificate_importer.h" |
| 6 | 6 |
| 7 #include <cert.h> | 7 #include <cert.h> |
| 8 #include <certdb.h> | |
| 8 #include <keyhi.h> | 9 #include <keyhi.h> |
| 9 #include <pk11pub.h> | 10 #include <pk11pub.h> |
| 10 #include <string> | 11 #include <string> |
| 11 | 12 |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 13 #include "base/string_number_conversions.h" | 14 #include "base/string_number_conversions.h" |
| 14 #include "base/values.h" | 15 #include "base/values.h" |
| 15 #include "chrome/browser/chromeos/cros/onc_constants.h" | 16 #include "chromeos/network/onc/onc_constants.h" |
| 16 #include "chrome/browser/chromeos/network_settings/onc_test_utils.h" | 17 #include "chromeos/network/onc/onc_test_utils.h" |
| 17 #include "chrome/common/net/x509_certificate_model.h" | |
| 18 #include "crypto/nss_util.h" | 18 #include "crypto/nss_util.h" |
| 19 #include "net/base/cert_type.h" | 19 #include "net/base/cert_type.h" |
| 20 #include "net/base/crypto_module.h" | 20 #include "net/base/crypto_module.h" |
| 21 #include "net/base/nss_cert_database.h" | 21 #include "net/base/nss_cert_database.h" |
| 22 #include "net/base/x509_certificate.h" | 22 #include "net/base/x509_certificate.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 24 |
| 25 namespace { | |
| 26 | |
| 27 net::CertType GetCertType(const net::X509Certificate* cert) { | |
| 28 DCHECK(cert); | |
| 29 return x509_certificate_model::GetType(cert->os_cert_handle()); | |
| 30 } | |
| 31 | |
| 32 } // namespace | |
| 33 | |
| 34 namespace chromeos { | 25 namespace chromeos { |
| 35 namespace onc { | 26 namespace onc { |
| 36 | 27 |
| 28 #if defined(USE_NSS) | |
| 29 net::CertType GetCertType(net::X509Certificate::OSCertHandle cert) { | |
|
pneubeck (no reviews)
2012/12/10 09:33:10
please add a comment where this code comes from.
m
| |
| 30 CERTCertTrust trust = {0}; | |
| 31 CERT_GetCertTrust(cert, &trust); | |
| 32 | |
| 33 unsigned all_flags = trust.sslFlags | trust.emailFlags | | |
| 34 trust.objectSigningFlags; | |
| 35 | |
| 36 if (cert->nickname && (all_flags & CERTDB_USER)) | |
| 37 return net::USER_CERT; | |
| 38 if ((all_flags & CERTDB_VALID_CA) || CERT_IsCACert(cert, NULL)) | |
| 39 return net::CA_CERT; | |
| 40 // TODO(mattm): http://crbug.com/128633. | |
| 41 if (trust.sslFlags & CERTDB_TERMINAL_RECORD) | |
| 42 return net::SERVER_CERT; | |
| 43 return net::UNKNOWN_CERT; | |
| 44 } | |
| 45 #else | |
| 46 net::CertType GetCertType(net::X509Certificate::OSCertHandle cert) { | |
| 47 NOTIMPLEMENTED(); | |
| 48 return net::UNKNOWN_CERT; | |
| 49 } | |
| 50 #endif // USE_NSS | |
| 51 | |
| 37 class ONCCertificateImporterTest : public testing::Test { | 52 class ONCCertificateImporterTest : public testing::Test { |
| 38 public: | 53 public: |
| 39 virtual void SetUp() { | 54 virtual void SetUp() { |
| 40 ASSERT_TRUE(test_nssdb_.is_open()); | 55 ASSERT_TRUE(test_nssdb_.is_open()); |
| 41 | 56 |
| 42 slot_ = net::NSSCertDatabase::GetInstance()->GetPublicModule(); | 57 slot_ = net::NSSCertDatabase::GetInstance()->GetPublicModule(); |
| 43 | 58 |
| 44 // Don't run the test if the setup failed. | 59 // Don't run the test if the setup failed. |
| 45 ASSERT_TRUE(slot_->os_module_handle()); | 60 ASSERT_TRUE(slot_->os_module_handle()); |
| 46 | 61 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 61 std::string* guid) { | 76 std::string* guid) { |
| 62 scoped_ptr<base::DictionaryValue> onc = | 77 scoped_ptr<base::DictionaryValue> onc = |
| 63 test_utils::ReadTestDictionary(filename); | 78 test_utils::ReadTestDictionary(filename); |
| 64 base::ListValue* certificates; | 79 base::ListValue* certificates; |
| 65 onc->GetListWithoutPathExpansion(kCertificates, &certificates); | 80 onc->GetListWithoutPathExpansion(kCertificates, &certificates); |
| 66 | 81 |
| 67 base::DictionaryValue* certificate; | 82 base::DictionaryValue* certificate; |
| 68 certificates->GetDictionary(0, &certificate); | 83 certificates->GetDictionary(0, &certificate); |
| 69 certificate->GetStringWithoutPathExpansion(kGUID, guid); | 84 certificate->GetStringWithoutPathExpansion(kGUID, guid); |
| 70 | 85 |
| 71 CertificateImporter importer(NetworkUIData::ONC_SOURCE_USER_IMPORT, | 86 CertificateImporter importer(ONC_SOURCE_USER_IMPORT, |
| 72 false /* don't allow webtrust */); | 87 false /* don't allow web trust */); |
| 73 std::string error; | 88 EXPECT_EQ(CertificateImporter::IMPORT_OK, |
| 74 EXPECT_TRUE(importer.ParseAndStoreCertificates(*certificates, &error)); | 89 importer.ParseAndStoreCertificates(*certificates)); |
| 75 EXPECT_TRUE(error.empty()); | |
| 76 | 90 |
| 77 net::CertificateList result_list; | 91 net::CertificateList result_list; |
| 78 CertificateImporter::ListCertsWithNickname(*guid, &result_list); | 92 CertificateImporter::ListCertsWithNickname(*guid, &result_list); |
| 79 ASSERT_EQ(1ul, result_list.size()); | 93 ASSERT_EQ(1ul, result_list.size()); |
| 80 EXPECT_EQ(expected_type, GetCertType(result_list[0].get())); | 94 EXPECT_EQ(expected_type, GetCertType(result_list[0]->os_cert_handle())); |
| 81 } | 95 } |
| 82 | 96 |
| 83 scoped_refptr<net::CryptoModule> slot_; | 97 scoped_refptr<net::CryptoModule> slot_; |
| 84 | 98 |
| 85 private: | 99 private: |
| 86 net::CertificateList ListCertsInSlot(PK11SlotInfo* slot) { | 100 net::CertificateList ListCertsInSlot(PK11SlotInfo* slot) { |
| 87 net::CertificateList result; | 101 net::CertificateList result; |
| 88 CERTCertList* cert_list = PK11_ListCertsInSlot(slot); | 102 CERTCertList* cert_list = PK11_ListCertsInSlot(slot); |
| 89 for (CERTCertListNode* node = CERT_LIST_HEAD(cert_list); | 103 for (CERTCertListNode* node = CERT_LIST_HEAD(cert_list); |
| 90 !CERT_LIST_END(node, cert_list); | 104 !CERT_LIST_END(node, cert_list); |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 232 std::make_pair(net::SERVER_CERT, | 246 std::make_pair(net::SERVER_CERT, |
| 233 std::make_pair("certificate-server.onc", | 247 std::make_pair("certificate-server.onc", |
| 234 "certificate-server-update.onc")), | 248 "certificate-server-update.onc")), |
| 235 std::make_pair( | 249 std::make_pair( |
| 236 net::CA_CERT, | 250 net::CA_CERT, |
| 237 std::make_pair("certificate-web-authority.onc", | 251 std::make_pair("certificate-web-authority.onc", |
| 238 "certificate-web-authority-update.onc")))); | 252 "certificate-web-authority-update.onc")))); |
| 239 | 253 |
| 240 } // namespace onc | 254 } // namespace onc |
| 241 } // namespace chromeos | 255 } // namespace chromeos |
| OLD | NEW |