26 #include "../encoding/oid.hpp" 27 #include "../encoding/buffer-stream.hpp" 35 : m_location(location)
54 uint8_t salt[8] = {0};
59 BOOST_THROW_EXCEPTION(
Error(
"Cannot generate salt or iv"));
61 uint32_t iterationCount = 2048;
63 PKCS5_PBKDF2_HMAC<SHA1> keyGenerator;
64 size_t derivedLen = 24;
65 byte derived[24] = {0};
69 keyGenerator.DeriveKey(derived, derivedLen, purpose,
70 reinterpret_cast<const byte*>(passwordStr.c_str()), passwordStr.size(),
71 salt, 8, iterationCount);
73 catch (
const CryptoPP::Exception& e) {
74 BOOST_THROW_EXCEPTION(
Error(
"Cannot derived the encryption key"));
78 CBC_Mode< DES_EDE3 >::Encryption e;
79 e.SetKeyWithIV(derived, derivedLen, iv);
83 if (pkcs8PrivateKey ==
nullptr)
84 BOOST_THROW_EXCEPTION(
Error(
"Cannot export the private key, #1"));
88 StringSource stringSource(pkcs8PrivateKey->buf(), pkcs8PrivateKey->size(),
true,
89 new StreamTransformationFilter(e,
new FileSink(encryptedOs)));
91 catch (
const CryptoPP::Exception& e) {
92 BOOST_THROW_EXCEPTION(
Error(
"Cannot export the private key, #2"));
96 Oid pbes2Id(
"1.2.840.113549.1.5.13");
97 Oid pbkdf2Id(
"1.2.840.113549.1.5.12");
98 Oid pbes2encsId(
"1.2.840.113549.3.7");
102 FileSink sink(pkcs8Os);
107 DERSequenceEncoder encryptedPrivateKeyInfo(sink);
112 DERSequenceEncoder encryptionAlgorithm(encryptedPrivateKeyInfo);
114 pbes2Id.
encode(encryptionAlgorithm);
118 DERSequenceEncoder pbes2Params(encryptionAlgorithm);
123 DERSequenceEncoder pbes2KDFs(pbes2Params);
125 pbkdf2Id.
encode(pbes2KDFs);
131 DERSequenceEncoder pbkdf2Params(pbes2KDFs);
133 DEREncodeOctetString(pbkdf2Params, salt, 8);
134 DEREncodeUnsigned<uint32_t>(pbkdf2Params, iterationCount, INTEGER);
136 pbkdf2Params.MessageEnd();
138 pbes2KDFs.MessageEnd();
143 DERSequenceEncoder pbes2Encs(pbes2Params);
145 pbes2encsId.
encode(pbes2Encs);
146 DEREncodeOctetString(pbes2Encs, iv, 8);
148 pbes2Encs.MessageEnd();
150 pbes2Params.MessageEnd();
152 encryptionAlgorithm.MessageEnd();
154 DEREncodeOctetString(encryptedPrivateKeyInfo,
155 encryptedOs.
buf()->buf(), encryptedOs.
buf()->size());
157 encryptedPrivateKeyInfo.MessageEnd();
159 return pkcs8Os.
buf();
161 catch (
const CryptoPP::Exception& e) {
162 BOOST_THROW_EXCEPTION(
Error(
"Cannot export the private key, #3"));
168 const uint8_t* buf,
size_t size,
169 const std::string& passwordStr)
175 SecByteBlock saltBlock;
176 uint32_t iterationCount;
178 SecByteBlock ivBlock;
179 SecByteBlock encryptedDataBlock;
184 StringSource source(buf, size,
true);
189 BERSequenceDecoder encryptedPrivateKeyInfo(source);
194 BERSequenceDecoder encryptionAlgorithm(encryptedPrivateKeyInfo);
196 pbes2Id.
decode(encryptionAlgorithm);
200 BERSequenceDecoder pbes2Params(encryptionAlgorithm);
205 BERSequenceDecoder pbes2KDFs(pbes2Params);
207 pbkdf2Id.
decode(pbes2KDFs);
213 BERSequenceDecoder pbkdf2Params(pbes2KDFs);
215 BERDecodeOctetString(pbkdf2Params, saltBlock);
216 BERDecodeUnsigned<uint32_t>(pbkdf2Params, iterationCount, INTEGER);
218 pbkdf2Params.MessageEnd();
220 pbes2KDFs.MessageEnd();
225 BERSequenceDecoder pbes2Encs(pbes2Params);
227 pbes2encsId.
decode(pbes2Encs);
228 BERDecodeOctetString(pbes2Encs, ivBlock);
230 pbes2Encs.MessageEnd();
232 pbes2Params.MessageEnd();
234 encryptionAlgorithm.MessageEnd();
236 BERDecodeOctetString(encryptedPrivateKeyInfo, encryptedDataBlock);
238 encryptedPrivateKeyInfo.MessageEnd();
240 catch (
const CryptoPP::Exception& e) {
244 PKCS5_PBKDF2_HMAC<SHA1> keyGenerator;
245 size_t derivedLen = 24;
246 byte derived[24] = {0};
250 keyGenerator.DeriveKey(derived, derivedLen,
252 reinterpret_cast<const byte*>(passwordStr.c_str()), passwordStr.size(),
253 saltBlock.BytePtr(), saltBlock.size(),
256 catch (
const CryptoPP::Exception& e) {
261 CBC_Mode< DES_EDE3 >::Decryption d;
262 d.SetKeyWithIV(derived, derivedLen, ivBlock.BytePtr());
266 StringSource encryptedSource(encryptedDataBlock.BytePtr(), encryptedDataBlock.size(),
true,
267 new StreamTransformationFilter(d,
new FileSink(privateKeyOs)));
269 catch (
const CryptoPP::Exception& e) {
274 privateKeyOs.
buf()->buf(), privateKeyOs.
buf()->size()))
278 StringSource privateKeySource(privateKeyOs.
buf()->buf(), privateKeyOs.
buf()->size(),
true);
281 SecByteBlock rawKeyBits;
286 BERSequenceDecoder privateKeyInfo(privateKeySource);
289 BERDecodeUnsigned<uint32_t>(privateKeyInfo, versionNum, INTEGER);
290 BERSequenceDecoder sequenceDecoder(privateKeyInfo);
293 keyTypeOid.
decode(sequenceDecoder);
308 switch (publicKeyType) {
310 RSA::PrivateKey privateKey;
311 privateKey.Load(StringStore(privateKeyOs.
buf()->buf(), privateKeyOs.
buf()->size()).Ref());
312 RSAFunction publicKey(privateKey);
314 FileSink publicKeySink(publicKeyOs);
315 publicKey.DEREncode(publicKeySink);
316 publicKeySink.MessageEnd();
321 ECDSA<ECP, SHA256>::PrivateKey privateKey;
322 privateKey.Load(StringStore(privateKeyOs.
buf()->buf(), privateKeyOs.
buf()->size()).Ref());
324 ECDSA<ECP, SHA256>::PublicKey publicKey;
325 privateKey.MakePublicKey(publicKey);
326 publicKey.AccessGroupParameters().SetEncodeAsOID(
true);
328 FileSink publicKeySink(publicKeyOs);
329 publicKey.DEREncode(publicKeySink);
330 publicKeySink.MessageEnd();
338 catch (
const CryptoPP::Exception& e) {
351 bool isInitialized =
false;
353 #ifdef NDN_CXX_HAVE_GETPASS 356 pw0 = getpass(prompt.c_str());
359 std::string password1 = pw0;
360 memset(pw0, 0, strlen(pw0));
362 pw0 = getpass(
"Confirm:");
363 if (pw0 ==
nullptr) {
364 std::fill(password1.begin(), password1.end(), 0);
368 if (password1.compare(pw0) == 0) {
369 isInitialized =
true;
370 password.swap(password1);
373 std::fill(password1.begin(), password1.end(), 0);
374 memset(pw0, 0, strlen(pw0));
376 if (password.empty())
379 #endif // NDN_CXX_HAVE_GETPASS 381 return isInitialized;
void encode(CryptoPP::BufferedTransformation &out) const
void decode(CryptoPP::BufferedTransformation &in)
const Oid ECDSA("1.2.840.10045.2.1")
Copyright (c) 2011-2015 Regents of the University of California.
Copyright (c) 2013-2016 Regents of the University of California.
ConstBufferPtr exportPrivateKeyPkcs5FromTpm(const Name &keyName, const std::string &password)
Export a private key in PKCS#5 format.
SecTpm(const std::string &location)
std::string getTpmLocator()
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.
virtual bool generateRandomBlock(uint8_t *res, size_t size)=0
Generate a random block.
const Oid RSA("1.2.840.113549.1.1.1")
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.
Name abstraction to represent an absolute name.
shared_ptr< Buffer > buf()
Flush written data to the stream and return shared pointer to the underlying buffer.
virtual ConstBufferPtr exportPrivateKeyPkcs8FromTpm(const Name &keyName)=0
Export a private key in PKCS#8 format.
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.
implements an output stream that constructs ndn::Buffer
virtual std::string getScheme()=0
virtual bool getImpExpPassWord(std::string &password, const std::string &prompt)
Get import/export password.
shared_ptr< const Buffer > ConstBufferPtr