All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sec-tpm-memory.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
8 #include "common.hpp"
9 
10 #include "sec-tpm-memory.hpp"
11 #include "public-key.hpp"
12 
13 #include "openssl.hpp"
14 #include "cryptopp.hpp"
15 
16 using namespace std;
17 
18 namespace ndn {
19 
24 {
25 public:
26  RsaPrivateKey(const uint8_t* keyDer, size_t keyDerLength)
27  {
28  // Use a temporary pointer since d2i updates it.
29  const uint8_t* derPointer = keyDer;
30  m_privateKey = d2i_RSAPrivateKey(NULL, &derPointer, keyDerLength);
31  if (!m_privateKey)
32  throw Error("RsaPrivateKey constructor: Error decoding private key DER");
33  }
34 
36  {
37  if (m_privateKey)
38  RSA_free(m_privateKey);
39  }
40 
41  rsa_st*
43  {
44  return m_privateKey;
45  }
46 
47 private:
48  rsa_st* m_privateKey;
49 };
50 
51 SecTpmMemory::~SecTpmMemory()
52 {
53 }
54 
55 void
56 SecTpmMemory::setKeyPairForKeyName(const Name& keyName,
57  const uint8_t* publicKeyDer, size_t publicKeyDerLength,
58  const uint8_t* privateKeyDer, size_t privateKeyDerLength)
59 {
60  m_publicKeyStore[keyName.toUri()] = make_shared<PublicKey>(publicKeyDer, publicKeyDerLength);
61  m_privateKeyStore[keyName.toUri()] = make_shared<RsaPrivateKey>(privateKeyDer,
62  privateKeyDerLength);
63 }
64 
65 void
66 SecTpmMemory::generateKeyPairInTpm(const Name& keyName, KeyType keyType, int keySize)
67 {
68  throw Error("SecTpmMemory::generateKeyPair not implemented");
69 }
70 
71 void
72 SecTpmMemory::deleteKeyPairInTpm(const Name& keyName)
73 {
74  throw Error("SecTpmMemory::deleteKeyPairInTpm not implemented");
75 }
76 
78 SecTpmMemory::exportPrivateKeyPkcs1FromTpm(const Name& keyName)
79 {
80  return shared_ptr<Buffer>();
81 }
82 
83 bool
84 SecTpmMemory::importPrivateKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size)
85 {
86  return false;
87 }
88 
89 bool
90 SecTpmMemory::importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size)
91 {
92  return false;
93 }
94 
95 shared_ptr<PublicKey>
96 SecTpmMemory::getPublicKeyFromTpm(const Name& keyName)
97 {
98  PublicKeyStore::iterator publicKey = m_publicKeyStore.find(keyName.toUri());
99  if (publicKey == m_publicKeyStore.end())
100  throw Error(string("MemoryPrivateKeyStorage: Cannot find public key ") + keyName.toUri());
101  return publicKey->second;
102 }
103 
104 Block
105 SecTpmMemory::signInTpm(const uint8_t* data, size_t dataLength,
106  const Name& keyName,
107  DigestAlgorithm digestAlgorithm)
108 {
109  if (digestAlgorithm != DIGEST_ALGORITHM_SHA256)
110  throw Error("Unsupported digest algorithm.");
111 
112  // Find the private key and sign.
113  PrivateKeyStore::iterator privateKey = m_privateKeyStore.find(keyName.toUri());
114  if (privateKey == m_privateKeyStore.end())
115  throw Error(string("MemoryPrivateKeyStorage: Cannot find private key ") + keyName.toUri());
116 
117  uint8_t digest[SHA256_DIGEST_LENGTH];
118  SHA256_CTX sha256;
119  SHA256_Init(&sha256);
120  SHA256_Update(&sha256, data, dataLength);
121  SHA256_Final(digest,& sha256);
122 
123  BufferPtr signatureBuffer = make_shared<Buffer>();
124  signatureBuffer->resize(RSA_size(privateKey->second->getPrivateKey()));
125 
126  unsigned int signatureBitsLength;
127  if (!RSA_sign(NID_sha256, digest, sizeof(digest),
128  signatureBuffer->buf(),
129  &signatureBitsLength,
130  privateKey->second->getPrivateKey()))
131  {
132  throw Error("Error in RSA_sign");
133  }
134 
135  return Block(Tlv::SignatureValue, signatureBuffer);
136 }
137 
139 SecTpmMemory::decryptInTpm(const uint8_t* data, size_t dataLength,
140  const Name& keyName, bool isSymmetric)
141 {
142  throw Error("MemoryPrivateKeyStorage::decrypt not implemented");
143 }
144 
146 SecTpmMemory::encryptInTpm(const uint8_t* data, size_t dataLength,
147  const Name& keyName, bool isSymmetric)
148 {
149  throw Error("MemoryPrivateKeyStorage::encrypt not implemented");
150 }
151 
152 void
153 SecTpmMemory::generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize)
154 {
155  throw Error("MemoryPrivateKeyStorage::generateKey not implemented");
156 }
157 
158 bool
159 SecTpmMemory::doesKeyExistInTpm(const Name& keyName, KeyClass keyClass)
160 {
161  if (keyClass == KEY_CLASS_PUBLIC)
162  return m_publicKeyStore.find(keyName.toUri()) != m_publicKeyStore.end();
163  else if (keyClass == KEY_CLASS_PRIVATE)
164  return m_privateKeyStore.find(keyName.toUri()) != m_privateKeyStore.end();
165  else
166  // KEY_CLASS_SYMMETRIC not implemented yet.
167  return false;
168 }
169 
170 bool
171 SecTpmMemory::generateRandomBlock(uint8_t* res, size_t size)
172 {
173  try
174  {
175  CryptoPP::AutoSeededRandomPool rng;
176  rng.GenerateBlock(res, size);
177  return true;
178  }
179  catch (CryptoPP::Exception& e)
180  {
181  return false;
182  }
183 }
184 
185 } // namespace ndn
ptr_lib::shared_ptr< Buffer > BufferPtr
Definition: buffer.hpp:19
Class representing wire element of the NDN packet.
Definition: block.hpp:26
std::string toUri() const
Encode this name as a URI.
Definition: name.hpp:536
ptr_lib::shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:17
RsaPrivateKey(const uint8_t *keyDer, size_t keyDerLength)
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:26
RsaPrivateKey is a simple class to hold an RSA private key.
ConstBufferPtr sha256(const uint8_t *data, size_t dataLength)
Compute the sha-256 digest of data.
Definition: crypto.cpp:33