26 #include <boost/lexical_cast.hpp>
32 Tpm::Tpm(
const std::string& scheme,
const std::string& location, unique_ptr<BackEnd> backEnd)
34 , m_location(location)
35 , m_backEnd(std::move(backEnd))
39 Tpm::~Tpm() =
default;
42 Tpm::getTpmLocator()
const
44 return m_scheme +
":" + m_location;
48 Tpm::hasKey(
const Name& keyName)
const
50 return m_backEnd->hasKey(keyName);
54 Tpm::createKey(
const Name& identityName,
const KeyParams& params)
56 auto keyHandle = m_backEnd->createKey(identityName, params);
57 auto keyName = keyHandle->getKeyName();
58 m_keys[keyName] = std::move(keyHandle);
63 Tpm::deleteKey(
const Name& keyName)
65 auto it = m_keys.find(keyName);
66 if (it != m_keys.end())
69 m_backEnd->deleteKey(keyName);
73 Tpm::getPublicKey(
const Name& keyName)
const
83 return key ? key->
sign(digestAlgorithm,
bufs) :
nullptr;
87 Tpm::verify(
const InputBuffers&
bufs, span<const uint8_t>
sig,
const Name& keyName,
92 return boost::logic::indeterminate;
98 Tpm::decrypt(span<const uint8_t> buf,
const Name& keyName)
const
101 return key ? key->
decrypt(buf) :
nullptr;
105 Tpm::isTerminalMode()
const
107 return m_backEnd->isTerminalMode();
111 Tpm::setTerminalMode(
bool isTerminal)
const
113 m_backEnd->setTerminalMode(isTerminal);
117 Tpm::isTpmLocked()
const
119 return m_backEnd->isTpmLocked();
123 Tpm::unlockTpm(
const char* password,
size_t passwordLength)
const
125 return m_backEnd->unlockTpm(password, passwordLength);
129 Tpm::exportPrivateKey(
const Name& keyName,
const char* pw,
size_t pwLen)
const
131 return m_backEnd->exportKey(keyName, pw, pwLen);
135 Tpm::importPrivateKey(
const Name& keyName, span<const uint8_t> pkcs8,
const char* pw,
size_t pwLen)
137 m_backEnd->importKey(keyName, pkcs8, pw, pwLen);
141 Tpm::importPrivateKey(
const Name& keyName, shared_ptr<transform::PrivateKey> key)
143 m_backEnd->importKey(keyName, std::move(key));
147 Tpm::findKey(
const Name& keyName)
const
149 auto it = m_keys.find(keyName);
150 if (it != m_keys.end())
151 return it->second.get();
153 auto handle = m_backEnd->getKeyHandle(keyName);
154 if (handle ==
nullptr)
157 const KeyHandle* key = handle.get();
158 m_keys[keyName] = std::move(handle);
Base class for key parameters.
Represents an absolute name.
Abstraction of TPM key handle.
ConstBufferPtr derivePublicKey() const
ConstBufferPtr decrypt(span< const uint8_t > cipherText) const
Return plain text content decrypted from cipherText using this key.
bool verify(DigestAlgorithm digestAlgorithm, const InputBuffers &bufs, span< const uint8_t > sig) const
Verify the signature sig over bufs using this key and digestAlgorithm.
ConstBufferPtr sign(DigestAlgorithm digestAlgorithm, const InputBuffers &bufs) const
Generate a digital signature for bufs using this key with digestAlgorithm.
shared_ptr< const Buffer > ConstBufferPtr
span< const uint8_t > sig