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