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 | |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 8 | #ifndef NDN_SEC_TPM_MEMORY_HPP |
| 9 | #define NDN_SEC_TPM_MEMORY_HPP |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 10 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 11 | #include "../common.hpp" |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 12 | #include "sec-tpm.hpp" |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 13 | |
| 14 | struct rsa_st; |
| 15 | |
| 16 | namespace ndn { |
| 17 | |
| 18 | /** |
| 19 | * MemoryPrivateKeyStorage extends PrivateKeyStorage to implement a simple in-memory private key store. You should |
| 20 | * initialize by calling setKeyPairForKeyName. |
| 21 | */ |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 22 | class SecTpmMemory : public SecTpm { |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 23 | public: |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 24 | struct Error : public SecTpm::Error { Error(const std::string &what) : SecTpm::Error(what) {} }; |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 25 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 26 | /** |
| 27 | * The virtual destructor |
| 28 | */ |
| 29 | virtual |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 30 | ~SecTpmMemory(); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 31 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame^] | 32 | /****************************** |
| 33 | * From TrustedPlatformModule * |
| 34 | ******************************/ |
| 35 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 36 | virtual void |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 37 | generateKeyPairInTpm(const Name& keyName, KeyType keyType, int keySize); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Get the public key |
| 41 | * @param keyName The name of public key. |
| 42 | * @return The public key. |
| 43 | */ |
| 44 | virtual ptr_lib::shared_ptr<PublicKey> |
Yingdi Yu | 8758158 | 2014-01-14 14:28:39 -0800 | [diff] [blame] | 45 | getPublicKeyFromTpm(const Name& keyName); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Delete a key pair of asymmetric keys. |
| 49 | * @param keyName The name of the key pair. |
| 50 | */ |
| 51 | virtual void |
| 52 | deleteKeyPairInTpm(const Name &keyName); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * Fetch the private key for keyName and sign the data, returning a signature Blob. |
| 56 | * @param data Pointer to the input byte array. |
| 57 | * @param dataLength The length of data. |
| 58 | * @param keyName The name of the signing key. |
| 59 | * @param digestAlgorithm the digest algorithm. |
| 60 | * @return The signature, or a null pointer if signing fails. |
| 61 | */ |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 62 | virtual Block |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 63 | signInTpm(const uint8_t *data, size_t dataLength, const Name& keyName, DigestAlgorithm digestAlgorithm); |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 64 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 65 | /** |
| 66 | * Decrypt data. |
| 67 | * @param keyName The name of the decrypting key. |
| 68 | * @param data The byte to be decrypted. |
| 69 | * @param dataLength the length of data. |
| 70 | * @param isSymmetric If true symmetric encryption is used, otherwise asymmetric decryption is used. |
| 71 | * @return The decrypted data. |
| 72 | */ |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 73 | virtual ConstBufferPtr |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 74 | 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] | 75 | |
| 76 | /** |
| 77 | * Encrypt data. |
| 78 | * @param keyName The name of the encrypting key. |
| 79 | * @param data The byte to be encrypted. |
| 80 | * @param dataLength the length of data. |
| 81 | * @param isSymmetric If true symmetric encryption is used, otherwise asymmetric decryption is used. |
| 82 | * @return The encrypted data. |
| 83 | */ |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 84 | virtual ConstBufferPtr |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 85 | 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] | 86 | |
| 87 | /** |
| 88 | * @brief Generate a symmetric key. |
| 89 | * @param keyName The name of the key. |
| 90 | * @param keyType The type of the key, e.g. KEY_TYPE_AES. |
| 91 | * @param keySize The size of the key. |
| 92 | */ |
| 93 | virtual void |
Yingdi Yu | b4bb85a | 2014-01-16 10:11:04 -0800 | [diff] [blame] | 94 | generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize); |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 95 | |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 96 | virtual bool |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame^] | 97 | doesKeyExistInTpm(const Name& keyName, KeyClass keyClass); |
| 98 | |
| 99 | virtual bool |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 100 | generateRandomBlock(uint8_t* res, size_t size); |
| 101 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame^] | 102 | /****************************** |
| 103 | * SecTpmMemory specific * |
| 104 | ******************************/ |
| 105 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 106 | /** |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame^] | 107 | * @brief Set the public and private key for the keyName. |
| 108 | * |
| 109 | * @param keyName The key name. |
| 110 | * @param publicKeyDer The public key DER byte array. |
| 111 | * @param publicKeyDerLength The length of publicKeyDer. |
| 112 | * @param privateKeyDer The private key DER byte array. |
| 113 | * @param privateKeyDerLength The length of privateKeyDer. |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 114 | */ |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame^] | 115 | void setKeyPairForKeyName(const Name& keyName, |
| 116 | uint8_t *publicKeyDer, size_t publicKeyDerLength, |
| 117 | uint8_t *privateKeyDer, size_t privateKeyDerLength); |
| 118 | |
| 119 | protected: |
| 120 | /****************************** |
| 121 | * From TrustedPlatformModule * |
| 122 | ******************************/ |
| 123 | virtual ConstBufferPtr |
| 124 | exportPrivateKeyPkcs1FromTpm(const Name& keyName); |
| 125 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 126 | virtual bool |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame^] | 127 | importPrivateKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size); |
| 128 | |
| 129 | virtual bool |
| 130 | importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size); |
| 131 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 132 | |
| 133 | private: |
Alexander Afanasyev | e64788e | 2014-01-05 22:38:21 -0800 | [diff] [blame] | 134 | class RsaPrivateKey; |
| 135 | |
| 136 | typedef std::map<std::string, ptr_lib::shared_ptr<PublicKey> > PublicKeyStore; |
| 137 | typedef std::map<std::string, ptr_lib::shared_ptr<RsaPrivateKey> > PrivateKeyStore; |
| 138 | |
| 139 | PublicKeyStore publicKeyStore_; /**< The map key is the keyName.toUri() */ |
| 140 | PrivateKeyStore privateKeyStore_; /**< The map key is the keyName.toUri() */ |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | } |
| 144 | |
| 145 | #endif |