ndn-cxx: NDN C++ Library 0.9.0-33-g832ea91d
Loading...
Searching...
No Matches
back-end.cpp
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2013-2024 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
23
31
32#include <boost/lexical_cast.hpp>
33
34namespace ndn::security::tpm {
35
36BackEnd::~BackEnd() = default;
37
38bool
39BackEnd::hasKey(const Name& keyName) const
40{
41 return doHasKey(keyName);
42}
43
44unique_ptr<KeyHandle>
45BackEnd::getKeyHandle(const Name& keyName) const
46{
47 return doGetKeyHandle(keyName);
48}
49
50unique_ptr<KeyHandle>
51BackEnd::createKey(const Name& identity, const KeyParams& params)
52{
53 if (params.getKeyType() == KeyType::HMAC) {
54 return doCreateKey(identity, params);
55 }
56
57 switch (params.getKeyIdType()) {
59 // check that the provided key id isn't already taken
60 Name keyName = constructKeyName(identity, params.getKeyId());
61 if (hasKey(keyName)) {
62 NDN_THROW(Error("Key `" + keyName.toUri() + "` already exists"));
63 }
64 break;
65 }
68 // key id will be determined after key is generated
69 break;
70 default:
71 NDN_THROW(std::invalid_argument("Unsupported key id type " +
72 boost::lexical_cast<std::string>(params.getKeyIdType())));
73 }
74
75 return doCreateKey(identity, params);
76}
77
78void
79BackEnd::deleteKey(const Name& keyName)
80{
81 doDeleteKey(keyName);
82}
83
85BackEnd::exportKey(const Name& keyName, const char* pw, size_t pwLen)
86{
87 if (!hasKey(keyName)) {
88 NDN_THROW(Error("Key `" + keyName.toUri() + "` does not exist"));
89 }
90 return doExportKey(keyName, pw, pwLen);
91}
92
93void
94BackEnd::importKey(const Name& keyName, span<const uint8_t> pkcs8, const char* pw, size_t pwLen)
95{
96 if (hasKey(keyName)) {
97 NDN_THROW(Error("Key `" + keyName.toUri() + "` already exists"));
98 }
99 doImportKey(keyName, pkcs8, pw, pwLen);
100}
101
102void
103BackEnd::importKey(const Name& keyName, shared_ptr<transform::PrivateKey> key)
104{
105 if (hasKey(keyName)) {
106 NDN_THROW(Error("Key `" + keyName.toUri() + "` already exists"));
107 }
108 doImportKey(keyName, std::move(key));
109}
110
111Name
112BackEnd::constructAsymmetricKeyName(const KeyHandle& keyHandle, const Name& identity,
113 const KeyParams& params) const
114{
115 switch (params.getKeyIdType()) {
117 return constructKeyName(identity, params.getKeyId());
118 }
119 case KeyIdType::SHA256: {
120 using namespace transform;
121 OBufferStream os;
122 bufferSource(*keyHandle.derivePublicKey()) >>
123 digestFilter(DigestAlgorithm::SHA256) >>
124 streamSink(os);
125 return constructKeyName(identity, name::Component(os.buf()));
126 }
127 case KeyIdType::RANDOM: {
128 Name keyName;
129 do {
131 keyName = constructKeyName(identity, keyId);
132 } while (hasKey(keyName));
133 return keyName;
134 }
135 default: {
136 NDN_THROW(Error("Unsupported key id type " + boost::lexical_cast<std::string>(params.getKeyIdType())));
137 }
138 }
139}
140
141Name
143 const KeyParams& params) const
144{
146}
147
148} // namespace ndn::security::tpm
Base class for key parameters.
const name::Component & getKeyId() const
KeyIdType getKeyIdType() const
KeyType getKeyType() const
Represents an absolute name.
Definition name.hpp:45
Name & append(const Component &component)
Append a name component.
Definition name.hpp:308
void toUri(std::ostream &os, name::UriFormat format=name::UriFormat::DEFAULT) const
Write URI representation of the name to the output stream.
Definition name.cpp:324
An output stream that writes to a Buffer.
std::shared_ptr< Buffer > buf()
Return a shared pointer to the underlying buffer.
Represents a name component.
static Component fromNumber(uint64_t number, uint32_t type=tlv::GenericNameComponent)
Create a component encoded as NonNegativeInteger.
unique_ptr< KeyHandle > createKey(const Name &identityName, const KeyParams &params)
Create a key for identityName according to params.
Definition back-end.cpp:51
void importKey(const Name &keyName, span< const uint8_t > pkcs8, const char *pw, size_t pwLen)
Import a private key in encrypted PKCS #8 format.
Definition back-end.cpp:94
bool hasKey(const Name &keyName) const
Check if the key with name keyName exists in the TPM.
Definition back-end.cpp:39
Name constructAsymmetricKeyName(const KeyHandle &key, const Name &identity, const KeyParams &params) const
Construct and return the name of a RSA or EC key, based on identity and params.
Definition back-end.cpp:112
void deleteKey(const Name &keyName)
Delete the key with name keyName.
Definition back-end.cpp:79
ConstBufferPtr exportKey(const Name &keyName, const char *pw, size_t pwLen)
Get the private key with name keyName in encrypted PKCS #8 format.
Definition back-end.cpp:85
unique_ptr< KeyHandle > getKeyHandle(const Name &keyName) const
Get the handle of the key with name keyName.
Definition back-end.cpp:45
Name constructHmacKeyName(const transform::PrivateKey &key, const Name &identity, const KeyParams &params) const
Construct and return the name of a HMAC key, based on identity and params.
Definition back-end.cpp:142
Abstraction of TPM key handle.
ConstBufferPtr derivePublicKey() const
Abstraction of a private key in crypto transformations.
ConstBufferPtr getKeyDigest(DigestAlgorithm algo) const
Returns a digest of the private key.
#define NDN_THROW(e)
Definition exception.hpp:56
uint64_t generateSecureWord64()
Generate a cryptographically secure random integer in the range [0, 2^64).
Definition random.cpp:39
Name constructKeyName(const Name &identity, const name::Component &keyId)
Construct key name based on the appropriate naming conventions.
Definition key.cpp:126
@ HMAC
HMAC key, supports sign/verify operations.
@ RANDOM
Use a 64-bit random number as key id.
@ USER_SPECIFIED
User-specified key id.
@ SHA256
Use the SHA-256 hash of the public key as key id.
std::shared_ptr< const Buffer > ConstBufferPtr
Definition buffer.hpp:140