blob: bb018be567c9cf67cab3943924a22883991bf300 [file] [log] [blame]
Yingdi Yu0b60e7a2015-07-16 21:05:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoff3bf2c2017-10-05 00:14:27 -04002/*
Junxiao Shi68b53852018-07-25 13:56:38 -06003 * Copyright (c) 2013-2018 Regents of the University of California.
Yingdi Yu0b60e7a2015-07-16 21:05:11 -07004 *
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 Shi0b1b4672017-07-02 22:02:31 -070026#include "key-ref-osx.hpp"
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070027
Alexander Afanasyev0cf887d2017-03-26 16:58:59 -050028#ifndef NDN_CXX_HAVE_OSX_FRAMEWORKS
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070029#error "This file should not be compiled ..."
30#endif
31
32namespace ndn {
33namespace security {
34namespace tpm {
35
36/**
Yingdi Yufe4733a2015-10-22 14:24:12 -070037 * @brief The back-end implementation of TPM based on macOS Keychain Services.
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070038 */
39class BackEndOsx : public BackEnd
40{
41public:
Yingdi Yufe4733a2015-10-22 14:24:12 -070042 /**
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 Yu0b60e7a2015-07-16 21:05:11 -070048
49 ~BackEndOsx() override;
50
Yingdi Yufe4733a2015-10-22 14:24:12 -070051 static const std::string&
52 getScheme();
53
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070054public: // management
Yingdi Yufe4733a2015-10-22 14:24:12 -070055 bool
56 isTerminalMode() const final;
57
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070058 void
Yingdi Yufe4733a2015-10-22 14:24:12 -070059 setTerminalMode(bool isTerminal) const final;
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070060
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070061 bool
Yingdi Yufe4733a2015-10-22 14:24:12 -070062 isTpmLocked() const final;
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070063
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070064 bool
Yingdi Yufe4733a2015-10-22 14:24:12 -070065 unlockTpm(const char* pw, size_t pwLen) const final;
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070066
67public: // crypto transformation
68 /**
69 * @brief Sign @p buf with @p key using @p digestAlgorithm.
70 */
Davide Pesaventoff3bf2c2017-10-05 00:14:27 -040071 static ConstBufferPtr
72 sign(const KeyRefOsx& key, DigestAlgorithm digestAlgorithm, const uint8_t* buf, size_t size);
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070073
Davide Pesaventoff3bf2c2017-10-05 00:14:27 -040074 static ConstBufferPtr
75 decrypt(const KeyRefOsx& key, const uint8_t* cipherText, size_t cipherSize);
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070076
Davide Pesaventoff3bf2c2017-10-05 00:14:27 -040077 static ConstBufferPtr
78 derivePublicKey(const KeyRefOsx& key);
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070079
80private: // inherited from tpm::BackEnd
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070081 /**
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 Yufe4733a2015-10-22 14:24:12 -0700107 * @throw Error the deletion failed
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700108 */
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 Yufe4733a2015-10-22 14:24:12 -0700114 * @throw Error the key cannot be exported, e.g., not enough privilege
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700115 */
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 Yufe4733a2015-10-22 14:24:12 -0700127 * @throw Error import fails
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700128 */
129 void
130 doImportKey(const Name& keyName, const uint8_t* buf, size_t size, const char* pw, size_t pwLen) final;
131
132private:
133 class Impl;
Davide Pesavento794f6872017-05-15 23:33:38 -0400134 const unique_ptr<Impl> m_impl;
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700135};
136
137} // namespace tpm
138} // namespace security
139} // namespace ndn
140
141#endif // NDN_SECURITY_TPM_BACK_END_OSX_HPP