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 | 8aad372 | 2017-09-16 20:57:28 -0400 | [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 "back-end.hpp" |
| 23 | #include "key-handle.hpp" |
| 24 | #include "tpm.hpp" |
Davide Pesavento | 8aad372 | 2017-09-16 20:57:28 -0400 | [diff] [blame^] | 25 | #include "../pib/key.hpp" |
| 26 | #include "../transform/buffer-source.hpp" |
| 27 | #include "../transform/digest-filter.hpp" |
| 28 | #include "../transform/stream-sink.hpp" |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 29 | #include "../../encoding/buffer-stream.hpp" |
| 30 | #include "../../util/random.hpp" |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 31 | |
| 32 | namespace ndn { |
| 33 | namespace security { |
| 34 | namespace tpm { |
| 35 | |
| 36 | BackEnd::~BackEnd() = default; |
| 37 | |
| 38 | bool |
| 39 | BackEnd::hasKey(const Name& keyName) const |
| 40 | { |
| 41 | return doHasKey(keyName); |
| 42 | } |
| 43 | |
| 44 | unique_ptr<KeyHandle> |
| 45 | BackEnd::getKeyHandle(const Name& keyName) const |
| 46 | { |
| 47 | return doGetKeyHandle(keyName); |
| 48 | } |
| 49 | |
| 50 | unique_ptr<KeyHandle> |
| 51 | BackEnd::createKey(const Name& identity, const KeyParams& params) |
| 52 | { |
| 53 | // key name checking |
| 54 | switch (params.getKeyIdType()) { |
| 55 | case KeyIdType::USER_SPECIFIED: { // keyId is pre-set. |
| 56 | Name keyName = v2::constructKeyName(identity, params.getKeyId()); |
| 57 | if (hasKey(keyName)) { |
| 58 | BOOST_THROW_EXCEPTION(Tpm::Error("Key `" + keyName.toUri() + "` already exists")); |
| 59 | } |
| 60 | break; |
| 61 | } |
| 62 | case KeyIdType::SHA256: { |
| 63 | // KeyName will be assigned in setKeyName after key is generated |
| 64 | break; |
| 65 | } |
| 66 | case KeyIdType::RANDOM: { |
| 67 | Name keyName; |
| 68 | name::Component keyId; |
| 69 | do { |
| 70 | keyId = name::Component::fromNumber(random::generateSecureWord64()); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 71 | keyName = v2::constructKeyName(identity, keyId); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 72 | } while (hasKey(keyName)); |
| 73 | |
| 74 | const_cast<KeyParams&>(params).setKeyId(keyId); |
| 75 | break; |
| 76 | } |
| 77 | default: { |
| 78 | BOOST_THROW_EXCEPTION(Error("Unsupported key id type")); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return doCreateKey(identity, params); |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | BackEnd::deleteKey(const Name& keyName) |
| 87 | { |
| 88 | doDeleteKey(keyName); |
| 89 | } |
| 90 | |
| 91 | ConstBufferPtr |
| 92 | BackEnd::exportKey(const Name& keyName, const char* pw, size_t pwLen) |
| 93 | { |
| 94 | if (!hasKey(keyName)) { |
| 95 | BOOST_THROW_EXCEPTION(Error("Key `" + keyName.toUri() + "` does not exist")); |
| 96 | } |
| 97 | return doExportKey(keyName, pw, pwLen); |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | BackEnd::importKey(const Name& keyName, const uint8_t* pkcs8, size_t pkcs8Len, const char* pw, size_t pwLen) |
| 102 | { |
| 103 | if (hasKey(keyName)) { |
| 104 | BOOST_THROW_EXCEPTION(Error("Key `" + keyName.toUri() + "` already exists")); |
| 105 | } |
| 106 | doImportKey(keyName, pkcs8, pkcs8Len, pw, pwLen); |
| 107 | } |
| 108 | |
| 109 | void |
| 110 | BackEnd::setKeyName(KeyHandle& keyHandle, const Name& identity, const KeyParams& params) |
| 111 | { |
| 112 | name::Component keyId; |
| 113 | switch (params.getKeyIdType()) { |
| 114 | case KeyIdType::USER_SPECIFIED: |
| 115 | keyId = params.getKeyId(); |
| 116 | break; |
| 117 | case KeyIdType::SHA256: { |
| 118 | using namespace transform; |
| 119 | |
| 120 | OBufferStream os; |
| 121 | bufferSource(*keyHandle.derivePublicKey()) >> digestFilter() >> streamSink(os); |
| 122 | keyId = name::Component(os.buf()); |
| 123 | break; |
| 124 | } |
| 125 | case KeyIdType::RANDOM: { |
| 126 | BOOST_ASSERT(!params.getKeyId().empty()); |
| 127 | keyId = params.getKeyId(); |
| 128 | break; |
| 129 | } |
| 130 | default: { |
| 131 | BOOST_ASSERT(false); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | keyHandle.setKeyName(v2::constructKeyName(identity, keyId)); |
| 136 | } |
| 137 | |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 138 | bool |
| 139 | BackEnd::isTerminalMode() const |
| 140 | { |
| 141 | return true; |
| 142 | } |
| 143 | |
| 144 | void |
| 145 | BackEnd::setTerminalMode(bool isTerminal) const |
| 146 | { |
| 147 | } |
| 148 | |
| 149 | bool |
| 150 | BackEnd::isTpmLocked() const |
| 151 | { |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | bool |
| 156 | BackEnd::unlockTpm(const char* pw, size_t pwLen) const |
| 157 | { |
| 158 | return !isTpmLocked(); |
| 159 | } |
| 160 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 161 | } // namespace tpm |
| 162 | } // namespace security |
| 163 | } // namespace ndn |