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-2019 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 uint8_t* buf, size_t size, const Name& keyName, DigestAlgorithm digestAlgorithm) const
85 {
86  const KeyHandle* key = findKey(keyName);
87 
88  if (key == nullptr)
89  return nullptr;
90  else
91  return key->sign(digestAlgorithm, buf, size);
92 }
93 
94 boost::logic::tribool
95 Tpm::verify(const uint8_t* buf, size_t bufLen, const uint8_t* sig, size_t sigLen,
96  const Name& keyName, DigestAlgorithm digestAlgorithm) const
97 {
98  const KeyHandle* key = findKey(keyName);
99 
100  if (key == nullptr)
101  return boost::logic::indeterminate;
102  else
103  return key->verify(digestAlgorithm, buf, bufLen, sig, sigLen);
104 }
105 
107 Tpm::decrypt(const uint8_t* buf, size_t size, const Name& keyName) const
108 {
109  const KeyHandle* key = findKey(keyName);
110 
111  if (key == nullptr)
112  return nullptr;
113  else
114  return key->decrypt(buf, size);
115 }
116 
117 bool
119 {
120  return m_backEnd->isTerminalMode();
121 }
122 
123 void
124 Tpm::setTerminalMode(bool isTerminal) const
125 {
126  m_backEnd->setTerminalMode(isTerminal);
127 }
128 
129 bool
131 {
132  return m_backEnd->isTpmLocked();
133 }
134 
135 bool
136 Tpm::unlockTpm(const char* password, size_t passwordLength) const
137 {
138  return m_backEnd->unlockTpm(password, passwordLength);
139 }
140 
142 Tpm::exportPrivateKey(const Name& keyName, const char* pw, size_t pwLen) const
143 {
144  return m_backEnd->exportKey(keyName, pw, pwLen);
145 }
146 
147 void
148 Tpm::importPrivateKey(const Name& keyName, const uint8_t* pkcs8, size_t pkcs8Len,
149  const char* pw, size_t pwLen)
150 {
151  m_backEnd->importKey(keyName, pkcs8, pkcs8Len, pw, pwLen);
152 }
153 
154 void
155 Tpm::importPrivateKey(const Name& keyName, shared_ptr<transform::PrivateKey> key)
156 {
157  m_backEnd->importKey(keyName, std::move(key));
158 }
159 
160 const KeyHandle*
161 Tpm::findKey(const Name& keyName) const
162 {
163  auto it = m_keys.find(keyName);
164  if (it != m_keys.end())
165  return it->second.get();
166 
167  auto handle = m_backEnd->getKeyHandle(keyName);
168  if (handle == nullptr)
169  return nullptr;
170 
171  const KeyHandle* key = handle.get();
172  m_keys[keyName] = std::move(handle);
173  return key;
174 }
175 
176 } // namespace tpm
177 } // namespace security
178 } // namespace ndn
buf
const uint8_t * buf
Definition: verification-helpers.cpp:47
ndn::security::tpm::KeyHandle::verify
bool verify(DigestAlgorithm digestAlgorithm, const uint8_t *buf, size_t bufLen, const uint8_t *sig, size_t sigLen) const
Verify the signature sig created on buf using this key and digestAlgorithm.
Definition: key-handle.cpp:37
ndn::KeyParams
Base class for key parameters.
Definition: key-params.hpp:36
ndn::security::tpm::KeyHandle::derivePublicKey
ConstBufferPtr derivePublicKey() const
Definition: key-handle.cpp:50
bufLen
size_t bufLen
Definition: verification-helpers.cpp:48
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:421
sigLen
size_t sigLen
Definition: verification-helpers.cpp:50
ndn::security::tpm::Tpm::exportPrivateKey
ConstBufferPtr exportPrivateKey(const Name &keyName, const char *pw, size_t pwLen) const
Export a private key.
Definition: tpm.cpp:142
ndn::security::tpm::KeyHandle::decrypt
ConstBufferPtr decrypt(const uint8_t *cipherText, size_t cipherTextLen) const
Definition: key-handle.cpp:44
ndn::security::tpm::Tpm::getPublicKey
ConstBufferPtr getPublicKey(const Name &keyName) const
Definition: tpm.cpp:73
ndn::security::tpm::Tpm::deleteKey
void deleteKey(const Name &keyName)
Delete a key pair with name keyName.
Definition: tpm.cpp:63
ndn::DigestAlgorithm
DigestAlgorithm
Definition: security-common.hpp:96
ndn::security::tpm::KeyHandle
Abstraction of TPM key handle.
Definition: key-handle.hpp:38
ndn::Name
Represents an absolute name.
Definition: name.hpp:44
ndn::security::tpm::Tpm::isTpmLocked
bool isTpmLocked() const
Definition: tpm.cpp:130
ndn::security::tpm::Tpm::verify
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.cpp:95
sig
const uint8_t * sig
Definition: verification-helpers.cpp:49
tpm.hpp
ndn::security::tpm::Tpm::getTpmLocator
std::string getTpmLocator() const
Definition: tpm.cpp:42
ndn::security::tpm::Tpm::importPrivateKey
void importPrivateKey(const Name &keyName, const uint8_t *pkcs8, size_t pkcs8Len, const char *pw, size_t pwLen)
Import a private key.
Definition: tpm.cpp:148
ndn::security::tpm::Tpm::unlockTpm
NDN_CXX_NODISCARD bool unlockTpm(const char *password, size_t passwordLength) const
Unlock the TPM.
Definition: tpm.cpp:136
ndn::security::tpm::Tpm::setTerminalMode
void setTerminalMode(bool isTerminal) const
Set the terminal mode of the TPM.
Definition: tpm.cpp:124
back-end.hpp
ndn::security::tpm::Tpm::isTerminalMode
bool isTerminalMode() const
Check if the TPM is in terminal mode.
Definition: tpm.cpp:118
ndn::security::tpm::Tpm::decrypt
ConstBufferPtr decrypt(const uint8_t *buf, size_t size, const Name &keyName) const
Decrypt blob using the key with name keyName.
Definition: tpm.cpp:107
buffer-stream.hpp
ndn::security::tpm::Tpm::hasKey
bool hasKey(const Name &keyName) const
Check if a private key exists.
Definition: tpm.cpp:48
ndn::security::tpm::KeyHandle::sign
ConstBufferPtr sign(DigestAlgorithm digestAlgorithm, const uint8_t *buf, size_t size) const
Definition: key-handle.cpp:31
ndn::security::tpm::Tpm::createKey
Name createKey(const Name &identityName, const KeyParams &params)
Create key for identityName according to params.
Definition: tpm.cpp:54
ndn::ConstBufferPtr
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:126
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
ndn::security::tpm::Tpm::~Tpm
~Tpm()
ndn::security::tpm::Tpm::sign
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.cpp:84