28 #include "../encoding/buffer-stream.hpp" 30 #include <boost/filesystem.hpp> 31 #include <boost/algorithm/string.hpp> 35 #include <sys/types.h> 44 using std::ostringstream;
55 boost::filesystem::path actualDir;
57 #ifdef NDN_CXX_HAVE_TESTS 58 if (getenv(
"TEST_HOME") !=
nullptr) {
59 actualDir = boost::filesystem::path(getenv(
"TEST_HOME")) /
".ndn";
62 #endif // NDN_CXX_HAVE_TESTS 63 if (getenv(
"HOME") !=
nullptr) {
64 actualDir = boost::filesystem::path(getenv(
"HOME")) /
".ndn";
67 actualDir = boost::filesystem::path(
".") /
".ndn";
71 actualDir = boost::filesystem::path(dir);
78 boost::filesystem::path
84 StringSource src(keyName,
87 new Base64Encoder(
new CryptoPP::StringSink(digest))));
90 std::replace(digest.begin(), digest.end(),
'/',
'%');
103 outfile.open(dirFile.c_str(), std::ios_base::app);
104 outfile << keyName <<
' ' << keyFileName <<
'\n';
117 , m_impl(new
Impl(location))
118 , m_inTerminal(false)
129 string keyURI = keyName.
toUri();
132 BOOST_THROW_EXCEPTION(
Error(
"public key exists"));
134 BOOST_THROW_EXCEPTION(
Error(
"private key exists"));
136 string keyFileName = m_impl->maintainMapping(keyURI);
144 AutoSeededRandomPool rng;
145 InvertibleRSAFunction privateKey;
146 privateKey.Initialize(rng, rsaParams.
getKeySize());
148 string privateKeyFileName = keyFileName +
".pri";
149 Base64Encoder privateKeySink(
new FileSink(privateKeyFileName.c_str()));
150 privateKey.DEREncode(privateKeySink);
151 privateKeySink.MessageEnd();
153 RSAFunction publicKey(privateKey);
154 string publicKeyFileName = keyFileName +
".pub";
155 Base64Encoder publicKeySink(
new FileSink(publicKeyFileName.c_str()));
156 publicKey.DEREncode(publicKeySink);
157 publicKeySink.MessageEnd();
160 chmod(privateKeyFileName.c_str(), 0000400);
161 chmod(publicKeyFileName.c_str(), 0000444);
173 curveName = ASN1::secp256r1();
176 curveName = ASN1::secp384r1();
179 curveName = ASN1::secp256r1();
183 AutoSeededRandomPool rng;
185 ECDSA<ECP, SHA256>::PrivateKey privateKey;
186 DL_GroupParameters_EC<ECP> cryptoParams(curveName);
187 cryptoParams.SetEncodeAsOID(
true);
188 privateKey.Initialize(rng, cryptoParams);
190 ECDSA<ECP, SHA256>::PublicKey publicKey;
191 privateKey.MakePublicKey(publicKey);
192 publicKey.AccessGroupParameters().SetEncodeAsOID(
true);
194 string privateKeyFileName = keyFileName +
".pri";
195 Base64Encoder privateKeySink(
new FileSink(privateKeyFileName.c_str()));
196 privateKey.DEREncode(privateKeySink);
197 privateKeySink.MessageEnd();
199 string publicKeyFileName = keyFileName +
".pub";
200 Base64Encoder publicKeySink(
new FileSink(publicKeyFileName.c_str()));
201 publicKey.Save(publicKeySink);
202 publicKeySink.MessageEnd();
205 chmod(privateKeyFileName.c_str(), 0000400);
206 chmod(publicKeyFileName.c_str(), 0000444);
211 BOOST_THROW_EXCEPTION(
Error(
"Unsupported key type"));
215 BOOST_THROW_EXCEPTION(
Error(e.what()));
217 catch (
const CryptoPP::Exception& e) {
218 BOOST_THROW_EXCEPTION(
Error(e.what()));
225 boost::filesystem::path publicKeyPath(m_impl->transformName(keyName.
toUri(),
".pub"));
226 boost::filesystem::path privateKeyPath(m_impl->transformName(keyName.
toUri(),
".pri"));
228 if (boost::filesystem::exists(publicKeyPath))
229 boost::filesystem::remove(publicKeyPath);
231 if (boost::filesystem::exists(privateKeyPath))
232 boost::filesystem::remove(privateKeyPath);
235 shared_ptr<v1::PublicKey>
238 string keyURI = keyName.
toUri();
241 BOOST_THROW_EXCEPTION(
Error(
"Public Key does not exist"));
246 FileSource(m_impl->transformName(keyURI,
".pub").string().c_str(),
248 new Base64Decoder(
new FileSink(os)));
250 catch (
const CryptoPP::Exception& e) {
251 BOOST_THROW_EXCEPTION(
Error(e.what()));
254 return make_shared<v1::PublicKey>(
reinterpret_cast<const uint8_t*
>(os.str().c_str()),
268 CryptoPP::FileSource(m_impl->transformName(keyName.
toUri(),
".pri").
string().c_str(),
true,
269 new CryptoPP::Base64Decoder(
new CryptoPP::FileSink(privateKeyOs)));
271 return privateKeyOs.
buf();
280 string keyFileName = m_impl->maintainMapping(keyName.
toUri());
281 keyFileName.append(
".pri");
282 StringSource(buf, size,
284 new Base64Encoder(
new FileSink(keyFileName.c_str())));
287 catch (
const CryptoPP::Exception& e) {
298 string keyFileName = m_impl->maintainMapping(keyName.
toUri());
299 keyFileName.append(
".pub");
300 StringSource(buf, size,
302 new Base64Encoder(
new FileSink(keyFileName.c_str())));
305 catch (
const CryptoPP::Exception& e) {
314 string keyURI = keyName.
toUri();
317 BOOST_THROW_EXCEPTION(
Error(
"private key doesn't exist"));
321 AutoSeededRandomPool rng;
324 shared_ptr<v1::PublicKey> pubkeyPtr;
327 switch (pubkeyPtr->getKeyType()) {
331 FileSource
file(m_impl->transformName(keyURI,
".pri").string().c_str(),
332 true,
new Base64Decoder);
333 file.TransferTo(bytes);
335 RSA::PrivateKey privateKey;
336 privateKey.Load(bytes);
339 switch (digestAlgorithm) {
341 RSASS<PKCS1v15, SHA256>::Signer signer(privateKey);
344 StringSource(data, dataLength,
346 new SignerFilter(rng, signer,
new FileSink(os)));
352 BOOST_THROW_EXCEPTION(
Error(
"Unsupported digest algorithm"));
359 FileSource
file(m_impl->transformName(keyURI,
".pri").string().c_str(),
360 true,
new Base64Decoder);
361 file.TransferTo(bytes);
365 switch (digestAlgorithm) {
367 ECDSA<ECP, SHA256>::PrivateKey privateKey;
368 privateKey.Load(bytes);
369 ECDSA<ECP, SHA256>::Signer signer(privateKey);
372 StringSource(data, dataLength,
374 new SignerFilter(rng, signer,
new FileSink(os)));
377 size_t bufSize = DSAConvertSignatureFormat(buf,
sizeof(buf), DSA_DER,
378 os.
buf()->buf(), os.
buf()->size(),
381 shared_ptr<Buffer> sigBuffer = make_shared<Buffer>(buf, bufSize);
387 BOOST_THROW_EXCEPTION(
Error(
"Unsupported digest algorithm"));
392 BOOST_THROW_EXCEPTION(
Error(
"Unsupported key type"));
395 catch (
const CryptoPP::Exception& e) {
396 BOOST_THROW_EXCEPTION(
Error(e.what()));
403 const Name& keyName,
bool isSymmetric)
405 BOOST_THROW_EXCEPTION(
Error(
"SecTpmFile::decryptInTpm is not supported"));
466 const Name& keyName,
bool isSymmetric)
468 BOOST_THROW_EXCEPTION(
Error(
"SecTpmFile::encryptInTpm is not supported"));
529 BOOST_THROW_EXCEPTION(
Error(
"SecTpmFile::generateSymmetricKeyInTpm is not supported"));
564 string keyURI = keyName.
toUri();
566 return boost::filesystem::exists(m_impl->transformName(keyURI,
".pub"));
569 return boost::filesystem::exists(m_impl->transformName(keyURI,
".pri"));
572 return boost::filesystem::exists(m_impl->transformName(keyURI,
".key"));
581 CryptoPP::AutoSeededRandomPool rng;
582 rng.GenerateBlock(res, size);
585 catch (
const CryptoPP::Exception& e) {
boost::filesystem::path m_keystorePath
Copyright (c) 2011-2015 Regents of the University of California.
virtual bool generateRandomBlock(uint8_t *res, size_t size)
Generate a random block.
std::string toUri() const
Encode this name as a URI.
static const std::string SCHEME
string maintainMapping(const string &keyName)
Copyright (c) 2013-2016 Regents of the University of California.
virtual void generateKeyPairInTpm(const Name &keyName, const KeyParams ¶ms)
Generate a pair of asymmetric keys.
virtual ConstBufferPtr encryptInTpm(const uint8_t *data, size_t dataLength, const Name &keyName, bool isSymmetric)
Encrypt data.
SecTpm is the base class of the TPM classes.
virtual std::string getScheme()
boost::filesystem::path transformName(const string &keyName, const string &extension)
Class representing a wire element of NDN-TLV packet format.
virtual bool importPublicKeyPkcs1IntoTpm(const Name &keyName, const uint8_t *buf, size_t size)
Import a public key in PKCS#1 formatted buffer of size bufferSize.
virtual ConstBufferPtr decryptInTpm(const uint8_t *data, size_t dataLength, const Name &keyName, bool isSymmetric)
Decrypt data.
virtual bool importPrivateKeyPkcs8IntoTpm(const Name &keyName, const uint8_t *buf, size_t size)
Import a private key from PKCS#8 formatted buffer of size bufferSize.
virtual void generateSymmetricKeyInTpm(const Name &keyName, const KeyParams ¶ms)
Generate a symmetric key.
virtual Block signInTpm(const uint8_t *data, size_t dataLength, const Name &keyName, DigestAlgorithm digestAlgorithm)
Sign data.
virtual shared_ptr< v1::PublicKey > getPublicKeyFromTpm(const Name &keyName)
Get a public key.
virtual void deleteKeyPairInTpm(const Name &keyName)
Delete a key pair of asymmetric keys.
Name abstraction to represent an absolute name.
SecTpmFile(const std::string &dir="")
KeyType getKeyType() const
shared_ptr< Buffer > buf()
Flush written data to the stream and return shared pointer to the underlying buffer.
Base class of key parameters.
void trim(std::string &str)
Modify str in place to erase whitespace on the left and right.
uint32_t getKeySize() const
implements an output stream that constructs ndn::Buffer
shared_ptr< const Buffer > ConstBufferPtr
virtual bool doesKeyExistInTpm(const Name &keyName, KeyClass keyClass)
Check if a particular key exists.
SimplePublicKeyParams is a template for public keys with only one parameter: size.
virtual ConstBufferPtr exportPrivateKeyPkcs8FromTpm(const Name &keyName)
Export a private key in PKCS#8 format.