blob: 3c77c15d9dc01a63aab1c61714957ee14b3e5a1a [file] [log] [blame]
Jeff Thompson6c314bc2013-09-23 18:09:38 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
5 * See COPYING for copyright and distribution information.
6 */
7
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -08008#include "common.hpp"
9
10#include "sec-tpm-memory.hpp"
11#include "public-key.hpp"
12
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070013#include "openssl.hpp"
Junxiao Shi482ccc52014-03-31 13:05:24 -070014#include "cryptopp.hpp"
Jeff Thompson6c314bc2013-09-23 18:09:38 -070015
16using namespace std;
Jeff Thompson6c314bc2013-09-23 18:09:38 -070017
18namespace ndn {
19
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080020/**
21 * RsaPrivateKey is a simple class to hold an RSA private key.
22 */
Yingdi Yu87581582014-01-14 14:28:39 -080023class SecTpmMemory::RsaPrivateKey {
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080024public:
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070025 RsaPrivateKey(const uint8_t* keyDer, size_t keyDerLength)
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080026 {
27 // Use a temporary pointer since d2i updates it.
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070028 const uint8_t* derPointer = keyDer;
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080029 privateKey_ = d2i_RSAPrivateKey(NULL, &derPointer, keyDerLength);
30 if (!privateKey_)
31 throw Error("RsaPrivateKey constructor: Error decoding private key DER");
32 }
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070033
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080034 ~RsaPrivateKey()
35 {
36 if (privateKey_)
37 RSA_free(privateKey_);
38 }
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070039
40 rsa_st*
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080041 getPrivateKey()
42 {
43 return privateKey_;
44 }
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070045
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080046private:
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070047 rsa_st* privateKey_;
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080048};
49
Yingdi Yu87581582014-01-14 14:28:39 -080050SecTpmMemory::~SecTpmMemory()
Jeff Thompson6c314bc2013-09-23 18:09:38 -070051{
52}
53
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080054void
Yingdi Yu87581582014-01-14 14:28:39 -080055SecTpmMemory::setKeyPairForKeyName(const Name& keyName,
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070056 uint8_t* publicKeyDer, size_t publicKeyDerLength,
57 uint8_t* privateKeyDer, size_t privateKeyDerLength)
Jeff Thompson6c314bc2013-09-23 18:09:38 -070058{
Yingdi Yu2e57a582014-02-20 23:34:43 -080059 publicKeyStore_[keyName.toUri()] = make_shared<PublicKey>(publicKeyDer, publicKeyDerLength);
60 privateKeyStore_[keyName.toUri()] = make_shared<RsaPrivateKey>(privateKeyDer, privateKeyDerLength);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070061}
62
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070063void
Yingdi Yu87581582014-01-14 14:28:39 -080064SecTpmMemory::generateKeyPairInTpm(const Name& keyName, KeyType keyType, int keySize)
Jeff Thompson6c314bc2013-09-23 18:09:38 -070065{
66#if 1
Yingdi Yu28fd32f2014-01-28 19:03:03 -080067 throw Error("SecTpmMemory::generateKeyPair not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -070068#endif
69}
70
Yingdi Yu28fd32f2014-01-28 19:03:03 -080071void
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070072SecTpmMemory::deleteKeyPairInTpm(const Name& keyName)
Yingdi Yu28fd32f2014-01-28 19:03:03 -080073{
74 throw Error("SecTpmMemory::deleteKeyPairInTpm not implemented");
75}
76
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080077ConstBufferPtr
78SecTpmMemory::exportPrivateKeyPkcs1FromTpm(const Name& keyName)
79{
Yingdi Yu2e57a582014-02-20 23:34:43 -080080 return shared_ptr<Buffer>();
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080081}
82
83bool
84SecTpmMemory::importPrivateKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size)
85{
Yingdi Yu2e57a582014-02-20 23:34:43 -080086 return false;
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080087}
88
89bool
90SecTpmMemory::importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size)
91{
Yingdi Yu2e57a582014-02-20 23:34:43 -080092 return false;
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080093}
94
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070095shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -080096SecTpmMemory::getPublicKeyFromTpm(const Name& keyName)
Jeff Thompson6c314bc2013-09-23 18:09:38 -070097{
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080098 PublicKeyStore::iterator publicKey = publicKeyStore_.find(keyName.toUri());
Jeff Thompson6c314bc2013-09-23 18:09:38 -070099 if (publicKey == publicKeyStore_.end())
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800100 throw Error(string("MemoryPrivateKeyStorage: Cannot find public key ") + keyName.toUri());
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700101 return publicKey->second;
102}
103
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700104Block
105SecTpmMemory::signInTpm(const uint8_t* data, size_t dataLength,
Yingdi Yu2e57a582014-02-20 23:34:43 -0800106 const Name& keyName,
107 DigestAlgorithm digestAlgorithm)
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700108{
109 if (digestAlgorithm != DIGEST_ALGORITHM_SHA256)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800110 throw Error("Unsupported digest algorithm.");
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700111
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700112 // Find the private key and sign.
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800113 PrivateKeyStore::iterator privateKey = privateKeyStore_.find(keyName.toUri());
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700114 if (privateKey == privateKeyStore_.end())
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800115 throw Error(string("MemoryPrivateKeyStorage: Cannot find private key ") + keyName.toUri());
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700116
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800117 uint8_t digest[SHA256_DIGEST_LENGTH];
118 SHA256_CTX sha256;
119 SHA256_Init(&sha256);
120 SHA256_Update(&sha256, data, dataLength);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700121 SHA256_Final(digest,& sha256);
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800122
Yingdi Yu2e57a582014-02-20 23:34:43 -0800123 BufferPtr signatureBuffer = make_shared<Buffer>();
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800124 signatureBuffer->resize(RSA_size(privateKey->second->getPrivateKey()));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700125
126 unsigned int signatureBitsLength;
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800127 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);
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700136}
137
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800138ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -0800139SecTpmMemory::decryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric)
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700140{
141#if 1
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800142 throw Error("MemoryPrivateKeyStorage::decrypt not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700143#endif
144}
145
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800146ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -0800147SecTpmMemory::encryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric)
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700148{
149#if 1
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800150 throw Error("MemoryPrivateKeyStorage::encrypt not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700151#endif
152}
153
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700154void
Yingdi Yub4bb85a2014-01-16 10:11:04 -0800155SecTpmMemory::generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize)
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700156{
157#if 1
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800158 throw Error("MemoryPrivateKeyStorage::generateKey not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700159#endif
160}
161
162bool
Yingdi Yub4bb85a2014-01-16 10:11:04 -0800163SecTpmMemory::doesKeyExistInTpm(const Name& keyName, KeyClass keyClass)
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700164{
165 if (keyClass == KEY_CLASS_PUBLIC)
166 return publicKeyStore_.find(keyName.toUri()) != publicKeyStore_.end();
167 else if (keyClass == KEY_CLASS_PRIVATE)
168 return privateKeyStore_.find(keyName.toUri()) != privateKeyStore_.end();
169 else
170 // KEY_CLASS_SYMMETRIC not implemented yet.
171 return false;
172}
173
Yingdi Yu4b752752014-02-18 12:24:03 -0800174bool
175SecTpmMemory::generateRandomBlock(uint8_t* res, size_t size)
176{
177 try{
178 CryptoPP::AutoSeededRandomPool rng;
179 rng.GenerateBlock(res, size);
180 return true;
181 }catch(const CryptoPP::Exception& e){
182 return false;
183 }
184}
185
Yingdi Yufc40d872014-02-18 12:56:04 -0800186} // namespace ndn