24 #include "../transform.hpp" 25 #include "../transform/private-key.hpp" 26 #include "../../encoding/buffer-stream.hpp" 27 #include <unordered_map> 30 #include <boost/filesystem.hpp> 42 Impl(
const std::string& dir)
47 #ifdef NDN_CXX_HAVE_TESTS 48 else if (getenv(
"TEST_HOME") !=
nullptr) {
49 keystorePath = boost::filesystem::path(getenv(
"TEST_HOME")) /
".ndn";
51 #endif // NDN_CXX_HAVE_TESTS 52 else if (getenv(
"HOME") !=
nullptr) {
53 keystorePath = boost::filesystem::path(getenv(
"HOME")) /
".ndn";
56 keystorePath = boost::filesystem::current_path() /
".ndn";
63 boost::filesystem::path
80 : m_impl(new
Impl(location))
89 static std::string scheme =
"tpm-file";
94 BackEndFile::doHasKey(
const Name& keyName)
const 96 if (!boost::filesystem::exists(m_impl->toFileName(keyName)))
103 catch (
const std::runtime_error&) {
108 unique_ptr<KeyHandle>
109 BackEndFile::doGetKeyHandle(
const Name& keyName)
const 111 if (!doHasKey(keyName))
114 return make_unique<KeyHandleMem>(loadKey(keyName));
117 unique_ptr<KeyHandle>
118 BackEndFile::doCreateKey(
const Name& identityName,
const KeyParams& params)
121 unique_ptr<KeyHandle> keyHandle = make_unique<KeyHandleMem>(key);
126 saveKey(keyHandle->getKeyName(), key);
129 catch (
const std::runtime_error& e) {
130 BOOST_THROW_EXCEPTION(
Error(std::string(
"Cannot write key to disk: ") + e.what()));
135 BackEndFile::doDeleteKey(
const Name& keyName)
137 boost::filesystem::path keyPath(m_impl->toFileName(keyName));
139 if (boost::filesystem::exists(keyPath)) {
141 boost::filesystem::remove(keyPath);
143 catch (
const boost::filesystem::filesystem_error&) {
144 BOOST_THROW_EXCEPTION(
Error(
"Cannot delete key"));
150 BackEndFile::doExportKey(
const Name& keyName,
const char* pw,
size_t pwLen)
152 shared_ptr<PrivateKey> key;
154 key = loadKey(keyName);
156 catch (
const PrivateKey::Error&) {
157 BOOST_THROW_EXCEPTION(
Error(
"Cannot export private key"));
160 key->savePkcs8(os, pw, pwLen);
165 BackEndFile::doImportKey(
const Name& keyName,
const uint8_t* buf,
size_t size,
const char* pw,
size_t pwLen)
168 auto key = make_shared<PrivateKey>();
169 key->loadPkcs8(buf, size, pw, pwLen);
170 saveKey(keyName, key);
172 catch (
const PrivateKey::Error&) {
173 BOOST_THROW_EXCEPTION(
Error(
"Cannot import private key"));
177 shared_ptr<PrivateKey>
178 BackEndFile::loadKey(
const Name& keyName)
const 180 auto key = make_shared<PrivateKey>();
181 std::fstream is(m_impl->toFileName(keyName).string(), std::ios_base::in);
182 key->loadPkcs1Base64(is);
187 BackEndFile::saveKey(
const Name& keyName, shared_ptr<PrivateKey> key)
189 std::string fileName = m_impl->toFileName(keyName).string();
190 std::fstream os(fileName, std::ios_base::out);
191 key->savePkcs1Base64(os);
194 chmod(fileName.c_str(), 0000400);
Copyright (c) 2011-2015 Regents of the University of California.
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
boost::filesystem::path keystorePath
Catch-all error for security policy errors that don't fit in other categories.
static const std::string & getScheme()
BackEndFile(const std::string &location="")
Create file-based TPM backend.
Use the SHA256 hash of the public key as the key id.
Represents an absolute name.
static void setKeyName(KeyHandle &keyHandle, const Name &identity, const KeyParams ¶ms)
Set the key name in keyHandle according to identity and params.
boost::filesystem::path toFileName(const Name &keyName)
shared_ptr< Buffer > buf()
Flush written data to the stream and return shared pointer to the underlying buffer.
Base class of key parameters.
implements an output stream that constructs ndn::Buffer
Impl(const std::string &dir)
shared_ptr< const Buffer > ConstBufferPtr