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 | ff3bf2c | 2017-10-05 00:14:27 -0400 | [diff] [blame] | 2 | /* |
Junxiao Shi | 68b5385 | 2018-07-25 13:56:38 -0600 | [diff] [blame] | 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_OSX_HPP |
| 23 | #define NDN_SECURITY_TPM_BACK_END_OSX_HPP |
| 24 | |
| 25 | #include "back-end.hpp" |
Junxiao Shi | 0b1b467 | 2017-07-02 22:02:31 -0700 | [diff] [blame] | 26 | #include "key-ref-osx.hpp" |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 27 | |
Alexander Afanasyev | 0cf887d | 2017-03-26 16:58:59 -0500 | [diff] [blame] | 28 | #ifndef NDN_CXX_HAVE_OSX_FRAMEWORKS |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 29 | #error "This file should not be compiled ..." |
| 30 | #endif |
| 31 | |
| 32 | namespace ndn { |
| 33 | namespace security { |
| 34 | namespace tpm { |
| 35 | |
| 36 | /** |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 37 | * @brief The back-end implementation of TPM based on macOS Keychain Services. |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 38 | */ |
| 39 | class BackEndOsx : public BackEnd |
| 40 | { |
| 41 | public: |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 42 | /** |
| 43 | * @brief Create TPM backed based on macOS KeyChain service |
| 44 | * @param location Not used (required by the TPM-registration interface) |
| 45 | */ |
| 46 | explicit |
| 47 | BackEndOsx(const std::string& location = ""); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 48 | |
| 49 | ~BackEndOsx() override; |
| 50 | |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 51 | static const std::string& |
| 52 | getScheme(); |
| 53 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 54 | public: // management |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 55 | bool |
| 56 | isTerminalMode() const final; |
| 57 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 58 | void |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 59 | setTerminalMode(bool isTerminal) const final; |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 60 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 61 | bool |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 62 | isTpmLocked() const final; |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 63 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 64 | bool |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 65 | unlockTpm(const char* pw, size_t pwLen) const final; |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 66 | |
| 67 | public: // crypto transformation |
| 68 | /** |
| 69 | * @brief Sign @p buf with @p key using @p digestAlgorithm. |
| 70 | */ |
Davide Pesavento | ff3bf2c | 2017-10-05 00:14:27 -0400 | [diff] [blame] | 71 | static ConstBufferPtr |
| 72 | sign(const KeyRefOsx& key, DigestAlgorithm digestAlgorithm, const uint8_t* buf, size_t size); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 73 | |
Davide Pesavento | ff3bf2c | 2017-10-05 00:14:27 -0400 | [diff] [blame] | 74 | static ConstBufferPtr |
| 75 | decrypt(const KeyRefOsx& key, const uint8_t* cipherText, size_t cipherSize); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 76 | |
Davide Pesavento | ff3bf2c | 2017-10-05 00:14:27 -0400 | [diff] [blame] | 77 | static ConstBufferPtr |
| 78 | derivePublicKey(const KeyRefOsx& key); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 79 | |
| 80 | private: // inherited from tpm::BackEnd |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 81 | /** |
| 82 | * @return True if a key with name @p keyName exists in TPM. |
| 83 | */ |
| 84 | bool |
| 85 | doHasKey(const Name& keyName) const final; |
| 86 | |
| 87 | /** |
| 88 | * @return The handle of a key with name @p keyName, or nullptr if the key does not exist |
| 89 | */ |
| 90 | unique_ptr<KeyHandle> |
| 91 | doGetKeyHandle(const Name& keyName) const final; |
| 92 | |
| 93 | /** |
| 94 | * @brief Create key for @p identityName according to @p params. |
| 95 | * |
| 96 | * The created key is named as: /<identityName>/[keyId]/KEY |
| 97 | * The key name is set in the returned KeyHandle. |
| 98 | * |
| 99 | * @return The handle of the created key. |
| 100 | */ |
| 101 | unique_ptr<KeyHandle> |
| 102 | doCreateKey(const Name& identityName, const KeyParams& params) final; |
| 103 | |
| 104 | /** |
| 105 | * @brief Delete a key with name @p keyName. |
| 106 | * |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 107 | * @throw Error the deletion failed |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 108 | */ |
| 109 | void |
| 110 | doDeleteKey(const Name& keyName) final; |
| 111 | |
| 112 | /** |
| 113 | * @return A private key with name @p keyName in encrypted PKCS #8 format using password @p pw |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 114 | * @throw Error the key cannot be exported, e.g., not enough privilege |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 115 | */ |
| 116 | ConstBufferPtr |
| 117 | doExportKey(const Name& keyName, const char* pw, size_t pwLen) final; |
| 118 | |
| 119 | /** |
| 120 | * @brief Import a private key in encrypted PKCS #8 format |
| 121 | * |
| 122 | * @param keyName The name of imported private key |
| 123 | * @param buf Pointer to the key in encrypted PKCS #8 format |
| 124 | * @param size The size of the key in encrypted PKCS #8 format |
| 125 | * @param pw The password to decrypt the private key |
| 126 | * @param pwLen The length of the password |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 127 | * @throw Error import fails |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 128 | */ |
| 129 | void |
| 130 | doImportKey(const Name& keyName, const uint8_t* buf, size_t size, const char* pw, size_t pwLen) final; |
| 131 | |
| 132 | private: |
| 133 | class Impl; |
Davide Pesavento | 794f687 | 2017-05-15 23:33:38 -0400 | [diff] [blame] | 134 | const unique_ptr<Impl> m_impl; |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | } // namespace tpm |
| 138 | } // namespace security |
| 139 | } // namespace ndn |
| 140 | |
| 141 | #endif // NDN_SECURITY_TPM_BACK_END_OSX_HPP |