Open-Transactions  0.93.0-ge03d287
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
opentxs::OTAsymmetricKey_OpenSSL::OTAsymmetricKey_OpenSSLPrivdp Class Reference

#include <OTAsymmetricKey_OpenSSLPrivdp.hpp>

Collaboration diagram for opentxs::OTAsymmetricKey_OpenSSL::OTAsymmetricKey_OpenSSLPrivdp:

Public Member Functions

 OTAsymmetricKey_OpenSSLPrivdp ()
 

Static Public Member Functions

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)
 
static EVP_PKEY * CopyPublicKey (EVP_PKEY &theKey, const OTPasswordData *pPWData=nullptr, const OTPassword *pImportPassword=nullptr)
 
static EVP_PKEY * CopyPrivateKey (EVP_PKEY &theKey, const OTPasswordData *pPWData=nullptr, const OTPassword *pImportPassword=nullptr)
 

Public Attributes

OTAsymmetricKey_OpenSSLbacklink
 

Friends

class OTAsymmetricKey
 
class OTLowLevelKeyData
 
class OTCrypto_OpenSSL
 
class OTAsymmetricKey_OpenSSL
 

Detailed Description

Definition at line 152 of file OTAsymmetricKey_OpenSSLPrivdp.hpp.

Constructor & Destructor Documentation

opentxs::OTAsymmetricKey_OpenSSL::OTAsymmetricKey_OpenSSLPrivdp::OTAsymmetricKey_OpenSSLPrivdp ( )
inlineexplicit

Definition at line 165 of file OTAsymmetricKey_OpenSSLPrivdp.hpp.

Member Function Documentation

bool opentxs::OTAsymmetricKey_OpenSSL::OTAsymmetricKey_OpenSSLPrivdp::ArmorPrivateKey ( EVP_PKEY &  theKey,
OTASCIIArmor ascKey,
Timer theTimer,
const OTPasswordData pPWData = nullptr,
const OTPassword pImportPassword = nullptr 
)
static

Definition at line 727 of file OTAsymmetricKeyOpenSSLPrivdp.cpp.

730 {
731  bool bReturnVal = false;
732 
733  ascKey.Release();
734 
735  // Create a new memory buffer on the OpenSSL side
736  OpenSSL_BIO bmem = BIO_new(BIO_s_mem());
737  OT_ASSERT(nullptr != bmem);
738 
739  // write a private key to that buffer, from theKey
740  //
741  OTPasswordData thePWData("OTAsymmetricKey_OpenSSL::ArmorPrivateKey is "
742  "calling PEM_write_bio_PrivateKey...");
743 
744  if (nullptr == pPWData) pPWData = &thePWData;
745 
746  int32_t nWriteBio = 0;
747 
748  if (nullptr == pImportPassword)
749  nWriteBio = PEM_write_bio_PrivateKey(
750  bmem, &theKey,
751  EVP_des_ede3_cbc(), // todo should this algorithm be hardcoded?
753  const_cast<OTPasswordData*>(pPWData));
754  else
755  nWriteBio = PEM_write_bio_PrivateKey(
756  bmem, &theKey,
757  EVP_des_ede3_cbc(), // todo should this algorithm be hardcoded?
758  nullptr, 0, 0, const_cast<void*>(reinterpret_cast<const void*>(
759  pImportPassword->getPassword())));
760 
761  if (0 == nWriteBio) {
762  otErr << __FUNCTION__
763  << ": Failed writing EVP_PKEY to memory buffer.\n";
764  }
765  else {
766  // TODO (remove theTimer entirely. OTCachedKey replaces already.)
767  // I set this timer because the above required a password. But now that
768  // master key is working,
769  // the above would flow through even WITHOUT the user typing his
770  // passphrase (since master key still
771  // not timed out.) Resulting in THIS timer being reset! Todo: I already
772  // shortened this timer to 30
773  // seconds, but need to phase it down to 0 and then remove it entirely!
774  // Master key takes over now!
775  //
776 
777  theTimer.start(); // Note: this isn't the ultimate timer solution. See
778  // notes in ReleaseKeyLowLevel.
779 
780  otLog5 << __FUNCTION__
781  << ": Success writing EVP_PKEY to memory buffer.\n";
782 
783  OTPayload theData;
784  char* pChar = nullptr;
785 
786  // After the below call, pChar will point to the memory buffer where the
787  // private key supposedly is,
788  // and lSize will contain the size of that memory.
789  //
790  int64_t lSize = BIO_get_mem_data(bmem, &pChar);
791  uint32_t nSize = static_cast<uint32_t>(lSize);
792 
793  if (nSize > 0) {
794  // Set the buffer size in our own memory.
795  theData.SetPayloadSize(nSize);
796 
797  // void * pv =
799  (static_cast<char*>(const_cast<void*>(
800  theData.GetPayloadPointer()))), // destination
801  theData.GetSize(), // size of destination buffer.
802  pChar, // source
803  nSize); // length of source.
804  // bool bZeroSource=false); // if true, sets the source buffer to
805  // zero after copying is done.
806 
807  // This base64 encodes the private key data, which
808  // is already encrypted to its passphase as well.
809  //
810  ascKey.SetData(theData);
811 
812  otLog5 << __FUNCTION__
813  << ": Success copying private key into memory.\n";
814  bReturnVal = true;
815  }
816  else {
817  otErr << __FUNCTION__
818  << ": Failed copying private key into memory.\n";
819  }
820  }
821 
822  return bReturnVal;
823 }
EXPORT void start()
Definition: Timer.cpp:41
#define OT_ASSERT(x)
Definition: Assert.hpp:150
OTLOG_IMPORT OTLogStream otErr
static EXPORT OT_OPENSSL_CALLBACK * GetPasswordCallback()
static EXPORT void * safe_memcpy(void *dest, uint32_t dsize, const void *src, uint32_t ssize, bool zeroSource=false)
Definition: OTPassword.cpp:357
OTLOG_IMPORT OTLogStream otLog5
bool opentxs::OTAsymmetricKey_OpenSSL::OTAsymmetricKey_OpenSSLPrivdp::ArmorPublicKey ( EVP_PKEY &  theKey,
OTASCIIArmor ascKey 
)
static

Definition at line 512 of file OTAsymmetricKeyOpenSSLPrivdp.cpp.

514 {
515  bool bReturnVal = false;
516 
517  const char* szFunc = "OTAsymmetricKey_OpenSSL::ArmorPublicKey";
518 
519  ascKey.Release();
520 
521  // Create a new memory buffer on the OpenSSL side
522  OpenSSL_BIO bmem = BIO_new(BIO_s_mem());
524  nullptr != bmem,
525  "OTAsymmetricKey_OpenSSL::ArmorPublicKey: ASSERT: nullptr != bmem");
526 
527  // write a public key to that buffer, from theKey (parameter.)
528  //
529  int32_t nWriteBio = PEM_write_bio_PUBKEY(bmem, &theKey);
530 
531  if (0 == nWriteBio) {
532  otErr << szFunc
533  << ": Error: Failed writing EVP_PKEY to memory buffer.\n";
534  }
535  else {
536  otLog5 << szFunc << ": Success writing EVP_PKEY to memory buffer.\n";
537 
538  OTPayload theData;
539  char* pChar = nullptr;
540 
541  // After the below call, pChar will point to the memory buffer where the
542  // public key
543  // supposedly is, and lSize will contain the size of that memory.
544  //
545  int64_t lSize = BIO_get_mem_data(bmem, &pChar);
546  uint32_t nSize = static_cast<uint32_t>(
547  lSize); // todo security, etc. Fix this assumed type conversion.
548 
549  if (nSize > 0) {
550  // Set the buffer size in our own memory.
551  theData.SetPayloadSize(nSize);
552 
553  // void * pv =
555  (static_cast<char*>(const_cast<void*>(
556  theData.GetPayloadPointer()))), // destination
557  theData.GetSize(), // size of destination buffer.
558  pChar, // source
559  nSize); // length of source.
560  // bool bZeroSource=false); // if true, sets the source buffer to
561  // zero after copying is done.
562 
563  // This base64 encodes the public key data
564  //
565  ascKey.SetData(theData);
566 
567  otLog5 << szFunc << ": Success copying public key into memory.\n";
568  bReturnVal = true;
569  }
570  else {
571  otErr << szFunc << ": Failed copying public key into memory.\n";
572  }
573  }
574 
575  return bReturnVal;
576 }
#define OT_ASSERT_MSG(x, s)
Definition: Assert.hpp:155
OTLOG_IMPORT OTLogStream otErr
static EXPORT void * safe_memcpy(void *dest, uint32_t dsize, const void *src, uint32_t ssize, bool zeroSource=false)
Definition: OTPassword.cpp:357
OTLOG_IMPORT OTLogStream otLog5
EVP_PKEY * opentxs::OTAsymmetricKey_OpenSSL::OTAsymmetricKey_OpenSSLPrivdp::CopyPrivateKey ( EVP_PKEY &  theKey,
const OTPasswordData pPWData = nullptr,
const OTPassword pImportPassword = nullptr 
)
static

Definition at line 390 of file OTAsymmetricKeyOpenSSLPrivdp.cpp.

392 {
393  const EVP_CIPHER* pCipher =
394  EVP_des_ede3_cbc(); // todo should this algorithm be hardcoded?
395 
396  // Create a new memory buffer on the OpenSSL side
397  OpenSSL_BIO bmem = BIO_new(BIO_s_mem());
398  OT_ASSERT(nullptr != bmem);
399 
400  EVP_PKEY* pReturnKey = nullptr;
401 
402  // write a private key to that buffer, from theKey
403  //
404  OTPasswordData thePWDataWrite("OTAsymmetricKey_OpenSSL::CopyPrivateKey is "
405  "calling PEM_write_bio_PrivateKey...");
406 
407  // todo optimization: might just remove the password callback here, and just
408  // write the private key in the clear,
409  // and then load it up again, saving the encrypt/decrypt step that otherwise
410  // occurs, and then as long as we OpenSSL_cleanse
411  // the BIO, then it SHOULD stil be safe, right?
412  //
413  int32_t nWriteBio = false;
414 
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));
421  else
422  nWriteBio = PEM_write_bio_PrivateKey(
423  bmem, &theKey, pCipher, nullptr, 0, 0,
424  const_cast<void*>(
425  reinterpret_cast<const void*>(pImportPassword->getPassword())));
426 
427  if (0 == nWriteBio) {
428  otErr << __FUNCTION__
429  << ": Failed writing EVP_PKEY to memory buffer.\n";
430  }
431  else {
432  otLog5 << __FUNCTION__
433  << ": Success writing EVP_PKEY to memory buffer.\n";
434 
435  char* pChar = nullptr;
436 
437  // After the below call, pChar will point to the memory buffer where the
438  // private key supposedly is,
439  // and lSize will contain the size of that memory.
440  //
441  const int64_t lSize = BIO_get_mem_data(bmem, &pChar);
442  const uint32_t nSize = static_cast<uint32_t>(lSize);
443 
444  if (nSize > 0) {
445  OTPayload theData;
446 
447  // Set the buffer size in our own memory.
448  theData.SetPayloadSize(nSize);
449 
450  void* pv = OTPassword::safe_memcpy(
451  (static_cast<char*>(const_cast<void*>(
452  theData.GetPayloadPointer()))), // destination
453  theData.GetSize(), // size of destination buffer.
454  pChar, // source
455  nSize); // length of source.
456  // bool bZeroSource=false); // if true, sets the source buffer to
457  // zero after copying is done.
458 
459  if (nullptr != pv) {
460 
461  // Next, copy theData's contents into a new BIO_mem_buf,
462  // so OpenSSL can load the key out of it.
463  //
464  OpenSSL_BIO keyBio =
465  BIO_new_mem_buf(static_cast<char*>(const_cast<void*>(
466  theData.GetPayloadPointer())),
467  theData.GetSize());
468  OT_ASSERT_MSG(nullptr != keyBio,
469  "OTAsymmetricKey_OpenSSL::"
470  "CopyPrivateKey: Assert: nullptr != "
471  "keyBio \n");
472 
473  // Next we load up the key from the BIO string into an
474  // instantiated key object.
475  //
476  OTPasswordData thePWData("OTAsymmetricKey_OpenSSL::"
477  "CopyPrivateKey is calling "
478  "PEM_read_bio_PUBKEY...");
479 
480  if (nullptr == pImportPassword)
481  pReturnKey = PEM_read_bio_PrivateKey(
482  keyBio, nullptr, OTAsymmetricKey::GetPasswordCallback(),
483  nullptr == pPWData
484  ? &thePWData
485  : const_cast<OTPasswordData*>(pPWData));
486  else
487  pReturnKey = PEM_read_bio_PrivateKey(
488  keyBio, nullptr, 0,
489  const_cast<void*>(reinterpret_cast<const void*>(
490  pImportPassword->getPassword())));
491 
492  }
493  else
494  otErr << __FUNCTION__ << ": Error: Failed copying memory from "
495  "BIO into OTPayload.\n";
496 
497  }
498  else {
499  otErr << __FUNCTION__
500  << ": Failed copying private key into memory.\n";
501  }
502  }
503 
504  return pReturnKey;
505 }
#define OT_ASSERT(x)
Definition: Assert.hpp:150
#define OT_ASSERT_MSG(x, s)
Definition: Assert.hpp:155
OTLOG_IMPORT OTLogStream otErr
static EXPORT OT_OPENSSL_CALLBACK * GetPasswordCallback()
static EXPORT void * safe_memcpy(void *dest, uint32_t dsize, const void *src, uint32_t ssize, bool zeroSource=false)
Definition: OTPassword.cpp:357
OTLOG_IMPORT OTLogStream otLog5
EVP_PKEY * opentxs::OTAsymmetricKey_OpenSSL::OTAsymmetricKey_OpenSSLPrivdp::CopyPublicKey ( EVP_PKEY &  theKey,
const OTPasswordData pPWData = nullptr,
const OTPassword pImportPassword = nullptr 
)
static

Definition at line 273 of file OTAsymmetricKeyOpenSSLPrivdp.cpp.

276 {
277  // Create a new memory buffer on the OpenSSL side
278  OpenSSL_BIO bmem = BIO_new(BIO_s_mem());
280  nullptr != bmem,
281  "OTAsymmetricKey_OpenSSL::CopyPublicKey: ASSERT: nullptr != bmem");
282 
283  EVP_PKEY* pReturnKey = nullptr;
284 
285  // write a public key to that buffer, from theKey (parameter.)
286  //
287  int32_t nWriteBio = PEM_write_bio_PUBKEY(bmem, &theKey);
288 
289  if (0 == nWriteBio) {
290  otErr << __FUNCTION__
291  << ": Error: Failed writing EVP_PKEY to memory buffer.\n";
292  }
293  else {
294  otLog5 << __FUNCTION__
295  << ": Success writing EVP_PKEY to memory buffer.\n";
296 
297  char* pChar = nullptr;
298 
299  // After the below call, pChar will point to the memory buffer where the
300  // public key
301  // supposedly is, and lSize will contain the size of that memory.
302  //
303  const int64_t lSize = BIO_get_mem_data(bmem, &pChar);
304  const uint32_t nSize = static_cast<uint32_t>(lSize);
305 
306  if (nSize > 0) {
307  OTPayload theData;
308 
309  // Set the buffer size in our own memory.
310  theData.SetPayloadSize(nSize);
311 
312  void* pv = OTPassword::safe_memcpy(
313  (static_cast<char*>(const_cast<void*>(
314  theData.GetPayloadPointer()))), // destination
315  theData.GetSize(), // size of destination buffer.
316  pChar, // source
317  nSize); // length of source.
318 
319  if (nullptr != pv) {
320  // Next, copy theData's contents into a new BIO_mem_buf,
321  // so OpenSSL can load the key out of it.
322  //
323  OpenSSL_BIO keyBio =
324  BIO_new_mem_buf(static_cast<char*>(const_cast<void*>(
325  theData.GetPayloadPointer())),
326  theData.GetSize());
327  OT_ASSERT_MSG(nullptr != keyBio,
328  "OTAsymmetricKey_OpenSSL::"
329  "CopyPublicKey: Assert: nullptr != "
330  "keyBio \n");
331 
332  // Next we load up the key from the BIO string into an
333  // instantiated key object.
334  //
335  OTPasswordData thePWData(
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.");
341 
342  if (nullptr == pImportPassword)
343  pReturnKey = PEM_read_bio_PUBKEY(
344  keyBio, nullptr, OTAsymmetricKey::GetPasswordCallback(),
345  nullptr == pPWData
346  ? &thePWData
347  : const_cast<OTPasswordData*>(pPWData));
348  else
349  pReturnKey = PEM_read_bio_PUBKEY(
350  keyBio, nullptr, 0,
351  const_cast<OTPassword*>(pImportPassword));
352 
353  // We don't need the BIO anymore.
354  // Free the BIO and related buffers, filters, etc. (auto with
355  // scope).
356  //
357  }
358  else {
359  otErr << __FUNCTION__ << ": Error: Failed copying memory from "
360  "BIO into OTPayload.\n";
361  }
362  }
363  else {
364  otErr << __FUNCTION__
365  << ": Failed copying private key into memory.\n";
366  }
367  }
368 
369  return pReturnKey;
370 }
#define OT_ASSERT_MSG(x, s)
Definition: Assert.hpp:155
OTLOG_IMPORT OTLogStream otErr
static EXPORT OT_OPENSSL_CALLBACK * GetPasswordCallback()
static EXPORT void * safe_memcpy(void *dest, uint32_t dsize, const void *src, uint32_t ssize, bool zeroSource=false)
Definition: OTPassword.cpp:357
OTLOG_IMPORT OTLogStream otLog5

Friends And Related Function Documentation

friend class OTAsymmetricKey
friend

Definition at line 155 of file OTAsymmetricKey_OpenSSLPrivdp.hpp.

friend class OTAsymmetricKey_OpenSSL
friend

Definition at line 160 of file OTAsymmetricKey_OpenSSLPrivdp.hpp.

friend class OTCrypto_OpenSSL
friend

Definition at line 158 of file OTAsymmetricKey_OpenSSLPrivdp.hpp.

friend class OTLowLevelKeyData
friend

Definition at line 156 of file OTAsymmetricKey_OpenSSLPrivdp.hpp.

Member Data Documentation

OTAsymmetricKey_OpenSSL* opentxs::OTAsymmetricKey_OpenSSL::OTAsymmetricKey_OpenSSLPrivdp::backlink

Definition at line 163 of file OTAsymmetricKey_OpenSSLPrivdp.hpp.


The documentation for this class was generated from the following files: