Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 8 | #include "common.hpp" |
| 9 | |
| 10 | #include "sec-tpm-memory.hpp" |
| 11 | #include "public-key.hpp" |
| 12 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 13 | #include <openssl/ssl.h> |
| 14 | #include <openssl/sha.h> |
| 15 | #include <openssl/rsa.h> |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame^] | 16 | #include <cryptopp/osrng.h> |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 17 | |
| 18 | using namespace std; |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 19 | |
| 20 | namespace ndn { |
| 21 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 22 | /** |
| 23 | * RsaPrivateKey is a simple class to hold an RSA private key. |
| 24 | */ |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 25 | class SecTpmMemory::RsaPrivateKey { |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 26 | public: |
| 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 | |
| 48 | private: |
| 49 | rsa_st * privateKey_; |
| 50 | }; |
| 51 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 52 | SecTpmMemory::~SecTpmMemory() |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 53 | { |
| 54 | } |
| 55 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 56 | void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 57 | SecTpmMemory::setKeyPairForKeyName(const Name& keyName, |
| 58 | uint8_t *publicKeyDer, size_t publicKeyDerLength, |
| 59 | uint8_t *privateKeyDer, size_t privateKeyDerLength) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 60 | { |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 61 | publicKeyStore_[keyName.toUri()] = ptr_lib::make_shared<PublicKey>(publicKeyDer, publicKeyDerLength); |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 62 | privateKeyStore_[keyName.toUri()] = ptr_lib::make_shared<RsaPrivateKey>(privateKeyDer, privateKeyDerLength); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 66 | SecTpmMemory::generateKeyPairInTpm(const Name& keyName, KeyType keyType, int keySize) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 67 | { |
| 68 | #if 1 |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 69 | throw Error("SecTpmMemory::generateKeyPair not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 70 | #endif |
| 71 | } |
| 72 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 73 | void |
| 74 | SecTpmMemory::deleteKeyPairInTpm(const Name &keyName) |
| 75 | { |
| 76 | throw Error("SecTpmMemory::deleteKeyPairInTpm not implemented"); |
| 77 | } |
| 78 | |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 79 | ptr_lib::shared_ptr<PublicKey> |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 80 | SecTpmMemory::getPublicKeyFromTpm(const Name& keyName) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 81 | { |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 82 | PublicKeyStore::iterator publicKey = publicKeyStore_.find(keyName.toUri()); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 83 | if (publicKey == publicKeyStore_.end()) |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 84 | throw Error(string("MemoryPrivateKeyStorage: Cannot find public key ") + keyName.toUri()); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 85 | return publicKey->second; |
| 86 | } |
| 87 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 88 | Block |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 89 | SecTpmMemory::signInTpm(const uint8_t *data, size_t dataLength, |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 90 | const Name& keyName, |
| 91 | DigestAlgorithm digestAlgorithm) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 92 | { |
| 93 | if (digestAlgorithm != DIGEST_ALGORITHM_SHA256) |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 94 | return ConstBufferPtr(); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 95 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 96 | // Find the private key and sign. |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 97 | PrivateKeyStore::iterator privateKey = privateKeyStore_.find(keyName.toUri()); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 98 | if (privateKey == privateKeyStore_.end()) |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 99 | throw Error(string("MemoryPrivateKeyStorage: Cannot find private key ") + keyName.toUri()); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 100 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 101 | 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 Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 122 | ConstBufferPtr |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 123 | SecTpmMemory::decryptInTpm(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 124 | { |
| 125 | #if 1 |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 126 | throw Error("MemoryPrivateKeyStorage::decrypt not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 127 | #endif |
| 128 | } |
| 129 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 130 | ConstBufferPtr |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 131 | SecTpmMemory::encryptInTpm(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 132 | { |
| 133 | #if 1 |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 134 | throw Error("MemoryPrivateKeyStorage::encrypt not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 135 | #endif |
| 136 | } |
| 137 | |
| 138 | void |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 139 | SecTpmMemory::generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 140 | { |
| 141 | #if 1 |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 142 | throw Error("MemoryPrivateKeyStorage::generateKey not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 143 | #endif |
| 144 | } |
| 145 | |
| 146 | bool |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 147 | SecTpmMemory::doesKeyExistInTpm(const Name& keyName, KeyClass keyClass) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 148 | { |
| 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 Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame^] | 158 | bool |
| 159 | SecTpmMemory::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 Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 170 | } |