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 
30 #include "ndn-cxx/util/random.hpp"
31 
32 #include <boost/lexical_cast.hpp>
33 
34 namespace ndn::security::tpm {
35 
36 BackEnd::~BackEnd() = default;
37 
38 bool
39 BackEnd::hasKey(const Name& keyName) const
40 {
41  return doHasKey(keyName);
42 }
43 
44 unique_ptr<KeyHandle>
45 BackEnd::getKeyHandle(const Name& keyName) const
46 {
47  return doGetKeyHandle(keyName);
48 }
49 
50 unique_ptr<KeyHandle>
51 BackEnd::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  }
66  case KeyIdType::SHA256:
67  case KeyIdType::RANDOM:
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 
78 void
79 BackEnd::deleteKey(const Name& keyName)
80 {
81  doDeleteKey(keyName);
82 }
83 
85 BackEnd::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 
93 void
94 BackEnd::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 
102 void
103 BackEnd::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 
111 Name
112 BackEnd::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()) >>
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 
141 Name
143  const KeyParams& params) const
144 {
145  return Name(identity).append(name::Component(key.getKeyDigest(DigestAlgorithm::SHA256)));
146 }
147 
148 } // namespace ndn::security::tpm
Base class for key parameters.
Definition: key-params.hpp:36
KeyIdType getKeyIdType() const
Definition: key-params.hpp:54
KeyType getKeyType() const
Definition: key-params.hpp:48
const name::Component & getKeyId() const
Definition: key-params.hpp:60
Represents an absolute name.
Definition: name.hpp:45
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.
Definition: key-handle.hpp:36
ConstBufferPtr derivePublicKey() const
Definition: key-handle.cpp:48
Abstraction of a private key in crypto transformations.
Definition: private-key.hpp:39
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
unique_ptr< Transform > digestFilter(DigestAlgorithm algo)
unique_ptr< Sink > streamSink(std::ostream &os)
Definition: stream-sink.cpp:51
Name constructKeyName(const Name &identity, const name::Component &keyId)
Construct key name based on the appropriate naming conventions.
Definition: key.cpp:126
@ Name
Definition: tlv.hpp:71
@ 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