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