• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "crypto/rsa_private_key.h"
6 
7 #include <keyhi.h>
8 #include <pk11pub.h>
9 
10 #include "base/memory/scoped_ptr.h"
11 #include "crypto/nss_util.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 
14 namespace crypto {
15 
16 class RSAPrivateKeyNSSTest : public testing::Test {
17  public:
RSAPrivateKeyNSSTest()18   RSAPrivateKeyNSSTest() {}
~RSAPrivateKeyNSSTest()19   virtual ~RSAPrivateKeyNSSTest() {}
20 
SetUp()21   virtual void SetUp() {
22 #if defined(OS_CHROMEOS)
23     OpenPersistentNSSDB();
24 #endif
25   }
26 
27  private:
28   ScopedTestNSSDB test_nssdb_;
29 
30   DISALLOW_COPY_AND_ASSIGN(RSAPrivateKeyNSSTest);
31 };
32 
TEST_F(RSAPrivateKeyNSSTest,CreateFromKeyTest)33 TEST_F(RSAPrivateKeyNSSTest, CreateFromKeyTest) {
34   scoped_ptr<crypto::RSAPrivateKey> key_pair(RSAPrivateKey::Create(256));
35 
36   scoped_ptr<crypto::RSAPrivateKey> key_copy(
37       RSAPrivateKey::CreateFromKey(key_pair->key()));
38   ASSERT_TRUE(key_copy.get());
39 
40   std::vector<uint8> privkey;
41   std::vector<uint8> pubkey;
42   ASSERT_TRUE(key_pair->ExportPrivateKey(&privkey));
43   ASSERT_TRUE(key_pair->ExportPublicKey(&pubkey));
44 
45   std::vector<uint8> privkey_copy;
46   std::vector<uint8> pubkey_copy;
47   ASSERT_TRUE(key_copy->ExportPrivateKey(&privkey_copy));
48   ASSERT_TRUE(key_copy->ExportPublicKey(&pubkey_copy));
49 
50   ASSERT_EQ(privkey, privkey_copy);
51   ASSERT_EQ(pubkey, pubkey_copy);
52 }
53 
TEST_F(RSAPrivateKeyNSSTest,FindFromPublicKey)54 TEST_F(RSAPrivateKeyNSSTest, FindFromPublicKey) {
55   // Create a keypair, which will put the keys in the user's NSSDB.
56   scoped_ptr<crypto::RSAPrivateKey> key_pair(RSAPrivateKey::Create(256));
57 
58   std::vector<uint8> public_key;
59   ASSERT_TRUE(key_pair->ExportPublicKey(&public_key));
60 
61   scoped_ptr<crypto::RSAPrivateKey> key_pair_2(
62       crypto::RSAPrivateKey::FindFromPublicKeyInfo(public_key));
63 
64   EXPECT_EQ(key_pair->key_->pkcs11ID, key_pair_2->key_->pkcs11ID);
65 }
66 
TEST_F(RSAPrivateKeyNSSTest,FailedFindFromPublicKey)67 TEST_F(RSAPrivateKeyNSSTest, FailedFindFromPublicKey) {
68   // Create a keypair, which will put the keys in the user's NSSDB.
69   scoped_ptr<crypto::RSAPrivateKey> key_pair(RSAPrivateKey::Create(256));
70 
71   std::vector<uint8> public_key;
72   ASSERT_TRUE(key_pair->ExportPublicKey(&public_key));
73 
74   // Remove the keys from the DB, and make sure we can't find them again.
75   if (key_pair->key_) {
76     PK11_DestroyTokenObject(key_pair->key_->pkcs11Slot,
77                             key_pair->key_->pkcs11ID);
78   }
79   if (key_pair->public_key_) {
80     PK11_DestroyTokenObject(key_pair->public_key_->pkcs11Slot,
81                             key_pair->public_key_->pkcs11ID);
82   }
83 
84   EXPECT_EQ(NULL, crypto::RSAPrivateKey::FindFromPublicKeyInfo(public_key));
85 }
86 
87 }  // namespace crypto
88