blob: 8310984690b12033c04c8d5ad9b918ff218f8320 [file] [log] [blame]
Yingdi Yu0b60e7a2015-07-16 21:05:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yingdi Yufe4733a2015-10-22 14:24:12 -07003 * Copyright (c) 2013-2017 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"
26#include "helper-osx.hpp"
27
28#ifndef NDN_CXX_HAVE_OSX_SECURITY
29#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:
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
52public:
Yingdi Yufe4733a2015-10-22 14:24:12 -070053 /**
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 Yu0b60e7a2015-07-16 21:05:11 -070059
60 ~BackEndOsx() override;
61
Yingdi Yufe4733a2015-10-22 14:24:12 -070062 static const std::string&
63 getScheme();
64
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070065public: // management
Yingdi Yufe4733a2015-10-22 14:24:12 -070066 bool
67 isTerminalMode() const final;
68
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070069 void
Yingdi Yufe4733a2015-10-22 14:24:12 -070070 setTerminalMode(bool isTerminal) const final;
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070071
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070072 bool
Yingdi Yufe4733a2015-10-22 14:24:12 -070073 isTpmLocked() const final;
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070074
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070075 bool
Yingdi Yufe4733a2015-10-22 14:24:12 -070076 unlockTpm(const char* pw, size_t pwLen) const final;
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070077
78public: // 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
91private: // 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 Yufe4733a2015-10-22 14:24:12 -0700119 * @throw Error the deletion failed
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700120 */
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 Yufe4733a2015-10-22 14:24:12 -0700126 * @throw Error the key cannot be exported, e.g., not enough privilege
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700127 */
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 Yufe4733a2015-10-22 14:24:12 -0700139 * @throw Error import fails
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700140 */
141 void
142 doImportKey(const Name& keyName, const uint8_t* buf, size_t size, const char* pw, size_t pwLen) final;
143
144private:
145 class Impl;
Davide Pesavento794f6872017-05-15 23:33:38 -0400146 const unique_ptr<Impl> m_impl;
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700147};
148
149} // namespace tpm
150} // namespace security
151} // namespace ndn
152
153#endif // NDN_SECURITY_TPM_BACK_END_OSX_HPP