blob: a115d48f20713c766f387c9a2de27fb752ec8f50 [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
Jeff Thompsonce115762013-12-18 14:59:56 -080079ptr_lib::shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -080080SecTpmMemory::getPublicKeyFromTpm(const Name& keyName)
Jeff Thompson6c314bc2013-09-23 18:09:38 -070081{
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080082 PublicKeyStore::iterator publicKey = publicKeyStore_.find(keyName.toUri());
Jeff Thompson6c314bc2013-09-23 18:09:38 -070083 if (publicKey == publicKeyStore_.end())
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080084 throw Error(string("MemoryPrivateKeyStorage: Cannot find public key ") + keyName.toUri());
Jeff Thompson6c314bc2013-09-23 18:09:38 -070085 return publicKey->second;
86}
87
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080088Block
Yingdi Yub4bb85a2014-01-16 10:11:04 -080089SecTpmMemory::signInTpm(const uint8_t *data, size_t dataLength,
Yingdi Yu87581582014-01-14 14:28:39 -080090 const Name& keyName,
91 DigestAlgorithm digestAlgorithm)
Jeff Thompson6c314bc2013-09-23 18:09:38 -070092{
93 if (digestAlgorithm != DIGEST_ALGORITHM_SHA256)
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080094 return ConstBufferPtr();
Jeff Thompson6c314bc2013-09-23 18:09:38 -070095
Jeff Thompson6c314bc2013-09-23 18:09:38 -070096 // Find the private key and sign.
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080097 PrivateKeyStore::iterator privateKey = privateKeyStore_.find(keyName.toUri());
Jeff Thompson6c314bc2013-09-23 18:09:38 -070098 if (privateKey == privateKeyStore_.end())
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080099 throw Error(string("MemoryPrivateKeyStorage: Cannot find private key ") + keyName.toUri());
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700100
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800101 uint8_t digest[SHA256_DIGEST_LENGTH];
102 SHA256_CTX sha256;
103 SHA256_Init(&sha256);
104 SHA256_Update(&sha256, data, dataLength);
105 SHA256_Final(digest, &sha256);
106
107 BufferPtr signatureBuffer = ptr_lib::make_shared<Buffer>();
108 signatureBuffer->resize(RSA_size(privateKey->second->getPrivateKey()));
109
110 unsigned int signatureBitsLength;
111 if (!RSA_sign(NID_sha256, digest, sizeof(digest),
112 signatureBuffer->buf(),
113 &signatureBitsLength,
114 privateKey->second->getPrivateKey()))
115 {
116 throw Error("Error in RSA_sign");
117 }
118
119 return Block(Tlv::SignatureValue, signatureBuffer);
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700120}
121
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800122ConstBufferPtr
Yingdi Yub4bb85a2014-01-16 10:11:04 -0800123SecTpmMemory::decryptInTpm(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric)
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700124{
125#if 1
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800126 throw Error("MemoryPrivateKeyStorage::decrypt not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700127#endif
128}
129
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800130ConstBufferPtr
Yingdi Yub4bb85a2014-01-16 10:11:04 -0800131SecTpmMemory::encryptInTpm(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric)
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700132{
133#if 1
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800134 throw Error("MemoryPrivateKeyStorage::encrypt not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700135#endif
136}
137
138void
Yingdi Yub4bb85a2014-01-16 10:11:04 -0800139SecTpmMemory::generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize)
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700140{
141#if 1
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800142 throw Error("MemoryPrivateKeyStorage::generateKey not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700143#endif
144}
145
146bool
Yingdi Yub4bb85a2014-01-16 10:11:04 -0800147SecTpmMemory::doesKeyExistInTpm(const Name& keyName, KeyClass keyClass)
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700148{
149 if (keyClass == KEY_CLASS_PUBLIC)
150 return publicKeyStore_.find(keyName.toUri()) != publicKeyStore_.end();
151 else if (keyClass == KEY_CLASS_PRIVATE)
152 return privateKeyStore_.find(keyName.toUri()) != privateKeyStore_.end();
153 else
154 // KEY_CLASS_SYMMETRIC not implemented yet.
155 return false;
156}
157
Yingdi Yu4b752752014-02-18 12:24:03 -0800158bool
159SecTpmMemory::generateRandomBlock(uint8_t* res, size_t size)
160{
161 try{
162 CryptoPP::AutoSeededRandomPool rng;
163 rng.GenerateBlock(res, size);
164 return true;
165 }catch(const CryptoPP::Exception& e){
166 return false;
167 }
168}
169
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700170}