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 | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2018 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-file.hpp" |
| 23 | #include "key-handle-mem.hpp" |
| 24 | #include "../transform.hpp" |
| 25 | #include "../transform/private-key.hpp" |
| 26 | #include "../../encoding/buffer-stream.hpp" |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 27 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 28 | #include <cstdlib> |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 29 | #include <fstream> |
| 30 | #include <sys/stat.h> |
| 31 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 32 | #include <boost/filesystem.hpp> |
| 33 | |
| 34 | namespace ndn { |
| 35 | namespace security { |
| 36 | namespace tpm { |
| 37 | |
| 38 | using transform::PrivateKey; |
| 39 | |
| 40 | class BackEndFile::Impl |
| 41 | { |
| 42 | public: |
| 43 | explicit |
| 44 | Impl(const std::string& dir) |
| 45 | { |
| 46 | if (!dir.empty()) { |
| 47 | keystorePath = boost::filesystem::path(dir); |
| 48 | } |
| 49 | #ifdef NDN_CXX_HAVE_TESTS |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 50 | else if (std::getenv("TEST_HOME") != nullptr) { |
| 51 | keystorePath = boost::filesystem::path(std::getenv("TEST_HOME")) / ".ndn"; |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 52 | } |
| 53 | #endif // NDN_CXX_HAVE_TESTS |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 54 | else if (std::getenv("HOME") != nullptr) { |
| 55 | keystorePath = boost::filesystem::path(std::getenv("HOME")) / ".ndn"; |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 56 | } |
| 57 | else { |
| 58 | keystorePath = boost::filesystem::current_path() / ".ndn"; |
| 59 | } |
| 60 | |
| 61 | keystorePath /= "ndnsec-key-file"; |
| 62 | boost::filesystem::create_directories(keystorePath); |
| 63 | } |
| 64 | |
| 65 | boost::filesystem::path |
| 66 | toFileName(const Name& keyName) |
| 67 | { |
| 68 | std::stringstream os; |
| 69 | { |
| 70 | using namespace transform; |
| 71 | bufferSource(keyName.wireEncode().wire(), keyName.wireEncode().size()) >> |
| 72 | digestFilter(DigestAlgorithm::SHA256) >> hexEncode() >> streamSink(os); |
| 73 | } |
| 74 | return keystorePath / (os.str() + ".privkey"); |
| 75 | } |
| 76 | |
| 77 | public: |
| 78 | boost::filesystem::path keystorePath; |
| 79 | }; |
| 80 | |
| 81 | BackEndFile::BackEndFile(const std::string& location) |
| 82 | : m_impl(new Impl(location)) |
| 83 | { |
| 84 | } |
| 85 | |
| 86 | BackEndFile::~BackEndFile() = default; |
| 87 | |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 88 | const std::string& |
| 89 | BackEndFile::getScheme() |
| 90 | { |
| 91 | static std::string scheme = "tpm-file"; |
| 92 | return scheme; |
| 93 | } |
| 94 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 95 | bool |
| 96 | BackEndFile::doHasKey(const Name& keyName) const |
| 97 | { |
| 98 | if (!boost::filesystem::exists(m_impl->toFileName(keyName))) |
| 99 | return false; |
| 100 | |
| 101 | try { |
| 102 | loadKey(keyName); |
| 103 | return true; |
| 104 | } |
| 105 | catch (const std::runtime_error&) { |
| 106 | return false; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | unique_ptr<KeyHandle> |
| 111 | BackEndFile::doGetKeyHandle(const Name& keyName) const |
| 112 | { |
| 113 | if (!doHasKey(keyName)) |
| 114 | return nullptr; |
| 115 | |
| 116 | return make_unique<KeyHandleMem>(loadKey(keyName)); |
| 117 | } |
| 118 | |
| 119 | unique_ptr<KeyHandle> |
| 120 | BackEndFile::doCreateKey(const Name& identityName, const KeyParams& params) |
| 121 | { |
| 122 | shared_ptr<PrivateKey> key(transform::generatePrivateKey(params).release()); |
| 123 | unique_ptr<KeyHandle> keyHandle = make_unique<KeyHandleMem>(key); |
| 124 | |
| 125 | setKeyName(*keyHandle, identityName, params); |
| 126 | |
| 127 | try { |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 128 | saveKey(keyHandle->getKeyName(), *key); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 129 | return keyHandle; |
| 130 | } |
| 131 | catch (const std::runtime_error& e) { |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 132 | BOOST_THROW_EXCEPTION(Error("Cannot write key to file: "s + e.what())); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | void |
| 137 | BackEndFile::doDeleteKey(const Name& keyName) |
| 138 | { |
| 139 | boost::filesystem::path keyPath(m_impl->toFileName(keyName)); |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 140 | if (!boost::filesystem::exists(keyPath)) |
| 141 | return; |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 142 | |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 143 | try { |
| 144 | boost::filesystem::remove(keyPath); |
| 145 | } |
| 146 | catch (const boost::filesystem::filesystem_error& e) { |
| 147 | BOOST_THROW_EXCEPTION(Error("Cannot remove key file: "s + e.what())); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
| 151 | ConstBufferPtr |
| 152 | BackEndFile::doExportKey(const Name& keyName, const char* pw, size_t pwLen) |
| 153 | { |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 154 | unique_ptr<PrivateKey> key; |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 155 | try { |
| 156 | key = loadKey(keyName); |
| 157 | } |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 158 | catch (const PrivateKey::Error& e) { |
| 159 | BOOST_THROW_EXCEPTION(Error("Cannot export private key: "s + e.what())); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 160 | } |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 161 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 162 | OBufferStream os; |
| 163 | key->savePkcs8(os, pw, pwLen); |
| 164 | return os.buf(); |
| 165 | } |
| 166 | |
| 167 | void |
| 168 | BackEndFile::doImportKey(const Name& keyName, const uint8_t* buf, size_t size, const char* pw, size_t pwLen) |
| 169 | { |
| 170 | try { |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 171 | PrivateKey key; |
| 172 | key.loadPkcs8(buf, size, pw, pwLen); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 173 | saveKey(keyName, key); |
| 174 | } |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 175 | catch (const PrivateKey::Error& e) { |
| 176 | BOOST_THROW_EXCEPTION(Error("Cannot import private key: "s + e.what())); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 180 | unique_ptr<PrivateKey> |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 181 | BackEndFile::loadKey(const Name& keyName) const |
| 182 | { |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 183 | std::ifstream is(m_impl->toFileName(keyName).string()); |
| 184 | auto key = make_unique<PrivateKey>(); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 185 | key->loadPkcs1Base64(is); |
| 186 | return key; |
| 187 | } |
| 188 | |
| 189 | void |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 190 | BackEndFile::saveKey(const Name& keyName, const PrivateKey& key) |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 191 | { |
| 192 | std::string fileName = m_impl->toFileName(keyName).string(); |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 193 | std::ofstream os(fileName); |
| 194 | key.savePkcs1Base64(os); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 195 | |
| 196 | // set file permission |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 197 | ::chmod(fileName.data(), 0000400); |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | } // namespace tpm |
| 201 | } // namespace security |
| 202 | } // namespace ndn |