Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 82d6a4c | 2017-12-23 19:47:20 -0500 | [diff] [blame^] | 2 | /* |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #ifndef NDN_SECURITY_TPM_TPM_HPP |
| 23 | #define NDN_SECURITY_TPM_TPM_HPP |
| 24 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 25 | #include "key-handle.hpp" |
Davide Pesavento | 82d6a4c | 2017-12-23 19:47:20 -0500 | [diff] [blame^] | 26 | #include "../key-params.hpp" |
| 27 | #include "../../name.hpp" |
Davide Pesavento | 794f687 | 2017-05-15 23:33:38 -0400 | [diff] [blame] | 28 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 29 | #include <unordered_map> |
| 30 | |
| 31 | namespace ndn { |
| 32 | namespace security { |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 33 | |
| 34 | namespace v2 { |
| 35 | class KeyChain; |
| 36 | } // namespace v2 |
| 37 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 38 | namespace tpm { |
| 39 | |
| 40 | class BackEnd; |
| 41 | |
| 42 | /** |
| 43 | * @brief represents the front-end of TPM |
| 44 | * |
| 45 | * The TPM (Trusted Platform Module) stores the private portion of a user's cryptography keys. |
| 46 | * The format and location of stored information is indicated by the TpmLocator. |
| 47 | * The TPM is designed to work with a PIB (Public Information Base) which stores public keys and |
| 48 | * related information such as certificate. |
| 49 | * |
| 50 | * The TPM also provides functionalities of crypto transformation, such as signing and decryption. |
| 51 | * |
| 52 | * A TPM consists of a unified front-end interface and a back-end implementation. The front-end |
| 53 | * cache the handles of private keys which is provided by the back-end implementation. |
| 54 | * |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 55 | * @note Tpm instance is created and managed only by v2::KeyChain. v2::KeyChain::getTpm() |
| 56 | * returns a const reference to the managed Tpm instance, through which it is possible to |
| 57 | * check existence of private keys, get public keys for the private keys, sign, and decrypt |
| 58 | * the supplied buffers using managed private keys. |
| 59 | * |
| 60 | * @throw BackEnd::Error Failure with the underlying implementation having non-semantic errors |
| 61 | * @throw Tpm::Error Failure with semantic error in the underlying implementation |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 62 | */ |
| 63 | class Tpm : noncopyable |
| 64 | { |
| 65 | public: |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 66 | class Error : public std::runtime_error |
| 67 | { |
| 68 | public: |
| 69 | explicit |
| 70 | Error(const std::string& what) |
| 71 | : std::runtime_error(what) |
| 72 | { |
| 73 | } |
| 74 | }; |
| 75 | |
| 76 | public: |
| 77 | ~Tpm(); |
| 78 | |
| 79 | std::string |
| 80 | getTpmLocator() const; |
| 81 | |
| 82 | /** |
Davide Pesavento | 9285686 | 2017-05-15 21:35:08 -0400 | [diff] [blame] | 83 | * @brief Check if a private key exists. |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 84 | * |
| 85 | * @param keyName The key name |
| 86 | * @return true if the key exists |
| 87 | */ |
| 88 | bool |
| 89 | hasKey(const Name& keyName) const; |
| 90 | |
| 91 | /** |
Davide Pesavento | 9285686 | 2017-05-15 21:35:08 -0400 | [diff] [blame] | 92 | * @return The public portion of an asymmetric key with name @p keyName, |
| 93 | * or nullptr if the key does not exist, |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 94 | * |
Davide Pesavento | 9285686 | 2017-05-15 21:35:08 -0400 | [diff] [blame] | 95 | * The public key is in PKCS#8 format. |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 96 | */ |
| 97 | ConstBufferPtr |
| 98 | getPublicKey(const Name& keyName) const; |
| 99 | |
| 100 | /** |
Davide Pesavento | 9285686 | 2017-05-15 21:35:08 -0400 | [diff] [blame] | 101 | * @brief Sign blob using the key with name @p keyName and using the digest @p digestAlgorithm. |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 102 | * |
Davide Pesavento | 9285686 | 2017-05-15 21:35:08 -0400 | [diff] [blame] | 103 | * @return The signature, or nullptr if the key does not exist. |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 104 | */ |
| 105 | ConstBufferPtr |
| 106 | sign(const uint8_t* buf, size_t size, const Name& keyName, DigestAlgorithm digestAlgorithm) const; |
| 107 | |
| 108 | /** |
Davide Pesavento | 9285686 | 2017-05-15 21:35:08 -0400 | [diff] [blame] | 109 | * @brief Decrypt blob using the key with name @p keyName. |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 110 | * |
Davide Pesavento | 9285686 | 2017-05-15 21:35:08 -0400 | [diff] [blame] | 111 | * @return The decrypted data, or nullptr if the key does not exist. |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 112 | */ |
| 113 | ConstBufferPtr |
| 114 | decrypt(const uint8_t* buf, size_t size, const Name& keyName) const; |
| 115 | |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 116 | public: // Management |
| 117 | /** |
Davide Pesavento | 9285686 | 2017-05-15 21:35:08 -0400 | [diff] [blame] | 118 | * @brief Check if the TPM is in terminal mode. |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 119 | */ |
| 120 | bool |
| 121 | isTerminalMode() const; |
| 122 | |
| 123 | /** |
Davide Pesavento | 9285686 | 2017-05-15 21:35:08 -0400 | [diff] [blame] | 124 | * @brief Set the terminal mode of the TPM. |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 125 | * |
Davide Pesavento | 9285686 | 2017-05-15 21:35:08 -0400 | [diff] [blame] | 126 | * When in terminal mode, the TPM will not ask user permission from GUI. |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 127 | */ |
| 128 | void |
| 129 | setTerminalMode(bool isTerminal) const; |
| 130 | |
| 131 | /** |
Davide Pesavento | 9285686 | 2017-05-15 21:35:08 -0400 | [diff] [blame] | 132 | * @return true if the TPM is locked, otherwise false. |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 133 | */ |
| 134 | bool |
| 135 | isTpmLocked() const; |
| 136 | |
| 137 | /** |
Davide Pesavento | 9285686 | 2017-05-15 21:35:08 -0400 | [diff] [blame] | 138 | * @brief Unlock the TPM. |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 139 | * |
Davide Pesavento | 9285686 | 2017-05-15 21:35:08 -0400 | [diff] [blame] | 140 | * @param password The password to unlock the TPM. |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 141 | * @param passwordLength The password size. |
| 142 | */ |
| 143 | bool |
| 144 | unlockTpm(const char* password, size_t passwordLength) const; |
| 145 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 146 | NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 147 | /* |
Davide Pesavento | 9285686 | 2017-05-15 21:35:08 -0400 | [diff] [blame] | 148 | * @brief Create a new TPM instance with the specified @p location. |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 149 | * |
| 150 | * @param scheme The scheme for the TPM |
| 151 | * @param location The location for the TPM |
| 152 | * @param impl The back-end implementation |
| 153 | */ |
| 154 | Tpm(const std::string& scheme, const std::string& location, unique_ptr<BackEnd> impl); |
| 155 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 156 | /** |
| 157 | * @brief Create key for @p identityName according to @p params. |
| 158 | * |
| 159 | * The created key is named as: /<identityName>/[keyId]/KEY |
| 160 | * |
Davide Pesavento | 9285686 | 2017-05-15 21:35:08 -0400 | [diff] [blame] | 161 | * @return The key name. |
| 162 | * @throw Tpm::Error the key already exists or @p params is invalid. |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 163 | */ |
| 164 | Name |
| 165 | createKey(const Name& identityName, const KeyParams& params); |
| 166 | |
| 167 | /** |
| 168 | * @brief Delete a key pair with name @p keyName. |
| 169 | */ |
| 170 | void |
| 171 | deleteKey(const Name& keyName); |
| 172 | |
| 173 | /** |
Davide Pesavento | 9285686 | 2017-05-15 21:35:08 -0400 | [diff] [blame] | 174 | * @brief Export a private key. |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 175 | * |
Davide Pesavento | 9285686 | 2017-05-15 21:35:08 -0400 | [diff] [blame] | 176 | * Export a private key in encrypted PKCS #8 format. |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 177 | * |
Davide Pesavento | 9285686 | 2017-05-15 21:35:08 -0400 | [diff] [blame] | 178 | * @param keyName The private key name |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 179 | * @param pw The password to encrypt the private key |
| 180 | * @param pwLen The length of the password |
Davide Pesavento | 9285686 | 2017-05-15 21:35:08 -0400 | [diff] [blame] | 181 | * @return The encoded private key wrapper. |
Davide Pesavento | 82d6a4c | 2017-12-23 19:47:20 -0500 | [diff] [blame^] | 182 | * @throw BackEnd::Error The key does not exist or it could not be exported. |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 183 | */ |
| 184 | ConstBufferPtr |
Davide Pesavento | 794f687 | 2017-05-15 23:33:38 -0400 | [diff] [blame] | 185 | exportPrivateKey(const Name& keyName, const char* pw, size_t pwLen) const; |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 186 | |
| 187 | /** |
Davide Pesavento | 9285686 | 2017-05-15 21:35:08 -0400 | [diff] [blame] | 188 | * @brief Import a private key. |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 189 | * |
| 190 | * @param keyName The private key name |
| 191 | * @param pkcs8 The private key wrapper |
| 192 | * @param pkcs8Len The length of the private key wrapper |
| 193 | * @param pw The password to encrypt the private key |
| 194 | * @param pwLen The length of the password |
Davide Pesavento | 82d6a4c | 2017-12-23 19:47:20 -0500 | [diff] [blame^] | 195 | * @throw BackEnd::Error The key could not be imported. |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 196 | */ |
Davide Pesavento | 82d6a4c | 2017-12-23 19:47:20 -0500 | [diff] [blame^] | 197 | void |
Davide Pesavento | 9285686 | 2017-05-15 21:35:08 -0400 | [diff] [blame] | 198 | importPrivateKey(const Name& keyName, const uint8_t* pkcs8, size_t pkcs8Len, |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 199 | const char* pw, size_t pwLen); |
| 200 | |
| 201 | /** |
Davide Pesavento | 9285686 | 2017-05-15 21:35:08 -0400 | [diff] [blame] | 202 | * @brief Clear the key cache. |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 203 | * |
Davide Pesavento | 82d6a4c | 2017-12-23 19:47:20 -0500 | [diff] [blame^] | 204 | * An empty cache can force Tpm to do key lookup in the back-end. |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 205 | */ |
| 206 | void |
| 207 | clearKeyCache() |
| 208 | { |
| 209 | m_keys.clear(); |
| 210 | } |
| 211 | |
| 212 | private: |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 213 | /** |
Davide Pesavento | 9285686 | 2017-05-15 21:35:08 -0400 | [diff] [blame] | 214 | * @brief Internal KeyHandle lookup. |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 215 | * |
| 216 | * @return A pointer to the handle of key @p keyName if it exists, otherwise nullptr. |
| 217 | */ |
| 218 | const KeyHandle* |
| 219 | findKey(const Name& keyName) const; |
| 220 | |
| 221 | private: |
| 222 | std::string m_scheme; |
| 223 | std::string m_location; |
| 224 | |
| 225 | mutable std::unordered_map<Name, unique_ptr<KeyHandle>> m_keys; |
| 226 | |
Davide Pesavento | 794f687 | 2017-05-15 23:33:38 -0400 | [diff] [blame] | 227 | const unique_ptr<BackEnd> m_backEnd; |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 228 | |
| 229 | friend class v2::KeyChain; |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 230 | }; |
| 231 | |
| 232 | } // namespace tpm |
| 233 | |
| 234 | using tpm::Tpm; |
| 235 | |
| 236 | } // namespace security |
| 237 | } // namespace ndn |
| 238 | |
| 239 | #endif // NDN_SECURITY_TPM_TPM_HPP |