blob: 638a17462c031bd397cd508fe6f8de62555ba1de [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 Afanasyev04b22a92014-01-05 22:40:17 -080013#include <openssl/ssl.h>
14#include <openssl/sha.h>
15#include <openssl/rsa.h>
Yingdi Yu4b752752014-02-18 12:24:03 -080016#include <cryptopp/osrng.h>
Jeff Thompson6c314bc2013-09-23 18:09:38 -070017
18using namespace std;
Jeff Thompson6c314bc2013-09-23 18:09:38 -070019
20namespace ndn {
21
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080022/**
23 * RsaPrivateKey is a simple class to hold an RSA private key.
24 */
Yingdi Yu87581582014-01-14 14:28:39 -080025class SecTpmMemory::RsaPrivateKey {
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080026public:
27 RsaPrivateKey(const uint8_t *keyDer, size_t keyDerLength)
28 {
29 // Use a temporary pointer since d2i updates it.
30 const uint8_t *derPointer = keyDer;
31 privateKey_ = d2i_RSAPrivateKey(NULL, &derPointer, keyDerLength);
32 if (!privateKey_)
33 throw Error("RsaPrivateKey constructor: Error decoding private key DER");
34 }
35
36 ~RsaPrivateKey()
37 {
38 if (privateKey_)
39 RSA_free(privateKey_);
40 }
41
42 rsa_st *
43 getPrivateKey()
44 {
45 return privateKey_;
46 }
47
48private:
49 rsa_st * privateKey_;
50};
51
Yingdi Yu87581582014-01-14 14:28:39 -080052SecTpmMemory::~SecTpmMemory()
Jeff Thompson6c314bc2013-09-23 18:09:38 -070053{
54}
55
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080056void
Yingdi Yu87581582014-01-14 14:28:39 -080057SecTpmMemory::setKeyPairForKeyName(const Name& keyName,
58 uint8_t *publicKeyDer, size_t publicKeyDerLength,
59 uint8_t *privateKeyDer, size_t privateKeyDerLength)
Jeff Thompson6c314bc2013-09-23 18:09:38 -070060{
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080061 publicKeyStore_[keyName.toUri()] = ptr_lib::make_shared<PublicKey>(publicKeyDer, publicKeyDerLength);
Jeff Thompsonce115762013-12-18 14:59:56 -080062 privateKeyStore_[keyName.toUri()] = ptr_lib::make_shared<RsaPrivateKey>(privateKeyDer, privateKeyDerLength);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070063}
64
65void
Yingdi Yu87581582014-01-14 14:28:39 -080066SecTpmMemory::generateKeyPairInTpm(const Name& keyName, KeyType keyType, int keySize)
Jeff Thompson6c314bc2013-09-23 18:09:38 -070067{
68#if 1
Yingdi Yu28fd32f2014-01-28 19:03:03 -080069 throw Error("SecTpmMemory::generateKeyPair not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -070070#endif
71}
72
Yingdi Yu28fd32f2014-01-28 19:03:03 -080073void
74SecTpmMemory::deleteKeyPairInTpm(const Name &keyName)
75{
76 throw Error("SecTpmMemory::deleteKeyPairInTpm not implemented");
77}
78
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080079ConstBufferPtr
80SecTpmMemory::exportPrivateKeyPkcs1FromTpm(const Name& keyName)
81{
82 throw Error("SecTpmMemory::exportPrivateKeyPkcs1FromTpm is not implemented");
83}
84
85bool
86SecTpmMemory::importPrivateKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size)
87{
88 throw Error("SecTpmMemory::importPrivateKeyPkcs1IntoTpm is not implemented");
89}
90
91bool
92SecTpmMemory::importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size)
93{
94 throw Error("SecTpmMemory::importPublicKeyPkcs1IntoTpm is not implemented");
95}
96
Jeff Thompsonce115762013-12-18 14:59:56 -080097ptr_lib::shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -080098SecTpmMemory::getPublicKeyFromTpm(const Name& keyName)
Jeff Thompson6c314bc2013-09-23 18:09:38 -070099{
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800100 PublicKeyStore::iterator publicKey = publicKeyStore_.find(keyName.toUri());
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700101 if (publicKey == publicKeyStore_.end())
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800102 throw Error(string("MemoryPrivateKeyStorage: Cannot find public key ") + keyName.toUri());
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700103 return publicKey->second;
104}
105
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800106Block
Yingdi Yub4bb85a2014-01-16 10:11:04 -0800107SecTpmMemory::signInTpm(const uint8_t *data, size_t dataLength,
Yingdi Yu87581582014-01-14 14:28:39 -0800108 const Name& keyName,
109 DigestAlgorithm digestAlgorithm)
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700110{
111 if (digestAlgorithm != DIGEST_ALGORITHM_SHA256)
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800112 return ConstBufferPtr();
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700113
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700114 // Find the private key and sign.
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800115 PrivateKeyStore::iterator privateKey = privateKeyStore_.find(keyName.toUri());
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700116 if (privateKey == privateKeyStore_.end())
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800117 throw Error(string("MemoryPrivateKeyStorage: Cannot find private key ") + keyName.toUri());
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700118
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800119 uint8_t digest[SHA256_DIGEST_LENGTH];
120 SHA256_CTX sha256;
121 SHA256_Init(&sha256);
122 SHA256_Update(&sha256, data, dataLength);
123 SHA256_Final(digest, &sha256);
124
125 BufferPtr signatureBuffer = ptr_lib::make_shared<Buffer>();
126 signatureBuffer->resize(RSA_size(privateKey->second->getPrivateKey()));
127
128 unsigned int signatureBitsLength;
129 if (!RSA_sign(NID_sha256, digest, sizeof(digest),
130 signatureBuffer->buf(),
131 &signatureBitsLength,
132 privateKey->second->getPrivateKey()))
133 {
134 throw Error("Error in RSA_sign");
135 }
136
137 return Block(Tlv::SignatureValue, signatureBuffer);
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700138}
139
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800140ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -0800141SecTpmMemory::decryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric)
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700142{
143#if 1
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800144 throw Error("MemoryPrivateKeyStorage::decrypt not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700145#endif
146}
147
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800148ConstBufferPtr
Yingdi Yufc40d872014-02-18 12:56:04 -0800149SecTpmMemory::encryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric)
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700150{
151#if 1
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800152 throw Error("MemoryPrivateKeyStorage::encrypt not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700153#endif
154}
155
156void
Yingdi Yub4bb85a2014-01-16 10:11:04 -0800157SecTpmMemory::generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize)
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700158{
159#if 1
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800160 throw Error("MemoryPrivateKeyStorage::generateKey not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700161#endif
162}
163
164bool
Yingdi Yub4bb85a2014-01-16 10:11:04 -0800165SecTpmMemory::doesKeyExistInTpm(const Name& keyName, KeyClass keyClass)
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700166{
167 if (keyClass == KEY_CLASS_PUBLIC)
168 return publicKeyStore_.find(keyName.toUri()) != publicKeyStore_.end();
169 else if (keyClass == KEY_CLASS_PRIVATE)
170 return privateKeyStore_.find(keyName.toUri()) != privateKeyStore_.end();
171 else
172 // KEY_CLASS_SYMMETRIC not implemented yet.
173 return false;
174}
175
Yingdi Yu4b752752014-02-18 12:24:03 -0800176bool
177SecTpmMemory::generateRandomBlock(uint8_t* res, size_t size)
178{
179 try{
180 CryptoPP::AutoSeededRandomPool rng;
181 rng.GenerateBlock(res, size);
182 return true;
183 }catch(const CryptoPP::Exception& e){
184 return false;
185 }
186}
187
Yingdi Yufc40d872014-02-18 12:56:04 -0800188} // namespace ndn