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-2018 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 namespace ndn {
30 namespace security {
31 namespace tpm {
32 
34 
36 {
37 public:
38  std::unordered_map<Name, shared_ptr<PrivateKey>> keys;
39 };
40 
41 BackEndMem::BackEndMem(const std::string&)
42  : m_impl(new Impl)
43 {
44 }
45 
46 BackEndMem::~BackEndMem() = default;
47 
48 const std::string&
50 {
51  static std::string scheme = "tpm-memory";
52  return scheme;
53 }
54 
55 bool
56 BackEndMem::doHasKey(const Name& keyName) const
57 {
58  return (m_impl->keys.count(keyName) > 0);
59 }
60 
61 unique_ptr<KeyHandle>
62 BackEndMem::doGetKeyHandle(const Name& keyName) const
63 {
64  auto it = m_impl->keys.find(keyName);
65  if (it == m_impl->keys.end())
66  return nullptr;
67  return make_unique<KeyHandleMem>(it->second);
68 }
69 
70 unique_ptr<KeyHandle>
71 BackEndMem::doCreateKey(const Name& identityName, const KeyParams& params)
72 {
73  shared_ptr<PrivateKey> key(transform::generatePrivateKey(params).release());
74  unique_ptr<KeyHandle> keyHandle = make_unique<KeyHandleMem>(key);
75 
76  setKeyName(*keyHandle, identityName, params);
77 
78  m_impl->keys[keyHandle->getKeyName()] = key;
79  return keyHandle;
80 }
81 
82 void
83 BackEndMem::doDeleteKey(const Name& keyName)
84 {
85  m_impl->keys.erase(keyName);
86 }
87 
89 BackEndMem::doExportKey(const Name& keyName, const char* pw, size_t pwLen)
90 {
91  OBufferStream os;
92  m_impl->keys[keyName]->savePkcs8(os, pw, pwLen);
93  return os.buf();
94 }
95 
96 void
97 BackEndMem::doImportKey(const Name& keyName, const uint8_t* buf, size_t size, const char* pw, size_t pwLen)
98 {
99  try {
100  auto key = make_shared<PrivateKey>();
101  key->loadPkcs8(buf, size, pw, pwLen);
102  m_impl->keys[keyName] = key;
103  }
104  catch (const PrivateKey::Error& e) {
105  BOOST_THROW_EXCEPTION(Error("Cannot import private key: "s + e.what()));
106  }
107 }
108 
109 } // namespace tpm
110 } // namespace security
111 } // namespace ndn
Copyright (c) 2011-2015 Regents of the University of California.
BackEndMem(const std::string &location="")
Create memory-based TPM backend.
unique_ptr< PrivateKey > generatePrivateKey(const KeyParams &keyParams)
Generate a private key according to keyParams.
Represents an absolute name.
Definition: name.hpp:43
static void setKeyName(KeyHandle &keyHandle, const Name &identity, const KeyParams &params)
Set the key name in keyHandle according to identity and params.
Definition: back-end.cpp:110
static const std::string & getScheme()
Base class of key parameters.
Definition: key-params.hpp:35
std::unordered_map< Name, shared_ptr< PrivateKey > > keys
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:126