ndn-cxx: NDN C++ Library 0.9.0-34-ga362e65e
Loading...
Searching...
No Matches
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-2025 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
28namespace ndn::security::tpm {
29
30Tpm::Tpm(const std::string& locator, unique_ptr<BackEnd> backEnd)
31 : m_locator(locator)
32 , m_backEnd(std::move(backEnd))
33{
34}
35
36Tpm::~Tpm() = default;
37
38bool
39Tpm::hasKey(const Name& keyName) const
40{
41 return m_backEnd->hasKey(keyName);
42}
43
44Name
45Tpm::createKey(const Name& identityName, const KeyParams& params)
46{
47 auto keyHandle = m_backEnd->createKey(identityName, params);
48 auto keyName = keyHandle->getKeyName();
49 m_keys[keyName] = std::move(keyHandle);
50 return keyName;
51}
52
53void
54Tpm::deleteKey(const Name& keyName)
55{
56 if (auto it = m_keys.find(keyName); it != m_keys.end())
57 m_keys.erase(it);
58
59 m_backEnd->deleteKey(keyName);
60}
61
63Tpm::getPublicKey(const Name& keyName) const
64{
65 const KeyHandle* key = findKey(keyName);
66 return key ? key->derivePublicKey() : nullptr;
67}
68
70Tpm::sign(const InputBuffers& bufs, const Name& keyName, DigestAlgorithm digestAlgorithm) const
71{
72 const KeyHandle* key = findKey(keyName);
73 return key ? key->sign(digestAlgorithm, bufs) : nullptr;
74}
75
76boost::logic::tribool
77Tpm::verify(const InputBuffers& bufs, span<const uint8_t> sig, const Name& keyName,
78 DigestAlgorithm digestAlgorithm) const
79{
80 const KeyHandle* key = findKey(keyName);
81 if (key == nullptr)
82 return boost::logic::indeterminate;
83
84 return key->verify(digestAlgorithm, bufs, sig);
85}
86
88Tpm::decrypt(span<const uint8_t> buf, const Name& keyName) const
89{
90 const KeyHandle* key = findKey(keyName);
91 return key ? key->decrypt(buf) : nullptr;
92}
93
95Tpm::exportPrivateKey(const Name& keyName, const char* pw, size_t pwLen) const
96{
97 return m_backEnd->exportKey(keyName, pw, pwLen);
98}
99
100void
101Tpm::importPrivateKey(const Name& keyName, span<const uint8_t> pkcs8, const char* pw, size_t pwLen)
102{
103 m_backEnd->importKey(keyName, pkcs8, pw, pwLen);
104}
105
106void
107Tpm::importPrivateKey(const Name& keyName, shared_ptr<transform::PrivateKey> key)
108{
109 m_backEnd->importKey(keyName, std::move(key));
110}
111
112const KeyHandle*
113Tpm::findKey(const Name& keyName) const
114{
115 if (auto it = m_keys.find(keyName); it != m_keys.end())
116 return it->second.get();
117
118 auto handle = m_backEnd->getKeyHandle(keyName);
119 if (handle == nullptr)
120 return nullptr;
121
122 const KeyHandle* key = handle.get();
123 m_keys[keyName] = std::move(handle);
124 return key;
125}
126
127} // namespace ndn::security::tpm
Base class for key parameters.
Represents an absolute name.
Definition name.hpp:45
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.
std::shared_ptr< const Buffer > ConstBufferPtr
Definition buffer.hpp:140
STL namespace.
InputBuffers bufs
span< const uint8_t > sig