24 #include "../transform.hpp" 25 #include "../transform/private-key.hpp" 26 #include "../../encoding/buffer-stream.hpp" 32 #include <boost/filesystem.hpp> 44 Impl(
const std::string& dir)
49 #ifdef NDN_CXX_HAVE_TESTS 50 else if (std::getenv(
"TEST_HOME") !=
nullptr) {
51 keystorePath = boost::filesystem::path(std::getenv(
"TEST_HOME")) /
".ndn";
53 #endif // NDN_CXX_HAVE_TESTS 54 else if (std::getenv(
"HOME") !=
nullptr) {
55 keystorePath = boost::filesystem::path(std::getenv(
"HOME")) /
".ndn";
58 keystorePath = boost::filesystem::current_path() /
".ndn";
65 boost::filesystem::path
82 : m_impl(new
Impl(location))
91 static std::string scheme =
"tpm-file";
96 BackEndFile::doHasKey(
const Name& keyName)
const 98 if (!boost::filesystem::exists(m_impl->toFileName(keyName)))
105 catch (
const std::runtime_error&) {
110 unique_ptr<KeyHandle>
111 BackEndFile::doGetKeyHandle(
const Name& keyName)
const 113 if (!doHasKey(keyName))
116 return make_unique<KeyHandleMem>(loadKey(keyName));
119 unique_ptr<KeyHandle>
120 BackEndFile::doCreateKey(
const Name& identityName,
const KeyParams& params)
123 unique_ptr<KeyHandle> keyHandle = make_unique<KeyHandleMem>(key);
128 saveKey(keyHandle->getKeyName(), key);
131 catch (
const std::runtime_error& e) {
132 BOOST_THROW_EXCEPTION(Error(std::string(
"Cannot write key to disk: ") + e.what()));
137 BackEndFile::doDeleteKey(
const Name& keyName)
139 boost::filesystem::path keyPath(m_impl->toFileName(keyName));
141 if (boost::filesystem::exists(keyPath)) {
143 boost::filesystem::remove(keyPath);
145 catch (
const boost::filesystem::filesystem_error&) {
146 BOOST_THROW_EXCEPTION(Error(
"Cannot delete key"));
152 BackEndFile::doExportKey(
const Name& keyName,
const char* pw,
size_t pwLen)
154 shared_ptr<PrivateKey> key;
156 key = loadKey(keyName);
158 catch (
const PrivateKey::Error&) {
159 BOOST_THROW_EXCEPTION(Error(
"Cannot export private key"));
162 key->savePkcs8(os, pw, pwLen);
167 BackEndFile::doImportKey(
const Name& keyName,
const uint8_t* buf,
size_t size,
const char* pw,
size_t pwLen)
170 auto key = make_shared<PrivateKey>();
171 key->loadPkcs8(buf, size, pw, pwLen);
172 saveKey(keyName, key);
174 catch (
const PrivateKey::Error&) {
175 BOOST_THROW_EXCEPTION(Error(
"Cannot import private key"));
179 shared_ptr<PrivateKey>
180 BackEndFile::loadKey(
const Name& keyName)
const 182 auto key = make_shared<PrivateKey>();
183 std::fstream is(m_impl->toFileName(keyName).string(), std::ios_base::in);
184 key->loadPkcs1Base64(is);
189 BackEndFile::saveKey(
const Name& keyName, shared_ptr<PrivateKey> key)
191 std::string fileName = m_impl->toFileName(keyName).string();
192 std::fstream os(fileName, std::ios_base::out);
193 key->savePkcs1Base64(os);
196 ::chmod(fileName.c_str(), 0000400);
Copyright (c) 2011-2015 Regents of the University of California.
boost::filesystem::path keystorePath
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)
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
Impl(const std::string &dir)
shared_ptr< const Buffer > ConstBufferPtr