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