NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: 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; -*- */
22 #include "back-end-mem.hpp"
23 #include "key-handle-mem.hpp"
24 #include "../transform/private-key.hpp"
25 #include "../../encoding/buffer-stream.hpp"
26 #include <unordered_map>
27 
28 namespace ndn {
29 namespace security {
30 namespace tpm {
31 
33 
35 {
36 public:
37  std::unordered_map<Name, shared_ptr<PrivateKey>> keys;
38 };
39 
40 BackEndMem::BackEndMem(const std::string&)
41  : m_impl(new Impl)
42 {
43 }
44 
45 BackEndMem::~BackEndMem() = default;
46 
47 const std::string&
49 {
50  static std::string scheme = "tpm-memory";
51  return scheme;
52 }
53 
54 bool
55 BackEndMem::doHasKey(const Name& keyName) const
56 {
57  return (m_impl->keys.count(keyName) > 0);
58 }
59 
60 unique_ptr<KeyHandle>
61 BackEndMem::doGetKeyHandle(const Name& keyName) const
62 {
63  auto it = m_impl->keys.find(keyName);
64  if (it == m_impl->keys.end())
65  return nullptr;
66  return make_unique<KeyHandleMem>(it->second);
67 }
68 
69 unique_ptr<KeyHandle>
70 BackEndMem::doCreateKey(const Name& identityName, const KeyParams& params)
71 {
72  shared_ptr<PrivateKey> key(transform::generatePrivateKey(params).release());
73  unique_ptr<KeyHandle> keyHandle = make_unique<KeyHandleMem>(key);
74 
75  setKeyName(*keyHandle, identityName, params);
76 
77  m_impl->keys[keyHandle->getKeyName()] = key;
78  return keyHandle;
79 }
80 
81 void
82 BackEndMem::doDeleteKey(const Name& keyName)
83 {
84  m_impl->keys.erase(keyName);
85 }
86 
88 BackEndMem::doExportKey(const Name& keyName, const char* pw, size_t pwLen)
89 {
90  OBufferStream os;
91  m_impl->keys[keyName]->savePkcs8(os, pw, pwLen);
92  return os.buf();
93 }
94 
95 void
96 BackEndMem::doImportKey(const Name& keyName, const uint8_t* buf, size_t size, const char* pw, size_t pwLen)
97 {
98  try {
99  auto key = make_shared<PrivateKey>();
100  key->loadPkcs8(buf, size, pw, pwLen);
101  m_impl->keys[keyName] = key;
102  }
103  catch (const PrivateKey::Error& e) {
104  BOOST_THROW_EXCEPTION(Error(std::string("Cannot import private key: ") + e.what()));
105  }
106 }
107 
108 } // namespace tpm
109 } // namespace security
110 } // 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.
Catch-all error for security policy errors that don&#39;t fit in other categories.
Definition: base.hpp:79
Represents an absolute name.
Definition: name.hpp:42
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()
shared_ptr< Buffer > buf()
Flush written data to the stream and return shared pointer to the underlying buffer.
Base class of 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:89