Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 68b5385 | 2018-07-25 13:56:38 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2018 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_BACK_END_HPP |
| 23 | #define NDN_SECURITY_TPM_BACK_END_HPP |
| 24 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 25 | #include "../key-params.hpp" |
Davide Pesavento | 5ee8ec0 | 2018-09-01 19:06:12 -0400 | [diff] [blame] | 26 | #include "../../encoding/buffer.hpp" |
| 27 | #include "../../name.hpp" |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 28 | |
| 29 | namespace ndn { |
| 30 | namespace security { |
| 31 | namespace tpm { |
| 32 | |
| 33 | class KeyHandle; |
| 34 | |
| 35 | /** |
Davide Pesavento | 5ee8ec0 | 2018-09-01 19:06:12 -0400 | [diff] [blame] | 36 | * @brief Abstract interface for a TPM backend implementation. |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 37 | * |
| 38 | * This class provides KeyHandle to the front-end and other TPM management operations. |
| 39 | */ |
| 40 | class BackEnd : noncopyable |
| 41 | { |
| 42 | public: |
| 43 | class Error : public std::runtime_error |
| 44 | { |
| 45 | public: |
Junxiao Shi | 68b5385 | 2018-07-25 13:56:38 -0600 | [diff] [blame] | 46 | using std::runtime_error::runtime_error; |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | public: |
| 50 | virtual |
| 51 | ~BackEnd(); |
| 52 | |
| 53 | public: // key management |
| 54 | /** |
Davide Pesavento | 5ee8ec0 | 2018-09-01 19:06:12 -0400 | [diff] [blame] | 55 | * @brief Check if the key with name @p keyName exists in the TPM. |
| 56 | * |
| 57 | * @return True if the key exists, false otherwise. |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 58 | */ |
| 59 | bool |
| 60 | hasKey(const Name& keyName) const; |
| 61 | |
| 62 | /** |
Davide Pesavento | 5ee8ec0 | 2018-09-01 19:06:12 -0400 | [diff] [blame] | 63 | * @brief Get the handle of the key with name @p keyName. |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 64 | * |
Davide Pesavento | 5ee8ec0 | 2018-09-01 19:06:12 -0400 | [diff] [blame] | 65 | * Calling this function multiple times with the same @p keyName will return different KeyHandle |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 66 | * objects that all refer to the same key. |
Davide Pesavento | 5ee8ec0 | 2018-09-01 19:06:12 -0400 | [diff] [blame] | 67 | * |
| 68 | * @return The handle of the key, or nullptr if the key does not exist. |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 69 | */ |
| 70 | unique_ptr<KeyHandle> |
| 71 | getKeyHandle(const Name& keyName) const; |
| 72 | |
| 73 | /** |
Davide Pesavento | 5ee8ec0 | 2018-09-01 19:06:12 -0400 | [diff] [blame] | 74 | * @brief Create a key for @p identityName according to @p params. |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 75 | * |
| 76 | * @return The handle of the created key. |
Davide Pesavento | 5ee8ec0 | 2018-09-01 19:06:12 -0400 | [diff] [blame] | 77 | * @throw Tpm::Error @p params are invalid. |
| 78 | * @throw Error The key could not be created. |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 79 | */ |
| 80 | unique_ptr<KeyHandle> |
Davide Pesavento | 5ee8ec0 | 2018-09-01 19:06:12 -0400 | [diff] [blame] | 81 | createKey(const Name& identityName, const KeyParams& params); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 82 | |
| 83 | /** |
Davide Pesavento | 5ee8ec0 | 2018-09-01 19:06:12 -0400 | [diff] [blame] | 84 | * @brief Delete the key with name @p keyName. |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 85 | * |
Davide Pesavento | 5ee8ec0 | 2018-09-01 19:06:12 -0400 | [diff] [blame] | 86 | * @warning Continuing to use existing KeyHandle objects for a deleted key |
| 87 | * results in undefined behavior. |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 88 | * |
Davide Pesavento | 5ee8ec0 | 2018-09-01 19:06:12 -0400 | [diff] [blame] | 89 | * @throw Error The key could not be deleted. |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 90 | */ |
| 91 | void |
| 92 | deleteKey(const Name& keyName); |
| 93 | |
| 94 | /** |
Davide Pesavento | 5ee8ec0 | 2018-09-01 19:06:12 -0400 | [diff] [blame] | 95 | * @brief Get the private key with name @p keyName in encrypted PKCS #8 format. |
| 96 | * |
| 97 | * @param keyName The name of the key. |
| 98 | * @param pw The password to encrypt the private key. |
| 99 | * @param pwLen The length of the password. |
| 100 | * |
| 101 | * @return The encoded private key. |
| 102 | * @throw Error The key does not exist or cannot be exported. |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 103 | */ |
| 104 | ConstBufferPtr |
| 105 | exportKey(const Name& keyName, const char* pw, size_t pwLen); |
| 106 | |
| 107 | /** |
Davide Pesavento | 5ee8ec0 | 2018-09-01 19:06:12 -0400 | [diff] [blame] | 108 | * @brief Import a private key in encrypted PKCS #8 format. |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 109 | * |
Davide Pesavento | 5ee8ec0 | 2018-09-01 19:06:12 -0400 | [diff] [blame] | 110 | * @param keyName The name of the key to use in the TPM. |
| 111 | * @param pkcs8 Pointer to the key in encrypted PKCS #8 format. |
| 112 | * @param pkcs8Len The size of the key in encrypted PKCS #8 format. |
| 113 | * @param pw The password to decrypt the private key. |
| 114 | * @param pwLen The length of the password. |
| 115 | * |
| 116 | * @throw Error The key could not be imported. |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 117 | */ |
| 118 | void |
| 119 | importKey(const Name& keyName, const uint8_t* pkcs8, size_t pkcs8Len, const char* pw, size_t pwLen); |
| 120 | |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 121 | /** |
Davide Pesavento | 5ee8ec0 | 2018-09-01 19:06:12 -0400 | [diff] [blame] | 122 | * @brief Check if the TPM is in terminal mode. |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 123 | * |
Davide Pesavento | 5ee8ec0 | 2018-09-01 19:06:12 -0400 | [diff] [blame] | 124 | * The default implementation always returns true. |
| 125 | * |
| 126 | * @return True if in terminal mode, false otherwise. |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 127 | */ |
| 128 | virtual bool |
| 129 | isTerminalMode() const; |
| 130 | |
| 131 | /** |
Davide Pesavento | 5ee8ec0 | 2018-09-01 19:06:12 -0400 | [diff] [blame] | 132 | * @brief Set the terminal mode of the TPM. |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 133 | * |
Davide Pesavento | 5ee8ec0 | 2018-09-01 19:06:12 -0400 | [diff] [blame] | 134 | * In terminal mode, the TPM will not ask for a password from the GUI. |
| 135 | * The default implementation does nothing. |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 136 | */ |
| 137 | virtual void |
| 138 | setTerminalMode(bool isTerminal) const; |
| 139 | |
| 140 | /** |
Davide Pesavento | 5ee8ec0 | 2018-09-01 19:06:12 -0400 | [diff] [blame] | 141 | * @brief Check if the TPM is locked. |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 142 | * |
Davide Pesavento | 5ee8ec0 | 2018-09-01 19:06:12 -0400 | [diff] [blame] | 143 | * The default implementation always returns false. |
| 144 | * |
| 145 | * @return True if locked, false otherwise. |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 146 | */ |
| 147 | virtual bool |
| 148 | isTpmLocked() const; |
| 149 | |
| 150 | /** |
Davide Pesavento | 5ee8ec0 | 2018-09-01 19:06:12 -0400 | [diff] [blame] | 151 | * @brief Unlock the TPM. |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 152 | * |
Davide Pesavento | 5ee8ec0 | 2018-09-01 19:06:12 -0400 | [diff] [blame] | 153 | * The default implementation does nothing and returns `!isTpmLocked()`. |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 154 | * |
Davide Pesavento | 5ee8ec0 | 2018-09-01 19:06:12 -0400 | [diff] [blame] | 155 | * @param pw The password to unlock the TPM. |
| 156 | * @param pwLen The length of the password. |
| 157 | * |
| 158 | * @return True if the TPM was unlocked. |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 159 | */ |
| 160 | virtual bool |
| 161 | unlockTpm(const char* pw, size_t pwLen) const; |
| 162 | |
Davide Pesavento | 5ee8ec0 | 2018-09-01 19:06:12 -0400 | [diff] [blame] | 163 | protected: // static helper methods |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 164 | /** |
Davide Pesavento | 5ee8ec0 | 2018-09-01 19:06:12 -0400 | [diff] [blame] | 165 | * @brief Set the key name in @p keyHandle according to @p identity and @p params. |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 166 | */ |
| 167 | static void |
| 168 | setKeyName(KeyHandle& keyHandle, const Name& identity, const KeyParams& params); |
| 169 | |
| 170 | private: // pure virtual methods |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 171 | virtual bool |
| 172 | doHasKey(const Name& keyName) const = 0; |
| 173 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 174 | virtual unique_ptr<KeyHandle> |
| 175 | doGetKeyHandle(const Name& keyName) const = 0; |
| 176 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 177 | virtual unique_ptr<KeyHandle> |
| 178 | doCreateKey(const Name& identity, const KeyParams& params) = 0; |
| 179 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 180 | virtual void |
| 181 | doDeleteKey(const Name& keyName) = 0; |
| 182 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 183 | virtual ConstBufferPtr |
| 184 | doExportKey(const Name& keyName, const char* pw, size_t pwLen) = 0; |
| 185 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 186 | virtual void |
| 187 | doImportKey(const Name& keyName, const uint8_t* pkcs8, size_t pkcs8Len, const char* pw, size_t pwLen) = 0; |
| 188 | }; |
| 189 | |
| 190 | } // namespace tpm |
| 191 | } // namespace security |
| 192 | } // namespace ndn |
| 193 | |
| 194 | #endif // NDN_SECURITY_TPM_BACK_END_HPP |