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 | 09c613f | 2014-01-29 00:23:58 -0800 | [diff] [blame] | 8 | #include "security/sec-tpm-memory.hpp" |
| 9 | #include "security/public-key.hpp" |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 10 | #include <openssl/ssl.h> |
| 11 | #include <openssl/sha.h> |
| 12 | #include <openssl/rsa.h> |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 13 | |
| 14 | using namespace std; |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 15 | |
| 16 | namespace ndn { |
| 17 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 18 | /** |
| 19 | * RsaPrivateKey is a simple class to hold an RSA private key. |
| 20 | */ |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 21 | class SecTpmMemory::RsaPrivateKey { |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 22 | public: |
| 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 | |
| 44 | private: |
| 45 | rsa_st * privateKey_; |
| 46 | }; |
| 47 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 48 | SecTpmMemory::~SecTpmMemory() |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 49 | { |
| 50 | } |
| 51 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 52 | void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 53 | SecTpmMemory::setKeyPairForKeyName(const Name& keyName, |
| 54 | uint8_t *publicKeyDer, size_t publicKeyDerLength, |
| 55 | uint8_t *privateKeyDer, size_t privateKeyDerLength) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 56 | { |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 57 | publicKeyStore_[keyName.toUri()] = ptr_lib::make_shared<PublicKey>(publicKeyDer, publicKeyDerLength); |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 58 | privateKeyStore_[keyName.toUri()] = ptr_lib::make_shared<RsaPrivateKey>(privateKeyDer, privateKeyDerLength); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 62 | SecTpmMemory::generateKeyPairInTpm(const Name& keyName, KeyType keyType, int keySize) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 63 | { |
| 64 | #if 1 |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 65 | throw Error("SecTpmMemory::generateKeyPair not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 66 | #endif |
| 67 | } |
| 68 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 69 | void |
| 70 | SecTpmMemory::deleteKeyPairInTpm(const Name &keyName) |
| 71 | { |
| 72 | throw Error("SecTpmMemory::deleteKeyPairInTpm not implemented"); |
| 73 | } |
| 74 | |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 75 | ptr_lib::shared_ptr<PublicKey> |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 76 | SecTpmMemory::getPublicKeyFromTpm(const Name& keyName) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 77 | { |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 78 | PublicKeyStore::iterator publicKey = publicKeyStore_.find(keyName.toUri()); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 79 | if (publicKey == publicKeyStore_.end()) |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 80 | throw Error(string("MemoryPrivateKeyStorage: Cannot find public key ") + keyName.toUri()); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 81 | return publicKey->second; |
| 82 | } |
| 83 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 84 | Block |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 85 | SecTpmMemory::signInTpm(const uint8_t *data, size_t dataLength, |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 86 | const Name& keyName, |
| 87 | DigestAlgorithm digestAlgorithm) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 88 | { |
| 89 | if (digestAlgorithm != DIGEST_ALGORITHM_SHA256) |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 90 | return ConstBufferPtr(); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 91 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 92 | // Find the private key and sign. |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 93 | PrivateKeyStore::iterator privateKey = privateKeyStore_.find(keyName.toUri()); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 94 | if (privateKey == privateKeyStore_.end()) |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 95 | throw Error(string("MemoryPrivateKeyStorage: Cannot find private key ") + keyName.toUri()); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 96 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 97 | uint8_t digest[SHA256_DIGEST_LENGTH]; |
| 98 | SHA256_CTX sha256; |
| 99 | SHA256_Init(&sha256); |
| 100 | SHA256_Update(&sha256, data, dataLength); |
| 101 | SHA256_Final(digest, &sha256); |
| 102 | |
| 103 | BufferPtr signatureBuffer = ptr_lib::make_shared<Buffer>(); |
| 104 | signatureBuffer->resize(RSA_size(privateKey->second->getPrivateKey())); |
| 105 | |
| 106 | unsigned int signatureBitsLength; |
| 107 | if (!RSA_sign(NID_sha256, digest, sizeof(digest), |
| 108 | signatureBuffer->buf(), |
| 109 | &signatureBitsLength, |
| 110 | privateKey->second->getPrivateKey())) |
| 111 | { |
| 112 | throw Error("Error in RSA_sign"); |
| 113 | } |
| 114 | |
| 115 | return Block(Tlv::SignatureValue, signatureBuffer); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 118 | ConstBufferPtr |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 119 | 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] | 120 | { |
| 121 | #if 1 |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 122 | throw Error("MemoryPrivateKeyStorage::decrypt not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 123 | #endif |
| 124 | } |
| 125 | |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 126 | ConstBufferPtr |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 127 | 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] | 128 | { |
| 129 | #if 1 |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 130 | throw Error("MemoryPrivateKeyStorage::encrypt not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 131 | #endif |
| 132 | } |
| 133 | |
| 134 | void |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 135 | SecTpmMemory::generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 136 | { |
| 137 | #if 1 |
Alexander Afanasyev | 04b22a9 | 2014-01-05 22:40:17 -0800 | [diff] [blame] | 138 | throw Error("MemoryPrivateKeyStorage::generateKey not implemented"); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 139 | #endif |
| 140 | } |
| 141 | |
| 142 | bool |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 143 | SecTpmMemory::doesKeyExistInTpm(const Name& keyName, KeyClass keyClass) |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 144 | { |
| 145 | if (keyClass == KEY_CLASS_PUBLIC) |
| 146 | return publicKeyStore_.find(keyName.toUri()) != publicKeyStore_.end(); |
| 147 | else if (keyClass == KEY_CLASS_PRIVATE) |
| 148 | return privateKeyStore_.find(keyName.toUri()) != privateKeyStore_.end(); |
| 149 | else |
| 150 | // KEY_CLASS_SYMMETRIC not implemented yet. |
| 151 | return false; |
| 152 | } |
| 153 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 154 | } |