146 #if defined(OT_CRYPTO_USING_OPENSSL)
158 #if defined(OT_CRYPTO_USING_OPENSSL)
160 #elif defined(OT_CRYPTO_USING_GPG)
162 otErr << __FUNCTION__ <<
": Open-Transactions doesn't support GPG (yet), "
163 "so it's impossible to instantiate a key.\n";
165 otErr << __FUNCTION__
166 <<
": Open-Transactions isn't built with any crypto engine, "
167 "so it's impossible to instantiate a key.\n";
203 const char* szFunc =
"OTAsymmetricKey::SetPasswordCallback";
206 otOut << szFunc <<
": WARNING: re-setting the password callback (one "
207 "was already there)...\n";
209 otWarn << szFunc <<
": FYI, setting the password callback to a "
210 "non-nullptr pointer (which is what we want.)\n";
212 if (
nullptr == pCallback)
214 <<
": WARNING, setting the password callback to nullptr! "
215 "(OpenSSL will thus "
216 "be forced to ask for the passphase on the console, "
217 "until this is called "
218 "again and set properly.)\n";
227 const char* szFunc =
"OTAsymmetricKey::GetPasswordCallback";
229 #if defined(OT_TEST_PASSWORD)
230 otInfo << szFunc <<
": WARNING, OT_TEST_PASSWORD *is* defined. The "
231 "internal 'C'-based password callback was just "
232 "requested by OT (to pass to OpenSSL). So, returning "
233 "the default_pass_cb password callback, which will "
234 "automatically return "
235 "the 'test' password to OpenSSL, if/when it calls that "
236 "callback function.\n";
241 <<
": FYI, the internal 'C'-based password callback is now "
242 "being returning to OT, "
243 "which is passing it to OpenSSL "
244 "during a crypto operation. (If OpenSSL invokes it, then we "
245 "should see other logs after this from when it triggers "
246 "whatever password-collection dialog is provided at startup "
247 "by the (probably Java) OTAPI client.)\n";
282 const char* szFunc =
"OTAsymmetricKey::SetPasswordCaller";
285 <<
": Attempting to set the password caller... "
286 "(the Java code has passed us its custom password dialog object "
287 "for later use if/when the "
288 "OT 'C'-based password callback is triggered by openssl.)\n";
291 otErr << szFunc <<
": ERROR: OTCaller::setCallback() "
292 "MUST be called first, with an OTCallback-extended "
293 "object passed to it,\n"
294 "BEFORE calling this function with that OTCaller. "
295 "(Returning false.)\n";
301 <<
": WARNING: Setting the password caller again, even though "
302 "it was apparently ALREADY set... (Meaning Java has probably "
303 "erroneously called this twice, "
304 "possibly passing the same OTCaller both times.)\n";
313 <<
": FYI, Successfully set the password caller object from "
314 "Java, and set the souped_up_pass_cb in C for OpenSSL (which "
316 "that Java object when the time is right.) Returning true.\n";
323 const char* szFunc =
"OTAsymmetricKey::GetPasswordCaller";
325 otLog4 << szFunc <<
": FYI, this was just called by souped_up_pass_cb "
326 "(which must have just been called by OpenSSL.) "
327 "Returning s_pCaller == "
328 << ((
nullptr ==
s_pCaller) ?
"nullptr" :
"VALID POINTER")
329 <<
" (Hopefully NOT nullptr, so the "
330 "custom password dialog can be triggered.)\n";
340 otErr << __FUNCTION__ <<
": ERROR:\nOTCaller::setCallback() "
341 "MUST be called first, with an "
342 "OTCallback-extended class passed to it,\n"
343 "before then invoking this function (and "
344 "passing that OTCaller as a parameter "
345 "into this function.)\n";
350 <<
": FYI, calling OTAsymmetricKey::SetPasswordCaller(theCaller) "
351 "now... (which is where "
352 "OT internally sets its pointer to the Java caller object, which "
353 "must have been passed in as a "
354 "parameter to this function. "
355 "This is also where OT either sets its internal 'C'-based "
356 "password callback to the souped_up "
357 "version which uses that Java caller object, "
358 "OR where OT sets its internal callback to nullptr--which causes "
359 "OpenSSL to ask for the passphrase "
360 "on the CONSOLE instead.)\n";
365 <<
": RESULT of call to OTAsymmetricKey::SetPasswordCaller: "
366 << (bSuccess ?
"SUCCESS" :
"FAILURE") <<
"\n";
386 const uint32_t theSize = uint32_t(size);
391 std::string str_userdata;
393 if (
nullptr != userdata) {
396 if (
nullptr != pPWData) {
406 otWarn << __FUNCTION__ <<
": Using DEFAULT TEST PASSWORD: "
407 "'test' (for \"" << str_userdata <<
"\")\n";
416 const char* tmp_passwd =
"test";
419 len =
static_cast<int32_t
>(strlen(tmp_passwd));
423 otOut << __FUNCTION__ <<
": Problem? Returning 0...\n";
428 if (len > size) len = size;
430 const uint32_t theLength =
static_cast<const uint32_t
>(len);
451 #ifndef _PASSWORD_LEN
452 #define _PASSWORD_LEN 128
469 bool bGotPassword =
false;
472 std::shared_ptr<OTCachedKey> pCachedKey(pPWData->
GetCachedKey());
486 const bool b3 = !(pCachedKey->isPaused());
546 otLog3 << __FUNCTION__ <<
": Using GetMasterPassword() call. \n";
548 bGotPassword = pCachedKey->GetMasterPassword(
549 pCachedKey, thePassword,
550 str_userdata.c_str());
570 otLog3 << __FUNCTION__ <<
": Using OT Password Callback. \n";
586 if (
nullptr == pCaller)
588 otOut <<
"Passphrase request for: \"" << str_userdata <<
"\"\n";
591 thePassword, (1 == rwflag) ?
true :
false);
600 static_cast<int32_t
>(str_userdata.size()));
604 <<
": Using OT Password Callback (asks twice) for \""
605 << str_userdata <<
"\"...\n";
611 <<
": Using OT Password Callback (asks once) for \""
612 << str_userdata <<
"\"...\n";
642 otOut << __FUNCTION__
643 <<
": Failure: (false == bGotPassword.) (Returning 0.)\n";
647 otInfo << __FUNCTION__ <<
": Success!\n";
657 otOut << __FUNCTION__ <<
": <0 length password was "
658 "returned from the API password callback. "
664 const char* szDefault =
"test";
666 otOut << __FUNCTION__
667 <<
": 0 length password was "
668 "returned from the API password callback. "
669 "Substituting default password 'test'.\n";
700 thePassword.
setMemory(static_cast<const void*>(szDefault),
713 *pMasterPW = thePassword;
716 else if (
nullptr != buf) {
718 if (len > size) len = size;
720 const uint32_t theSize =
static_cast<uint32_t
>(size);
721 const uint32_t theLength =
static_cast<uint32_t
>(len);
733 buf[theLength] =
'\0';
767 const char* szFunc =
"OTAsymmetricKey::CalculateID";
772 otErr << szFunc <<
": Error: !IsPublic() (This function should only be "
773 "called on a public key.)\n";
778 bool bGotPublicKey = GetPublicKey(strPublicKey);
780 if (!bGotPublicKey) {
781 otErr << szFunc <<
": Error getting public key.\n";
785 bool bSuccessCalculateDigest = theOutput.CalculateDigest(strPublicKey);
787 if (!bSuccessCalculateDigest) {
789 otErr << szFunc <<
": Error calculating digest of public key.\n";
817 "- -----BEGIN PUBLIC KEY-----\n"
819 "- -----END PUBLIC KEY-----\n",
824 "-----BEGIN PUBLIC KEY-----\n"
826 "-----END PUBLIC KEY-----\n",
832 otErr <<
"OTAsymmetricKey::GetPublicKey: Error: GetPublicKey(armored) "
833 "returned false. (Returning false.)\n";
843 "OTAsymmetricKey::GetPublicKey: ASSERT: IsPublic()\n");
870 if (theArmor.
LoadFromString(const_cast<OTString&>(strKey), bEscaped)) {
874 otErr <<
"OTAsymmetricKey::SetPublicKey: Error: failed loading "
875 "ascii-armored contents from bookended string:\n\n" << strKey
914 strKey.
Concatenate(
"- -----BEGIN ENCRYPTED PRIVATE KEY-----\n"
916 "- -----END ENCRYPTED PRIVATE KEY-----\n",
920 strKey.
Concatenate(
"-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
922 "-----END ENCRYPTED PRIVATE KEY-----\n",
928 otErr <<
"OTAsymmetricKey::GetPrivateKey: Error: "
929 "GetPrivateKey(armored) returned false. (Returning false.)\n";
942 if (
nullptr == m_p_ascKey)
return false;
944 ascKey.Set(*m_p_ascKey);
962 const char* szPrivateKeyStarts =
"-----BEGIN ENCRYPTED PRIVATE KEY-----";
963 if (theArmor.
LoadFromString(const_cast<OTString&>(strKey), bEscaped,
995 : m_p_ascKey(nullptr)
996 , m_bIsPublicKey(false)
997 , m_bIsPrivateKey(false)
1187 const char* szFoldername = strFoldername.
Get();
1188 const char* szFilename = strFilename.
Get();
1194 otOut <<
"OTAsymmetricKey::LoadPrivateKey: Unable to find private key "
1196 << szFilename <<
"\n";
1206 szFoldername, szFilename));
1208 if (strFileContents.length() < 2) {
1209 otErr <<
"OTAsymmetricKey::LoadPrivateKey: Error reading file: "
1214 const OTString strCert(strFileContents),
1215 strReason(
nullptr == pstrReason ?
"OTAsymmetricKey::LoadPrivateKey"
1237 if (theArmor.
LoadFromFile(strFoldername, strFilename)) {
1239 otLog4 <<
"Success setting public key from OTASCIIArmor in "
1240 "OTAsymmetricKey::LoadPublicKey.\n";
1244 otErr <<
"Unable to convert from OTASCIIArmor to public key in "
1245 "OTAsymmetricKey::LoadPublicKey: " << strFilename <<
"\n";
1251 <<
"Unable to read pubkey file in OTAsymmetricKey::LoadPublicKey: "
1252 << strFilename <<
"\n";
1267 const char* szFoldername = strFoldername.
Get();
1268 const char* szFilename = strFilename.
Get();
1274 otErr << __FUNCTION__ <<
": File does not exist: " << szFoldername
1281 szFoldername, szFilename));
1283 if (strFileContents.length() < 2) {
1284 otErr << __FUNCTION__ <<
": Error reading file: " << szFoldername
1289 const OTString strCert(strFileContents);
1292 strCert,
false, pstrReason,
EXPORT const uint8_t * getMemory_uint8() const
static EXPORT OTCrypto * It()
OTLOG_IMPORT OTLogStream otLog4
virtual OTAsymmetricKey * ClonePubKey() const
static EXPORT OTAsymmetricKey * KeyFactory()
EXPORT const char * GetDisplayString() const
virtual bool LoadPublicKeyFromCertString(const OTString &strCert, bool bEscaped=true, const OTString *pstrReason=nullptr, const OTPassword *pImportPassword=nullptr)=0
static OTCaller * s_pCaller
EXPORT uint32_t getMemorySize() const
EXPORT bool isUsingOldSystem() const
void ReleaseKeyLowLevel()
OTASCIIArmor * m_p_ascKey
EXPORT OT_OPENSSL_CALLBACK souped_up_pass_cb
bool LoadPublicKey(const OTString &strFoldername, const OTString &strFilename)
EXPORT bool GetPasswordFromConsole(OTPassword &theOutput, bool bRepeat=false) const
int32_t OT_OPENSSL_CALLBACK(char *buf, int32_t size, int32_t rwflag, void *userdata)
static EXPORT const char * PathSeparator()
OTSignatureMetadata * m_pMetadata
OTLOG_IMPORT OTLogStream otOut
virtual bool LoadPrivateKeyFromCertString(const OTString &strCert, bool bEscaped=true, const OTString *pstrReason=nullptr, const OTPassword *pImportPassword=nullptr)=0
EXPORT OT_OPENSSL_CALLBACK default_pass_cb
EXPORT bool isPassword() const
OTLOG_IMPORT OTLogStream otLog3
bool LoadPublicKeyFromCertFile(const OTString &strFoldername, const OTString &strFilename, const OTString *pstrReason=nullptr, const OTPassword *pImportPassword=nullptr)
EXPORT int32_t setMemory(const void *input, uint32_t size)
EXPORT int32_t setPassword(const char *input, int32_t size)
EXPORT void Concatenate(const char *arg,...)
static OTCaller * GetPasswordCaller()
EXPORT bool Exists() const
static EXPORT std::shared_ptr< OTCachedKey > It(OTIdentifier *pIdentifier=nullptr)
std::shared_ptr< OTCachedKey > GetCachedKey() const
bool SetPrivateKey(const OTString &strKey, bool bEscaped=false)
static OT_OPENSSL_CALLBACK * s_pwCallback
static EXPORT bool LoadFromString(OTASCIIArmor &ascArmor, const OTString &strInput, std::string str_bookend="-----BEGIN")
EXPORT bool LoadFromFile(const OTString &foldername, const OTString &filename)
static bool SetPasswordCaller(OTCaller &theCaller)
EXPORT bool SetPublicKey(const OTASCIIArmor &strKey)
static size_t safe_strlen(const char *s, size_t max)
EXPORT void Set(const char *data, uint32_t enforcedMaxLength=0)
bool GetPrivateKey(OTString &strKey, bool bEscaped=true) const
EXPORT std::string QueryPlainString(std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")
EXPORT bool GetPublicKey(OTASCIIArmor &strKey) const
virtual bool CalculateID(OTIdentifier &theOutput) const
static void SetPasswordCallback(OT_OPENSSL_CALLBACK *pCallback)
void Release_AsymmetricKey()
EXPORT void SetDisplay(const char *szDisplay, int32_t nLength)
#define OT_ASSERT_MSG(x, s)
virtual void ReleaseKeyLowLevel_Hook() const =0
OTLOG_IMPORT OTLogStream otInfo
EXPORT uint32_t getPasswordSize() const
EXPORT bool isCallbackSet() const
static bool IsPasswordCallbackSet()
EXPORT bool GetPassword(OTPassword &theOutput) const
OTLOG_IMPORT OTLogStream otWarn
bool LoadPrivateKey(const OTString &strFoldername, const OTString &strFilename, const OTString *pstrReason=nullptr, const OTPassword *pImportPassword=nullptr)
EXPORT const char * Get() const
OTLOG_IMPORT OTLogStream otErr
EXPORT const uint8_t * getPassword_uint8() const
static EXPORT OT_OPENSSL_CALLBACK * GetPasswordCallback()
OTPassword * GetMasterPW() const
EXPORT bool Exists(std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")
virtual EXPORT ~OTAsymmetricKey()
static EXPORT void * safe_memcpy(void *dest, uint32_t dsize, const void *src, uint32_t ssize, bool zeroSource=false)
EXPORT bool isForCachedKey() const
EXPORT bool isForNormalNym() const
virtual EXPORT void Release()
EXPORT bool OT_API_Set_PasswordCallback(OTCaller &theCaller)
OTLOG_IMPORT OTLogStream otLog5