blob: 9d051c605b97e5117d277459557bb99531a1d8e1 [file] [log] [blame]
Yingdi Yu0b60e7a2015-07-16 21:05:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yingdi Yufe4733a2015-10-22 14:24:12 -07003 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yu0b60e7a2015-07-16 21:05:11 -07004 *
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
Yingdi Yufe4733a2015-10-22 14:24:12 -070040BackEndMem::BackEndMem(const std::string&)
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070041 : m_impl(new Impl)
42{
43}
44
45BackEndMem::~BackEndMem() = default;
46
Yingdi Yufe4733a2015-10-22 14:24:12 -070047const std::string&
48BackEndMem::getScheme()
49{
50 static std::string scheme = "tpm-memory";
51 return scheme;
52}
53
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070054bool
55BackEndMem::doHasKey(const Name& keyName) const
56{
57 return (m_impl->keys.count(keyName) > 0);
58}
59
60unique_ptr<KeyHandle>
61BackEndMem::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
69unique_ptr<KeyHandle>
70BackEndMem::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
81void
82BackEndMem::doDeleteKey(const Name& keyName)
83{
84 m_impl->keys.erase(keyName);
85}
86
87ConstBufferPtr
88BackEndMem::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
95void
96BackEndMem::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