152 #pragma GCC diagnostic ignored "-Wunused-parameter"
155 #if defined(OT_KEYRING_WINDOWS) && defined(_WIN32)
163 #include <wincrypt.h>
167 #pragma comment(lib, "crypt32.lib")
170 #elif defined(OT_KEYRING_MAC) && defined(__APPLE__)
176 #import <Security/Security.h>
179 #elif defined(OT_KEYRING_GNOME)
189 #include <gnome-keyring.h>
196 #elif defined(OT_KEYRING_KWALLET)
200 #ifndef G_GNUC_nullptr_TERMINATED
202 #define G_GNUC_nullptr_TERMINATED __attribute__((__sentinel__))
204 #define G_GNUC_nullptr_TERMINATED
208 #include <kapplication.h>
209 #include <kaboutdata.h>
210 #include <kcmdlineargs.h>
230 #elif defined(OT_KEYRING_FLATFILE)
237 #if defined(OT_KEYRING_WINDOWS) && defined(_WIN32)
244 bool OTKeyring::Windows_StoreSecret(
const OTString& strUser,
245 const OTPassword& thePassword,
246 const std::string& str_display)
249 OT_ASSERT(thePassword.getMemorySize() > 0);
252 input.pbData =
const_cast<BYTE*
>(
253 reinterpret_cast<const BYTE*
>(thePassword.getMemory()));
254 input.cbData =
static_cast<DWORD
>(thePassword.getMemorySize());
264 BOOL result = CryptProtectData(&input, L
"",
270 otErr << __FUNCTION__ <<
": Failed calling Win32: CryptProtectData \n";
282 theOutput.Assign(static_cast<void*>(output.pbData),
283 static_cast<uint32_t>(output.cbData));
285 LocalFree(output.pbData);
293 if (theOutput.IsEmpty()) {
294 otErr << __FUNCTION__
295 <<
": Error: Output of Win32 CryptProtectData was empty.\n";
298 OTASCIIArmor ascData(theOutput);
299 const OTString strFoldername(
"win32_data");
301 if (ascData.Exists())
302 return ascData.WriteArmoredFile(strFoldername,
304 "WINDOWS KEYRING MASTERKEY");
311 bool OTKeyring::Windows_RetrieveSecret(
const OTString& strUser,
312 OTPassword& thePassword,
313 const std::string& str_display)
317 OTString strFoldername(
"win32_data");
318 OTASCIIArmor ascFileContents;
319 bool bLoaded = (strFoldername.Exists() &&
320 ascFileContents.LoadFromFile(strFoldername, strUser) &&
321 ascFileContents.Exists());
323 otWarn <<
"%s: No cached ciphertext of master key loaded during "
324 "attempted retrieval. "
325 "(However, once one is available, it WILL be cached using "
332 const OTPayload theCipherblob(ascFileContents);
334 if (theCipherblob.IsEmpty()) {
335 otErr << __FUNCTION__ <<
": Error: OTPayload is empty after decoding "
336 "OTASCIIArmor (that wasn't empty.)\n";
340 input.pbData =
const_cast<BYTE*
>(
341 reinterpret_cast<const BYTE*
>(theCipherblob.GetPayloadPointer()));
342 input.cbData =
static_cast<DWORD
>(theCipherblob.GetSize());
353 BOOL result = CryptUnprotectData(&input,
nullptr,
359 otErr << __FUNCTION__
360 <<
": Error: Output of Win32 CryptUnprotectData was empty.\n";
363 thePassword.setMemory(reinterpret_cast<void*>(output.pbData),
364 static_cast<uint32_t>(output.cbData));
365 SecureZeroMemory(output.pbData, output.cbData);
366 LocalFree(output.pbData);
376 bool OTKeyring::Windows_DeleteSecret(
const OTString& strUser,
377 const std::string& str_display)
381 OTString strFoldername(
"win32_data");
387 otErr << __FUNCTION__
396 #elif defined(OT_KEYRING_MAC) && defined(__APPLE__)
405 OSStatus FindSecret(CFTypeRef keychainOrArray, uint32_t serviceNameLength,
406 const char* serviceName, uint32_t accountNameLength,
407 const char* accountName, uint32_t* passwordLength,
408 void** passwordData, SecKeychainItemRef* itemRef)
const;
410 OSStatus AddSecret(SecKeychainRef keychain, uint32_t serviceNameLength,
411 const char* serviceName, uint32_t accountNameLength,
412 const char* accountName, uint32_t passwordLength,
413 const void* passwordData,
414 SecKeychainItemRef* itemRef)
const;
416 OSStatus ItemFreeContent(SecKeychainAttributeList* attrList,
419 OSStatus ItemDelete(SecKeychainItemRef itemRef)
const;
421 OSStatus SearchCreateFromAttributes(
422 CFTypeRef keychainOrArray, CFTypeRef SecItemClass,
423 CFTypeRef itemClass,
const SecKeychainAttributeList* attrList,
424 SecKeychainSearchRef* searchRef)
const;
426 OSStatus SearchCopyNext(SecKeychainSearchRef searchRef,
427 SecKeychainItemRef* itemRef)
const;
430 OSStatus OTMacKeychain::FindSecret(
431 CFTypeRef keychainOrArray, uint32_t serviceNameLength,
432 const char* serviceName, uint32_t accountNameLength,
433 const char* accountName, uint32_t* passwordLength,
void** passwordData,
434 SecKeychainItemRef* itemRef)
const
436 return SecKeychainFindGenericPassword(
437 keychainOrArray, serviceNameLength, serviceName, accountNameLength,
438 accountName, passwordLength, passwordData, itemRef);
441 OSStatus OTMacKeychain::AddSecret(
442 SecKeychainRef keychain, uint32_t serviceNameLength,
443 const char* serviceName, uint32_t accountNameLength,
444 const char* accountName, uint32_t passwordLength,
const void* passwordData,
445 SecKeychainItemRef* itemRef)
const
447 return SecKeychainAddGenericPassword(
448 keychain, serviceNameLength, serviceName, accountNameLength,
449 accountName, passwordLength, passwordData, itemRef);
452 OSStatus OTMacKeychain::ItemDelete(SecKeychainItemRef itemRef)
const
454 return SecKeychainItemDelete(itemRef);
457 OSStatus OTMacKeychain::SearchCreateFromAttributes(
458 CFTypeRef keychainOrArray, CFTypeRef SecItemClass, CFTypeRef itemClass,
459 const SecKeychainAttributeList* attrList,
460 SecKeychainSearchRef* searchRef)
const
462 return SecKeychainSearchCreateFromAttributes(keychainOrArray, itemClass,
463 attrList, searchRef);
466 OSStatus OTMacKeychain::SearchCopyNext(SecKeychainSearchRef searchRef,
467 SecKeychainItemRef* itemRef)
const
469 return SecKeychainSearchCopyNext(searchRef, itemRef);
472 OSStatus OTMacKeychain::ItemFreeContent(SecKeychainAttributeList* attrList,
475 return SecKeychainItemFreeContent(attrList, data);
479 bool OTKeyring::Mac_StoreSecret(
const OTString& strUser,
480 const OTPassword& thePassword,
481 const std::string& str_display)
484 OT_ASSERT(thePassword.getMemorySize() > 0);
486 const std::string service_name =
"opentxs";
487 const std::string account_name = strUser.Get();
489 OTMacKeychain theKeychain;
491 const_cast<void*
>(
static_cast<const void*
>(thePassword.getMemory()));
493 OSStatus theError = theKeychain.AddSecret(
494 nullptr, service_name.size(), service_name.data(), account_name.size(),
495 account_name.data(), thePassword.getMemorySize(),
498 if (theError != noErr) {
500 <<
"OTKeyring::Mac_StoreSecret: Error in theKeychain.AddSecret.\n";
508 bool OTKeyring::Mac_RetrieveSecret(
const OTString& strUser,
509 OTPassword& thePassword,
510 const std::string& str_display)
514 const std::string service_name =
"opentxs";
515 const std::string account_name = strUser.Get();
517 uint32_t password_length = 0;
518 void* password_data =
nullptr;
520 OTMacKeychain theKeychain;
522 OSStatus theError = theKeychain.FindSecret(
523 nullptr, service_name.size(), service_name.data(), account_name.size(),
524 account_name.data(), &password_length,
525 &password_data,
nullptr);
526 if (theError == noErr) {
527 thePassword.setMemory(password_data, password_length);
528 theKeychain.ItemFreeContent(
nullptr, password_data);
532 otErr <<
"OTKeyring::Mac_RetrieveSecret: Error in "
533 "theKeychain.FindSecret.\n";
539 bool OTKeyring::Mac_DeleteSecret(
const OTString& strUser,
540 const std::string& str_display)
544 const std::string service_name =
"opentxs";
545 const std::string account_name = strUser.Get();
547 OTMacKeychain theKeychain;
550 SecKeychainAttribute attrs[] = {{kSecServiceItemAttr, service_name.length(),
551 (
char*)service_name.c_str()},
552 {kSecAccountItemAttr, account_name.length(),
553 (
char*)account_name.c_str()}};
554 SecKeychainAttributeList attributes = {
sizeof(attrs) /
sizeof(attrs[0]),
557 SecKeychainItemRef theItem =
nullptr;
558 SecKeychainSearchRef theSearch =
nullptr;
559 OSStatus theStatus = 0;
562 theResult = theKeychain.SearchCreateFromAttributes(
564 kSecGenericPasswordItemClass, &attributes, &theSearch);
566 bool bReturnVal =
false;
568 if (errSecSuccess == theResult)
571 int32_t numberOfItemsFound = 0;
572 while (theKeychain.SearchCopyNext(theSearch, &theItem) == noErr) {
573 numberOfItemsFound++;
576 if (numberOfItemsFound > 0) {
577 theStatus = theKeychain.ItemDelete(theItem);
580 otErr <<
"OTKeyring::Mac_DeleteSecret: Error deleting item "
587 CFRelease(theSearch);
594 #elif defined(OT_KEYRING_IOS) && defined(__APPLE__)
600 #import <Security/Security.h>
601 #import <CoreFoundation/CoreFoundation.h>
604 bool OTKeyring::IOS_StoreSecret(
const OTString& strUser,
605 const OTPassword& thePassword,
606 const std::string& str_display)
609 OT_ASSERT(thePassword.getMemorySize() > 0);
611 CFStringRef service_name = CFSTR(
"opentxs");
612 CFStringRef account_name = CFStringCreateWithCString(
nullptr, strUser.Get(),
613 kCFStringEncodingUTF8);
614 CFDataRef vData = CFDataCreateWithBytesNoCopy(
615 nullptr, thePassword.getMemory_uint8(), thePassword.getMemorySize(),
618 const void* keys[] = {kSecClass, kSecAttrService, kSecAttrAccount,
620 const void* values[] = {kSecClassGenericPassword, service_name,
621 account_name, vData};
622 CFDictionaryRef item =
623 CFDictionaryCreate(
nullptr, keys, values, 4,
nullptr,
nullptr);
625 OSStatus theError = SecItemAdd(item,
nullptr);
629 CFRelease(account_name);
631 if (theError != noErr) {
632 otErr <<
"OTKeyring::IOS_StoreSecret: Error in SecItemAdd.\n";
640 bool OTKeyring::IOS_RetrieveSecret(
const OTString& strUser,
641 OTPassword& thePassword,
642 const std::string& str_display)
646 CFStringRef service_name = CFSTR(
"opentxs");
647 CFStringRef account_name = CFStringCreateWithCString(
nullptr, strUser.Get(),
648 kCFStringEncodingUTF8);
649 CFDataRef vData =
nullptr;
651 const void* keys[] = {kSecClass, kSecAttrService, kSecAttrAccount,
653 const void* values[] = {kSecClassGenericPassword, service_name,
654 account_name, kCFBooleanTrue};
655 CFDictionaryRef query =
656 CFDictionaryCreate(
nullptr, keys, values, 4,
nullptr,
nullptr);
658 OSStatus theError = SecItemCopyMatching(query, (CFTypeRef*)&vData);
661 CFRelease(account_name);
663 if (theError != noErr) {
665 <<
"OTKeyring::IOS_RetrieveSecret: Error in SecItemCopyMatching.\n";
669 thePassword.setMemory(CFDataGetBytePtr(vData), CFDataGetLength(vData));
676 bool OTKeyring::IOS_DeleteSecret(
const OTString& strUser,
677 const std::string& str_display)
681 CFStringRef service_name = CFSTR(
"opentxs");
682 CFStringRef account_name = CFStringCreateWithCString(
nullptr, strUser.Get(),
683 kCFStringEncodingUTF8);
685 const void* keys[] = {kSecClass, kSecAttrService, kSecAttrAccount};
686 const void* values[] = {kSecClassGenericPassword, service_name,
688 CFDictionaryRef query =
689 CFDictionaryCreate(
nullptr, keys, values, 3,
nullptr,
nullptr);
691 OSStatus theError = SecItemDelete(query);
694 CFRelease(account_name);
696 if (theError != noErr) {
697 otErr <<
"OTKeyring::IOS_RetrieveSecret: Error in SecItemDelete.\n";
705 #elif defined(OT_KEYRING_GNOME)
728 bool OTKeyring::Gnome_StoreSecret(
const OTString& strUser,
729 const OTPassword& thePassword,
730 const std::string& str_display)
733 OT_ASSERT(thePassword.getMemorySize() > 0);
735 OTData theData(thePassword.getMemory(), thePassword.getMemorySize());
736 OTASCIIArmor ascData(theData);
737 theData.zeroMemory();
740 const bool bSuccess =
742 ascData.WriteArmoredString(strOutput,
"DERIVED KEY");
747 ascData.zeroMemory();
749 GnomeKeyringResult theResult = GNOME_KEYRING_RESULT_IO_ERROR;
751 if (bSuccess && strOutput.Exists()) {
752 theResult = gnome_keyring_store_password_sync(
753 GNOME_KEYRING_NETWORK_PASSWORD,
754 GNOME_KEYRING_DEFAULT,
755 str_display.c_str(), strOutput.Get(),
"user", strUser.Get(),
756 "protocol",
"opentxs",
758 strOutput.zeroMemory();
760 bool bResult =
false;
762 if (theResult == GNOME_KEYRING_RESULT_OK)
765 otErr <<
"OTKeyring::Gnome_StoreSecret: "
766 <<
"Failure in gnome_keyring_store_password_sync: "
767 << gnome_keyring_result_to_message(theResult) <<
'\n';
772 otOut <<
"OTKeyring::Gnome_StoreSecret: No secret to store.\n";
792 bool OTKeyring::Gnome_RetrieveSecret(
const OTString& strUser,
793 OTPassword& thePassword,
794 const std::string& str_display)
798 GnomeKeyringResult theResult = GNOME_KEYRING_RESULT_IO_ERROR;
799 gchar* gchar_p_password =
nullptr;
807 while ((GNOME_KEYRING_RESULT_OK != theResult)) {
810 theResult = gnome_keyring_find_password_sync(
811 GNOME_KEYRING_NETWORK_PASSWORD, &gchar_p_password,
"user",
812 strUser.Get(),
"protocol",
"opentxs",
815 if (GNOME_KEYRING_RESULT_OK == theResult)
break;
820 OTString strGnomeError(gnome_keyring_result_to_message(theResult));
850 otErr << __FUNCTION__ <<
": gnome_keyring_find_password_sync returned "
851 << strGnomeError.Get() <<
'\n';
852 otErr <<
"Remedy: Sleeping for " << lSleep
853 <<
" seconds and then retrying (attempt " << (nCount + 2) <<
'\n';
861 if ((theResult == GNOME_KEYRING_RESULT_OK) &&
862 (
nullptr != gchar_p_password)) {
863 size_t sizePassword =
866 if (sizePassword > 0) {
867 OTString strData(gchar_p_password, sizePassword);
869 gnome_keyring_free_password(gchar_p_password);
870 gchar_p_password =
nullptr;
872 OTASCIIArmor ascData;
874 strData.Exists() && ascData.LoadFromString(strData);
875 strData.zeroMemory();
878 otErr << __FUNCTION__ <<
": Failed trying to decode secret "
879 "from Gnome Keyring contents:\n\n"
880 << strData.Get() <<
"\n\n";
882 OTPayload thePayload(ascData);
883 ascData.zeroMemory();
884 if (thePayload.IsEmpty())
885 otErr << __FUNCTION__ <<
": Failed trying to decode secret "
886 "OTPayload from OTASCIIArmor "
887 <<
"from Gnome Keyring contents:\n\n" << strData.Get()
890 thePassword.setMemory(thePayload.GetPayloadPointer(),
891 thePayload.GetSize());
892 thePayload.zeroMemory();
902 otOut <<
"OTKeyring::Gnome_RetrieveSecret: "
903 <<
"No secret found: gnome_keyring_find_password_sync: "
904 << gnome_keyring_result_to_message(theResult) <<
'\n';
910 bool OTKeyring::Gnome_DeleteSecret(
const OTString& strUser,
911 const std::string& str_display)
915 GnomeKeyringResult theResult = gnome_keyring_delete_password_sync(
916 GNOME_KEYRING_NETWORK_PASSWORD,
"user", strUser.Get(),
"protocol",
920 if (theResult == GNOME_KEYRING_RESULT_OK) {
924 otErr <<
"OTKeyring::Gnome_DeleteSecret: "
925 <<
"Failure in gnome_keyring_delete_password_sync: "
926 << gnome_keyring_result_to_message(theResult) <<
'\n';
932 #elif defined(OT_KEYRING_KWALLET)
940 KWallet::Wallet* OTKeyring::s_pWallet =
nullptr;
941 KApplication* OTKeyring::s_pApp =
nullptr;
943 bool OTKeyring::InitKApp()
945 static bool bInitialized =
false;
948 if (!KApplication::instance()) {
949 static char kdeAppName[] =
"opentxs-kwallet";
951 char* argv[2] = {kdeAppName,
nullptr};
952 QByteArray qbApp(kdeAppName);
953 KAboutData about(qbApp, qbApp, KLocalizedString(),
955 KCmdLineArgs::init(argc, argv, &about);
957 OTKeyring::s_pApp =
new KApplication(
true);
959 otErr <<
"OTKeyring::InitKApp: Error: qApp already existed.\n";
968 KWallet::Wallet* OTKeyring::OpenKWallet()
970 if (OTKeyring::InitKApp()) {
974 if (
nullptr == OTKeyring::s_pWallet) {
975 OTKeyring::s_pWallet = KWallet::Wallet::openWallet(
976 KWallet::Wallet::NetworkWallet(),
nullptr);
978 if (
nullptr == OTKeyring::s_pWallet) {
979 otErr <<
"OTKeyring::OpenKWallet: Failed "
980 <<
"calling: KWallet::Wallet::openWallet"
981 <<
"(KWallet::Wallet::NetworkWallet(), nullptr)\n";
989 if (!KWallet::Wallet::isOpen(KWallet::Wallet::NetworkWallet())) {
992 if (
nullptr != OTKeyring::s_pWallet)
delete OTKeyring::s_pWallet;
993 OTKeyring::s_pWallet = KWallet::Wallet::openWallet(
994 KWallet::Wallet::NetworkWallet(),
nullptr);
996 if (
nullptr == OTKeyring::s_pWallet) {
997 otErr <<
"OTKeyring::OpenKWallet (while re-opening): Failed "
998 <<
"calling: KWallet::Wallet::openWallet"
999 <<
"(KWallet::Wallet::NetworkWallet(), nullptr)\n";
1006 if (!OTKeyring::s_pWallet->setFolder(
1007 QString::fromAscii(
"opentxs")))
1009 OTKeyring::s_pWallet->createFolder(QString::fromAscii(
"opentxs"));
1011 if (!OTKeyring::s_pWallet->setFolder(
1012 QString::fromAscii(
"opentxs"))) {
1013 otErr <<
"OTKeyring::OpenKWallet: Failed calling: "
1014 "KWallet::Wallet::setFolder"
1015 <<
"(QString::fromAscii(\"opentxs\")) -- Tried creating "
1023 return OTKeyring::s_pWallet;
1042 bool OTKeyring::KWallet_StoreSecret(
const OTString& strUser,
1043 const OTPassword& thePassword,
1044 const std::string& str_display)
1047 OT_ASSERT(thePassword.getMemorySize() > 0);
1049 KWallet::Wallet* pWallet = OTKeyring::OpenKWallet();
1051 if (
nullptr != pWallet) {
1052 const QString qstrKey(strUser.Get());
1054 OTData theData(thePassword.getMemory(), thePassword.getMemorySize());
1055 OTASCIIArmor ascData(theData);
1056 theData.zeroMemory();
1059 const bool bSuccess =
1061 ascData.WriteArmoredString(
1062 strOutput,
"DERIVED KEY");
1064 ascData.zeroMemory();
1068 bool bReturnVal =
false;
1070 if (bSuccess && strOutput.Exists() &&
1071 pWallet->writePassword(qstrKey,
1072 QString::fromUtf8(strOutput.Get())) == 0)
1075 otErr <<
"OTKeyring::KWallet_StoreSecret: Failed trying to store "
1076 "secret into KWallet.\n";
1078 strOutput.zeroMemory();
1083 otErr <<
"OTKeyring::KWallet_StoreSecret: Unable to open kwallet.\n";
1089 bool OTKeyring::KWallet_RetrieveSecret(
const OTString& strUser,
1090 OTPassword& thePassword,
1091 const std::string& str_display)
1095 KWallet::Wallet* pWallet = OTKeyring::OpenKWallet();
1097 if (
nullptr != pWallet) {
1098 const QString qstrKey(strUser.Get());
1103 if (pWallet->readPassword(qstrKey, qstrPwd) == 0) {
1104 const std::string str_password =
1105 qstrPwd.toStdString();
1108 OTString strData(str_password);
1109 OTASCIIArmor ascData;
1111 const bool bLoaded =
1112 strData.Exists() && ascData.LoadFromString(strData);
1113 strData.zeroMemory();
1116 otErr << __FUNCTION__ <<
": Failed trying to decode secret "
1117 "from KWallet contents.\n";
1119 OTPayload thePayload(ascData);
1120 ascData.zeroMemory();
1121 if (thePayload.IsEmpty())
1122 otErr << __FUNCTION__ <<
": Failed trying to decode secret "
1123 "OTPayload from OTASCIIArmor from "
1124 "KWallet contents.\n";
1126 thePassword.setMemory(thePayload.GetPayloadPointer(),
1127 thePayload.GetSize());
1128 thePayload.zeroMemory();
1134 otErr << __FUNCITON__
1135 <<
": Failed trying to retrieve secret from KWallet.\n";
1140 otWarn <<
"OTKeyring::KWallet_RetrieveSecret: No secret found.\n";
1146 bool OTKeyring::KWallet_DeleteSecret(
const OTString& strUser,
1147 const std::string& str_display)
1151 KWallet::Wallet* pWallet = OTKeyring::OpenKWallet();
1153 if (
nullptr != pWallet) {
1154 const QString qstrKey(strUser.Get());
1156 bool bResult =
false;
1158 if (pWallet->removeEntry(qstrKey) == 0)
1161 otErr <<
"OTKeyring::KWallet_DeleteSecret: Failed trying to erase "
1162 "secret from KWallet.\n";
1167 otErr <<
"OTKeyring::KWallet_DeleteSecret: Unable to open kwallet.\n";
1172 #elif defined(OT_KEYRING_FLATFILE)
1180 std::string OTKeyring::s_str_passwd_folder;
1183 void OTKeyring::FlatFile_SetPasswordFolder(std::string folder)
1185 OTKeyring::s_str_passwd_folder = folder;
1189 const char* OTKeyring::FlatFile_GetPasswordFolder()
1191 return s_str_passwd_folder.c_str();
1195 bool OTKeyring::FlatFile_StoreSecret(
const OTString& strUser,
1196 const OTPassword& thePassword,
1197 const std::string& str_display)
1200 OT_ASSERT(thePassword.getMemorySize() > 0);
1202 const std::string str_pw_folder(OTKeyring::FlatFile_GetPasswordFolder());
1203 if (!str_pw_folder.empty()) {
1204 OTString strExactPath;
1205 strExactPath.Format(
"%s%s%s", str_pw_folder.c_str(),
1207 const std::string str_ExactPath(strExactPath.Get());
1209 OTData theData(thePassword.getMemory(), thePassword.getMemorySize());
1210 OTASCIIArmor ascData(theData);
1211 theData.zeroMemory();
1216 ascData.Exists() && ascData.SaveToExactPath(str_ExactPath);
1217 ascData.zeroMemory();
1220 otErr <<
"OTKeyring::FlatFile_StoreSecret: Failed trying to store "
1226 otErr <<
"OTKeyring::FlatFile_StoreSecret: Unable to cache derived key, "
1227 "since password_folder not provided in config file.\n";
1233 bool OTKeyring::FlatFile_RetrieveSecret(
const OTString& strUser,
1234 OTPassword& thePassword,
1235 const std::string& str_display)
1238 const std::string str_pw_folder(OTKeyring::FlatFile_GetPasswordFolder());
1239 if (!str_pw_folder.empty()) {
1240 OTString strExactPath;
1241 strExactPath.Format(
"%s%s%s", str_pw_folder.c_str(),
1243 const std::string str_ExactPath(strExactPath.Get());
1247 OTASCIIArmor ascData;
1249 if (!ascData.LoadFromExactPath(str_ExactPath))
1250 otErr <<
"OTKeyring::FlatFile_RetrieveSecret: "
1251 <<
"Failed trying to decode secret from flat file contents."
1254 OTPayload thePayload(ascData);
1255 ascData.zeroMemory();
1256 if (thePayload.IsEmpty())
1257 otErr << __FUNCTION__ <<
": Failed trying to decode secret "
1258 "OTPayload from OTASCIIArmor from "
1259 "flat file contents.\n";
1261 thePassword.setMemory(thePayload.GetPayloadPointer(),
1262 thePayload.GetSize());
1263 thePayload.zeroMemory();
1271 otWarn << __FUNCTION__ <<
": Unable to retrieve any derived key, since "
1272 "password_folder not provided in config file.\n";
1278 bool OTKeyring::FlatFile_DeleteSecret(
const OTString& strUser,
1279 const std::string& str_display)
1283 const std::string str_pw_folder(OTKeyring::FlatFile_GetPasswordFolder());
1284 if (!str_pw_folder.empty()) {
1285 OTString strExactPath;
1286 strExactPath.Format(
"%s%s%s", str_pw_folder.c_str(),
1288 const std::string str_ExactPath(strExactPath.Get());
1290 std::ofstream ofs(str_ExactPath.c_str(),
1291 std::ios::out | std::ios::binary);
1294 otErr << __FUNCTION__ <<
": Error opening file (to delete it): "
1295 << str_ExactPath.c_str() <<
'\n';
1299 ofs <<
"(This space intentionally left blank.)\n";
1301 bool bSuccess = ofs.good() ?
true :
false;
1311 if (
remove(str_ExactPath.c_str()) != 0) {
1313 otErr <<
"** (OTKeyring::FlatFile_DeleteSecret) Failed trying to "
1314 <<
"delete file (containing secret): "
1315 << str_ExactPath.c_str() <<
'\n';
1319 otInfo <<
"** (OTKeyring::FlatFile_DeleteSecret) Success "
1320 <<
"deleting file: " << str_ExactPath.c_str() <<
'\n';
1326 otErr <<
"OTKeyring::FlatFile_DeleteSecret: Unable to delete any derived "
1327 "key, since password_folder not provided in config file.\n";
1337 const std::string& str_display)
1340 #if defined(OT_KEYRING_WINDOWS) && defined(_WIN32)
1341 return OTKeyring::Windows_StoreSecret(strUser, thePassword,
1343 #elif defined(OT_KEYRING_MAC) && defined(__APPLE__)
1344 return OTKeyring::Mac_StoreSecret(strUser, thePassword, str_display);
1345 #elif defined(OT_KEYRING_IOS) && defined(__APPLE__)
1346 return OTKeyring::IOS_StoreSecret(strUser, thePassword, str_display);
1347 #elif defined(OT_KEYRING_GNOME)
1348 return OTKeyring::Gnome_StoreSecret(strUser, thePassword, str_display);
1349 #elif defined(OT_KEYRING_KWALLET)
1350 return OTKeyring::KWallet_StoreSecret(strUser, thePassword,
1352 #elif defined(OT_KEYRING_FLATFILE)
1353 return OTKeyring::FlatFile_StoreSecret(strUser, thePassword,
1356 otErr <<
"OTKeyring::StoreSecret: WARNING: The OT config file says to "
1357 "use the system keyring, "
1358 "but OT wasn't compiled to support any keyrings.\n";
1366 const std::string& str_display)
1369 #if defined(OT_KEYRING_WINDOWS) && defined(_WIN32)
1370 return OTKeyring::Windows_RetrieveSecret(strUser, thePassword,
1372 #elif defined(OT_KEYRING_MAC) && defined(__APPLE__)
1373 return OTKeyring::Mac_RetrieveSecret(strUser, thePassword, str_display);
1374 #elif defined(OT_KEYRING_IOS) && defined(__APPLE__)
1375 return OTKeyring::IOS_RetrieveSecret(strUser, thePassword, str_display);
1376 #elif defined(OT_KEYRING_GNOME)
1377 return OTKeyring::Gnome_RetrieveSecret(strUser, thePassword,
1379 #elif defined(OT_KEYRING_KWALLET)
1380 return OTKeyring::KWallet_RetrieveSecret(strUser, thePassword,
1382 #elif defined(OT_KEYRING_FLATFILE)
1383 return OTKeyring::FlatFile_RetrieveSecret(strUser, thePassword,
1386 otErr <<
"OTKeyring::RetrieveSecret: WARNING: The OT config file says "
1387 "to use the system keyring, "
1388 "but OT wasn't compiled to support any keyrings.\n";
1396 const std::string& str_display)
1399 #if defined(OT_KEYRING_WINDOWS) && defined(_WIN32)
1400 return OTKeyring::Windows_DeleteSecret(strUser, str_display);
1401 #elif defined(OT_KEYRING_MAC) && defined(__APPLE__)
1402 return OTKeyring::Mac_DeleteSecret(strUser, str_display);
1403 #elif defined(OT_KEYRING_IOS) && defined(__APPLE__)
1404 return OTKeyring::IOS_DeleteSecret(strUser, str_display);
1405 #elif defined(OT_KEYRING_GNOME)
1406 return OTKeyring::Gnome_DeleteSecret(strUser, str_display);
1407 #elif defined(OT_KEYRING_KWALLET)
1408 return OTKeyring::KWallet_DeleteSecret(strUser, str_display);
1409 #elif defined(OT_KEYRING_FLATFILE)
1410 return OTKeyring::FlatFile_DeleteSecret(strUser, str_display);
1412 otErr <<
"OTKeyring::DeleteSecret: WARNING: The OT config file says to "
1413 "use the system keyring, "
1414 "but OT wasn't compiled to support any keyrings.\n";
static EXPORT const char * PathSeparator()
OTLOG_IMPORT OTLogStream otOut
#define MAX_STRING_LENGTH
static EXPORT std::shared_ptr< OTCachedKey > It(OTIdentifier *pIdentifier=nullptr)
static EXPORT const OTString & AppDataFolder()
static size_t safe_strlen(const char *s, size_t max)
EXPORT bool EraseValueByKey(std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")
OTLOG_IMPORT OTLogStream otInfo
static EXPORT bool SleepSeconds(int64_t lSeconds)
OTLOG_IMPORT OTLogStream otWarn
OTLOG_IMPORT OTLogStream otErr
static EXPORT bool RetrieveSecret(const OTString &strUser, OTPassword &thePassword, const std::string &str_display)
static EXPORT bool DeleteSecret(const OTString &strUser, const std::string &str_display)
static EXPORT bool StoreSecret(const OTString &strUser, const OTPassword &thePassword, const std::string &str_display)