32 #if BOOST_VERSION >= 107200 33 #include <boost/filesystem/exception.hpp> 35 #include <boost/filesystem/operations.hpp> 36 #include <boost/filesystem/path.hpp> 37 #include <boost/lexical_cast.hpp> 43 namespace fs = boost::filesystem;
50 Impl(
const std::string& dir)
53 m_keystorePath = fs::path(dir);
55 #ifdef NDN_CXX_HAVE_TESTS 56 else if (std::getenv(
"TEST_HOME") !=
nullptr) {
57 m_keystorePath = fs::path(std::getenv(
"TEST_HOME")) /
".ndn";
59 #endif // NDN_CXX_HAVE_TESTS 60 else if (std::getenv(
"HOME") !=
nullptr) {
61 m_keystorePath = fs::path(std::getenv(
"HOME")) /
".ndn";
64 m_keystorePath = fs::current_path() /
".ndn";
67 m_keystorePath /=
"ndnsec-key-file";
68 fs::create_directories(m_keystorePath);
74 std::ostringstream os;
82 return m_keystorePath / (os.str() +
".privkey");
86 fs::path m_keystorePath;
90 : m_impl(make_unique<
Impl>(location))
99 static std::string scheme =
"tpm-file";
104 BackEndFile::doHasKey(
const Name& keyName)
const 106 if (!fs::exists(m_impl->toFileName(keyName)))
113 catch (
const std::runtime_error&) {
118 unique_ptr<KeyHandle>
119 BackEndFile::doGetKeyHandle(
const Name& keyName)
const 121 if (!doHasKey(keyName))
124 return make_unique<KeyHandleMem>(loadKey(keyName));
127 unique_ptr<KeyHandle>
128 BackEndFile::doCreateKey(
const Name& identityName,
const KeyParams& params)
135 NDN_THROW(std::invalid_argument(
"File-based TPM does not support creating a key of type " +
136 boost::lexical_cast<std::string>(params.
getKeyType())));
140 unique_ptr<KeyHandle> keyHandle = make_unique<KeyHandleMem>(key);
143 keyHandle->setKeyName(keyName);
146 saveKey(keyName, *key);
149 catch (
const std::runtime_error&) {
155 BackEndFile::doDeleteKey(
const Name& keyName)
157 auto keyPath = m_impl->toFileName(keyName);
158 if (!fs::exists(keyPath))
164 catch (
const fs::filesystem_error&) {
170 BackEndFile::doExportKey(
const Name& keyName,
const char* pw,
size_t pwLen)
172 unique_ptr<PrivateKey> key;
174 key = loadKey(keyName);
181 key->savePkcs8(os, pw, pwLen);
186 BackEndFile::doImportKey(
const Name& keyName, span<const uint8_t> pkcs8,
const char* pw,
size_t pwLen)
191 saveKey(keyName, key);
199 BackEndFile::doImportKey(
const Name& keyName, shared_ptr<transform::PrivateKey> key)
202 saveKey(keyName, *key);
209 unique_ptr<PrivateKey>
210 BackEndFile::loadKey(
const Name& keyName)
const 212 std::ifstream is(m_impl->toFileName(keyName).string());
213 auto key = make_unique<PrivateKey>();
214 key->loadPkcs1Base64(is);
221 std::string fileName = m_impl->toFileName(keyName).string();
222 std::ofstream os(fileName);
226 ::chmod(fileName.data(), 0000400);
#define NDN_THROW_NESTED(e)
Copyright (c) 2011-2015 Regents of the University of California.
RSA key, supports sign/verify and encrypt/decrypt operations.
Name constructAsymmetricKeyName(const KeyHandle &key, const Name &identity, const KeyParams ¶ms) const
Construct and return the name of a RSA or EC key, based on identity and params.
static const std::string & getScheme()
BackEndFile(const std::string &location="")
Create file-based TPM backend.
Elliptic Curve key (e.g. for ECDSA), supports sign/verify operations.
Use the SHA-256 hash of the public key as key id.
Represents an absolute name.
KeyType getKeyType() const
shared_ptr< Buffer > buf()
Flush written data to the stream and return shared pointer to the underlying buffer.
Base class for key parameters.
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
implements an output stream that constructs ndn::Buffer
Impl(const std::string &dir)
fs::path toFileName(const Name &keyName) const
shared_ptr< const Buffer > ConstBufferPtr