blob: 0fcd49f74a0089b7a8a06a0f346e4afe3a610919 [file] [log] [blame]
Yingdi Yu0b60e7a2015-07-16 21:05:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016 Regents of the University of California.
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
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/**
37 * @brief The back-end implementation of TPM based on OS X KeyChain service.
38 */
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:
53 BackEndOsx();
54
55 ~BackEndOsx() override;
56
57public: // management
58 /**
59 * @brief Set the terminal mode of TPM.
60 *
61 * In terminal mode, TPM will not ask user permission from GUI.
62 */
63 void
64 setTerminalMode(bool isTerminal);
65
66 /**
67 * @brief Check if TPM is in terminal mode
68 */
69 bool
70 isTerminalMode() const;
71
72 /**
73 * @return True if TPM is locked, otherwise false
74 */
75 bool
76 isLocked() const;
77
78 /**
79 * @brief Unlock TPM
80 *
81 * @param password The password to unlock TPM
82 * @param passwordLength The password size.
83 */
84 bool
85 unlockTpm(const char* password = nullptr, size_t passwordLength = 0);
86
87public: // crypto transformation
88 /**
89 * @brief Sign @p buf with @p key using @p digestAlgorithm.
90 */
91 ConstBufferPtr
92 sign(const KeyRefOsx& key, DigestAlgorithm digestAlgorithm, const uint8_t* buf, size_t size) const;
93
94 ConstBufferPtr
95 decrypt(const KeyRefOsx& key, const uint8_t* cipherText, size_t cipherSize) const;
96
97 ConstBufferPtr
98 derivePublicKey(const KeyRefOsx& key) const;
99
100private: // inherited from tpm::BackEnd
101
102 /**
103 * @return True if a key with name @p keyName exists in TPM.
104 */
105 bool
106 doHasKey(const Name& keyName) const final;
107
108 /**
109 * @return The handle of a key with name @p keyName, or nullptr if the key does not exist
110 */
111 unique_ptr<KeyHandle>
112 doGetKeyHandle(const Name& keyName) const final;
113
114 /**
115 * @brief Create key for @p identityName according to @p params.
116 *
117 * The created key is named as: /<identityName>/[keyId]/KEY
118 * The key name is set in the returned KeyHandle.
119 *
120 * @return The handle of the created key.
121 */
122 unique_ptr<KeyHandle>
123 doCreateKey(const Name& identityName, const KeyParams& params) final;
124
125 /**
126 * @brief Delete a key with name @p keyName.
127 *
128 * @throws Error if the deletion fails.
129 */
130 void
131 doDeleteKey(const Name& keyName) final;
132
133 /**
134 * @return A private key with name @p keyName in encrypted PKCS #8 format using password @p pw
135 * @throws Error if the key cannot be exported, e.g., not enough privilege
136 */
137 ConstBufferPtr
138 doExportKey(const Name& keyName, const char* pw, size_t pwLen) final;
139
140 /**
141 * @brief Import a private key in encrypted PKCS #8 format
142 *
143 * @param keyName The name of imported private key
144 * @param buf Pointer to the key in encrypted PKCS #8 format
145 * @param size The size of the key in encrypted PKCS #8 format
146 * @param pw The password to decrypt the private key
147 * @param pwLen The length of the password
148 * @throws Error if import fails
149 */
150 void
151 doImportKey(const Name& keyName, const uint8_t* buf, size_t size, const char* pw, size_t pwLen) final;
152
153private:
154 class Impl;
155 unique_ptr<Impl> m_impl;
156};
157
158} // namespace tpm
159} // namespace security
160} // namespace ndn
161
162#endif // NDN_SECURITY_TPM_BACK_END_OSX_HPP