blob: 9561cb9bf51dc15e0a66a0adc5381eeff812aa06 [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
Yingdi Yu87581582014-01-14 14:28:39 -08008#include <ndn-cpp/security/identity/sec-tpm-memory.hpp>
Alexander Afanasyev6be1a6a2014-01-06 00:08:14 -08009#include <ndn-cpp/security/certificate/public-key.hpp>
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080010#include <openssl/ssl.h>
11#include <openssl/sha.h>
12#include <openssl/rsa.h>
Jeff Thompson6c314bc2013-09-23 18:09:38 -070013
14using namespace std;
Jeff Thompson6c314bc2013-09-23 18:09:38 -070015
16namespace ndn {
17
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080018/**
19 * RsaPrivateKey is a simple class to hold an RSA private key.
20 */
Yingdi Yu87581582014-01-14 14:28:39 -080021class SecTpmMemory::RsaPrivateKey {
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080022public:
23 RsaPrivateKey(const uint8_t *keyDer, size_t keyDerLength)
24 {
25 // Use a temporary pointer since d2i updates it.
26 const uint8_t *derPointer = keyDer;
27 privateKey_ = d2i_RSAPrivateKey(NULL, &derPointer, keyDerLength);
28 if (!privateKey_)
29 throw Error("RsaPrivateKey constructor: Error decoding private key DER");
30 }
31
32 ~RsaPrivateKey()
33 {
34 if (privateKey_)
35 RSA_free(privateKey_);
36 }
37
38 rsa_st *
39 getPrivateKey()
40 {
41 return privateKey_;
42 }
43
44private:
45 rsa_st * privateKey_;
46};
47
Yingdi Yu87581582014-01-14 14:28:39 -080048SecTpmMemory::~SecTpmMemory()
Jeff Thompson6c314bc2013-09-23 18:09:38 -070049{
50}
51
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080052void
Yingdi Yu87581582014-01-14 14:28:39 -080053SecTpmMemory::setKeyPairForKeyName(const Name& keyName,
54 uint8_t *publicKeyDer, size_t publicKeyDerLength,
55 uint8_t *privateKeyDer, size_t privateKeyDerLength)
Jeff Thompson6c314bc2013-09-23 18:09:38 -070056{
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080057 publicKeyStore_[keyName.toUri()] = ptr_lib::make_shared<PublicKey>(publicKeyDer, publicKeyDerLength);
Jeff Thompsonce115762013-12-18 14:59:56 -080058 privateKeyStore_[keyName.toUri()] = ptr_lib::make_shared<RsaPrivateKey>(privateKeyDer, privateKeyDerLength);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070059}
60
61void
Yingdi Yu87581582014-01-14 14:28:39 -080062SecTpmMemory::generateKeyPairInTpm(const Name& keyName, KeyType keyType, int keySize)
Jeff Thompson6c314bc2013-09-23 18:09:38 -070063{
64#if 1
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080065 throw Error("MemoryPrivateKeyStorage::generateKeyPair not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -070066#endif
67}
68
Jeff Thompsonce115762013-12-18 14:59:56 -080069ptr_lib::shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -080070SecTpmMemory::getPublicKeyFromTpm(const Name& keyName)
Jeff Thompson6c314bc2013-09-23 18:09:38 -070071{
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080072 PublicKeyStore::iterator publicKey = publicKeyStore_.find(keyName.toUri());
Jeff Thompson6c314bc2013-09-23 18:09:38 -070073 if (publicKey == publicKeyStore_.end())
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080074 throw Error(string("MemoryPrivateKeyStorage: Cannot find public key ") + keyName.toUri());
Jeff Thompson6c314bc2013-09-23 18:09:38 -070075 return publicKey->second;
76}
77
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080078Block
Yingdi Yu87581582014-01-14 14:28:39 -080079SecTpmMemory::sign(const uint8_t *data, size_t dataLength,
80 const Name& keyName,
81 DigestAlgorithm digestAlgorithm)
Jeff Thompson6c314bc2013-09-23 18:09:38 -070082{
83 if (digestAlgorithm != DIGEST_ALGORITHM_SHA256)
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080084 return ConstBufferPtr();
Jeff Thompson6c314bc2013-09-23 18:09:38 -070085
Jeff Thompson6c314bc2013-09-23 18:09:38 -070086 // Find the private key and sign.
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080087 PrivateKeyStore::iterator privateKey = privateKeyStore_.find(keyName.toUri());
Jeff Thompson6c314bc2013-09-23 18:09:38 -070088 if (privateKey == privateKeyStore_.end())
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080089 throw Error(string("MemoryPrivateKeyStorage: Cannot find private key ") + keyName.toUri());
Jeff Thompson6c314bc2013-09-23 18:09:38 -070090
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080091 uint8_t digest[SHA256_DIGEST_LENGTH];
92 SHA256_CTX sha256;
93 SHA256_Init(&sha256);
94 SHA256_Update(&sha256, data, dataLength);
95 SHA256_Final(digest, &sha256);
96
97 BufferPtr signatureBuffer = ptr_lib::make_shared<Buffer>();
98 signatureBuffer->resize(RSA_size(privateKey->second->getPrivateKey()));
99
100 unsigned int signatureBitsLength;
101 if (!RSA_sign(NID_sha256, digest, sizeof(digest),
102 signatureBuffer->buf(),
103 &signatureBitsLength,
104 privateKey->second->getPrivateKey()))
105 {
106 throw Error("Error in RSA_sign");
107 }
108
109 return Block(Tlv::SignatureValue, signatureBuffer);
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700110}
111
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800112void
Yingdi Yu87581582014-01-14 14:28:39 -0800113SecTpmMemory::sign(Data &d,
114 const Name& keyName,
115 DigestAlgorithm digestAlgorithm)
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800116{
117 if (digestAlgorithm != DIGEST_ALGORITHM_SHA256)
118 Error("MemoryPrivateKeyStorage::sign only SHA256 digest is supported");
119
120 // Find the private key and sign.
121 PrivateKeyStore::iterator privateKey = privateKeyStore_.find(keyName.toUri());
122 if (privateKey == privateKeyStore_.end())
123 throw Error(string("MemoryPrivateKeyStorage: Cannot find private key ") + keyName.toUri());
124
125 uint8_t digest[SHA256_DIGEST_LENGTH];
126 SHA256_CTX sha256;
127 SHA256_Init(&sha256);
128
129 SHA256_Update(&sha256, d.getName(). wireEncode().wire(), d.getName(). wireEncode().size());
130 SHA256_Update(&sha256, d.getMetaInfo().wireEncode().wire(), d.getMetaInfo().wireEncode().size());
131 SHA256_Update(&sha256, d.getContent(). wire(), d.getContent(). size());
132 SHA256_Update(&sha256, d.getSignature().getInfo(). wire(), d.getSignature().getInfo(). size());
133
134 SHA256_Final(digest, &sha256);
135
136 BufferPtr signatureBuffer = ptr_lib::make_shared<Buffer>();
137 signatureBuffer->resize(RSA_size(privateKey->second->getPrivateKey()));
138
139 unsigned int signatureBitsLength;
140 if (!RSA_sign(NID_sha256, digest, sizeof(digest),
141 signatureBuffer->buf(),
142 &signatureBitsLength,
143 privateKey->second->getPrivateKey()))
144 {
145 throw Error("Error in RSA_sign");
146 }
147
148 d.setSignatureValue(Block(Tlv::SignatureValue, signatureBuffer));
149}
150
151ConstBufferPtr
Yingdi Yu87581582014-01-14 14:28:39 -0800152SecTpmMemory::decrypt(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric)
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700153{
154#if 1
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800155 throw Error("MemoryPrivateKeyStorage::decrypt not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700156#endif
157}
158
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800159ConstBufferPtr
Yingdi Yu87581582014-01-14 14:28:39 -0800160SecTpmMemory::encrypt(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric)
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700161{
162#if 1
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800163 throw Error("MemoryPrivateKeyStorage::encrypt not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700164#endif
165}
166
167void
Yingdi Yu87581582014-01-14 14:28:39 -0800168SecTpmMemory::generateSymmetricKey(const Name& keyName, KeyType keyType, int keySize)
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700169{
170#if 1
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800171 throw Error("MemoryPrivateKeyStorage::generateKey not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700172#endif
173}
174
175bool
Yingdi Yu87581582014-01-14 14:28:39 -0800176SecTpmMemory::doesKeyExist(const Name& keyName, KeyClass keyClass)
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700177{
178 if (keyClass == KEY_CLASS_PUBLIC)
179 return publicKeyStore_.find(keyName.toUri()) != publicKeyStore_.end();
180 else if (keyClass == KEY_CLASS_PRIVATE)
181 return privateKeyStore_.find(keyName.toUri()) != privateKeyStore_.end();
182 else
183 // KEY_CLASS_SYMMETRIC not implemented yet.
184 return false;
185}
186
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700187}