blob: 2427229c99a5b5391eca914bdc036aa1e6a320b9 [file] [log] [blame]
Yingdi Yu0b60e7a2015-07-16 21:05:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventodb4da5e2018-06-15 11:37:52 -04002/*
3 * Copyright (c) 2013-2018 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"
Davide Pesaventodb4da5e2018-06-15 11:37:52 -040026
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070027#include <unordered_map>
28
29namespace ndn {
30namespace security {
31namespace tpm {
32
33using transform::PrivateKey;
34
35class BackEndMem::Impl
36{
37public:
38 std::unordered_map<Name, shared_ptr<PrivateKey>> keys;
39};
40
Yingdi Yufe4733a2015-10-22 14:24:12 -070041BackEndMem::BackEndMem(const std::string&)
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070042 : m_impl(new Impl)
43{
44}
45
46BackEndMem::~BackEndMem() = default;
47
Yingdi Yufe4733a2015-10-22 14:24:12 -070048const std::string&
49BackEndMem::getScheme()
50{
51 static std::string scheme = "tpm-memory";
52 return scheme;
53}
54
Yingdi Yu0b60e7a2015-07-16 21:05:11 -070055bool
56BackEndMem::doHasKey(const Name& keyName) const
57{
58 return (m_impl->keys.count(keyName) > 0);
59}
60
61unique_ptr<KeyHandle>
62BackEndMem::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
70unique_ptr<KeyHandle>
71BackEndMem::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
82void
83BackEndMem::doDeleteKey(const Name& keyName)
84{
85 m_impl->keys.erase(keyName);
86}
87
88ConstBufferPtr
89BackEndMem::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
96void
97BackEndMem::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) {
Davide Pesaventodb4da5e2018-06-15 11:37:52 -0400105 BOOST_THROW_EXCEPTION(Error("Cannot import private key: "s + e.what()));
Yingdi Yu0b60e7a2015-07-16 21:05:11 -0700106 }
107}
108
109} // namespace tpm
110} // namespace security
111} // namespace ndn