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-2019 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, const uint8_t* buf, size_t size, const char* pw, size_t pwLen)
117 {
118  auto key = make_shared<PrivateKey>();
119  try {
120  key->loadPkcs8(buf, size, 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
buf
const uint8_t * buf
Definition: verification-helpers.cpp:47
ndn::KeyParams
Base class for key parameters.
Definition: key-params.hpp:36
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:421
ndn::security::tpm::BackEndMem::BackEndMem
BackEndMem(const std::string &location="")
Create memory-based TPM backend.
Definition: back-end-mem.cpp:43
ndn::security::tpm::BackEnd::constructHmacKeyName
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
ndn::security::transform::generatePrivateKey
unique_ptr< PrivateKey > generatePrivateKey(const KeyParams &keyParams)
Generate a private key according to keyParams.
Definition: private-key.cpp:536
ndn::security::tpm::BackEnd::Error
Tpm::Error Error
Definition: back-end.hpp:39
ndn::security::tpm::BackEndMem::getScheme
static const std::string & getScheme()
Definition: back-end-mem.cpp:51
NDN_THROW_NESTED
#define NDN_THROW_NESTED(e)
Definition: exception.hpp:71
ndn::KeyType::EC
@ EC
Elliptic Curve key (e.g. for ECDSA), supports sign/verify operations.
ndn::security::tpm::BackEndMem::~BackEndMem
~BackEndMem() final
ndn::Name
Represents an absolute name.
Definition: name.hpp:44
ns3::ndn::Name
Name
Definition: ndn-common.cpp:25
private-key.hpp
NDN_THROW
#define NDN_THROW(e)
Definition: exception.hpp:61
ndn::KeyParams::getKeyType
KeyType getKeyType() const
Definition: key-params.hpp:48
ndn::KeyType::HMAC
@ HMAC
HMAC key, supports sign/verify operations.
ndn::security::tpm::BackEnd::constructAsymmetricKeyName
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
ndn::security::tpm::BackEndMem::Impl
Definition: back-end-mem.cpp:38
transform::PrivateKey
key-handle-mem.hpp
ndn::security::tpm::BackEndMem::Impl::keys
std::unordered_map< Name, shared_ptr< PrivateKey > > keys
Definition: back-end-mem.cpp:40
buffer-stream.hpp
ndn::KeyType::RSA
@ RSA
RSA key, supports sign/verify and encrypt/decrypt operations.
ndn::ConstBufferPtr
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:126
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
back-end-mem.hpp