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(
"Cannot write key to file: "s + e.what()));
137 BackEndFile::doDeleteKey(
const Name& keyName)
139 boost::filesystem::path keyPath(m_impl->toFileName(keyName));
140 if (!boost::filesystem::exists(keyPath))
144 boost::filesystem::remove(keyPath);
146 catch (
const boost::filesystem::filesystem_error& e) {
147 BOOST_THROW_EXCEPTION(Error(
"Cannot remove key file: "s + e.what()));
152 BackEndFile::doExportKey(
const Name& keyName,
const char* pw,
size_t pwLen)
154 unique_ptr<PrivateKey> key;
156 key = loadKey(keyName);
158 catch (
const PrivateKey::Error& e) {
159 BOOST_THROW_EXCEPTION(Error(
"Cannot export private key: "s + e.what()));
163 key->savePkcs8(os, pw, pwLen);
168 BackEndFile::doImportKey(
const Name& keyName,
const uint8_t* buf,
size_t size,
const char* pw,
size_t pwLen)
172 key.loadPkcs8(buf, size, pw, pwLen);
173 saveKey(keyName, key);
175 catch (
const PrivateKey::Error& e) {
176 BOOST_THROW_EXCEPTION(Error(
"Cannot import private key: "s + e.what()));
180 unique_ptr<PrivateKey>
181 BackEndFile::loadKey(
const Name& keyName)
const 183 std::ifstream is(m_impl->toFileName(keyName).string());
184 auto key = make_unique<PrivateKey>();
185 key->loadPkcs1Base64(is);
190 BackEndFile::saveKey(
const Name& keyName,
const PrivateKey& key)
192 std::string fileName = m_impl->toFileName(keyName).string();
193 std::ofstream os(fileName);
194 key.savePkcs1Base64(os);
197 ::chmod(fileName.data(), 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