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-2020 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& scheme, const std::string& location, unique_ptr<BackEnd> backEnd)
33  : m_scheme(scheme)
34  , m_location(location)
35  , m_backEnd(std::move(backEnd))
36 {
37 }
38 
39 Tpm::~Tpm() = default;
40 
41 std::string
43 {
44  return m_scheme + ":" + m_location;
45 }
46 
47 bool
48 Tpm::hasKey(const Name& keyName) const
49 {
50  return m_backEnd->hasKey(keyName);
51 }
52 
53 Name
54 Tpm::createKey(const Name& identityName, const KeyParams& params)
55 {
56  auto keyHandle = m_backEnd->createKey(identityName, params);
57  auto keyName = keyHandle->getKeyName();
58  m_keys[keyName] = std::move(keyHandle);
59  return keyName;
60 }
61 
62 void
63 Tpm::deleteKey(const Name& keyName)
64 {
65  auto it = m_keys.find(keyName);
66  if (it != m_keys.end())
67  m_keys.erase(it);
68 
69  m_backEnd->deleteKey(keyName);
70 }
71 
73 Tpm::getPublicKey(const Name& keyName) const
74 {
75  const KeyHandle* key = findKey(keyName);
76 
77  if (key == nullptr)
78  return nullptr;
79  else
80  return key->derivePublicKey();
81 }
82 
84 Tpm::sign(const InputBuffers& bufs, const Name& keyName, DigestAlgorithm digestAlgorithm) const
85 {
86  const KeyHandle* key = findKey(keyName);
87 
88  if (key == nullptr) {
89  return nullptr;
90  }
91  else {
92  return key->sign(digestAlgorithm, bufs);
93  }
94 }
95 
96 boost::logic::tribool
97 Tpm::verify(const InputBuffers& bufs, const uint8_t* sig, size_t sigLen, const Name& keyName,
98  DigestAlgorithm digestAlgorithm) const
99 {
100  const KeyHandle* key = findKey(keyName);
101 
102  if (key == nullptr) {
103  return boost::logic::indeterminate;
104  }
105  else {
106  return key->verify(digestAlgorithm, bufs, sig, sigLen);
107  }
108 }
109 
111 Tpm::decrypt(const uint8_t* buf, size_t size, const Name& keyName) const
112 {
113  const KeyHandle* key = findKey(keyName);
114 
115  if (key == nullptr)
116  return nullptr;
117  else
118  return key->decrypt(buf, size);
119 }
120 
121 bool
123 {
124  return m_backEnd->isTerminalMode();
125 }
126 
127 void
128 Tpm::setTerminalMode(bool isTerminal) const
129 {
130  m_backEnd->setTerminalMode(isTerminal);
131 }
132 
133 bool
135 {
136  return m_backEnd->isTpmLocked();
137 }
138 
139 bool
140 Tpm::unlockTpm(const char* password, size_t passwordLength) const
141 {
142  return m_backEnd->unlockTpm(password, passwordLength);
143 }
144 
146 Tpm::exportPrivateKey(const Name& keyName, const char* pw, size_t pwLen) const
147 {
148  return m_backEnd->exportKey(keyName, pw, pwLen);
149 }
150 
151 void
152 Tpm::importPrivateKey(const Name& keyName, const uint8_t* pkcs8, size_t pkcs8Len,
153  const char* pw, size_t pwLen)
154 {
155  m_backEnd->importKey(keyName, pkcs8, pkcs8Len, pw, pwLen);
156 }
157 
158 void
159 Tpm::importPrivateKey(const Name& keyName, shared_ptr<transform::PrivateKey> key)
160 {
161  m_backEnd->importKey(keyName, std::move(key));
162 }
163 
164 const KeyHandle*
165 Tpm::findKey(const Name& keyName) const
166 {
167  auto it = m_keys.find(keyName);
168  if (it != m_keys.end())
169  return it->second.get();
170 
171  auto handle = m_backEnd->getKeyHandle(keyName);
172  if (handle == nullptr)
173  return nullptr;
174 
175  const KeyHandle* key = handle.get();
176  m_keys[keyName] = std::move(handle);
177  return key;
178 }
179 
180 } // namespace tpm
181 } // namespace security
182 } // namespace ndn
ConstBufferPtr sign(DigestAlgorithm digestAlgorithm, const InputBuffers &bufs) const
Generate a digital signature for bufs using this key with digestAlgorithm.
Definition: key-handle.cpp:31
Definition: data.cpp:26
ConstBufferPtr derivePublicKey() const
Definition: key-handle.cpp:63
ConstBufferPtr decrypt(const uint8_t *cipherText, size_t cipherTextLen) const
Return plain text content decrypted from cipherText using this key.
Definition: key-handle.cpp:57
bool verify(DigestAlgorithm digestAlgorithm, const InputBuffers &bufs, const uint8_t *sig, size_t sigLen) const
Verify the signature sig for bufs using this key and digestAlgorithm.
Definition: key-handle.cpp:43
bool hasKey(const Name &keyName) const
Check if a private key exists.
Definition: tpm.cpp:48
Abstraction of TPM key handle.
Definition: key-handle.hpp:37
STL namespace.
ConstBufferPtr getPublicKey(const Name &keyName) const
Definition: tpm.cpp:73
bool isTpmLocked() const
Definition: tpm.cpp:134
boost::logic::tribool verify(const InputBuffers &bufs, const uint8_t *sig, size_t sigLen, const Name &keyName, DigestAlgorithm digestAlgorithm) const
Verify discontiguous ranges using the key with name keyName and using the digest digestAlgorithm.
Definition: tpm.cpp:97
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:84
size_t sigLen
const uint8_t * sig
bool unlockTpm(const char *password, size_t passwordLength) const
Unlock the TPM.
Definition: tpm.cpp:140
void setTerminalMode(bool isTerminal) const
Set the terminal mode of the TPM.
Definition: tpm.cpp:128
Represents an absolute name.
Definition: name.hpp:44
std::string getTpmLocator() const
Definition: tpm.cpp:42
Base class for key parameters.
Definition: key-params.hpp:35
ConstBufferPtr decrypt(const uint8_t *buf, size_t size, const Name &keyName) const
Decrypt blob using the key with name keyName.
Definition: tpm.cpp:111
InputBuffers bufs
bool isTerminalMode() const
Check if the TPM is in terminal mode.
Definition: tpm.cpp:122
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:126