tpm.cpp
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 
25 
26 #include <boost/lexical_cast.hpp>
27 
28 namespace ndn {
29 namespace security {
30 namespace tpm {
31 
32 Tpm::Tpm(const std::string& locator, unique_ptr<BackEnd> backEnd)
33  : m_locator(locator)
34  , m_backEnd(std::move(backEnd))
35 {
36 }
37 
38 Tpm::~Tpm() = default;
39 
40 bool
41 Tpm::hasKey(const Name& keyName) const
42 {
43  return m_backEnd->hasKey(keyName);
44 }
45 
46 Name
47 Tpm::createKey(const Name& identityName, const KeyParams& params)
48 {
49  auto keyHandle = m_backEnd->createKey(identityName, params);
50  auto keyName = keyHandle->getKeyName();
51  m_keys[keyName] = std::move(keyHandle);
52  return keyName;
53 }
54 
55 void
56 Tpm::deleteKey(const Name& keyName)
57 {
58  auto it = m_keys.find(keyName);
59  if (it != m_keys.end())
60  m_keys.erase(it);
61 
62  m_backEnd->deleteKey(keyName);
63 }
64 
66 Tpm::getPublicKey(const Name& keyName) const
67 {
68  const KeyHandle* key = findKey(keyName);
69  return key ? key->derivePublicKey() : nullptr;
70 }
71 
73 Tpm::sign(const InputBuffers& bufs, const Name& keyName, DigestAlgorithm digestAlgorithm) const
74 {
75  const KeyHandle* key = findKey(keyName);
76  return key ? key->sign(digestAlgorithm, bufs) : nullptr;
77 }
78 
79 boost::logic::tribool
80 Tpm::verify(const InputBuffers& bufs, span<const uint8_t> sig, const Name& keyName,
81  DigestAlgorithm digestAlgorithm) const
82 {
83  const KeyHandle* key = findKey(keyName);
84  if (key == nullptr)
85  return boost::logic::indeterminate;
86 
87  return key->verify(digestAlgorithm, bufs, sig);
88 }
89 
91 Tpm::decrypt(span<const uint8_t> buf, const Name& keyName) const
92 {
93  const KeyHandle* key = findKey(keyName);
94  return key ? key->decrypt(buf) : nullptr;
95 }
96 
97 bool
98 Tpm::isTerminalMode() const
99 {
100  return m_backEnd->isTerminalMode();
101 }
102 
103 void
104 Tpm::setTerminalMode(bool isTerminal) const
105 {
106  m_backEnd->setTerminalMode(isTerminal);
107 }
108 
109 bool
110 Tpm::isTpmLocked() const
111 {
112  return m_backEnd->isTpmLocked();
113 }
114 
115 bool
116 Tpm::unlockTpm(const char* password, size_t passwordLength) const
117 {
118  return m_backEnd->unlockTpm(password, passwordLength);
119 }
120 
122 Tpm::exportPrivateKey(const Name& keyName, const char* pw, size_t pwLen) const
123 {
124  return m_backEnd->exportKey(keyName, pw, pwLen);
125 }
126 
127 void
128 Tpm::importPrivateKey(const Name& keyName, span<const uint8_t> pkcs8, const char* pw, size_t pwLen)
129 {
130  m_backEnd->importKey(keyName, pkcs8, pw, pwLen);
131 }
132 
133 void
134 Tpm::importPrivateKey(const Name& keyName, shared_ptr<transform::PrivateKey> key)
135 {
136  m_backEnd->importKey(keyName, std::move(key));
137 }
138 
139 const KeyHandle*
140 Tpm::findKey(const Name& keyName) const
141 {
142  auto it = m_keys.find(keyName);
143  if (it != m_keys.end())
144  return it->second.get();
145 
146  auto handle = m_backEnd->getKeyHandle(keyName);
147  if (handle == nullptr)
148  return nullptr;
149 
150  const KeyHandle* key = handle.get();
151  m_keys[keyName] = std::move(handle);
152  return key;
153 }
154 
155 } // namespace tpm
156 } // namespace security
157 } // namespace ndn
Base class for key parameters.
Definition: key-params.hpp:36
Represents an absolute name.
Definition: name.hpp:44
Abstraction of TPM key handle.
Definition: key-handle.hpp:38
ConstBufferPtr derivePublicKey() const
Definition: key-handle.cpp:50
ConstBufferPtr decrypt(span< const uint8_t > cipherText) const
Return plain text content decrypted from cipherText using this key.
Definition: key-handle.cpp:44
bool verify(DigestAlgorithm digestAlgorithm, const InputBuffers &bufs, span< const uint8_t > sig) const
Verify the signature sig over bufs using this key and digestAlgorithm.
Definition: key-handle.cpp:37
ConstBufferPtr sign(DigestAlgorithm digestAlgorithm, const InputBuffers &bufs) const
Generate a digital signature for bufs using this key with digestAlgorithm.
Definition: key-handle.cpp:31
@ Name
Definition: tlv.hpp:71
Definition: data.cpp:25
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:139
InputBuffers bufs
span< const uint8_t > sig