32 #include <boost/filesystem.hpp> 40 class BackEndFile::Impl
44 Impl(
const std::string& dir)
47 keystorePath = boost::filesystem::path(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";
61 keystorePath /=
"ndnsec-key-file";
62 boost::filesystem::create_directories(keystorePath);
65 boost::filesystem::path
66 toFileName(
const Name& keyName)
74 return keystorePath / (os.str() +
".privkey");
78 boost::filesystem::path keystorePath;
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&) {
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&) {
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&) {
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&) {
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);
#define NDN_THROW_NESTED(e)
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
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.
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
shared_ptr< const Buffer > ConstBufferPtr