Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 7 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 8 | * |
| 9 | * This file licensed under New BSD License. See COPYING for detailed information about |
| 10 | * ndn-cxx library copyright, permissions, and redistribution restrictions. |
| 11 | * |
| 12 | * @author Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 13 | */ |
| 14 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 15 | #include "common.hpp" |
| 16 | |
| 17 | #include "sec-tpm-memory.hpp" |
| 18 | #include "public-key.hpp" |
| 19 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 20 | #include "openssl.hpp" |
Junxiao Shi | 482ccc5 | 2014-03-31 13:05:24 -0700 | [diff] [blame] | 21 | #include "cryptopp.hpp" |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 22 | |
| 23 | using namespace std; |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 24 | |
| 25 | namespace ndn { |
| 26 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 27 | /** |
| 28 | * RsaPrivateKey is a simple class to hold an RSA private key. |
| 29 | */ |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 30 | class SecTpmMemory::RsaPrivateKey |
| 31 | { |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 32 | public: |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 33 | RsaPrivateKey(const uint8_t* keyDer, size_t keyDerLength) |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 34 | { |
| 35 | // Use a temporary pointer since d2i updates it. |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 36 | const uint8_t* derPointer = keyDer; |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 37 | m_privateKey = d2i_RSAPrivateKey(NULL, &derPointer, keyDerLength); |
| 38 | if (!m_privateKey) |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 39 | throw Error("RsaPrivateKey constructor: Error decoding private key DER"); |
| 40 | } |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 41 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 42 | ~RsaPrivateKey() |
| 43 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 44 | if (m_privateKey) |
| 45 | RSA_free(m_privateKey); |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 46 | } |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 47 | |
| 48 | rsa_st* |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 49 | getPrivateKey() |
| 50 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 51 | return m_privateKey; |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 52 | } |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 53 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 54 | private: |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 55 | rsa_st* m_privateKey; |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 56 | }; |
| 57 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 58 | SecTpmMemory::~SecTpmMemory() |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 59 | { |
| 60 | } |
| 61 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 62 | void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 63 | SecTpmMemory::setKeyPairForKeyName(const Name& keyName, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 64 | const uint8_t* publicKeyDer, size_t publicKeyDerLength, |
| 65 | const uint8_t* privateKeyDer, size_t privateKeyDerLength) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 66 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 67 | m_publicKeyStore[keyName.toUri()] = make_shared<PublicKey>(publicKeyDer, publicKeyDerLength); |
| 68 | m_privateKeyStore[keyName.toUri()] = make_shared<RsaPrivateKey>(privateKeyDer, |
| 69 | privateKeyDerLength); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 72 | void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 73 | SecTpmMemory::generateKeyPairInTpm(const Name& keyName, KeyType keyType, int keySize) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 74 | { |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 75 | throw Error("SecTpmMemory::generateKeyPair not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 78 | void |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 79 | SecTpmMemory::deleteKeyPairInTpm(const Name& keyName) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 80 | { |
| 81 | throw Error("SecTpmMemory::deleteKeyPairInTpm not implemented"); |
| 82 | } |
| 83 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 84 | ConstBufferPtr |
| 85 | SecTpmMemory::exportPrivateKeyPkcs1FromTpm(const Name& keyName) |
| 86 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 87 | return shared_ptr<Buffer>(); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | bool |
| 91 | SecTpmMemory::importPrivateKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size) |
| 92 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 93 | return false; |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | bool |
| 97 | SecTpmMemory::importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size) |
| 98 | { |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 99 | return false; |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 102 | shared_ptr<PublicKey> |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 103 | SecTpmMemory::getPublicKeyFromTpm(const Name& keyName) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 104 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 105 | PublicKeyStore::iterator publicKey = m_publicKeyStore.find(keyName.toUri()); |
| 106 | if (publicKey == m_publicKeyStore.end()) |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 107 | throw Error(string("MemoryPrivateKeyStorage: Cannot find public key ") + keyName.toUri()); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 108 | return publicKey->second; |
| 109 | } |
| 110 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 111 | Block |
| 112 | SecTpmMemory::signInTpm(const uint8_t* data, size_t dataLength, |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 113 | const Name& keyName, |
| 114 | DigestAlgorithm digestAlgorithm) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 115 | { |
| 116 | if (digestAlgorithm != DIGEST_ALGORITHM_SHA256) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 117 | throw Error("Unsupported digest algorithm."); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 118 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 119 | // Find the private key and sign. |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 120 | PrivateKeyStore::iterator privateKey = m_privateKeyStore.find(keyName.toUri()); |
| 121 | if (privateKey == m_privateKeyStore.end()) |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 122 | throw Error(string("MemoryPrivateKeyStorage: Cannot find private key ") + keyName.toUri()); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 123 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 124 | uint8_t digest[SHA256_DIGEST_LENGTH]; |
| 125 | SHA256_CTX sha256; |
| 126 | SHA256_Init(&sha256); |
| 127 | SHA256_Update(&sha256, data, dataLength); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 128 | SHA256_Final(digest,& sha256); |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 129 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 130 | BufferPtr signatureBuffer = make_shared<Buffer>(); |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 131 | signatureBuffer->resize(RSA_size(privateKey->second->getPrivateKey())); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 132 | |
| 133 | unsigned int signatureBitsLength; |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 134 | if (!RSA_sign(NID_sha256, digest, sizeof(digest), |
| 135 | signatureBuffer->buf(), |
| 136 | &signatureBitsLength, |
| 137 | privateKey->second->getPrivateKey())) |
| 138 | { |
| 139 | throw Error("Error in RSA_sign"); |
| 140 | } |
| 141 | |
| 142 | return Block(Tlv::SignatureValue, signatureBuffer); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 145 | ConstBufferPtr |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 146 | SecTpmMemory::decryptInTpm(const uint8_t* data, size_t dataLength, |
| 147 | const Name& keyName, bool isSymmetric) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 148 | { |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 149 | throw Error("MemoryPrivateKeyStorage::decrypt not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 152 | ConstBufferPtr |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 153 | SecTpmMemory::encryptInTpm(const uint8_t* data, size_t dataLength, |
| 154 | const Name& keyName, bool isSymmetric) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 155 | { |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 156 | throw Error("MemoryPrivateKeyStorage::encrypt not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 159 | void |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 160 | SecTpmMemory::generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 161 | { |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 162 | throw Error("MemoryPrivateKeyStorage::generateKey not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | bool |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 166 | SecTpmMemory::doesKeyExistInTpm(const Name& keyName, KeyClass keyClass) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 167 | { |
| 168 | if (keyClass == KEY_CLASS_PUBLIC) |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 169 | return m_publicKeyStore.find(keyName.toUri()) != m_publicKeyStore.end(); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 170 | else if (keyClass == KEY_CLASS_PRIVATE) |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 171 | return m_privateKeyStore.find(keyName.toUri()) != m_privateKeyStore.end(); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 172 | else |
| 173 | // KEY_CLASS_SYMMETRIC not implemented yet. |
| 174 | return false; |
| 175 | } |
| 176 | |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 177 | bool |
| 178 | SecTpmMemory::generateRandomBlock(uint8_t* res, size_t size) |
| 179 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 180 | try |
| 181 | { |
| 182 | CryptoPP::AutoSeededRandomPool rng; |
| 183 | rng.GenerateBlock(res, size); |
| 184 | return true; |
| 185 | } |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 186 | catch (CryptoPP::Exception& e) |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 187 | { |
| 188 | return false; |
| 189 | } |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 192 | } // namespace ndn |