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