NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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& 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  return key ? key->derivePublicKey() : nullptr;
77 }
78 
80 Tpm::sign(const InputBuffers& bufs, const Name& keyName, DigestAlgorithm digestAlgorithm) const
81 {
82  const KeyHandle* key = findKey(keyName);
83  return key ? key->sign(digestAlgorithm, bufs) : nullptr;
84 }
85 
86 boost::logic::tribool
87 Tpm::verify(const InputBuffers& bufs, span<const uint8_t> sig, const Name& keyName,
88  DigestAlgorithm digestAlgorithm) const
89 {
90  const KeyHandle* key = findKey(keyName);
91  if (key == nullptr)
92  return boost::logic::indeterminate;
93 
94  return key->verify(digestAlgorithm, bufs, sig);
95 }
96 
98 Tpm::decrypt(span<const uint8_t> buf, const Name& keyName) const
99 {
100  const KeyHandle* key = findKey(keyName);
101  return key ? key->decrypt(buf) : nullptr;
102 }
103 
104 bool
106 {
107  return m_backEnd->isTerminalMode();
108 }
109 
110 void
111 Tpm::setTerminalMode(bool isTerminal) const
112 {
113  m_backEnd->setTerminalMode(isTerminal);
114 }
115 
116 bool
118 {
119  return m_backEnd->isTpmLocked();
120 }
121 
122 bool
123 Tpm::unlockTpm(const char* password, size_t passwordLength) const
124 {
125  return m_backEnd->unlockTpm(password, passwordLength);
126 }
127 
129 Tpm::exportPrivateKey(const Name& keyName, const char* pw, size_t pwLen) const
130 {
131  return m_backEnd->exportKey(keyName, pw, pwLen);
132 }
133 
134 void
135 Tpm::importPrivateKey(const Name& keyName, span<const uint8_t> pkcs8, const char* pw, size_t pwLen)
136 {
137  m_backEnd->importKey(keyName, pkcs8, pw, pwLen);
138 }
139 
140 void
141 Tpm::importPrivateKey(const Name& keyName, shared_ptr<transform::PrivateKey> key)
142 {
143  m_backEnd->importKey(keyName, std::move(key));
144 }
145 
146 const KeyHandle*
147 Tpm::findKey(const Name& keyName) const
148 {
149  auto it = m_keys.find(keyName);
150  if (it != m_keys.end())
151  return it->second.get();
152 
153  auto handle = m_backEnd->getKeyHandle(keyName);
154  if (handle == nullptr)
155  return nullptr;
156 
157  const KeyHandle* key = handle.get();
158  m_keys[keyName] = std::move(handle);
159  return key;
160 }
161 
162 } // namespace tpm
163 } // namespace security
164 } // 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
Copyright (c) 2011-2015 Regents of the University of California.
NDN_CXX_NODISCARD 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 derivePublicKey() const
Definition: key-handle.cpp:50
void importPrivateKey(const Name &keyName, span< const uint8_t > pkcs8, const char *pw, size_t pwLen)
Import a private key.
Definition: tpm.cpp:135
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.
Name createKey(const Name &identityName, const KeyParams &params)
Create key for identityName according to params.
Definition: tpm.cpp:54
ConstBufferPtr getPublicKey(const Name &keyName) const
Definition: tpm.cpp:73
bool isTpmLocked() const
Definition: tpm.cpp:117
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 exportPrivateKey(const Name &keyName, const char *pw, size_t pwLen) const
Export a private key.
Definition: tpm.cpp:129
NDN_CXX_NODISCARD 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
Represents an absolute name.
Definition: name.hpp:41
void deleteKey(const Name &keyName)
Delete a key pair with name keyName.
Definition: tpm.cpp:63
std::string getTpmLocator() const
Definition: tpm.cpp:42
Base class for key parameters.
Definition: key-params.hpp:35
ConstBufferPtr decrypt(span< const uint8_t > cipherText) const
Return plain text content decrypted from cipherText using this key.
Definition: key-handle.cpp:44
InputBuffers bufs
bool isTerminalMode() const
Check if the TPM is in terminal mode.
Definition: tpm.cpp:105
ConstBufferPtr decrypt(span< const uint8_t > buf, const Name &keyName) const
Decrypt blob using the key with name keyName.
Definition: tpm.cpp:98
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
span< const uint8_t > sig
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:139