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 | 82d6a4c | 2017-12-23 19:47:20 -0500 | [diff] [blame] | 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 "tpm.hpp" |
| 23 | #include "back-end.hpp" |
| 24 | #include "../../encoding/buffer-stream.hpp" |
| 25 | |
| 26 | namespace ndn { |
| 27 | namespace security { |
| 28 | namespace tpm { |
| 29 | |
| 30 | Tpm::Tpm(const std::string& scheme, const std::string& location, unique_ptr<BackEnd> backEnd) |
| 31 | : m_scheme(scheme) |
| 32 | , m_location(location) |
| 33 | , m_backEnd(std::move(backEnd)) |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | Tpm::~Tpm() = default; |
| 38 | |
| 39 | std::string |
| 40 | Tpm::getTpmLocator() const |
| 41 | { |
| 42 | return m_scheme + ":" + m_location; |
| 43 | } |
| 44 | |
| 45 | bool |
| 46 | Tpm::hasKey(const Name& keyName) const |
| 47 | { |
| 48 | return m_backEnd->hasKey(keyName); |
| 49 | } |
| 50 | |
| 51 | Name |
| 52 | Tpm::createKey(const Name& identityName, const KeyParams& params) |
| 53 | { |
| 54 | switch (params.getKeyType()) { |
| 55 | case KeyType::RSA: |
| 56 | case KeyType::EC: { |
| 57 | unique_ptr<KeyHandle> keyHandle = m_backEnd->createKey(identityName, params); |
| 58 | Name keyName = keyHandle->getKeyName(); |
| 59 | m_keys[keyName] = std::move(keyHandle); |
| 60 | return keyName; |
| 61 | } |
| 62 | default: { |
| 63 | BOOST_THROW_EXCEPTION(Error("Fail to create a key pair: Unsupported key type")); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | void |
| 69 | Tpm::deleteKey(const Name& keyName) |
| 70 | { |
| 71 | auto it = m_keys.find(keyName); |
| 72 | if (it != m_keys.end()) |
| 73 | m_keys.erase(it); |
| 74 | |
| 75 | m_backEnd->deleteKey(keyName); |
| 76 | } |
| 77 | |
| 78 | ConstBufferPtr |
| 79 | Tpm::getPublicKey(const Name& keyName) const |
| 80 | { |
| 81 | const KeyHandle* key = findKey(keyName); |
| 82 | |
| 83 | if (key == nullptr) |
| 84 | return nullptr; |
| 85 | else |
| 86 | return key->derivePublicKey(); |
| 87 | } |
| 88 | |
| 89 | ConstBufferPtr |
| 90 | Tpm::sign(const uint8_t* buf, size_t size, const Name& keyName, DigestAlgorithm digestAlgorithm) const |
| 91 | { |
| 92 | const KeyHandle* key = findKey(keyName); |
| 93 | |
| 94 | if (key == nullptr) |
| 95 | return nullptr; |
| 96 | else |
| 97 | return key->sign(digestAlgorithm, buf, size); |
| 98 | } |
| 99 | |
| 100 | ConstBufferPtr |
| 101 | Tpm::decrypt(const uint8_t* buf, size_t size, const Name& keyName) const |
| 102 | { |
| 103 | const KeyHandle* key = findKey(keyName); |
| 104 | |
| 105 | if (key == nullptr) |
| 106 | return nullptr; |
| 107 | else |
| 108 | return key->decrypt(buf, size); |
| 109 | } |
| 110 | |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 111 | bool |
| 112 | Tpm::isTerminalMode() const |
| 113 | { |
| 114 | return m_backEnd->isTerminalMode(); |
| 115 | } |
| 116 | |
| 117 | void |
| 118 | Tpm::setTerminalMode(bool isTerminal) const |
| 119 | { |
| 120 | m_backEnd->setTerminalMode(isTerminal); |
| 121 | } |
| 122 | |
| 123 | bool |
| 124 | Tpm::isTpmLocked() const |
| 125 | { |
| 126 | return m_backEnd->isTpmLocked(); |
| 127 | } |
| 128 | |
| 129 | bool |
| 130 | Tpm::unlockTpm(const char* password, size_t passwordLength) const |
| 131 | { |
| 132 | return m_backEnd->unlockTpm(password, passwordLength); |
| 133 | } |
| 134 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 135 | ConstBufferPtr |
Davide Pesavento | 794f687 | 2017-05-15 23:33:38 -0400 | [diff] [blame] | 136 | Tpm::exportPrivateKey(const Name& keyName, const char* pw, size_t pwLen) const |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 137 | { |
| 138 | return m_backEnd->exportKey(keyName, pw, pwLen); |
| 139 | } |
| 140 | |
Davide Pesavento | 82d6a4c | 2017-12-23 19:47:20 -0500 | [diff] [blame] | 141 | void |
Davide Pesavento | 794f687 | 2017-05-15 23:33:38 -0400 | [diff] [blame] | 142 | Tpm::importPrivateKey(const Name& keyName, const uint8_t* pkcs8, size_t pkcs8Len, |
| 143 | const char* pw, size_t pwLen) |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 144 | { |
Davide Pesavento | 82d6a4c | 2017-12-23 19:47:20 -0500 | [diff] [blame] | 145 | m_backEnd->importKey(keyName, pkcs8, pkcs8Len, pw, pwLen); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | const KeyHandle* |
| 149 | Tpm::findKey(const Name& keyName) const |
| 150 | { |
| 151 | auto it = m_keys.find(keyName); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 152 | if (it != m_keys.end()) |
| 153 | return it->second.get(); |
| 154 | |
Davide Pesavento | 82d6a4c | 2017-12-23 19:47:20 -0500 | [diff] [blame] | 155 | auto handle = m_backEnd->getKeyHandle(keyName); |
| 156 | if (handle == nullptr) |
| 157 | return nullptr; |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 158 | |
Davide Pesavento | 82d6a4c | 2017-12-23 19:47:20 -0500 | [diff] [blame] | 159 | const KeyHandle* key = handle.get(); |
| 160 | m_keys[keyName] = std::move(handle); |
| 161 | return key; |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | } // namespace tpm |
| 165 | } // namespace security |
| 166 | } // namespace ndn |