26 #include "../../encoding/oid.hpp" 27 #include "../../encoding/buffer-stream.hpp" 36 : m_location(location)
55 uint8_t salt[8] = {0};
60 BOOST_THROW_EXCEPTION(
Error(
"Cannot generate salt or iv"));
62 uint32_t iterationCount = 2048;
64 PKCS5_PBKDF2_HMAC<SHA1> keyGenerator;
65 size_t derivedLen = 24;
66 byte derived[24] = {0};
70 keyGenerator.DeriveKey(derived, derivedLen, purpose,
71 reinterpret_cast<const byte*>(passwordStr.c_str()), passwordStr.size(),
72 salt, 8, iterationCount);
74 catch (
const CryptoPP::Exception& e) {
75 BOOST_THROW_EXCEPTION(
Error(
"Cannot derived the encryption key"));
79 CBC_Mode< DES_EDE3 >::Encryption e;
80 e.SetKeyWithIV(derived, derivedLen, iv);
84 if (pkcs8PrivateKey ==
nullptr)
85 BOOST_THROW_EXCEPTION(
Error(
"Cannot export the private key, #1"));
89 StringSource stringSource(pkcs8PrivateKey->buf(), pkcs8PrivateKey->size(),
true,
90 new StreamTransformationFilter(e,
new FileSink(encryptedOs)));
92 catch (
const CryptoPP::Exception& e) {
93 BOOST_THROW_EXCEPTION(
Error(
"Cannot export the private key, #2"));
97 Oid pbes2Id(
"1.2.840.113549.1.5.13");
98 Oid pbkdf2Id(
"1.2.840.113549.1.5.12");
99 Oid pbes2encsId(
"1.2.840.113549.3.7");
103 FileSink sink(pkcs8Os);
108 DERSequenceEncoder encryptedPrivateKeyInfo(sink);
113 DERSequenceEncoder encryptionAlgorithm(encryptedPrivateKeyInfo);
115 pbes2Id.
encode(encryptionAlgorithm);
119 DERSequenceEncoder pbes2Params(encryptionAlgorithm);
124 DERSequenceEncoder pbes2KDFs(pbes2Params);
126 pbkdf2Id.
encode(pbes2KDFs);
132 DERSequenceEncoder pbkdf2Params(pbes2KDFs);
134 DEREncodeOctetString(pbkdf2Params, salt, 8);
135 DEREncodeUnsigned<uint32_t>(pbkdf2Params, iterationCount, INTEGER);
137 pbkdf2Params.MessageEnd();
139 pbes2KDFs.MessageEnd();
144 DERSequenceEncoder pbes2Encs(pbes2Params);
146 pbes2encsId.
encode(pbes2Encs);
147 DEREncodeOctetString(pbes2Encs, iv, 8);
149 pbes2Encs.MessageEnd();
151 pbes2Params.MessageEnd();
153 encryptionAlgorithm.MessageEnd();
155 DEREncodeOctetString(encryptedPrivateKeyInfo,
156 encryptedOs.
buf()->buf(), encryptedOs.
buf()->size());
158 encryptedPrivateKeyInfo.MessageEnd();
160 return pkcs8Os.
buf();
162 catch (
const CryptoPP::Exception& e) {
163 BOOST_THROW_EXCEPTION(
Error(
"Cannot export the private key, #3"));
169 const uint8_t* buf,
size_t size,
170 const std::string& passwordStr)
176 SecByteBlock saltBlock;
177 uint32_t iterationCount;
179 SecByteBlock ivBlock;
180 SecByteBlock encryptedDataBlock;
185 StringSource source(buf, size,
true);
190 BERSequenceDecoder encryptedPrivateKeyInfo(source);
195 BERSequenceDecoder encryptionAlgorithm(encryptedPrivateKeyInfo);
197 pbes2Id.
decode(encryptionAlgorithm);
201 BERSequenceDecoder pbes2Params(encryptionAlgorithm);
206 BERSequenceDecoder pbes2KDFs(pbes2Params);
208 pbkdf2Id.
decode(pbes2KDFs);
214 BERSequenceDecoder pbkdf2Params(pbes2KDFs);
216 BERDecodeOctetString(pbkdf2Params, saltBlock);
217 BERDecodeUnsigned<uint32_t>(pbkdf2Params, iterationCount, INTEGER);
219 pbkdf2Params.MessageEnd();
221 pbes2KDFs.MessageEnd();
226 BERSequenceDecoder pbes2Encs(pbes2Params);
228 pbes2encsId.
decode(pbes2Encs);
229 BERDecodeOctetString(pbes2Encs, ivBlock);
231 pbes2Encs.MessageEnd();
233 pbes2Params.MessageEnd();
235 encryptionAlgorithm.MessageEnd();
237 BERDecodeOctetString(encryptedPrivateKeyInfo, encryptedDataBlock);
239 encryptedPrivateKeyInfo.MessageEnd();
241 catch (
const CryptoPP::Exception& e) {
245 PKCS5_PBKDF2_HMAC<SHA1> keyGenerator;
246 size_t derivedLen = 24;
247 byte derived[24] = {0};
251 keyGenerator.DeriveKey(derived, derivedLen,
253 reinterpret_cast<const byte*>(passwordStr.c_str()), passwordStr.size(),
254 saltBlock.BytePtr(), saltBlock.size(),
257 catch (
const CryptoPP::Exception& e) {
262 CBC_Mode< DES_EDE3 >::Decryption d;
263 d.SetKeyWithIV(derived, derivedLen, ivBlock.BytePtr());
267 StringSource encryptedSource(encryptedDataBlock.BytePtr(), encryptedDataBlock.size(),
true,
268 new StreamTransformationFilter(d,
new FileSink(privateKeyOs)));
270 catch (
const CryptoPP::Exception& e) {
275 privateKeyOs.
buf()->buf(), privateKeyOs.
buf()->size()))
279 StringSource privateKeySource(privateKeyOs.
buf()->buf(), privateKeyOs.
buf()->size(),
true);
282 SecByteBlock rawKeyBits;
287 BERSequenceDecoder privateKeyInfo(privateKeySource);
290 BERDecodeUnsigned<uint32_t>(privateKeyInfo, versionNum, INTEGER);
291 BERSequenceDecoder sequenceDecoder(privateKeyInfo);
294 keyTypeOid.
decode(sequenceDecoder);
309 switch (publicKeyType) {
311 RSA::PrivateKey privateKey;
312 privateKey.Load(StringStore(privateKeyOs.
buf()->buf(), privateKeyOs.
buf()->size()).Ref());
313 RSAFunction publicKey(privateKey);
315 FileSink publicKeySink(publicKeyOs);
316 publicKey.DEREncode(publicKeySink);
317 publicKeySink.MessageEnd();
322 ECDSA<ECP, SHA256>::PrivateKey privateKey;
323 privateKey.Load(StringStore(privateKeyOs.
buf()->buf(), privateKeyOs.
buf()->size()).Ref());
325 ECDSA<ECP, SHA256>::PublicKey publicKey;
326 privateKey.MakePublicKey(publicKey);
327 publicKey.AccessGroupParameters().SetEncodeAsOID(
true);
329 FileSink publicKeySink(publicKeyOs);
330 publicKey.DEREncode(publicKeySink);
331 publicKeySink.MessageEnd();
339 catch (
const CryptoPP::Exception& e) {
352 bool isInitialized =
false;
354 #ifdef NDN_CXX_HAVE_GETPASS 357 pw0 = getpass(prompt.c_str());
360 std::string password1 = pw0;
361 memset(pw0, 0, strlen(pw0));
363 pw0 = getpass(
"Confirm:");
364 if (pw0 ==
nullptr) {
365 std::fill(password1.begin(), password1.end(), 0);
369 if (password1.compare(pw0) == 0) {
370 isInitialized =
true;
371 password.swap(password1);
374 std::fill(password1.begin(), password1.end(), 0);
375 memset(pw0, 0, strlen(pw0));
377 if (password.empty())
380 #endif // NDN_CXX_HAVE_GETPASS 382 return isInitialized;
void decode(CryptoPP::BufferedTransformation &in)
const Oid ECDSA("1.2.840.10045.2.1")
Copyright (c) 2013-2017 Regents of the University of California.
Copyright (c) 2013-2017 Regents of the University of California.
void encode(CryptoPP::BufferedTransformation &out) const
virtual ConstBufferPtr exportPrivateKeyPkcs8FromTpm(const Name &keyName)=0
Export a private key in PKCS#8 format.
virtual std::string getScheme()=0
std::string getTpmLocator()
virtual bool generateRandomBlock(uint8_t *res, size_t size)=0
Generate a random block.
bool importPrivateKeyPkcs5IntoTpm(const Name &keyName, const uint8_t *buffer, size_t bufferSize, const std::string &password)
Import a private key in PKCS#5 formatted buffer of size bufferSize.
const Oid RSA("1.2.840.113549.1.1.1")
Represents an absolute name.
virtual bool importPublicKeyPkcs1IntoTpm(const Name &keyName, const uint8_t *buffer, size_t bufferSize)=0
Import a public key in PKCS#1 formatted buffer of size bufferSize.
shared_ptr< Buffer > buf()
Flush written data to the stream and return shared pointer to the underlying buffer.
SecTpm(const std::string &location)
implements an output stream that constructs ndn::Buffer
ConstBufferPtr exportPrivateKeyPkcs5FromTpm(const Name &keyName, const std::string &password)
Export a private key in PKCS#5 format.
shared_ptr< const Buffer > ConstBufferPtr
virtual bool getImpExpPassWord(std::string &password, const std::string &prompt)
Get import/export password.
virtual bool importPrivateKeyPkcs8IntoTpm(const Name &keyName, const uint8_t *buffer, size_t bufferSize)=0
Import a private key from PKCS#8 formatted buffer of size bufferSize.