blob: beac9db5abbece2da272aa2a3140a0e063578f47 [file] [log] [blame]
Yingdi Yu0b60e7a2015-07-16 21:05:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016 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
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
28namespace ndn {
29namespace security {
30namespace tpm {
31
32using transform::PrivateKey;
33
34class BackEndMem::Impl
35{
36public:
37 std::unordered_map<Name, shared_ptr<PrivateKey>> keys;
38};
39
40BackEndMem::BackEndMem()
41 : m_impl(new Impl)
42{
43}
44
45BackEndMem::~BackEndMem() = default;
46
47bool
48BackEndMem::doHasKey(const Name& keyName) const
49{
50 return (m_impl->keys.count(keyName) > 0);
51}
52
53unique_ptr<KeyHandle>
54BackEndMem::doGetKeyHandle(const Name& keyName) const
55{
56 auto it = m_impl->keys.find(keyName);
57 if (it == m_impl->keys.end())
58 return nullptr;
59 return make_unique<KeyHandleMem>(it->second);
60}
61
62unique_ptr<KeyHandle>
63BackEndMem::doCreateKey(const Name& identityName, const KeyParams& params)
64{
65 shared_ptr<PrivateKey> key(transform::generatePrivateKey(params).release());
66 unique_ptr<KeyHandle> keyHandle = make_unique<KeyHandleMem>(key);
67
68 setKeyName(*keyHandle, identityName, params);
69
70 m_impl->keys[keyHandle->getKeyName()] = key;
71 return keyHandle;
72}
73
74void
75BackEndMem::doDeleteKey(const Name& keyName)
76{
77 m_impl->keys.erase(keyName);
78}
79
80ConstBufferPtr
81BackEndMem::doExportKey(const Name& keyName, const char* pw, size_t pwLen)
82{
83 OBufferStream os;
84 m_impl->keys[keyName]->savePkcs8(os, pw, pwLen);
85 return os.buf();
86}
87
88void
89BackEndMem::doImportKey(const Name& keyName, const uint8_t* buf, size_t size, const char* pw, size_t pwLen)
90{
91 try {
92 auto key = make_shared<PrivateKey>();
93 key->loadPkcs8(buf, size, pw, pwLen);
94 m_impl->keys[keyName] = key;
95 }
96 catch (const PrivateKey::Error& e) {
97 BOOST_THROW_EXCEPTION(Error(std::string("Cannot import private key: ") + e.what()));
98 }
99}
100
101} // namespace tpm
102} // namespace security
103} // namespace ndn