NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
back-end-mem.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2021 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 
26 
27 #include <unordered_map>
28 
29 #include <boost/lexical_cast.hpp>
30 
31 namespace ndn {
32 namespace security {
33 namespace tpm {
34 
36 
38 {
39 public:
40  std::unordered_map<Name, shared_ptr<PrivateKey>> keys;
41 };
42 
43 BackEndMem::BackEndMem(const std::string&)
44  : m_impl(make_unique<Impl>())
45 {
46 }
47 
48 BackEndMem::~BackEndMem() = default;
49 
50 const std::string&
52 {
53  static std::string scheme = "tpm-memory";
54  return scheme;
55 }
56 
57 bool
58 BackEndMem::doHasKey(const Name& keyName) const
59 {
60  return (m_impl->keys.count(keyName) > 0);
61 }
62 
63 unique_ptr<KeyHandle>
64 BackEndMem::doGetKeyHandle(const Name& keyName) const
65 {
66  auto it = m_impl->keys.find(keyName);
67  if (it == m_impl->keys.end())
68  return nullptr;
69  return make_unique<KeyHandleMem>(it->second);
70 }
71 
72 unique_ptr<KeyHandle>
73 BackEndMem::doCreateKey(const Name& identityName, const KeyParams& params)
74 {
75  switch (params.getKeyType()) {
76  case KeyType::RSA:
77  case KeyType::EC:
78  case KeyType::HMAC:
79  break;
80  default:
81  NDN_THROW(std::invalid_argument("Memory-based TPM does not support creating a key of type " +
82  boost::lexical_cast<std::string>(params.getKeyType())));
83  }
84 
85  shared_ptr<PrivateKey> key(transform::generatePrivateKey(params).release());
86  unique_ptr<KeyHandle> keyHandle = make_unique<KeyHandleMem>(key);
87 
88  Name keyName;
89  if (params.getKeyType() == KeyType::HMAC) {
90  keyName = constructHmacKeyName(*key, identityName, params);
91  }
92  else {
93  keyName = constructAsymmetricKeyName(*keyHandle, identityName, params);
94  }
95  keyHandle->setKeyName(keyName);
96 
97  m_impl->keys[keyName] = std::move(key);
98  return keyHandle;
99 }
100 
101 void
102 BackEndMem::doDeleteKey(const Name& keyName)
103 {
104  m_impl->keys.erase(keyName);
105 }
106 
108 BackEndMem::doExportKey(const Name& keyName, const char* pw, size_t pwLen)
109 {
110  OBufferStream os;
111  m_impl->keys[keyName]->savePkcs8(os, pw, pwLen);
112  return os.buf();
113 }
114 
115 void
116 BackEndMem::doImportKey(const Name& keyName, span<const uint8_t> pkcs8, const char* pw, size_t pwLen)
117 {
118  auto key = make_shared<PrivateKey>();
119  try {
120  key->loadPkcs8(pkcs8, pw, pwLen);
121  }
122  catch (const PrivateKey::Error&) {
123  NDN_THROW_NESTED(Error("Cannot import private key"));
124  }
125  doImportKey(keyName, std::move(key));
126 }
127 
128 void
129 BackEndMem::doImportKey(const Name& keyName, shared_ptr<transform::PrivateKey> key)
130 {
131  m_impl->keys[keyName] = std::move(key);
132 }
133 
134 } // namespace tpm
135 } // namespace security
136 } // namespace ndn
#define NDN_THROW_NESTED(e)
Definition: exception.hpp:71
Copyright (c) 2011-2015 Regents of the University of California.
RSA key, supports sign/verify and encrypt/decrypt operations.
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:144
BackEndMem(const std::string &location="")
Create memory-based TPM backend.
unique_ptr< PrivateKey > generatePrivateKey(const KeyParams &keyParams)
Generate a private key according to keyParams.
#define NDN_THROW(e)
Definition: exception.hpp:61
HMAC key, supports sign/verify operations.
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:114
Elliptic Curve key (e.g. for ECDSA), supports sign/verify operations.
Represents an absolute name.
Definition: name.hpp:41
KeyType getKeyType() const
Definition: key-params.hpp:48
static const std::string & getScheme()
shared_ptr< Buffer > buf()
Flush written data to the stream and return shared pointer to the underlying buffer.
Base class for key parameters.
Definition: key-params.hpp:35
implements an output stream that constructs ndn::Buffer
std::unordered_map< Name, shared_ptr< PrivateKey > > keys
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:139