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