14 #include <boost/filesystem.hpp>
15 #include <boost/algorithm/string.hpp>
19 #include <sys/types.h>
34 m_keystorePath = boost::filesystem::path(getenv(
"HOME")) /
".ndn" /
"ndnsec-tpm-file";
38 boost::filesystem::create_directories (m_keystorePath);
41 boost::filesystem::path
44 using namespace CryptoPP;
47 StringSource src(keyName,
50 new Base64Encoder(
new CryptoPP::StringSink(digest))));
53 std::replace(digest.begin(), digest.end(),
'/',
'%');
55 return m_keystorePath / (digest + extension);
61 string keyFileName = nameTransform(keyName,
"").string();
64 string dirFile = (m_keystorePath /
"mapping.txt").
string();
66 outfile.open(dirFile.c_str(), std::ios_base::app);
67 outfile << keyName <<
' ' << keyFileName <<
'\n';
78 SecTpmFile::SecTpmFile(
const string& dir)
79 : m_impl(new
Impl(dir))
86 string keyURI = keyName.
toUri();
89 throw Error(
"public key exists");
91 throw Error(
"private key exists");
93 string keyFileName = m_impl->maintainMapping(keyURI);
101 using namespace CryptoPP;
102 AutoSeededRandomPool rng;
104 InvertibleRSAFunction privateKey;
105 privateKey.Initialize(rng, keySize);
107 string privateKeyFileName = keyFileName +
".pri";
108 Base64Encoder privateKeySink(
new FileSink(privateKeyFileName.c_str()));
109 privateKey.DEREncode(privateKeySink);
110 privateKeySink.MessageEnd();
112 RSAFunction publicKey(privateKey);
113 string publicKeyFileName = keyFileName +
".pub";
114 Base64Encoder publicKeySink(
new FileSink(publicKeyFileName.c_str()));
115 publicKey.DEREncode(publicKeySink);
116 publicKeySink.MessageEnd();
119 chmod(privateKeyFileName.c_str(), 0000400);
120 chmod(publicKeyFileName.c_str(), 0000444);
124 throw Error(
"Unsupported key type!");
127 catch (CryptoPP::Exception& e)
129 throw Error(e.what());
136 boost::filesystem::path publicKeyPath(m_impl->nameTransform(keyName.
toUri(),
".pub"));
137 boost::filesystem::path privateKeyPath(m_impl->nameTransform(keyName.
toUri(),
".pri"));
139 if (boost::filesystem::exists(publicKeyPath))
140 boost::filesystem::remove(publicKeyPath);
142 if (boost::filesystem::exists(privateKeyPath))
143 boost::filesystem::remove(privateKeyPath);
146 shared_ptr<PublicKey>
149 string keyURI = keyName.
toUri();
152 throw Error(
"Public Key does not exist");
157 using namespace CryptoPP;
158 FileSource(m_impl->nameTransform(keyURI,
".pub").string().c_str(),
160 new Base64Decoder(
new FileSink(os)));
162 catch (CryptoPP::Exception& e)
164 throw Error(e.what());
167 return make_shared<PublicKey>(
reinterpret_cast<const uint8_t*
>(os.str().c_str()),
175 CryptoPP::FileSource(m_impl->nameTransform(keyName.
toUri(),
".pri").
string().c_str(),
true,
176 new CryptoPP::Base64Decoder(
new CryptoPP::FileSink(privateKeyOs)));
178 return privateKeyOs.
buf();
186 using namespace CryptoPP;
188 string keyFileName = m_impl->maintainMapping(keyName.
toUri());
189 keyFileName.append(
".pri");
190 StringSource(buf, size,
192 new Base64Encoder(
new FileSink(keyFileName.c_str())));
195 catch (CryptoPP::Exception& e)
206 using namespace CryptoPP;
208 string keyFileName = m_impl->maintainMapping(keyName.
toUri());
209 keyFileName.append(
".pub");
210 StringSource(buf, size,
212 new Base64Encoder(
new FileSink(keyFileName.c_str())));
215 catch (CryptoPP::Exception& e)
225 string keyURI = keyName.
toUri();
228 throw Error(
"private key doesn't exists");
232 using namespace CryptoPP;
233 AutoSeededRandomPool rng;
237 FileSource file(m_impl->nameTransform(keyURI,
".pri").string().c_str(),
238 true,
new Base64Decoder);
239 file.TransferTo(bytes);
241 RSA::PrivateKey privateKey;
242 privateKey.Load(bytes);
245 switch (digestAlgorithm)
249 RSASS<PKCS1v15, SHA256>::Signer signer(privateKey);
252 StringSource(data, dataLength,
254 new SignerFilter(rng, signer,
new FileSink(os)));
259 throw Error(
"Unsupported digest algorithm!");
262 catch (CryptoPP::Exception& e)
264 throw Error(e.what());
271 const Name& keyName,
bool isSymmetric)
273 throw Error(
"SecTpmFile::decryptInTpm is not supported!");
333 const Name& keyName,
bool isSymmetric)
335 throw Error(
"SecTpmFile::encryptInTpm is not supported!");
397 throw Error(
"SecTpmFile::generateSymmetricKeyInTpm is not supported!");
432 string keyURI = keyName.
toUri();
435 if (boost::filesystem::exists(m_impl->nameTransform(keyURI,
".pub")))
442 if (boost::filesystem::exists(m_impl->nameTransform(keyURI,
".pri")))
449 if (boost::filesystem::exists(m_impl->nameTransform(keyURI,
".key")))
462 CryptoPP::AutoSeededRandomPool rng;
463 rng.GenerateBlock(res, size);
466 catch (CryptoPP::Exception& e)
boost::filesystem::path m_keystorePath
Class implementing interface similar to ostringstream, but to construct ndn::Buffer.
virtual void generateSymmetricKeyInTpm(const Name &keyName, KeyType keyType, int keySize)
Generate a symmetric key.
virtual bool doesKeyExistInTpm(const Name &keyName, KeyClass keyClass)
Check if a particular key exists.
Class representing wire element of the NDN packet.
std::string toUri() const
Encode this name as a URI.
ptr_lib::shared_ptr< const Buffer > ConstBufferPtr
virtual void deleteKeyPairInTpm(const Name &keyName)
Delete a key pair of asymmetric keys.
virtual shared_ptr< PublicKey > getPublicKeyFromTpm(const Name &keyName)
Get a public key.
boost::filesystem::path nameTransform(const string &keyName, const string &extension)
virtual ConstBufferPtr decryptInTpm(const uint8_t *data, size_t dataLength, const Name &keyName, bool isSymmetric)
Decrypt data.
ptr_lib::shared_ptr< Buffer > buf()
Flush written data to the stream and return shared pointer to the underlying buffer.
virtual bool generateRandomBlock(uint8_t *res, size_t size)
Generate a random block.
A Name holds an array of Name::Component and represents an NDN name.
virtual ConstBufferPtr exportPrivateKeyPkcs8FromTpm(const Name &keyName)
Export a private key in PKCS#8 format.
string maintainMapping(const string &keyName)
virtual Block signInTpm(const uint8_t *data, size_t dataLength, const Name &keyName, DigestAlgorithm digestAlgorithm)
Sign data.
virtual bool importPublicKeyPkcs1IntoTpm(const Name &keyName, const uint8_t *buf, size_t size)
Import a public key in PKCS#1 format.
virtual ConstBufferPtr encryptInTpm(const uint8_t *data, size_t dataLength, const Name &keyName, bool isSymmetric)
Encrypt data.
void trim(std::string &str)
Modify str in place to erase whitespace on the left and right.
virtual bool importPrivateKeyPkcs8IntoTpm(const Name &keyName, const uint8_t *buf, size_t size)
Import a private key in PKCS#8 format.
virtual void generateKeyPairInTpm(const Name &keyName, KeyType keyType, int keySize)
Generate a pair of asymmetric keys.