blob: a72e38525117add01ac5c04dc2095916aa0a57ee [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>
Jeff Thompson6c314bc2013-09-23 18:09:38 -070016
17using namespace std;
Jeff Thompson6c314bc2013-09-23 18:09:38 -070018
19namespace ndn {
20
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080021/**
22 * RsaPrivateKey is a simple class to hold an RSA private key.
23 */
Yingdi Yu87581582014-01-14 14:28:39 -080024class SecTpmMemory::RsaPrivateKey {
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080025public:
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 privateKey_ = d2i_RSAPrivateKey(NULL, &derPointer, keyDerLength);
31 if (!privateKey_)
32 throw Error("RsaPrivateKey constructor: Error decoding private key DER");
33 }
34
35 ~RsaPrivateKey()
36 {
37 if (privateKey_)
38 RSA_free(privateKey_);
39 }
40
41 rsa_st *
42 getPrivateKey()
43 {
44 return privateKey_;
45 }
46
47private:
48 rsa_st * privateKey_;
49};
50
Yingdi Yu87581582014-01-14 14:28:39 -080051SecTpmMemory::~SecTpmMemory()
Jeff Thompson6c314bc2013-09-23 18:09:38 -070052{
53}
54
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080055void
Yingdi Yu87581582014-01-14 14:28:39 -080056SecTpmMemory::setKeyPairForKeyName(const Name& keyName,
57 uint8_t *publicKeyDer, size_t publicKeyDerLength,
58 uint8_t *privateKeyDer, size_t privateKeyDerLength)
Jeff Thompson6c314bc2013-09-23 18:09:38 -070059{
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080060 publicKeyStore_[keyName.toUri()] = ptr_lib::make_shared<PublicKey>(publicKeyDer, publicKeyDerLength);
Jeff Thompsonce115762013-12-18 14:59:56 -080061 privateKeyStore_[keyName.toUri()] = ptr_lib::make_shared<RsaPrivateKey>(privateKeyDer, privateKeyDerLength);
Jeff Thompson6c314bc2013-09-23 18:09:38 -070062}
63
64void
Yingdi Yu87581582014-01-14 14:28:39 -080065SecTpmMemory::generateKeyPairInTpm(const Name& keyName, KeyType keyType, int keySize)
Jeff Thompson6c314bc2013-09-23 18:09:38 -070066{
67#if 1
Yingdi Yu28fd32f2014-01-28 19:03:03 -080068 throw Error("SecTpmMemory::generateKeyPair not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -070069#endif
70}
71
Yingdi Yu28fd32f2014-01-28 19:03:03 -080072void
73SecTpmMemory::deleteKeyPairInTpm(const Name &keyName)
74{
75 throw Error("SecTpmMemory::deleteKeyPairInTpm not implemented");
76}
77
Jeff Thompsonce115762013-12-18 14:59:56 -080078ptr_lib::shared_ptr<PublicKey>
Yingdi Yu87581582014-01-14 14:28:39 -080079SecTpmMemory::getPublicKeyFromTpm(const Name& keyName)
Jeff Thompson6c314bc2013-09-23 18:09:38 -070080{
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080081 PublicKeyStore::iterator publicKey = publicKeyStore_.find(keyName.toUri());
Jeff Thompson6c314bc2013-09-23 18:09:38 -070082 if (publicKey == publicKeyStore_.end())
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080083 throw Error(string("MemoryPrivateKeyStorage: Cannot find public key ") + keyName.toUri());
Jeff Thompson6c314bc2013-09-23 18:09:38 -070084 return publicKey->second;
85}
86
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080087Block
Yingdi Yub4bb85a2014-01-16 10:11:04 -080088SecTpmMemory::signInTpm(const uint8_t *data, size_t dataLength,
Yingdi Yu87581582014-01-14 14:28:39 -080089 const Name& keyName,
90 DigestAlgorithm digestAlgorithm)
Jeff Thompson6c314bc2013-09-23 18:09:38 -070091{
92 if (digestAlgorithm != DIGEST_ALGORITHM_SHA256)
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080093 return ConstBufferPtr();
Jeff Thompson6c314bc2013-09-23 18:09:38 -070094
Jeff Thompson6c314bc2013-09-23 18:09:38 -070095 // Find the private key and sign.
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080096 PrivateKeyStore::iterator privateKey = privateKeyStore_.find(keyName.toUri());
Jeff Thompson6c314bc2013-09-23 18:09:38 -070097 if (privateKey == privateKeyStore_.end())
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080098 throw Error(string("MemoryPrivateKeyStorage: Cannot find private key ") + keyName.toUri());
Jeff Thompson6c314bc2013-09-23 18:09:38 -070099
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800100 uint8_t digest[SHA256_DIGEST_LENGTH];
101 SHA256_CTX sha256;
102 SHA256_Init(&sha256);
103 SHA256_Update(&sha256, data, dataLength);
104 SHA256_Final(digest, &sha256);
105
106 BufferPtr signatureBuffer = ptr_lib::make_shared<Buffer>();
107 signatureBuffer->resize(RSA_size(privateKey->second->getPrivateKey()));
108
109 unsigned int signatureBitsLength;
110 if (!RSA_sign(NID_sha256, digest, sizeof(digest),
111 signatureBuffer->buf(),
112 &signatureBitsLength,
113 privateKey->second->getPrivateKey()))
114 {
115 throw Error("Error in RSA_sign");
116 }
117
118 return Block(Tlv::SignatureValue, signatureBuffer);
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700119}
120
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800121ConstBufferPtr
Yingdi Yub4bb85a2014-01-16 10:11:04 -0800122SecTpmMemory::decryptInTpm(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric)
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700123{
124#if 1
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800125 throw Error("MemoryPrivateKeyStorage::decrypt not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700126#endif
127}
128
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800129ConstBufferPtr
Yingdi Yub4bb85a2014-01-16 10:11:04 -0800130SecTpmMemory::encryptInTpm(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric)
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700131{
132#if 1
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800133 throw Error("MemoryPrivateKeyStorage::encrypt not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700134#endif
135}
136
137void
Yingdi Yub4bb85a2014-01-16 10:11:04 -0800138SecTpmMemory::generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize)
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700139{
140#if 1
Alexander Afanasyev04b22a92014-01-05 22:40:17 -0800141 throw Error("MemoryPrivateKeyStorage::generateKey not implemented");
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700142#endif
143}
144
145bool
Yingdi Yub4bb85a2014-01-16 10:11:04 -0800146SecTpmMemory::doesKeyExistInTpm(const Name& keyName, KeyClass keyClass)
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700147{
148 if (keyClass == KEY_CLASS_PUBLIC)
149 return publicKeyStore_.find(keyName.toUri()) != publicKeyStore_.end();
150 else if (keyClass == KEY_CLASS_PRIVATE)
151 return privateKeyStore_.find(keyName.toUri()) != privateKeyStore_.end();
152 else
153 // KEY_CLASS_SYMMETRIC not implemented yet.
154 return false;
155}
156
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700157}