nss_key_util_unittest.cc 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2015 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. #include "crypto/nss_key_util.h"
  5. #include <keyhi.h>
  6. #include <pk11pub.h>
  7. #include <stdint.h>
  8. #include <vector>
  9. #include "crypto/nss_util.h"
  10. #include "crypto/scoped_nss_types.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. namespace crypto {
  13. class NSSKeyUtilTest : public testing::Test {
  14. public:
  15. void SetUp() override {
  16. EnsureNSSInit();
  17. internal_slot_.reset(PK11_GetInternalSlot());
  18. ASSERT_TRUE(internal_slot_);
  19. }
  20. PK11SlotInfo* internal_slot() { return internal_slot_.get(); }
  21. private:
  22. ScopedPK11Slot internal_slot_;
  23. };
  24. TEST_F(NSSKeyUtilTest, GenerateRSAKeyPairNSS) {
  25. const int kKeySizeBits = 1024;
  26. ScopedSECKEYPublicKey public_key;
  27. ScopedSECKEYPrivateKey private_key;
  28. ASSERT_TRUE(GenerateRSAKeyPairNSS(internal_slot(), kKeySizeBits,
  29. false /* not permanent */, &public_key,
  30. &private_key));
  31. EXPECT_EQ(rsaKey, SECKEY_GetPublicKeyType(public_key.get()));
  32. EXPECT_EQ(rsaKey, SECKEY_GetPrivateKeyType(private_key.get()));
  33. EXPECT_EQ((kKeySizeBits + 7) / 8,
  34. PK11_GetPrivateModulusLen(private_key.get()));
  35. }
  36. TEST_F(NSSKeyUtilTest, FindNSSKeyFromPublicKeyInfo) {
  37. // Create an NSS keypair, which will put the keys in the user's NSSDB.
  38. ScopedSECKEYPublicKey public_key;
  39. ScopedSECKEYPrivateKey private_key;
  40. ASSERT_TRUE(GenerateRSAKeyPairNSS(internal_slot(), 512,
  41. false /* not permanent */, &public_key,
  42. &private_key));
  43. ScopedSECItem item(SECKEY_EncodeDERSubjectPublicKeyInfo(public_key.get()));
  44. ASSERT_TRUE(item);
  45. std::vector<uint8_t> public_key_der(item->data, item->data + item->len);
  46. ScopedSECKEYPrivateKey private_key2 =
  47. FindNSSKeyFromPublicKeyInfo(public_key_der);
  48. ASSERT_TRUE(private_key2);
  49. EXPECT_EQ(private_key->pkcs11ID, private_key2->pkcs11ID);
  50. }
  51. TEST_F(NSSKeyUtilTest, FailedFindNSSKeyFromPublicKeyInfo) {
  52. // Create an NSS keypair, which will put the keys in the user's NSSDB.
  53. ScopedSECKEYPublicKey public_key;
  54. ScopedSECKEYPrivateKey private_key;
  55. ASSERT_TRUE(GenerateRSAKeyPairNSS(internal_slot(), 512,
  56. false /* not permanent */, &public_key,
  57. &private_key));
  58. ScopedSECItem item(SECKEY_EncodeDERSubjectPublicKeyInfo(public_key.get()));
  59. ASSERT_TRUE(item);
  60. std::vector<uint8_t> public_key_der(item->data, item->data + item->len);
  61. // Remove the keys from the DB, and make sure we can't find them again.
  62. PK11_DestroyTokenObject(private_key->pkcs11Slot, private_key->pkcs11ID);
  63. PK11_DestroyTokenObject(public_key->pkcs11Slot, public_key->pkcs11ID);
  64. EXPECT_FALSE(FindNSSKeyFromPublicKeyInfo(public_key_der));
  65. }
  66. } // namespace crypto