Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2021 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 | |
Davide Pesavento | 4fb35d8 | 2019-10-31 19:33:10 -0400 | [diff] [blame] | 22 | #include "ndn-cxx/security/tpm/impl/back-end-mem.hpp" |
| 23 | #include "ndn-cxx/security/tpm/impl/key-handle-mem.hpp" |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "ndn-cxx/security/transform/private-key.hpp" |
| 25 | #include "ndn-cxx/encoding/buffer-stream.hpp" |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 26 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 27 | #include <unordered_map> |
| 28 | |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 29 | #include <boost/lexical_cast.hpp> |
| 30 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 31 | namespace ndn { |
| 32 | namespace security { |
| 33 | namespace tpm { |
| 34 | |
| 35 | using transform::PrivateKey; |
| 36 | |
| 37 | class BackEndMem::Impl |
| 38 | { |
| 39 | public: |
| 40 | std::unordered_map<Name, shared_ptr<PrivateKey>> keys; |
| 41 | }; |
| 42 | |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 43 | BackEndMem::BackEndMem(const std::string&) |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 44 | : m_impl(make_unique<Impl>()) |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 45 | { |
| 46 | } |
| 47 | |
| 48 | BackEndMem::~BackEndMem() = default; |
| 49 | |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 50 | const std::string& |
| 51 | BackEndMem::getScheme() |
| 52 | { |
| 53 | static std::string scheme = "tpm-memory"; |
| 54 | return scheme; |
| 55 | } |
| 56 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 57 | bool |
| 58 | BackEndMem::doHasKey(const Name& keyName) const |
| 59 | { |
| 60 | return (m_impl->keys.count(keyName) > 0); |
| 61 | } |
| 62 | |
| 63 | unique_ptr<KeyHandle> |
| 64 | BackEndMem::doGetKeyHandle(const Name& keyName) const |
| 65 | { |
| 66 | auto it = m_impl->keys.find(keyName); |
| 67 | if (it == m_impl->keys.end()) |
| 68 | return nullptr; |
| 69 | return make_unique<KeyHandleMem>(it->second); |
| 70 | } |
| 71 | |
| 72 | unique_ptr<KeyHandle> |
| 73 | BackEndMem::doCreateKey(const Name& identityName, const KeyParams& params) |
| 74 | { |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 75 | switch (params.getKeyType()) { |
| 76 | case KeyType::RSA: |
| 77 | case KeyType::EC: |
| 78 | case KeyType::HMAC: |
| 79 | break; |
| 80 | default: |
Davide Pesavento | 12cef87 | 2019-10-31 02:24:03 -0400 | [diff] [blame] | 81 | NDN_THROW(std::invalid_argument("Memory-based TPM does not support creating a key of type " + |
| 82 | boost::lexical_cast<std::string>(params.getKeyType()))); |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 83 | } |
| 84 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 85 | shared_ptr<PrivateKey> key(transform::generatePrivateKey(params).release()); |
| 86 | unique_ptr<KeyHandle> keyHandle = make_unique<KeyHandleMem>(key); |
| 87 | |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 88 | Name keyName; |
| 89 | if (params.getKeyType() == KeyType::HMAC) { |
| 90 | keyName = constructHmacKeyName(*key, identityName, params); |
| 91 | } |
| 92 | else { |
| 93 | keyName = constructAsymmetricKeyName(*keyHandle, identityName, params); |
| 94 | } |
| 95 | keyHandle->setKeyName(keyName); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 96 | |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 97 | m_impl->keys[keyName] = std::move(key); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 98 | return keyHandle; |
| 99 | } |
| 100 | |
| 101 | void |
| 102 | BackEndMem::doDeleteKey(const Name& keyName) |
| 103 | { |
| 104 | m_impl->keys.erase(keyName); |
| 105 | } |
| 106 | |
| 107 | ConstBufferPtr |
| 108 | BackEndMem::doExportKey(const Name& keyName, const char* pw, size_t pwLen) |
| 109 | { |
| 110 | OBufferStream os; |
| 111 | m_impl->keys[keyName]->savePkcs8(os, pw, pwLen); |
| 112 | return os.buf(); |
| 113 | } |
| 114 | |
| 115 | void |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 116 | BackEndMem::doImportKey(const Name& keyName, span<const uint8_t> pkcs8, const char* pw, size_t pwLen) |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 117 | { |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 118 | auto key = make_shared<PrivateKey>(); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 119 | try { |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 120 | key->loadPkcs8(pkcs8, pw, pwLen); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 121 | } |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 122 | catch (const PrivateKey::Error&) { |
| 123 | NDN_THROW_NESTED(Error("Cannot import private key")); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 124 | } |
laqinfan | 56a812d | 2019-06-03 15:33:58 -0500 | [diff] [blame] | 125 | doImportKey(keyName, std::move(key)); |
| 126 | } |
| 127 | |
| 128 | void |
| 129 | BackEndMem::doImportKey(const Name& keyName, shared_ptr<transform::PrivateKey> key) |
| 130 | { |
| 131 | m_impl->keys[keyName] = std::move(key); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | } // namespace tpm |
| 135 | } // namespace security |
| 136 | } // namespace ndn |