Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 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 | |
| 28 | namespace ndn { |
| 29 | namespace security { |
| 30 | namespace tpm { |
| 31 | |
| 32 | using transform::PrivateKey; |
| 33 | |
| 34 | class BackEndMem::Impl |
| 35 | { |
| 36 | public: |
| 37 | std::unordered_map<Name, shared_ptr<PrivateKey>> keys; |
| 38 | }; |
| 39 | |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame^] | 40 | BackEndMem::BackEndMem(const std::string&) |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 41 | : m_impl(new Impl) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | BackEndMem::~BackEndMem() = default; |
| 46 | |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame^] | 47 | const std::string& |
| 48 | BackEndMem::getScheme() |
| 49 | { |
| 50 | static std::string scheme = "tpm-memory"; |
| 51 | return scheme; |
| 52 | } |
| 53 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 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 | |
| 87 | ConstBufferPtr |
| 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 |