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

#include <OTCrypto.hpp>

Inheritance diagram for opentxs::OTCrypto:

Public Member Functions

virtual ~OTCrypto ()
 
virtual OTPasswordInstantiateBinarySecret () const =0
 
EXPORT bool GetPasswordFromConsole (OTPassword &theOutput, bool bRepeat=false) const
 
EXPORT bool GetPasswordFromConsoleLowLevel (OTPassword &theOutput, const char *szPrompt) const
 
virtual bool RandomizeMemory (uint8_t *szDestination, uint32_t nNewSize) const =0
 
virtual bool CalculateDigest (const OTString &strInput, const OTString &strHashAlgorithm, OTIdentifier &theOutput) const =0
 
virtual bool CalculateDigest (const OTData &dataInput, const OTString &strHashAlgorithm, OTIdentifier &theOutput) const =0
 
bool IsBase62 (const std::string &str) const
 
virtual void SetIDFromBase62String (const OTString &strInput, OTIdentifier &theOutput) const =0
 
virtual void SetBase62StringFromID (const OTIdentifier &theInput, OTString &strOutput) const =0
 
virtual bool Base64Encode (const OTData &theInput, OTString &strOutput, bool bLineBreaks=true) const
 
virtual bool Base64Decode (const OTString &strInput, OTData &theOutput, bool bLineBreaks=true) const
 
virtual char * Base64Encode (const uint8_t *input, int32_t in_len, bool bLineBreaks) const =0
 
virtual uint8_t * Base64Decode (const char *input, size_t *out_len, bool bLineBreaks) const =0
 
virtual OTPasswordDeriveKey (const OTPassword &userPassword, const OTPayload &dataSalt, uint32_t uIterations, const OTPayload &dataCheckHash=OTPayload()) const =0
 
virtual OTPasswordDeriveNewKey (const OTPassword &userPassword, const OTPayload &dataSalt, uint32_t uIterations, OTPayload &dataCheckHash) const =0
 
virtual bool Encrypt (const OTPassword &theRawSymmetricKey, const char *szInput, uint32_t lInputLength, const OTPayload &theIV, OTPayload &theEncryptedOutput) const =0
 
virtual bool Decrypt (const OTPassword &theRawSymmetricKey, const char *szInput, uint32_t lInputLength, const OTPayload &theIV, OTCrypto_Decrypt_Output theDecryptedOutput) const =0
 
virtual bool Seal (mapOfAsymmetricKeys &RecipPubKeys, const OTString &theInput, OTData &dataOutput) const =0
 
virtual bool Open (OTData &dataInput, const OTPseudonym &theRecipient, OTString &theOutput, const OTPasswordData *pPWData=nullptr) const =0
 
virtual bool SignContract (const OTString &strContractUnsigned, const OTAsymmetricKey &theKey, OTSignature &theSignature, const OTString &strHashType, const OTPasswordData *pPWData=nullptr)=0
 
virtual bool VerifySignature (const OTString &strContractToVerify, const OTAsymmetricKey &theKey, const OTSignature &theSignature, const OTString &strHashType, const OTPasswordData *pPWData=nullptr) const =0
 
virtual bool SignContract (const OTString &strContractUnsigned, const OTString &strSigHashType, const std::string &strCertFileContents, OTSignature &theSignature, const OTPasswordData *pPWData=nullptr)=0
 
virtual bool VerifySignature (const OTString &strContractToVerify, const OTString &strSigHashType, const std::string &strCertFileContents, const OTSignature &theSignature, const OTPasswordData *pPWData=nullptr) const =0
 
EXPORT void Init () const
 
EXPORT void Cleanup () const
 

Static Public Member Functions

static EXPORT OTCryptoIt ()
 

Protected Member Functions

 OTCrypto ()
 
virtual void Init_Override () const
 
virtual void Cleanup_Override () const
 

Detailed Description

Definition at line 232 of file OTCrypto.hpp.

Constructor & Destructor Documentation

opentxs::OTCrypto::OTCrypto ( )
protected

Definition at line 420 of file OTCrypto.cpp.

421 {
422 }
opentxs::OTCrypto::~OTCrypto ( )
virtual

Definition at line 423 of file OTCrypto.cpp.

424 {
425 }

Member Function Documentation

bool opentxs::OTCrypto::Base64Decode ( const OTString strInput,
OTData theOutput,
bool  bLineBreaks = true 
) const
virtual

Definition at line 740 of file OTCrypto.cpp.

742 {
743 
744  const char* szInput = strInput.Get();
745  size_t theSize = 0;
746 
747  // Caller is responsible to delete.
748  uint8_t* pOutput = Base64Decode(szInput, &theSize, bLineBreaks);
749 
750  if (nullptr == pOutput) {
751  otErr << __FUNCTION__
752  << ": Base64Decode returned nullptr. (Failure.)\n";
753  return false;
754  }
755 
756  // pOutput not nullptr, and must be cleaned up.
757  //
758  const void* pVoid = reinterpret_cast<void*>(pOutput);
759  uint32_t lNewSize = static_cast<uint32_t>(theSize);
760 
761  theOutput.Assign(pVoid, lNewSize);
762  delete pOutput;
763  pOutput = nullptr;
764 
765  return true; // <=== Success.
766 }
OTLOG_IMPORT OTLogStream otErr
virtual bool Base64Decode(const OTString &strInput, OTData &theOutput, bool bLineBreaks=true) const
Definition: OTCrypto.cpp:740
virtual uint8_t* opentxs::OTCrypto::Base64Decode ( const char *  input,
size_t *  out_len,
bool  bLineBreaks 
) const
pure virtual

Implemented in opentxs::OTCrypto_OpenSSL.

bool opentxs::OTCrypto::Base64Encode ( const OTData theInput,
OTString strOutput,
bool  bLineBreaks = true 
) const
virtual

Definition at line 710 of file OTCrypto.cpp.

712 {
713 
714  const uint8_t* pDataIn = static_cast<const uint8_t*>(theInput.GetPointer());
715  int32_t nLength = static_cast<int32_t>(theInput.GetSize());
716 
717  OT_ASSERT_MSG(nLength >= 0, "ASSERT!!! nLength is an int32_t, matching the "
718  "openssl interface, and a size was just "
719  "attempted that wouldn't fit into an int32_t, "
720  "after static casting.\n");
721 
722  // Caller is responsible to delete.
723  char* pChar = Base64Encode(pDataIn, nLength, bLineBreaks);
724 
725  if (nullptr == pChar) {
726  otErr << __FUNCTION__
727  << ": Base64Encode returned nullptr. (Failure.)\n";
728  return false;
729  }
730 
731  // pChar not nullptr, and must be cleaned up.
732  //
733  strOutput.Set(pChar);
734  delete pChar;
735  pChar = nullptr;
736 
737  return true; // <=== Success.
738 }
virtual bool Base64Encode(const OTData &theInput, OTString &strOutput, bool bLineBreaks=true) const
Definition: OTCrypto.cpp:710
#define OT_ASSERT_MSG(x, s)
Definition: Assert.hpp:155
OTLOG_IMPORT OTLogStream otErr
virtual char* opentxs::OTCrypto::Base64Encode ( const uint8_t *  input,
int32_t  in_len,
bool  bLineBreaks 
) const
pure virtual

Implemented in opentxs::OTCrypto_OpenSSL.

virtual bool opentxs::OTCrypto::CalculateDigest ( const OTString strInput,
const OTString strHashAlgorithm,
OTIdentifier theOutput 
) const
pure virtual

Implemented in opentxs::OTCrypto_OpenSSL.

virtual bool opentxs::OTCrypto::CalculateDigest ( const OTData dataInput,
const OTString strHashAlgorithm,
OTIdentifier theOutput 
) const
pure virtual

Implemented in opentxs::OTCrypto_OpenSSL.

void opentxs::OTCrypto::Cleanup ( ) const

Definition at line 678 of file OTCrypto.cpp.

679 {
680  // This is only supposed to happen once per run.
681  //
682  if (1 == OTCrypto::s_nCount) {
683  --(OTCrypto::s_nCount);
684 
685  // Any crypto-related cleanup code NOT specific to OpenSSL (which is
686  // handled in OTCrypto_OpenSSL, a subclass) would go here.
687  //
688 
690  }
691  else
692  otErr << "OTCrypto::Cleanup: ERROR: Somehow this erroneously got "
693  "called more than once! (Doing nothing.)\n";
694 }
virtual void Cleanup_Override() const
Definition: OTCrypto.cpp:704
OTLOG_IMPORT OTLogStream otErr
void opentxs::OTCrypto::Cleanup_Override ( ) const
protectedvirtual

Reimplemented in opentxs::OTCrypto_OpenSSL.

Definition at line 704 of file OTCrypto.cpp.

705 {
706  otErr << "OTCrypto::Cleanup_Override: ERROR: This function should NEVER be "
707  "called (you should be overriding it...)\n";
708 }
OTLOG_IMPORT OTLogStream otErr
virtual bool opentxs::OTCrypto::Decrypt ( const OTPassword theRawSymmetricKey,
const char *  szInput,
uint32_t  lInputLength,
const OTPayload theIV,
OTCrypto_Decrypt_Output  theDecryptedOutput 
) const
pure virtual

Implemented in opentxs::OTCrypto_OpenSSL.

virtual OTPassword* opentxs::OTCrypto::DeriveKey ( const OTPassword userPassword,
const OTPayload dataSalt,
uint32_t  uIterations,
const OTPayload dataCheckHash = OTPayload() 
) const
pure virtual

Implemented in opentxs::OTCrypto_OpenSSL.

virtual OTPassword* opentxs::OTCrypto::DeriveNewKey ( const OTPassword userPassword,
const OTPayload dataSalt,
uint32_t  uIterations,
OTPayload dataCheckHash 
) const
pure virtual

Implemented in opentxs::OTCrypto_OpenSSL.

virtual bool opentxs::OTCrypto::Encrypt ( const OTPassword theRawSymmetricKey,
const char *  szInput,
uint32_t  lInputLength,
const OTPayload theIV,
OTPayload theEncryptedOutput 
) const
pure virtual

Implemented in opentxs::OTCrypto_OpenSSL.

bool opentxs::OTCrypto::GetPasswordFromConsole ( OTPassword theOutput,
bool  bRepeat = false 
) const

Definition at line 587 of file OTCrypto.cpp.

588 {
589  int32_t nAttempts = 0;
590 
591  for (;;) {
592  theOutput.zeroMemory();
593 
594  if (GetPasswordFromConsoleLowLevel(theOutput, "(OT) passphrase: ")) {
595  if (!bRepeat) {
596  std::cout << std::endl;
597  return true;
598  }
599  }
600  else {
601  std::cout << "Sorry." << std::endl;
602  return false;
603  }
604 
605  OTPassword tempPassword;
606 
607  if (!GetPasswordFromConsoleLowLevel(tempPassword,
608  "(Verifying) passphrase again: ")) {
609  std::cout << "Sorry." << std::endl;
610  return false;
611  }
612 
613  if (!tempPassword.Compare(theOutput)) {
614  if (++nAttempts >= 3) break;
615 
616  std::cout << "(Mismatch, try again.)\n" << std::endl;
617  }
618  else {
619  std::cout << std::endl;
620  return true;
621  }
622  }
623 
624  std::cout << "Sorry." << std::endl;
625 
626  return false;
627 }
EXPORT bool GetPasswordFromConsoleLowLevel(OTPassword &theOutput, const char *szPrompt) const
Definition: OTCrypto.cpp:488
bool opentxs::OTCrypto::GetPasswordFromConsoleLowLevel ( OTPassword theOutput,
const char *  szPrompt 
) const

Definition at line 488 of file OTCrypto.cpp.

490 {
491  OT_ASSERT(nullptr != szPrompt);
492 
493 #ifdef _WIN32
494  {
495  std::cout << szPrompt;
496 
497  {
498  std::string strPassword = "";
499 
500 #ifdef UNICODE
501 
502  const wchar_t enter[] = {L'\x000D', L'\x0000'}; // carrage return
503  const std::wstring wstrENTER = enter;
504 
505  std::wstring wstrPass = L"";
506 
507  for (;;) {
508  const wchar_t ch[] = {_getwch(), L'\x0000'};
509  const std::wstring wstrCH = ch;
510  if (wstrENTER == wstrCH) break;
511  wstrPass.append(wstrCH);
512  }
513  strPassword = OTString::ws2s(wstrPass);
514 
515 #else
516 
517  const char enter[] = {'\x0D', '\x00'}; // carrage return
518  const std::string strENTER = enter;
519 
520  std::string strPass = "";
521 
522  for (;;) {
523  const char ch[] = {_getch(), '\x00'};
524  const std::string strCH = ch;
525  if (strENTER == strCH) break;
526  strPass.append(strCH);
527  }
528  strPassword = strPass;
529 
530 #endif
531  theOutput.setPassword(
532  strPassword.c_str(),
533  static_cast<int32_t>(strPassword.length() - 1));
534  }
535 
536  std::cout << std::endl; // new line.
537  return true;
538  }
539 #elif defined(OT_CRYPTO_USING_OPENSSL)
540  // todo security: might want to allow to set OTPassword's size and copy
541  // directly into it,
542  // so that we aren't using this temp buf in between, which, although we're
543  // zeroing it, could
544  // technically end up getting swapped to disk.
545  //
546  {
547  char buf[_PASSWORD_LEN + 10] = "", buff[_PASSWORD_LEN + 10] = "";
548 
549  if (UI_UTIL_read_pw(buf, buff, _PASSWORD_LEN, szPrompt, 0) == 0) {
550  size_t nPassLength = OTString::safe_strlen(buf, _PASSWORD_LEN);
551  theOutput.setPassword_uint8(reinterpret_cast<uint8_t*>(buf),
552  nPassLength);
553  OTPassword::zeroMemory(buf, nPassLength);
554  OTPassword::zeroMemory(buff, nPassLength);
555  return true;
556  }
557  else
558  return false;
559  }
560 #else
561  {
562  otErr << "__FUNCTION__: Open-Transactions is not compiled to collect "
563  << "the passphrase from the console!\n";
564  return false;
565  }
566 #endif
567 }
EXPORT void zeroMemory()
Definition: OTPassword.cpp:281
static size_t safe_strlen(const char *s, size_t max)
Definition: OTString.cpp:388
#define OT_ASSERT(x)
Definition: Assert.hpp:150
OTLOG_IMPORT OTLogStream otErr
#define _PASSWORD_LEN
Definition: OTCrypto.cpp:485
void opentxs::OTCrypto::Init ( ) const

Definition at line 646 of file OTCrypto.cpp.

647 {
648  // This is only supposed to happen once per run.
649  //
650  if (0 == OTCrypto::s_nCount) {
651  ++(OTCrypto::s_nCount);
652 
653  otWarn << "OT_Init: Setting up rlimits, and crypto library...\n";
654 
655 // Here is a security measure intended to make it more difficult to capture a
656 // core
657 // dump. (Not used in debug mode, obviously.)
658 //
659 #if !defined(PREDEF_MODE_DEBUG) && defined(PREDEF_PLATFORM_UNIX)
660  struct rlimit rlim;
661  getrlimit(RLIMIT_CORE, &rlim);
662  rlim.rlim_max = rlim.rlim_cur = 0;
663  if (setrlimit(RLIMIT_CORE, &rlim)) {
664  OT_FAIL_MSG("OTCrypto::Init: ASSERT: setrlimit failed. (Used for "
665  "preventing core dumps.)\n");
666  }
667 #endif
668 
669  Init_Override();
670  }
671  else
672  otErr << "OTCrypto::Init: ERROR: Somehow this erroneously got called "
673  "more than once! (Doing nothing.)\n";
674 }
#define OT_FAIL_MSG(s)
Definition: Assert.hpp:144
OTLOG_IMPORT OTLogStream otWarn
OTLOG_IMPORT OTLogStream otErr
virtual void Init_Override() const
Definition: OTCrypto.cpp:697
void opentxs::OTCrypto::Init_Override ( ) const
protectedvirtual

Reimplemented in opentxs::OTCrypto_OpenSSL.

Definition at line 697 of file OTCrypto.cpp.

698 {
699  otErr << "OTCrypto::Init_Override: ERROR: This function should NEVER be "
700  "called (you should be overriding it...)\n";
701 }
OTLOG_IMPORT OTLogStream otErr
virtual OTPassword* opentxs::OTCrypto::InstantiateBinarySecret ( ) const
pure virtual

Implemented in opentxs::OTCrypto_OpenSSL.

bool opentxs::OTCrypto::IsBase62 ( const std::string &  str) const

Definition at line 427 of file OTCrypto.cpp.

428 {
429  return str.find_first_not_of("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHI"
430  "JKLMNOPQRSTUVWXYZ") == std::string::npos;
431 }
OTCrypto * opentxs::OTCrypto::It ( )
static

Definition at line 630 of file OTCrypto.cpp.

631 {
632  // Todo: someday, swapping the crypto lib should be as easy as changing this
633  // compile flag to OT_CRYPTO_USING_GPG. We'll get there.
634  //
635  static
636 #ifdef OT_CRYPTO_USING_OPENSSL
637  OTCrypto_OpenSSL
638 #endif
639  s_theSingleton; // For now we're only allowing a single instance.
640 
641  return &s_theSingleton;
642 }
virtual bool opentxs::OTCrypto::Open ( OTData dataInput,
const OTPseudonym theRecipient,
OTString theOutput,
const OTPasswordData pPWData = nullptr 
) const
pure virtual

Implemented in opentxs::OTCrypto_OpenSSL.

virtual bool opentxs::OTCrypto::RandomizeMemory ( uint8_t *  szDestination,
uint32_t  nNewSize 
) const
pure virtual

Implemented in opentxs::OTCrypto_OpenSSL.

virtual bool opentxs::OTCrypto::Seal ( mapOfAsymmetricKeys RecipPubKeys,
const OTString theInput,
OTData dataOutput 
) const
pure virtual

Implemented in opentxs::OTCrypto_OpenSSL.

virtual void opentxs::OTCrypto::SetBase62StringFromID ( const OTIdentifier theInput,
OTString strOutput 
) const
pure virtual

Implemented in opentxs::OTCrypto_OpenSSL.

virtual void opentxs::OTCrypto::SetIDFromBase62String ( const OTString strInput,
OTIdentifier theOutput 
) const
pure virtual

Implemented in opentxs::OTCrypto_OpenSSL.

virtual bool opentxs::OTCrypto::SignContract ( const OTString strContractUnsigned,
const OTAsymmetricKey theKey,
OTSignature theSignature,
const OTString strHashType,
const OTPasswordData pPWData = nullptr 
)
pure virtual

Implemented in opentxs::OTCrypto_OpenSSL.

virtual bool opentxs::OTCrypto::SignContract ( const OTString strContractUnsigned,
const OTString strSigHashType,
const std::string &  strCertFileContents,
OTSignature theSignature,
const OTPasswordData pPWData = nullptr 
)
pure virtual

Implemented in opentxs::OTCrypto_OpenSSL.

virtual bool opentxs::OTCrypto::VerifySignature ( const OTString strContractToVerify,
const OTAsymmetricKey theKey,
const OTSignature theSignature,
const OTString strHashType,
const OTPasswordData pPWData = nullptr 
) const
pure virtual

Implemented in opentxs::OTCrypto_OpenSSL.

virtual bool opentxs::OTCrypto::VerifySignature ( const OTString strContractToVerify,
const OTString strSigHashType,
const std::string &  strCertFileContents,
const OTSignature theSignature,
const OTPasswordData pPWData = nullptr 
) const
pure virtual

Implemented in opentxs::OTCrypto_OpenSSL.


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