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 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 79 | ConstBufferPtr |
| 80 | SecTpmMemory::exportPrivateKeyPkcs1FromTpm(const Name& keyName) |
| 81 | { |
| 82 | throw Error("SecTpmMemory::exportPrivateKeyPkcs1FromTpm is not implemented"); |
| 83 | } |
| 84 | |
| 85 | bool |
| 86 | SecTpmMemory::importPrivateKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size) |
| 87 | { |
| 88 | throw Error("SecTpmMemory::importPrivateKeyPkcs1IntoTpm is not implemented"); |
| 89 | } |
| 90 | |
| 91 | bool |
| 92 | SecTpmMemory::importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size) |
| 93 | { |
| 94 | throw Error("SecTpmMemory::importPublicKeyPkcs1IntoTpm is not implemented"); |
| 95 | } |
| 96 | |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 97 | ptr_lib::shared_ptr<PublicKey> |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 98 | SecTpmMemory::getPublicKeyFromTpm(const Name& keyName) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 99 | { |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 100 | PublicKeyStore::iterator publicKey = publicKeyStore_.find(keyName.toUri()); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 101 | if (publicKey == publicKeyStore_.end()) |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 102 | throw Error(string("MemoryPrivateKeyStorage: Cannot find public key ") + keyName.toUri()); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 103 | return publicKey->second; |
| 104 | } |
| 105 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 106 | Block |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 107 | SecTpmMemory::signInTpm(const uint8_t *data, size_t dataLength, |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 108 | const Name& keyName, |
| 109 | DigestAlgorithm digestAlgorithm) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 110 | { |
| 111 | if (digestAlgorithm != DIGEST_ALGORITHM_SHA256) |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 112 | return ConstBufferPtr(); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 113 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 114 | // Find the private key and sign. |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 115 | PrivateKeyStore::iterator privateKey = privateKeyStore_.find(keyName.toUri()); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 116 | if (privateKey == privateKeyStore_.end()) |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 117 | throw Error(string("MemoryPrivateKeyStorage: Cannot find private key ") + keyName.toUri()); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 118 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 119 | uint8_t digest[SHA256_DIGEST_LENGTH]; |
| 120 | SHA256_CTX sha256; |
| 121 | SHA256_Init(&sha256); |
| 122 | SHA256_Update(&sha256, data, dataLength); |
| 123 | SHA256_Final(digest, &sha256); |
| 124 | |
| 125 | BufferPtr signatureBuffer = ptr_lib::make_shared<Buffer>(); |
| 126 | signatureBuffer->resize(RSA_size(privateKey->second->getPrivateKey())); |
| 127 | |
| 128 | unsigned int signatureBitsLength; |
| 129 | if (!RSA_sign(NID_sha256, digest, sizeof(digest), |
| 130 | signatureBuffer->buf(), |
| 131 | &signatureBitsLength, |
| 132 | privateKey->second->getPrivateKey())) |
| 133 | { |
| 134 | throw Error("Error in RSA_sign"); |
| 135 | } |
| 136 | |
| 137 | return Block(Tlv::SignatureValue, signatureBuffer); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 140 | ConstBufferPtr |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 141 | SecTpmMemory::decryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 142 | { |
| 143 | #if 1 |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 144 | throw Error("MemoryPrivateKeyStorage::decrypt not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 145 | #endif |
| 146 | } |
| 147 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 148 | ConstBufferPtr |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 149 | SecTpmMemory::encryptInTpm(const uint8_t* data, size_t dataLength, const Name& keyName, bool isSymmetric) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 150 | { |
| 151 | #if 1 |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 152 | throw Error("MemoryPrivateKeyStorage::encrypt not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 153 | #endif |
| 154 | } |
| 155 | |
| 156 | void |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 157 | SecTpmMemory::generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 158 | { |
| 159 | #if 1 |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 160 | throw Error("MemoryPrivateKeyStorage::generateKey not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 161 | #endif |
| 162 | } |
| 163 | |
| 164 | bool |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 165 | SecTpmMemory::doesKeyExistInTpm(const Name& keyName, KeyClass keyClass) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 166 | { |
| 167 | if (keyClass == KEY_CLASS_PUBLIC) |
| 168 | return publicKeyStore_.find(keyName.toUri()) != publicKeyStore_.end(); |
| 169 | else if (keyClass == KEY_CLASS_PRIVATE) |
| 170 | return privateKeyStore_.find(keyName.toUri()) != privateKeyStore_.end(); |
| 171 | else |
| 172 | // KEY_CLASS_SYMMETRIC not implemented yet. |
| 173 | return false; |
| 174 | } |
| 175 | |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 176 | bool |
| 177 | SecTpmMemory::generateRandomBlock(uint8_t* res, size_t size) |
| 178 | { |
| 179 | try{ |
| 180 | CryptoPP::AutoSeededRandomPool rng; |
| 181 | rng.GenerateBlock(res, size); |
| 182 | return true; |
| 183 | }catch(const CryptoPP::Exception& e){ |
| 184 | return false; |
| 185 | } |
| 186 | } |
| 187 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 188 | } // namespace ndn |