143 #if defined(OT_CRYPTO_USING_OPENSSL)
147 #include "util/stacktrace.h"
152 #if defined(OT_CRYPTO_USING_OPENSSL)
154 void OTAsymmetricKey_OpenSSL::OTAsymmetricKey_OpenSSLPrivdp::SetX509(X509* x509)
156 if (m_pX509 == x509)
return;
158 if (
nullptr != m_pX509) {
166 void OTAsymmetricKey_OpenSSL::OTAsymmetricKey_OpenSSLPrivdp::SetKeyAsCopyOf(
167 EVP_PKEY& theKey,
bool bIsPrivateKey,
const OTPasswordData* pPWData,
168 const OTPassword* pImportPassword)
171 OTPasswordData thePWData(!pImportPassword
172 ?
"Enter your wallet's master passphrase. "
173 "(OTAsymmetricKey_OpenSSL::SetKeyAsCopyOf)"
174 :
"Enter your exported Nym's passphrase. "
175 "(OTAsymmetricKey_OpenSSL::SetKeyAsCopyOf)");
177 m_pKey = bIsPrivateKey
180 nullptr == pPWData ? &thePWData : pPWData,
183 CopyPublicKey(theKey,
184 nullptr == pPWData ? &thePWData : pPWData,
186 OT_ASSERT_MSG(
nullptr != m_pKey,
"OTAsymmetricKey_OpenSSL::SetKeyAsCopyOf: "
187 "ASSERT: nullptr != m_pKey \n");
189 backlink->m_bIsPublicKey = !bIsPrivateKey;
190 backlink->m_bIsPrivateKey = bIsPrivateKey;
192 if (
nullptr == backlink->m_p_ascKey) {
193 backlink->m_p_ascKey =
new OTASCIIArmor;
194 OT_ASSERT(
nullptr != backlink->m_p_ascKey);
197 backlink->m_p_ascKey->Release();
201 if (backlink->m_bIsPrivateKey) {
203 *m_pKey, *backlink->m_p_ascKey, backlink->m_timer,
204 nullptr == pPWData ? &thePWData : pPWData, pImportPassword);
209 else if (backlink->m_bIsPublicKey) {
211 *m_pKey, *backlink->m_p_ascKey);
214 otErr << __FUNCTION__
215 <<
": Error: This key is NEITHER public NOR private!\n";
219 EVP_PKEY* OTAsymmetricKey_OpenSSL::OTAsymmetricKey_OpenSSLPrivdp::
220 GetKeyLowLevel()
const
225 const EVP_PKEY* OTAsymmetricKey_OpenSSL::OTAsymmetricKey_OpenSSLPrivdp::GetKey(
226 const OTPasswordData* pPWData)
229 "OTAsymmetricKey_OpenSSL::GetKey: nullptr != m_p_ascKey\n");
231 if (
nullptr == backlink->m_p_ascKey) {
232 otErr << __FUNCTION__
233 <<
": Unexpected nullptr m_p_ascKey. Printing stack "
234 "trace (and returning nullptr):\n";
239 if (backlink->m_timer.getElapsedTimeInSec() >
OT_KEY_TIMER)
240 backlink->ReleaseKeyLowLevel();
246 if (
nullptr == m_pKey)
247 return InstantiateKey(pPWData);
253 EVP_PKEY* OTAsymmetricKey_OpenSSL::OTAsymmetricKey_OpenSSLPrivdp::
254 InstantiateKey(
const OTPasswordData* pPWData)
256 if (backlink->IsPublic())
257 return InstantiatePublicKey(pPWData);
261 else if (backlink->IsPrivate())
262 return InstantiatePrivateKey(pPWData);
267 otErr <<
"OTAsymmetricKey_OpenSSL::InstantiateKey: Error: Key is "
268 "neither public nor private!\n";
281 "OTAsymmetricKey_OpenSSL::CopyPublicKey: ASSERT: nullptr != bmem");
283 EVP_PKEY* pReturnKey =
nullptr;
287 int32_t nWriteBio = PEM_write_bio_PUBKEY(bmem, &theKey);
289 if (0 == nWriteBio) {
290 otErr << __FUNCTION__
291 <<
": Error: Failed writing EVP_PKEY to memory buffer.\n";
295 <<
": Success writing EVP_PKEY to memory buffer.\n";
297 char* pChar =
nullptr;
303 const int64_t lSize = BIO_get_mem_data(bmem, &pChar);
304 const uint32_t nSize =
static_cast<uint32_t
>(lSize);
313 (static_cast<char*>(const_cast<void*>(
324 BIO_new_mem_buf(static_cast<char*>(const_cast<void*>(
328 "OTAsymmetricKey_OpenSSL::"
329 "CopyPublicKey: Assert: nullptr != "
336 nullptr == pImportPassword
337 ?
"Enter your wallet master passphrase. "
338 "(OTAsymmetricKey_OpenSSL::CopyPublicKey is calling "
339 "PEM_read_bio_PUBKEY...)"
340 :
"Enter the passphrase for your exported Nym.");
342 if (
nullptr == pImportPassword)
343 pReturnKey = PEM_read_bio_PUBKEY(
347 : const_cast<OTPasswordData*>(pPWData));
349 pReturnKey = PEM_read_bio_PUBKEY(
351 const_cast<OTPassword*>(pImportPassword));
359 otErr << __FUNCTION__ <<
": Error: Failed copying memory from "
360 "BIO into OTPayload.\n";
364 otErr << __FUNCTION__
365 <<
": Failed copying private key into memory.\n";
393 const EVP_CIPHER* pCipher =
400 EVP_PKEY* pReturnKey =
nullptr;
404 OTPasswordData thePWDataWrite(
"OTAsymmetricKey_OpenSSL::CopyPrivateKey is "
405 "calling PEM_write_bio_PrivateKey...");
413 int32_t nWriteBio =
false;
415 if (
nullptr == pImportPassword)
416 nWriteBio = PEM_write_bio_PrivateKey(
417 bmem, &theKey, pCipher,
nullptr, 0,
419 nullptr == pPWData ? &thePWDataWrite
420 : const_cast<OTPasswordData*>(pPWData));
422 nWriteBio = PEM_write_bio_PrivateKey(
423 bmem, &theKey, pCipher,
nullptr, 0, 0,
425 reinterpret_cast<const void*>(pImportPassword->
getPassword())));
427 if (0 == nWriteBio) {
428 otErr << __FUNCTION__
429 <<
": Failed writing EVP_PKEY to memory buffer.\n";
433 <<
": Success writing EVP_PKEY to memory buffer.\n";
435 char* pChar =
nullptr;
441 const int64_t lSize = BIO_get_mem_data(bmem, &pChar);
442 const uint32_t nSize =
static_cast<uint32_t
>(lSize);
451 (static_cast<char*>(const_cast<void*>(
465 BIO_new_mem_buf(static_cast<char*>(const_cast<void*>(
469 "OTAsymmetricKey_OpenSSL::"
470 "CopyPrivateKey: Assert: nullptr != "
477 "CopyPrivateKey is calling "
478 "PEM_read_bio_PUBKEY...");
480 if (
nullptr == pImportPassword)
481 pReturnKey = PEM_read_bio_PrivateKey(
485 : const_cast<OTPasswordData*>(pPWData));
487 pReturnKey = PEM_read_bio_PrivateKey(
489 const_cast<void*>(reinterpret_cast<const void*>(
494 otErr << __FUNCTION__ <<
": Error: Failed copying memory from "
495 "BIO into OTPayload.\n";
499 otErr << __FUNCTION__
500 <<
": Failed copying private key into memory.\n";
515 bool bReturnVal =
false;
517 const char* szFunc =
"OTAsymmetricKey_OpenSSL::ArmorPublicKey";
525 "OTAsymmetricKey_OpenSSL::ArmorPublicKey: ASSERT: nullptr != bmem");
529 int32_t nWriteBio = PEM_write_bio_PUBKEY(bmem, &theKey);
531 if (0 == nWriteBio) {
533 <<
": Error: Failed writing EVP_PKEY to memory buffer.\n";
536 otLog5 << szFunc <<
": Success writing EVP_PKEY to memory buffer.\n";
539 char* pChar =
nullptr;
545 int64_t lSize = BIO_get_mem_data(bmem, &pChar);
546 uint32_t nSize =
static_cast<uint32_t
>(
555 (static_cast<char*>(const_cast<void*>(
567 otLog5 << szFunc <<
": Success copying public key into memory.\n";
571 otErr << szFunc <<
": Failed copying public key into memory.\n";
580 EVP_PKEY* OTAsymmetricKey_OpenSSL::OTAsymmetricKey_OpenSSLPrivdp::
584 OT_ASSERT(backlink->m_p_ascKey !=
nullptr);
587 const char* szFunc =
"OTAsymmetricKey_OpenSSL::InstantiatePublicKey";
589 EVP_PKEY* pReturnKey =
nullptr;
595 backlink->m_p_ascKey->GetData(theData);
606 "OTAsymmetricKey_OpenSSL::"
607 "InstantiatePublicKey: Assert: nullptr != "
614 "InstantiatePublicKey is calling "
615 "PEM_read_bio_PUBKEY...");
617 if (
nullptr == pPWData) pPWData = &thePWData;
619 pReturnKey = PEM_read_bio_PUBKEY(keyBio,
nullptr,
621 const_cast<OTPasswordData*>(pPWData));
623 backlink->ReleaseKeyLowLevel();
626 if (
nullptr != pReturnKey) {
630 <<
": Success reading public key from ASCII-armored data:\n\n"
631 << backlink->m_p_ascKey->Get() <<
"\n\n";
637 <<
": Failed reading public key from ASCII-armored data:\n\n"
638 << backlink->m_p_ascKey->Get() <<
"\n\n";
642 EVP_PKEY* OTAsymmetricKey_OpenSSL::OTAsymmetricKey_OpenSSLPrivdp::
643 InstantiatePrivateKey(
const OTPasswordData* pPWData)
646 OT_ASSERT(backlink->m_p_ascKey !=
nullptr);
649 EVP_PKEY* pReturnKey =
nullptr;
656 backlink->m_p_ascKey->GetData(theData);
672 if (theData.GetSize() > 0) {
673 OpenSSL_BIO keyBio = BIO_new_mem_buf(
674 static_cast<char*>(const_cast<void*>(theData.GetPayloadPointer())),
677 "OTAsymmetricKey_OpenSSL::"
678 "InstantiatePrivateKey: Assert: nullptr != "
683 OTPasswordData thePWData(
"OTAsymmetricKey_OpenSSL::"
684 "InstantiatePrivateKey is calling "
685 "PEM_read_bio_PrivateKey...");
687 if (
nullptr == pPWData) pPWData = &thePWData;
689 pReturnKey = PEM_read_bio_PrivateKey(
691 const_cast<OTPasswordData*>(pPWData));
694 backlink->ReleaseKeyLowLevel();
696 if (
nullptr != pReturnKey) {
709 backlink->m_timer.start();
714 <<
": Success reading private key from ASCII-armored data.\n\n";
720 otErr << __FUNCTION__
721 <<
": Failed reading private key from ASCII-armored data.\n\n";
731 bool bReturnVal =
false;
741 OTPasswordData thePWData(
"OTAsymmetricKey_OpenSSL::ArmorPrivateKey is "
742 "calling PEM_write_bio_PrivateKey...");
744 if (
nullptr == pPWData) pPWData = &thePWData;
746 int32_t nWriteBio = 0;
748 if (
nullptr == pImportPassword)
749 nWriteBio = PEM_write_bio_PrivateKey(
753 const_cast<OTPasswordData*>(pPWData));
755 nWriteBio = PEM_write_bio_PrivateKey(
758 nullptr, 0, 0, const_cast<void*>(reinterpret_cast<const void*>(
761 if (0 == nWriteBio) {
762 otErr << __FUNCTION__
763 <<
": Failed writing EVP_PKEY to memory buffer.\n";
781 <<
": Success writing EVP_PKEY to memory buffer.\n";
784 char* pChar =
nullptr;
790 int64_t lSize = BIO_get_mem_data(bmem, &pChar);
791 uint32_t nSize =
static_cast<uint32_t
>(lSize);
799 (static_cast<char*>(const_cast<void*>(
813 <<
": Success copying private key into memory.\n";
817 otErr << __FUNCTION__
818 <<
": Failed copying private key into memory.\n";
OTLOG_IMPORT OTLogStream otLog4
static bool ArmorPrivateKey(EVP_PKEY &theKey, OTASCIIArmor &ascKey, Timer &theTimer, const OTPasswordData *pPWData=nullptr, const OTPassword *pImportPassword=nullptr)
static bool ArmorPublicKey(EVP_PKEY &theKey, OTASCIIArmor &ascKey)
OTAsymmetricKey_OpenSSL()
static EVP_PKEY * CopyPublicKey(EVP_PKEY &theKey, const OTPasswordData *pPWData=nullptr, const OTPassword *pImportPassword=nullptr)
#define OT_ASSERT_MSG(x, s)
EXPORT void SetPayloadSize(uint32_t lNewSize)
static EVP_PKEY * CopyPrivateKey(EVP_PKEY &theKey, const OTPasswordData *pPWData=nullptr, const OTPassword *pImportPassword=nullptr)
EXPORT const char * getPassword() const
EXPORT const void * GetPayloadPointer() const
OTLOG_IMPORT OTLogStream otErr
static EXPORT OT_OPENSSL_CALLBACK * GetPasswordCallback()
EXPORT bool SetData(const OTData &theData, bool bLineBreaks=true)
static EXPORT void * safe_memcpy(void *dest, uint32_t dsize, const void *src, uint32_t ssize, bool zeroSource=false)
virtual EXPORT void Release()
OTLOG_IMPORT OTLogStream otLog5