tpm.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2022 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_CXX_SECURITY_TPM_TPM_HPP
23 #define NDN_CXX_SECURITY_TPM_TPM_HPP
24 
25 #include "ndn-cxx/name.hpp"
28 
29 #include <unordered_map>
30 #include <boost/logic/tribool.hpp>
31 
32 namespace ndn {
33 namespace security {
34 
35 namespace transform {
36 class PrivateKey;
37 } // namespace transform
38 
39 inline namespace v2 {
40 class KeyChain;
41 } // inline namespace v2
42 
43 namespace tpm {
44 
45 class BackEnd;
46 
65 class Tpm : noncopyable
66 {
67 public:
68  class Error : public std::runtime_error
69  {
70  public:
71  using std::runtime_error::runtime_error;
72  };
73 
74  ~Tpm();
75 
76  std::string
77  getTpmLocator() const;
78 
85  bool
86  hasKey(const Name& keyName) const;
87 
95  getPublicKey(const Name& keyName) const;
96 
104  sign(const InputBuffers& bufs, const Name& keyName, DigestAlgorithm digestAlgorithm) const;
105 
111  [[deprecated("use the overload that takes InputBuffers")]]
113  sign(const uint8_t* buf, size_t size, const Name& keyName, DigestAlgorithm digestAlgorithm) const
114  {
115  return sign({{buf, size}}, keyName, digestAlgorithm);
116  }
117 
126  NDN_CXX_NODISCARD boost::logic::tribool
127  verify(const InputBuffers& bufs, span<const uint8_t> sig, const Name& keyName,
128  DigestAlgorithm digestAlgorithm) const;
129 
137  [[deprecated("use the overload that takes InputBuffers and span")]]
138  boost::logic::tribool
139  verify(const uint8_t* buf, size_t bufLen, const uint8_t* sig, size_t sigLen,
140  const Name& keyName, DigestAlgorithm digestAlgorithm) const
141  {
142  return verify({{buf, bufLen}}, {sig, sigLen}, keyName, digestAlgorithm);
143  }
144 
151  decrypt(span<const uint8_t> buf, const Name& keyName) const;
152 
158  [[deprecated("use the overload that takes a span<>")]]
160  decrypt(const uint8_t* buf, size_t size, const Name& keyName) const
161  {
162  return decrypt({buf, size}, keyName);
163  }
164 
165 public: // Management
169  bool
170  isTerminalMode() const;
171 
177  void
178  setTerminalMode(bool isTerminal) const;
179 
183  bool
184  isTpmLocked() const;
185 
192  NDN_CXX_NODISCARD bool
193  unlockTpm(const char* password, size_t passwordLength) const;
194 
203  Tpm(const std::string& scheme, const std::string& location, unique_ptr<BackEnd> impl);
204 
215  Name
216  createKey(const Name& identityName, const KeyParams& params);
217 
221  void
222  deleteKey(const Name& keyName);
223 
237  exportPrivateKey(const Name& keyName, const char* pw, size_t pwLen) const;
238 
251  void
252  importPrivateKey(const Name& keyName, span<const uint8_t> pkcs8, const char* pw, size_t pwLen);
253 
257  void
258  importPrivateKey(const Name& keyName, shared_ptr<transform::PrivateKey> key);
259 
265  void
266  clearKeyCache()
267  {
268  m_keys.clear();
269  }
270 
271 private:
277  const KeyHandle*
278  findKey(const Name& keyName) const;
279 
280 private:
281  std::string m_scheme;
282  std::string m_location;
283 
284  mutable std::unordered_map<Name, unique_ptr<KeyHandle>> m_keys;
285 
286  const unique_ptr<BackEnd> m_backEnd;
287 
288  friend KeyChain;
289 };
290 
291 } // namespace tpm
292 
293 using tpm::Tpm;
294 
295 } // namespace security
296 } // namespace ndn
297 
298 #endif // NDN_CXX_SECURITY_TPM_TPM_HPP
#define NDN_CXX_NODISCARD
Definition: backports.hpp:68
Base class for key parameters.
Definition: key-params.hpp:36
Represents an absolute name.
Definition: name.hpp:46
TPM front-end class.
Definition: tpm.hpp:66
boost::logic::tribool verify(const uint8_t *buf, size_t bufLen, const uint8_t *sig, size_t sigLen, const Name &keyName, DigestAlgorithm digestAlgorithm) const
Verify blob using the key with name keyName and using the digest digestAlgorithm.
Definition: tpm.hpp:139
bool unlockTpm(const char *password, size_t passwordLength) const
Unlock the TPM.
Definition: tpm.cpp:123
void setTerminalMode(bool isTerminal) const
Set the terminal mode of the TPM.
Definition: tpm.cpp:111
ConstBufferPtr getPublicKey(const Name &keyName) const
Definition: tpm.cpp:73
ConstBufferPtr decrypt(span< const uint8_t > buf, const Name &keyName) const
Decrypt blob using the key with name keyName.
Definition: tpm.cpp:98
boost::logic::tribool verify(const InputBuffers &bufs, span< const uint8_t > sig, const Name &keyName, DigestAlgorithm digestAlgorithm) const
Verify discontiguous ranges using the key with name keyName and using the digest digestAlgorithm.
Definition: tpm.cpp:87
ConstBufferPtr sign(const InputBuffers &bufs, const Name &keyName, DigestAlgorithm digestAlgorithm) const
Sign discontiguous ranges using the key with name keyName and using the digest digestAlgorithm.
Definition: tpm.cpp:80
ConstBufferPtr decrypt(const uint8_t *buf, size_t size, const Name &keyName) const
Decrypt blob using the key with name keyName.
Definition: tpm.hpp:160
bool isTerminalMode() const
Check if the TPM is in terminal mode.
Definition: tpm.cpp:105
ConstBufferPtr sign(const uint8_t *buf, size_t size, const Name &keyName, DigestAlgorithm digestAlgorithm) const
Sign blob using the key with name keyName and using the digest digestAlgorithm.
Definition: tpm.hpp:113
bool hasKey(const Name &keyName) const
Check if a private key exists.
Definition: tpm.cpp:48
std::string getTpmLocator() const
Definition: tpm.cpp:42
bool isTpmLocked() const
Definition: tpm.cpp:117
Abstraction of private key in crypto transformation.
Definition: private-key.hpp:39
The interface of signing key management.
Definition: key-chain.hpp:46
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition: common.hpp:48
Definition: data.cpp:25
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:139
InputBuffers bufs
span< const uint8_t > sig