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
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
size_t bufLen
ConstBufferPtr sign(DigestAlgorithm digestAlgorithm, const uint8_t *buf, size_t size) const
Definition: key-handle.cpp:31
Definition: data.cpp:26
ConstBufferPtr derivePublicKey() const
Definition: key-handle.cpp:50
ConstBufferPtr decrypt(const uint8_t *cipherText, size_t cipherTextLen) const
Definition: key-handle.cpp:44
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:130
size_t sigLen
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
const uint8_t * sig
bool unlockTpm(const char *password, size_t passwordLength) const
Unlock the TPM.
Definition: tpm.cpp:136
void setTerminalMode(bool isTerminal) const
Set the terminal mode of the TPM.
Definition: tpm.cpp:124
Represents an absolute name.
Definition: name.hpp:43
std::string getTpmLocator() const
Definition: tpm.cpp:42
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
Base class for key parameters.
Definition: key-params.hpp:35
const uint8_t * buf
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
bool isTerminalMode() const
Check if the TPM is in terminal mode.
Definition: tpm.cpp:118
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:126