| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <cert.h> | 5 #include <cert.h> |
| 6 #include <pk11pub.h> | 6 #include <pk11pub.h> |
| 7 | 7 |
| 8 #include "base/crypto/scoped_nss_types.h" |
| 8 #include "base/file_path.h" | 9 #include "base/file_path.h" |
| 9 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 10 #include "base/nss_util.h" | 11 #include "base/nss_util.h" |
| 11 #include "base/nss_util_internal.h" | 12 #include "base/nss_util_internal.h" |
| 12 #include "base/path_service.h" | 13 #include "base/path_service.h" |
| 13 #include "base/scoped_temp_dir.h" | 14 #include "base/scoped_temp_dir.h" |
| 14 #include "base/string_util.h" | 15 #include "base/string_util.h" |
| 15 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
| 16 #include "net/base/cert_database.h" | 17 #include "net/base/cert_database.h" |
| 17 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
| 18 #include "net/base/x509_certificate.h" | 19 #include "net/base/x509_certificate.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 21 |
| 21 namespace net { | 22 namespace net { |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 // Returns a FilePath object representing the src/net/data/ssl/certificates | 26 // Returns a FilePath object representing the src/net/data/ssl/certificates |
| 26 // directory in the source tree. | 27 // directory in the source tree. |
| 27 FilePath GetTestCertsDirectory() { | 28 FilePath GetTestCertsDirectory() { |
| 28 FilePath certs_dir; | 29 FilePath certs_dir; |
| 29 PathService::Get(base::DIR_SOURCE_ROOT, &certs_dir); | 30 PathService::Get(base::DIR_SOURCE_ROOT, &certs_dir); |
| 30 certs_dir = certs_dir.AppendASCII("net"); | 31 certs_dir = certs_dir.AppendASCII("net"); |
| 31 certs_dir = certs_dir.AppendASCII("data"); | 32 certs_dir = certs_dir.AppendASCII("data"); |
| 32 certs_dir = certs_dir.AppendASCII("ssl"); | 33 certs_dir = certs_dir.AppendASCII("ssl"); |
| 33 certs_dir = certs_dir.AppendASCII("certificates"); | 34 certs_dir = certs_dir.AppendASCII("certificates"); |
| 34 return certs_dir; | 35 return certs_dir; |
| 35 } | 36 } |
| 36 | 37 |
| 38 CertificateList ListCertsInSlot(PK11SlotInfo* slot) { |
| 39 CertificateList result; |
| 40 CERTCertList* cert_list = PK11_ListCertsInSlot(slot); |
| 41 for (CERTCertListNode* node = CERT_LIST_HEAD(cert_list); |
| 42 !CERT_LIST_END(node, cert_list); |
| 43 node = CERT_LIST_NEXT(node)) { |
| 44 result.push_back( |
| 45 X509Certificate::CreateFromHandle( |
| 46 node->cert, |
| 47 X509Certificate::SOURCE_LONE_CERT_IMPORT, |
| 48 X509Certificate::OSCertHandles())); |
| 49 } |
| 50 CERT_DestroyCertList(cert_list); |
| 51 return result; |
| 52 } |
| 53 |
| 54 std::string ReadTestFile(const std::string& name) { |
| 55 std::string result; |
| 56 FilePath cert_path = GetTestCertsDirectory().AppendASCII(name); |
| 57 EXPECT_TRUE(file_util::ReadFileToString(cert_path, &result)); |
| 58 return result; |
| 59 } |
| 60 |
| 37 } // namespace | 61 } // namespace |
| 38 | 62 |
| 39 | |
| 40 class CertDatabaseNSSTest : public testing::Test { | 63 class CertDatabaseNSSTest : public testing::Test { |
| 41 public: | 64 public: |
| 42 virtual void SetUp() { | 65 virtual void SetUp() { |
| 43 ASSERT_TRUE(temp_db_dir_.CreateUniqueTempDir()); | 66 ASSERT_TRUE(temp_db_dir_.CreateUniqueTempDir()); |
| 44 ASSERT_TRUE( | 67 ASSERT_TRUE( |
| 45 base::OpenTestNSSDB(temp_db_dir_.path(), "CertDatabaseNSSTest db")); | 68 base::OpenTestNSSDB(temp_db_dir_.path(), "CertDatabaseNSSTest db")); |
| 69 slot_.reset(base::GetDefaultNSSKeySlot()); |
| 70 |
| 71 // Test db should be empty at start of test. |
| 72 EXPECT_EQ(0U, ListCertsInSlot(slot_.get()).size()); |
| 46 } | 73 } |
| 47 virtual void TearDown() { | 74 virtual void TearDown() { |
| 48 base::CloseTestNSSDB(); | 75 base::CloseTestNSSDB(); |
| 49 } | 76 } |
| 77 |
| 78 protected: |
| 79 base::ScopedPK11Slot slot_; |
| 80 CertDatabase cert_db_; |
| 81 |
| 50 private: | 82 private: |
| 51 ScopedTempDir temp_db_dir_; | 83 ScopedTempDir temp_db_dir_; |
| 52 }; | 84 }; |
| 53 | 85 |
| 54 TEST_F(CertDatabaseNSSTest, ImportFromPKCS12WrongPassword) { | 86 TEST_F(CertDatabaseNSSTest, ImportFromPKCS12WrongPassword) { |
| 55 PK11SlotInfo* slot = base::GetDefaultNSSKeySlot(); | 87 std::string pkcs12_data = ReadTestFile("client.p12"); |
| 56 CertDatabase cert_db; | |
| 57 | 88 |
| 58 CERTCertList* cert_list = PK11_ListCertsInSlot(slot); | 89 EXPECT_EQ(ERR_PKCS12_IMPORT_BAD_PASSWORD, |
| 59 // Test db should be empty at start of test. | 90 cert_db_.ImportFromPKCS12(pkcs12_data, ASCIIToUTF16(""))); |
| 60 EXPECT_TRUE(CERT_LIST_END(CERT_LIST_HEAD(cert_list), cert_list)); | |
| 61 CERT_DestroyCertList(cert_list); | |
| 62 | 91 |
| 63 FilePath cert_path = GetTestCertsDirectory().AppendASCII("client.p12"); | |
| 64 std::string cert_data; | |
| 65 ASSERT_TRUE(file_util::ReadFileToString(cert_path, &cert_data)); | |
| 66 EXPECT_EQ(ERR_PKCS12_IMPORT_BAD_PASSWORD, | |
| 67 cert_db.ImportFromPKCS12(cert_data, ASCIIToUTF16(""))); | |
| 68 | |
| 69 | |
| 70 cert_list = PK11_ListCertsInSlot(slot); | |
| 71 // Test db should still be empty. | 92 // Test db should still be empty. |
| 72 EXPECT_TRUE(CERT_LIST_END(CERT_LIST_HEAD(cert_list), cert_list)); | 93 EXPECT_EQ(0U, ListCertsInSlot(slot_.get()).size()); |
| 73 CERT_DestroyCertList(cert_list); | |
| 74 | |
| 75 PK11_FreeSlot(slot); | |
| 76 } | 94 } |
| 77 | 95 |
| 78 TEST_F(CertDatabaseNSSTest, ImportFromPKCS12AndExportAgain) { | 96 TEST_F(CertDatabaseNSSTest, ImportFromPKCS12AndExportAgain) { |
| 79 PK11SlotInfo* slot = base::GetDefaultNSSKeySlot(); | 97 std::string pkcs12_data = ReadTestFile("client.p12"); |
| 80 CertDatabase cert_db; | |
| 81 | 98 |
| 82 CERTCertList* cert_list = PK11_ListCertsInSlot(slot); | 99 EXPECT_EQ(OK, cert_db_.ImportFromPKCS12(pkcs12_data, ASCIIToUTF16("12345"))); |
| 83 // Test db should be empty at start of test. | |
| 84 EXPECT_TRUE(CERT_LIST_END(CERT_LIST_HEAD(cert_list), cert_list)); | |
| 85 CERT_DestroyCertList(cert_list); | |
| 86 | 100 |
| 87 FilePath cert_path = GetTestCertsDirectory().AppendASCII("client.p12"); | 101 CertificateList cert_list = ListCertsInSlot(slot_.get()); |
| 88 std::string cert_data; | 102 ASSERT_EQ(1U, cert_list.size()); |
| 89 ASSERT_TRUE(file_util::ReadFileToString(cert_path, &cert_data)); | 103 scoped_refptr<X509Certificate> cert(cert_list[0]); |
| 90 EXPECT_EQ(OK, cert_db.ImportFromPKCS12(cert_data, ASCIIToUTF16("12345"))); | |
| 91 | |
| 92 cert_list = PK11_ListCertsInSlot(slot); | |
| 93 // Test db should be empty at start of test. | |
| 94 ASSERT_FALSE(CERT_LIST_END(CERT_LIST_HEAD(cert_list), cert_list)); | |
| 95 scoped_refptr<X509Certificate> cert( | |
| 96 X509Certificate::CreateFromHandle( | |
| 97 CERT_LIST_HEAD(cert_list)->cert, | |
| 98 X509Certificate::SOURCE_LONE_CERT_IMPORT, | |
| 99 X509Certificate::OSCertHandles())); | |
| 100 CERT_DestroyCertList(cert_list); | |
| 101 | 104 |
| 102 EXPECT_EQ("testusercert", | 105 EXPECT_EQ("testusercert", |
| 103 cert->subject().common_name); | 106 cert->subject().common_name); |
| 104 | 107 |
| 105 // TODO(mattm): move export test to seperate test case? | 108 // TODO(mattm): move export test to seperate test case? |
| 106 CertificateList certs; | |
| 107 certs.push_back(cert); | |
| 108 std::string exported_data; | 109 std::string exported_data; |
| 109 EXPECT_EQ(1, cert_db.ExportToPKCS12(certs, ASCIIToUTF16("exportpw"), | 110 EXPECT_EQ(1, cert_db_.ExportToPKCS12(cert_list, ASCIIToUTF16("exportpw"), |
| 110 &exported_data)); | 111 &exported_data)); |
| 111 ASSERT_LT(0U, exported_data.size()); | 112 ASSERT_LT(0U, exported_data.size()); |
| 112 // TODO(mattm): further verification of exported data? | 113 // TODO(mattm): further verification of exported data? |
| 113 | |
| 114 PK11_FreeSlot(slot); | |
| 115 } | 114 } |
| 116 | 115 |
| 117 } // namespace net | 116 } // namespace net |
| OLD | NEW |