summaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
authorGeorge Hazan <ghazan@miranda.im>2021-01-08 17:55:06 +0300
committerGeorge Hazan <ghazan@miranda.im>2021-01-08 17:55:06 +0300
commit8f3e583fffeba6606cf4442008c65e6902308080 (patch)
tree3dc085bc50bb64993faaaa5e4aeeaa273396887e /plugins
parent64579d553e4089d55c136140bd0058696d2cf526 (diff)
encryption code unification, part 2: initialization & data storage
Diffstat (limited to 'plugins')
-rw-r--r--plugins/Db3x_mmap/res/db3x_mmap.rc18
-rw-r--r--plugins/Db3x_mmap/src/dbcrypt.cpp176
-rw-r--r--plugins/Db3x_mmap/src/dbintf.cpp1
-rw-r--r--plugins/Db3x_mmap/src/dbintf.h11
-rw-r--r--plugins/Db3x_mmap/src/resource.h1
-rw-r--r--plugins/Db3x_mmap/src/ui.cpp117
-rw-r--r--plugins/Dbx_mdbx/res/dbx_mdbx.rc14
-rw-r--r--plugins/Dbx_mdbx/src/dbcrypt.cpp185
-rw-r--r--plugins/Dbx_mdbx/src/dbintf.cpp1
-rw-r--r--plugins/Dbx_mdbx/src/dbintf.h12
-rw-r--r--plugins/Dbx_mdbx/src/resource.h1
-rw-r--r--plugins/Dbx_mdbx/src/ui.cpp4
-rw-r--r--plugins/Dbx_sqlite/res/dbx_sqlite.rc14
-rw-r--r--plugins/Dbx_sqlite/src/dbcrypt.cpp58
-rwxr-xr-xplugins/Dbx_sqlite/src/dbintf.cpp5
-rwxr-xr-xplugins/Dbx_sqlite/src/dbintf.h8
-rw-r--r--plugins/Dbx_sqlite/src/resource.h1
-rw-r--r--plugins/Import/src/dbrw/dbintf.h4
-rw-r--r--plugins/Import/src/dbrw/dbsettings.cpp10
19 files changed, 182 insertions, 459 deletions
diff --git a/plugins/Db3x_mmap/res/db3x_mmap.rc b/plugins/Db3x_mmap/res/db3x_mmap.rc
index a2a65076cc..464f7aa8fb 100644
--- a/plugins/Db3x_mmap/res/db3x_mmap.rc
+++ b/plugins/Db3x_mmap/res/db3x_mmap.rc
@@ -23,20 +23,6 @@ LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
// Dialog
//
-IDD_LOGIN DIALOGEX 0, 0, 190, 86
-STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU
-EXSTYLE WS_EX_TOPMOST | WS_EX_TOOLWINDOW
-CAPTION "Login to Miranda NG"
-FONT 8, "MS Shell Dlg", 400, 0, 0x1
-BEGIN
- CONTROL "",IDC_HEADERBAR,"MHeaderbarCtrl",0x0,0,0,190,26
- CTEXT "",IDC_LANG,158,34,13,13,SS_CENTERIMAGE | NOT WS_GROUP
- EDITTEXT IDC_USERPASS,21,34,128,14,ES_PASSWORD | ES_AUTOHSCROLL | WS_GROUP
- DEFPUSHBUTTON "OK",IDOK,36,64,50,14
- PUSHBUTTON "Cancel",IDCANCEL,102,64,50,14
- CONTROL "",IDC_STATIC,"Static",SS_ETCHEDFRAME,0,55,190,1
-END
-
IDD_NEWPASS DIALOGEX 0, 0, 190, 102
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU
EXSTYLE WS_EX_TOPMOST | WS_EX_TOOLWINDOW
@@ -94,10 +80,6 @@ END
#ifdef APSTUDIO_INVOKED
GUIDELINES DESIGNINFO
BEGIN
- IDD_LOGIN, DIALOG
- BEGIN
- END
-
IDD_CHANGEPASS, DIALOG
BEGIN
END
diff --git a/plugins/Db3x_mmap/src/dbcrypt.cpp b/plugins/Db3x_mmap/src/dbcrypt.cpp
index 340464eed8..42a0cc57db 100644
--- a/plugins/Db3x_mmap/src/dbcrypt.cpp
+++ b/plugins/Db3x_mmap/src/dbcrypt.cpp
@@ -25,20 +25,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
/////////////////////////////////////////////////////////////////////////////////////////
-//VERY VERY VERY BASIC ENCRYPTION FUNCTION
-
-static void Encrypt(char *msg, BOOL up)
-{
- int jump = (up) ? 5 : -5;
- for (int i = 0; msg[i]; i++)
- msg[i] = msg[i] + jump;
-}
-
-__forceinline void DecodeString(LPSTR buf)
-{
- Encrypt(buf, FALSE);
-}
-
struct VarDescr
{
VarDescr(LPCSTR var, LPCSTR value) :
@@ -63,138 +49,29 @@ struct VarDescr
int iLen;
};
-struct SettingUgraderParam
-{
- CDb3Mmap *db;
- LPCSTR szModule;
- MCONTACT contactID;
- OBJLIST<VarDescr>* pList;
-};
-
-int sttSettingUgrader(const char *szSetting, void *lParam)
-{
- SettingUgraderParam *param = (SettingUgraderParam*)lParam;
- if (param->db->IsSettingEncrypted(param->szModule, szSetting)) {
- DBVARIANT dbv = { DBVT_UTF8 };
- if (!param->db->GetContactSettingStr(param->contactID, param->szModule, szSetting, &dbv)) {
- if (dbv.type == DBVT_UTF8) {
- DecodeString(dbv.pszVal);
- param->pList->insert(new VarDescr(szSetting, (LPCSTR)dbv.pszVal));
- }
- param->db->FreeVariant(&dbv);
- }
- }
- return 0;
-}
-
-void sttContactEnum(MCONTACT contactID, const char *szModule, CDb3Mmap *db)
-{
- OBJLIST<VarDescr> arSettings(1);
- SettingUgraderParam param = { db, szModule, contactID, &arSettings };
- db->EnumContactSettings(0, sttSettingUgrader, szModule, &param);
-
- for (auto &p : arSettings) {
- size_t len;
- BYTE *pResult = db->getCrypt()->encodeString(p->szValue, &len);
- if (pResult != nullptr) {
- DBCONTACTWRITESETTING dbcws = { szModule, p->szVar };
- dbcws.value.type = DBVT_ENCRYPTED;
- dbcws.value.pbVal = pResult;
- dbcws.value.cpbVal = (WORD)len;
- db->WriteContactSetting(contactID, &dbcws);
-
- mir_free(pResult);
- }
- }
-}
-
-int sttModuleEnum(const char *szModule, void *lParam)
-{
- CDb3Mmap *db = (CDb3Mmap*)lParam;
- sttContactEnum(0, szModule, db);
-
- for (MCONTACT contactID = db->FindFirstContact(); contactID; contactID = db->FindNextContact(contactID))
- sttContactEnum(contactID, szModule, db);
-
- return 0;
-}
-
-/////////////////////////////////////////////////////////////////////////////////////////
-
int CDb3Mmap::InitCrypt()
{
if (m_dbHeader.version == DB_OLD_VERSION)
return 0;
- CRYPTO_PROVIDER *pProvider;
- bool bMissingKey = false;
-
- DBVARIANT dbv = { 0 };
- dbv.type = DBVT_BLOB;
- if (GetContactSetting(0, "CryptoEngine", "Provider", &dbv)) {
-LBL_CreateProvider:
- pProvider = SelectProvider();
- if (pProvider == nullptr)
- return 1;
- }
- else {
- if (dbv.type != DBVT_BLOB) { // old version, clean it up
- bMissingKey = true;
- goto LBL_CreateProvider;
- }
-
- pProvider = Crypto_GetProvider(LPCSTR(dbv.pbVal));
- FreeVariant(&dbv);
- if (pProvider == nullptr)
- goto LBL_CreateProvider;
- }
+ return MDatabaseCommon::InitCrypt();
+}
- if ((m_crypto = pProvider->pFactory()) == nullptr)
- return 3;
+/////////////////////////////////////////////////////////////////////////////////////////
+// Saving encryption key in a database
+STDMETHODIMP_(BOOL) CDb3Mmap::ReadCryptoKey(MBinBuffer &buf)
+{
+ DBVARIANT dbv = {};
dbv.type = DBVT_BLOB;
- if (GetContactSetting(0, "CryptoEngine", "StoredKey", &dbv)) {
- bMissingKey = true;
-
- LBL_SetNewKey:
- m_crypto->generateKey(); // unencrypted key
- StoreKey();
- }
- else {
- size_t iKeyLength = m_crypto->getKeyLength();
- if (dbv.cpbVal != (WORD)iKeyLength)
- goto LBL_SetNewKey;
-
- if (!m_crypto->setKey(dbv.pbVal, iKeyLength)) {
- if (memcmp(m_dbHeader.signature, &dbSignatureE, sizeof(m_dbHeader.signature)))
- goto LBL_SetNewKey;
-
- if (!EnterPassword(dbv.pbVal, iKeyLength)) { // password protected?
- if (m_dbHeader.version >= DB_094_VERSION)
- return 4;
-
- // one of the early used version of mmap was replaced then by mmap_sa
- // simply remove old badly generated key
- bMissingKey = true;
- goto LBL_SetNewKey;
- }
- }
-
- FreeVariant(&dbv);
- }
-
- if (bMissingKey)
- EnumModuleNames(sttModuleEnum, this);
+ if (GetContactSetting(0, "CryptoEngine", "StoredKey", &dbv))
+ return FALSE;
- dbv.type = DBVT_BYTE;
- if (!GetContactSetting(0, "CryptoEngine", "DatabaseEncryption", &dbv))
- m_bEncrypted = dbv.bVal != 0;
-
- InitDialogs();
- return 0;
+ buf.append(dbv.pbVal, dbv.cpbVal);
+ return TRUE;
}
-void CDb3Mmap::StoreKey()
+STDMETHODIMP_(BOOL) CDb3Mmap::StoreCryptoKey()
{
size_t iKeyLength = m_crypto->getKeyLength();
BYTE *pKey = (BYTE*)_alloca(iKeyLength);
@@ -207,6 +84,35 @@ void CDb3Mmap::StoreKey()
WriteContactSetting(0, &dbcws);
SecureZeroMemory(pKey, iKeyLength);
+ return TRUE;
+}
+
+/////////////////////////////////////////////////////////////////////////////////////////
+// Saving encryption flag
+
+STDMETHODIMP_(BOOL) CDb3Mmap::ReadEncryption()
+{
+ DBVARIANT dbv = {};
+ dbv.type = DBVT_BYTE;
+ return (GetContactSetting(0, "CryptoEngine", "DatabaseEncryption", &dbv)) ? false : dbv.bVal != 0;
+}
+
+/////////////////////////////////////////////////////////////////////////////////////////
+// Saving provider in a database
+
+STDMETHODIMP_(CRYPTO_PROVIDER *) CDb3Mmap::ReadProvider()
+{
+ DBVARIANT dbv = {};
+ dbv.type = DBVT_BLOB;
+ if (GetContactSetting(0, "CryptoEngine", "Provider", &dbv))
+ return nullptr;
+
+ if (dbv.type != DBVT_BLOB)
+ return nullptr;
+
+ auto *pProvider = Crypto_GetProvider(LPCSTR(dbv.pbVal));
+ FreeVariant(&dbv);
+ return pProvider;
}
STDMETHODIMP_(BOOL) CDb3Mmap::StoreProvider(CRYPTO_PROVIDER *pProvider)
diff --git a/plugins/Db3x_mmap/src/dbintf.cpp b/plugins/Db3x_mmap/src/dbintf.cpp
index cee46868a9..121d5a3d72 100644
--- a/plugins/Db3x_mmap/src/dbintf.cpp
+++ b/plugins/Db3x_mmap/src/dbintf.cpp
@@ -116,6 +116,7 @@ int CDb3Mmap::Load(bool bSkipInit)
if (InitMap()) return 1;
if (InitModuleNames()) return 1;
if (InitCrypt()) return EGROKPRF_CANTREAD;
+ InitDialogs();
// everything is ok, go on
if (!m_bReadOnly) {
diff --git a/plugins/Db3x_mmap/src/dbintf.h b/plugins/Db3x_mmap/src/dbintf.h
index ea3a99c364..81aca25423 100644
--- a/plugins/Db3x_mmap/src/dbintf.h
+++ b/plugins/Db3x_mmap/src/dbintf.h
@@ -193,7 +193,6 @@ struct CDb3Mmap : public MDatabaseCommon, public MZeroedObject
int CheckDbHeaders(bool bInteractive);
void ToggleEncryption(void);
- void StoreKey(void);
void SetPassword(const wchar_t *ptszPassword);
void UpdateMenuItem(void);
@@ -203,7 +202,6 @@ struct CDb3Mmap : public MDatabaseCommon, public MZeroedObject
void WriteSignature(DBSignature&);
__forceinline HANDLE getFile() const { return m_hDbFile; }
- __forceinline bool usesPassword() const { return m_bUsesPassword; }
public:
STDMETHODIMP_(BOOL) IsRelational(void) override { return FALSE; }
@@ -231,6 +229,12 @@ public:
STDMETHODIMP_(BOOL) EnumModuleNames(DBMODULEENUMPROC pFunc, void *pParam) override;
+ STDMETHODIMP_(BOOL) ReadCryptoKey(MBinBuffer&);
+ STDMETHODIMP_(BOOL) StoreCryptoKey(void);
+
+ STDMETHODIMP_(BOOL) ReadEncryption(void);
+
+ STDMETHODIMP_(CRYPTO_PROVIDER*) ReadProvider();
STDMETHODIMP_(BOOL) StoreProvider(CRYPTO_PROVIDER*);
STDMETHODIMP_(BOOL) GetContactSettingWorker(MCONTACT contactID, LPCSTR szModule, LPCSTR szSetting, DBVARIANT *dbv, int isStatic) override;
@@ -265,7 +269,7 @@ protected:
HANDLE m_hDbFile;
DBHeader m_dbHeader;
DWORD m_ChunkSize;
- bool m_safetyMode, m_bReadOnly, m_bShared, m_bUsesPassword;
+ bool m_safetyMode, m_bReadOnly, m_bShared;
////////////////////////////////////////////////////////////////////////////
// database stuff
@@ -322,5 +326,4 @@ protected:
void ToggleSettingsEncryption(MCONTACT contactID);
void InitDialogs();
- bool EnterPassword(const BYTE *pKey, const size_t keyLen);
};
diff --git a/plugins/Db3x_mmap/src/resource.h b/plugins/Db3x_mmap/src/resource.h
index f0734b5662..0f3113cc29 100644
--- a/plugins/Db3x_mmap/src/resource.h
+++ b/plugins/Db3x_mmap/src/resource.h
@@ -4,7 +4,6 @@
//
#define IDREMOVE 3
#define IDI_LOGO 101
-#define IDD_LOGIN 102
#define IDD_NEWPASS 103
#define IDD_CHANGEPASS 104
#define IDD_OPTIONS 105
diff --git a/plugins/Db3x_mmap/src/ui.cpp b/plugins/Db3x_mmap/src/ui.cpp
index 19e8a1c4f2..10c428b9fd 100644
--- a/plugins/Db3x_mmap/src/ui.cpp
+++ b/plugins/Db3x_mmap/src/ui.cpp
@@ -23,13 +23,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "stdafx.h"
-struct DlgChangePassParam
-{
- CDb3Mmap *db;
- wchar_t newPass[100];
- int wrongPass;
-};
-
#define MS_DB_CHANGEPASSWORD "DB/UI/ChangePassword"
static IconItem iconList[] =
@@ -55,105 +48,6 @@ void LanguageChanged(HWND hwndDlg)
/////////////////////////////////////////////////////////////////////////////////////////
-static INT_PTR CALLBACK sttEnterPassword(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
-{
- DlgChangePassParam *param = (DlgChangePassParam*)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
-
- switch (uMsg) {
- case WM_INITDIALOG:
- TranslateDialogDefault(hwndDlg);
- SendDlgItemMessage(hwndDlg, IDC_HEADERBAR, WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(g_plugin.getInst(), MAKEINTRESOURCE(iconList[0].defIconID)));
-
- param = (DlgChangePassParam*)lParam;
- SetWindowLongPtr(hwndDlg, GWLP_USERDATA, lParam);
-
- if (param->wrongPass) {
- if (param->wrongPass > 2) {
- HWND hwndCtrl = GetDlgItem(hwndDlg, IDC_USERPASS);
- EnableWindow(hwndCtrl, FALSE);
- hwndCtrl = GetDlgItem(hwndDlg, IDOK);
- EnableWindow(hwndCtrl, FALSE);
- SetDlgItemText(hwndDlg, IDC_HEADERBAR, TranslateT("Too many errors!"));
- }
- else SetDlgItemText(hwndDlg, IDC_HEADERBAR, TranslateT("Password is not correct!"));
- }
- else SetDlgItemText(hwndDlg, IDC_HEADERBAR, TranslateT("Please type in your password"));
-
- oldLangID = 0;
- SetTimer(hwndDlg, 1, 200, nullptr);
- LanguageChanged(hwndDlg);
- return TRUE;
-
- case WM_CTLCOLORSTATIC:
- if ((HWND)lParam == GetDlgItem(hwndDlg, IDC_LANG)) {
- SetTextColor((HDC)wParam, GetSysColor(COLOR_HIGHLIGHTTEXT));
- SetBkMode((HDC)wParam, TRANSPARENT);
- return (INT_PTR)GetSysColorBrush(COLOR_HIGHLIGHT);
- }
- return FALSE;
-
- case WM_COMMAND:
- switch (LOWORD(wParam)) {
- case IDCANCEL:
- EndDialog(hwndDlg, IDCANCEL);
- break;
-
- case IDOK:
- GetDlgItemText(hwndDlg, IDC_USERPASS, param->newPass, _countof(param->newPass));
-
- if (Profile_GetSettingInt(L"Database/RememberPassword")) {
- CREDENTIAL cred = { 0 };
- cred.Type = CRED_TYPE_GENERIC;
- cred.TargetName = L"Miranda NG/Database";
- cred.CredentialBlobSize = DWORD(mir_wstrlen(param->newPass) * sizeof(wchar_t) + sizeof(wchar_t));
- cred.CredentialBlob = (LPBYTE)param->newPass;
- cred.Persist = CRED_PERSIST_LOCAL_MACHINE;
- CredWrite(&cred, 0);
- }
-
- EndDialog(hwndDlg, IDOK);
- }
- break;
-
- case WM_TIMER:
- LanguageChanged(hwndDlg);
- return FALSE;
-
- case WM_DESTROY:
- KillTimer(hwndDlg, 1);
- DestroyIcon((HICON)SendMessage(hwndDlg, WM_GETICON, ICON_SMALL, 0));
- }
-
- return FALSE;
-}
-
-bool CDb3Mmap::EnterPassword(const BYTE *pKey, const size_t keyLen)
-{
- DlgChangePassParam param = { this };
- while (true) {
- PCREDENTIAL pCred;
- if (param.wrongPass == 0 && CredRead(L"Miranda NG/Dbx_mmap", CRED_TYPE_GENERIC, 0, &pCred)) {
- m_crypto->setPassword(T2Utf((wchar_t*)pCred->CredentialBlob));
- CredFree(pCred);
- }
- else {
- if (IDOK != DialogBoxParam(g_plugin.getInst(), MAKEINTRESOURCE(IDD_LOGIN), nullptr, sttEnterPassword, (LPARAM)&param))
- return false;
- m_crypto->setPassword(T2Utf(param.newPass));
- }
-
- if (m_crypto->setKey(pKey, keyLen)) {
- m_bUsesPassword = true;
- SecureZeroMemory(&param, sizeof(param));
- return true;
- }
-
- param.wrongPass++;
- }
-}
-
-/////////////////////////////////////////////////////////////////////////////////////////
-
static bool CheckOldPassword(HWND hwndDlg, CDb3Mmap *db)
{
if (db->usesPassword()) {
@@ -167,6 +61,13 @@ static bool CheckOldPassword(HWND hwndDlg, CDb3Mmap *db)
return true;
}
+struct DlgChangePassParam
+{
+ CDb3Mmap *db;
+ wchar_t newPass[100];
+ int wrongPass;
+};
+
static INT_PTR CALLBACK sttChangePassword(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
DlgChangePassParam *param = (DlgChangePassParam*)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
@@ -209,7 +110,7 @@ static INT_PTR CALLBACK sttChangePassword(HWND hwndDlg, UINT uMsg, WPARAM wParam
else {
param->db->WriteSignature(dbSignatureU);
param->db->SetPassword(nullptr);
- param->db->StoreKey();
+ param->db->StoreCryptoKey();
EndDialog(hwndDlg, IDREMOVE);
}
break;
@@ -233,7 +134,7 @@ static INT_PTR CALLBACK sttChangePassword(HWND hwndDlg, UINT uMsg, WPARAM wParam
param->db->WriteSignature(dbSignatureE);
param->db->SetPassword(buf2);
- param->db->StoreKey();
+ param->db->StoreCryptoKey();
SecureZeroMemory(buf2, sizeof(buf2));
EndDialog(hwndDlg, IDOK);
}
diff --git a/plugins/Dbx_mdbx/res/dbx_mdbx.rc b/plugins/Dbx_mdbx/res/dbx_mdbx.rc
index 61530296f2..83b1661322 100644
--- a/plugins/Dbx_mdbx/res/dbx_mdbx.rc
+++ b/plugins/Dbx_mdbx/res/dbx_mdbx.rc
@@ -23,20 +23,6 @@ LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
// Dialog
//
-IDD_LOGIN DIALOGEX 0, 0, 190, 86
-STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU
-EXSTYLE WS_EX_TOPMOST | WS_EX_TOOLWINDOW
-CAPTION "Login to Miranda NG"
-FONT 8, "MS Shell Dlg", 400, 0, 0x1
-BEGIN
- CONTROL "",IDC_HEADERBAR,"MHeaderbarCtrl",0x0,0,0,190,26
- CTEXT "",IDC_LANG,158,34,13,13,SS_CENTERIMAGE | NOT WS_GROUP
- EDITTEXT IDC_USERPASS,21,34,128,14,ES_PASSWORD | ES_AUTOHSCROLL | WS_GROUP
- DEFPUSHBUTTON "OK",IDOK,36,64,50,14
- PUSHBUTTON "Cancel",IDCANCEL,102,64,50,14
- CONTROL "",IDC_STATIC,"Static",SS_ETCHEDFRAME,0,55,190,1
-END
-
IDD_NEWPASS DIALOGEX 0, 0, 190, 102
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU
EXSTYLE WS_EX_TOPMOST | WS_EX_TOOLWINDOW
diff --git a/plugins/Dbx_mdbx/src/dbcrypt.cpp b/plugins/Dbx_mdbx/src/dbcrypt.cpp
index d20315b773..3ee4ce9fc6 100644
--- a/plugins/Dbx_mdbx/src/dbcrypt.cpp
+++ b/plugins/Dbx_mdbx/src/dbcrypt.cpp
@@ -27,155 +27,21 @@ char DBKey_Crypto_Provider[] = "Provider";
char DBKey_Crypto_Key[] = "Key";
char DBKey_Crypto_IsEncrypted[] = "EncryptedDB";
-STDMETHODIMP_(BOOL) CDbxMDBX::StoreProvider(CRYPTO_PROVIDER *pProv)
-{
- txn_ptr trnlck(this);
- MDBX_val key = { DBKey_Crypto_Provider, sizeof(DBKey_Crypto_Provider) }, value = { pProv->pszName, mir_strlen(pProv->pszName) + 1 };
- if (mdbx_put(trnlck, m_dbCrypto, &key, &value, MDBX_UPSERT) != MDBX_SUCCESS)
- return FALSE;
-
- DBFlush();
- return TRUE;
-}
-
/////////////////////////////////////////////////////////////////////////////////////////
+// Saving encryption key in a database
-class CEnterPasswordDialog : public CDlgBase
+STDMETHODIMP_(BOOL) CDbxMDBX::ReadCryptoKey(MBinBuffer &buf)
{
- friend class CDbxMDBX;
-
- CTimer m_timer;
- CCtrlData m_header;
- CCtrlData m_language;
- CCtrlEdit m_passwordEdit;
-
- int m_wrongPass = 0;
- wchar_t m_newPass[100];
- CDbxMDBX *m_db;
-
- void OnTimer(CTimer*)
- {
- UINT_PTR LangID = (UINT_PTR)GetKeyboardLayout(0);
- char Lang[3] = { 0 };
- GetLocaleInfoA(MAKELCID((LangID & 0xffffffff), SORT_DEFAULT), LOCALE_SABBREVLANGNAME, Lang, 2);
- Lang[0] = toupper(Lang[0]);
- Lang[1] = tolower(Lang[1]);
- m_language.SetTextA(Lang);
- }
-
- INT_PTR DlgProc(UINT msg, WPARAM wParam, LPARAM lParam) override
- {
- if (msg == WM_CTLCOLORSTATIC) {
- if ((HWND)lParam == m_language.GetHwnd()) {
- SetTextColor((HDC)wParam, GetSysColor(COLOR_HIGHLIGHTTEXT));
- SetBkMode((HDC)wParam, TRANSPARENT);
- return (INT_PTR)GetSysColorBrush(COLOR_HIGHLIGHT);
- }
- }
- return CDlgBase::DlgProc(msg, wParam, lParam);
- }
-
-public:
- CEnterPasswordDialog(CDbxMDBX *db) :
- CDlgBase(g_plugin, IDD_LOGIN),
- m_timer(this, 1),
- m_header(this, IDC_HEADERBAR),
- m_language(this, IDC_LANG),
- m_passwordEdit(this, IDC_USERPASS),
- m_db(db)
- {
- m_newPass[0] = 0;
- m_timer.OnEvent = Callback(this, &CEnterPasswordDialog::OnTimer);
- }
-
- bool OnInitDialog() override
- {
- m_header.SendMsg(WM_SETICON, ICON_SMALL, (LPARAM)g_plugin.getIcon(IDI_LOGO, true));
-
- if (m_wrongPass) {
- if (m_wrongPass > 2) {
- m_passwordEdit.Disable();
- EnableWindow(GetDlgItem(m_hwnd, IDOK), false);
- m_header.SetText(TranslateT("Too many errors!"));
- }
- else m_header.SetText(TranslateT("Password is not correct!"));
- }
- else m_header.SetText(TranslateT("Please type in your password"));
-
- m_timer.Start(200);
- return true;
- }
-
- bool OnApply() override
- {
- m_passwordEdit.GetText(m_newPass, _countof(m_newPass));
- return true;
- }
-
- void OnDestroy() override
- {
- Window_FreeIcon_IcoLib(m_header.GetHwnd());
- }
-};
-
-int CDbxMDBX::InitCrypt()
-{
- if (m_crypto != nullptr)
- return 0;
-
- CRYPTO_PROVIDER *pProvider;
- MDBX_val key = { DBKey_Crypto_Provider, sizeof(DBKey_Crypto_Provider) }, value;
-
+ MDBX_val key = { DBKey_Crypto_Key, sizeof(DBKey_Crypto_Key) }, value;
int rc = mdbx_get(StartTran(), m_dbCrypto, &key, &value);
- if (rc == MDBX_SUCCESS) {
- pProvider = Crypto_GetProvider((const char*)value.iov_base);
- if (pProvider == nullptr)
- pProvider = SelectProvider();
- }
- else pProvider = SelectProvider();
-
- if (pProvider == nullptr)
- return 1;
-
- if ((m_crypto = pProvider->pFactory()) == nullptr)
- return 3;
-
- key.iov_len = sizeof(DBKey_Crypto_Key); key.iov_base = DBKey_Crypto_Key;
- rc = mdbx_get(StartTran(), m_dbCrypto, &key, &value);
- if (rc == MDBX_SUCCESS && (value.iov_len == m_crypto->getKeyLength())) {
- if (!m_crypto->setKey((const BYTE*)value.iov_base, value.iov_len)) {
- CEnterPasswordDialog dlg(this);
- while (true) {
- if (!dlg.DoModal())
- return 4;
-
- m_crypto->setPassword(pass_ptrA(mir_utf8encodeW(dlg.m_newPass)));
- if (m_crypto->setKey((const BYTE*)value.iov_base, value.iov_len)) {
- m_bUsesPassword = true;
- SecureZeroMemory(&dlg.m_newPass, sizeof(dlg.m_newPass));
- break;
- }
- dlg.m_wrongPass++;
- }
- }
- }
- else {
- if (!m_crypto->generateKey())
- return 6;
- StoreKey();
- }
-
- key.iov_len = sizeof(DBKey_Crypto_IsEncrypted); key.iov_base = DBKey_Crypto_IsEncrypted;
- if (mdbx_get(StartTran(), m_dbCrypto, &key, &value) == MDBX_SUCCESS)
- m_bEncrypted = *(const bool *)value.iov_base;
- else
- m_bEncrypted = false;
+ if (rc != MDBX_SUCCESS)
+ return FALSE;
- InitDialogs();
- return 0;
+ buf.append(value.iov_base, value.iov_len);
+ return TRUE;
}
-void CDbxMDBX::StoreKey()
+STDMETHODIMP_(BOOL) CDbxMDBX::StoreCryptoKey()
{
size_t iKeyLength = m_crypto->getKeyLength();
BYTE *pKey = (BYTE*)_alloca(iKeyLength);
@@ -191,8 +57,43 @@ void CDbxMDBX::StoreKey()
SecureZeroMemory(pKey, iKeyLength);
DBFlush();
+ return TRUE;
+}
+
+/////////////////////////////////////////////////////////////////////////////////////////
+// Saving encryption flag
+
+STDMETHODIMP_(BOOL) CDbxMDBX::ReadEncryption()
+{
+ MDBX_val key = { DBKey_Crypto_IsEncrypted, sizeof(DBKey_Crypto_IsEncrypted) }, value;
+ if (mdbx_get(StartTran(), m_dbCrypto, &key, &value) == MDBX_SUCCESS)
+ return *(const bool *)value.iov_base;
+ return false;
}
+/////////////////////////////////////////////////////////////////////////////////////////
+// Saving provider in a database
+
+STDMETHODIMP_(CRYPTO_PROVIDER *) CDbxMDBX::ReadProvider()
+{
+ MDBX_val key = { DBKey_Crypto_Provider, sizeof(DBKey_Crypto_Provider) }, value;
+ int rc = mdbx_get(StartTran(), m_dbCrypto, &key, &value);
+ return (rc == MDBX_SUCCESS) ? Crypto_GetProvider((const char *)value.iov_base) : nullptr;
+}
+
+STDMETHODIMP_(BOOL) CDbxMDBX::StoreProvider(CRYPTO_PROVIDER *pProv)
+{
+ txn_ptr trnlck(this);
+ MDBX_val key = { DBKey_Crypto_Provider, sizeof(DBKey_Crypto_Provider) }, value = { pProv->pszName, mir_strlen(pProv->pszName) + 1 };
+ if (mdbx_put(trnlck, m_dbCrypto, &key, &value, MDBX_UPSERT) != MDBX_SUCCESS)
+ return FALSE;
+
+ DBFlush();
+ return TRUE;
+}
+
+/////////////////////////////////////////////////////////////////////////////////////////
+
void CDbxMDBX::SetPassword(const wchar_t *ptszPassword)
{
if (ptszPassword == nullptr || *ptszPassword == 0) {
diff --git a/plugins/Dbx_mdbx/src/dbintf.cpp b/plugins/Dbx_mdbx/src/dbintf.cpp
index a9d57566c2..69c09f265a 100644
--- a/plugins/Dbx_mdbx/src/dbintf.cpp
+++ b/plugins/Dbx_mdbx/src/dbintf.cpp
@@ -221,6 +221,7 @@ int CDbxMDBX::Load()
if (InitModules()) return EGROKPRF_DAMAGED;
if (InitCrypt()) return EGROKPRF_DAMAGED;
+ InitDialogs();
FillContacts();
FillSettings();
diff --git a/plugins/Dbx_mdbx/src/dbintf.h b/plugins/Dbx_mdbx/src/dbintf.h
index 1003c31c7c..e4d542ae07 100644
--- a/plugins/Dbx_mdbx/src/dbintf.h
+++ b/plugins/Dbx_mdbx/src/dbintf.h
@@ -171,7 +171,7 @@ class CDbxMDBX : public MDatabaseCommon, public MIDatabaseChecker, public MZeroe
// database stuff
ptrW m_pwszProfileName;
- bool m_safetyMode = true, m_bReadOnly, m_bUsesPassword;
+ bool m_safetyMode = true, m_bReadOnly;
MDBX_env *m_env;
MDBX_txn *m_pWriteTran;
@@ -224,7 +224,6 @@ class CDbxMDBX : public MDatabaseCommon, public MIDatabaseChecker, public MZeroe
MDBX_dbi m_dbCrypto;
- int InitCrypt(void);
void InitDialogs();
public:
@@ -236,7 +235,6 @@ public:
int EnableEncryption(bool bEnable);
int Load();
int Map();
- void StoreKey(void);
void SetPassword(const wchar_t *ptszPassword);
int CheckEvents1(void);
@@ -245,8 +243,6 @@ public:
__forceinline LPSTR GetMenuTitle() const { return m_bUsesPassword ? (char*)LPGEN("Change/remove password") : (char*)LPGEN("Set password"); }
- __forceinline bool usesPassword() const { return m_bUsesPassword; }
-
public:
STDMETHODIMP_(BOOL) IsRelational(void) override { return TRUE; }
STDMETHODIMP_(void) SetCacheSafetyMode(BOOL) override;
@@ -281,8 +277,14 @@ public:
STDMETHODIMP_(BOOL) MetaSplitHistory(DBCachedContact *ccMeta, DBCachedContact *ccSub) override;
STDMETHODIMP_(BOOL) MetaRemoveSubHistory(DBCachedContact *ccSub) override;
+ STDMETHODIMP_(CRYPTO_PROVIDER*) ReadProvider(void);
STDMETHODIMP_(BOOL) StoreProvider(CRYPTO_PROVIDER*);
+ STDMETHODIMP_(BOOL) ReadEncryption(void);
+
+ STDMETHODIMP_(BOOL) ReadCryptoKey(MBinBuffer&);
+ STDMETHODIMP_(BOOL) StoreCryptoKey(void);
+
STDMETHODIMP_(BOOL) Compact();
STDMETHODIMP_(BOOL) Backup(const wchar_t*);
diff --git a/plugins/Dbx_mdbx/src/resource.h b/plugins/Dbx_mdbx/src/resource.h
index a6c8f8644e..c705e3ad15 100644
--- a/plugins/Dbx_mdbx/src/resource.h
+++ b/plugins/Dbx_mdbx/src/resource.h
@@ -4,7 +4,6 @@
//
#define IDREMOVE 3
#define IDI_LOGO 101
-#define IDD_LOGIN 102
#define IDD_NEWPASS 103
#define IDD_CHANGEPASS 104
#define IDD_OPTIONS 105
diff --git a/plugins/Dbx_mdbx/src/ui.cpp b/plugins/Dbx_mdbx/src/ui.cpp
index 6b7ce1fe83..f538d99c51 100644
--- a/plugins/Dbx_mdbx/src/ui.cpp
+++ b/plugins/Dbx_mdbx/src/ui.cpp
@@ -104,7 +104,7 @@ static INT_PTR CALLBACK sttChangePassword(HWND hwndDlg, UINT uMsg, WPARAM wParam
else {
// param->db->WriteSignature(dbSignatureU);
param->db->SetPassword(nullptr);
- param->db->StoreKey();
+ param->db->StoreCryptoKey();
EndDialog(hwndDlg, IDREMOVE);
}
break;
@@ -128,7 +128,7 @@ static INT_PTR CALLBACK sttChangePassword(HWND hwndDlg, UINT uMsg, WPARAM wParam
// param->db->WriteSignature(dbSignatureE);
param->db->SetPassword(buf2);
- param->db->StoreKey();
+ param->db->StoreCryptoKey();
SecureZeroMemory(buf2, sizeof(buf2));
EndDialog(hwndDlg, IDOK);
}
diff --git a/plugins/Dbx_sqlite/res/dbx_sqlite.rc b/plugins/Dbx_sqlite/res/dbx_sqlite.rc
index 141e211876..7f2c2b022e 100644
--- a/plugins/Dbx_sqlite/res/dbx_sqlite.rc
+++ b/plugins/Dbx_sqlite/res/dbx_sqlite.rc
@@ -23,20 +23,6 @@ LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
// Dialog
//
-IDD_LOGIN DIALOGEX 0, 0, 190, 86
-STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU
-EXSTYLE WS_EX_TOPMOST | WS_EX_TOOLWINDOW
-CAPTION "Login to Miranda NG"
-FONT 8, "MS Shell Dlg", 400, 0, 0x1
-BEGIN
- CONTROL "",IDC_HEADERBAR,"MHeaderbarCtrl",0x0,0,0,190,26
- CTEXT "",IDC_LANG,158,34,13,13,SS_CENTERIMAGE | NOT WS_GROUP
- EDITTEXT IDC_USERPASS,21,34,128,14,ES_PASSWORD | ES_AUTOHSCROLL | WS_GROUP
- DEFPUSHBUTTON "OK",IDOK,36,64,50,14
- PUSHBUTTON "Cancel",IDCANCEL,102,64,50,14
- CONTROL "",IDC_STATIC,"Static",SS_ETCHEDFRAME,0,55,190,1
-END
-
IDD_NEWPASS DIALOGEX 0, 0, 190, 102
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU
EXSTYLE WS_EX_TOPMOST | WS_EX_TOOLWINDOW
diff --git a/plugins/Dbx_sqlite/src/dbcrypt.cpp b/plugins/Dbx_sqlite/src/dbcrypt.cpp
index f0dca5a0bc..af4aa22682 100644
--- a/plugins/Dbx_sqlite/src/dbcrypt.cpp
+++ b/plugins/Dbx_sqlite/src/dbcrypt.cpp
@@ -1,5 +1,63 @@
#include "stdafx.h"
+/////////////////////////////////////////////////////////////////////////////////////////
+// Saving encryption key in a database
+
+STDMETHODIMP_(BOOL) CDbxSQLite::ReadCryptoKey(MBinBuffer &buf)
+{
+ DBVARIANT dbv = {};
+ dbv.type = DBVT_BLOB;
+ if (GetContactSetting(0, "CryptoEngine", "StoredKey", &dbv))
+ return FALSE;
+
+ buf.append(dbv.pbVal, dbv.cpbVal);
+ return TRUE;
+}
+
+STDMETHODIMP_(BOOL) CDbxSQLite::StoreCryptoKey()
+{
+ size_t iKeyLength = m_crypto->getKeyLength();
+ BYTE *pKey = (BYTE*)_alloca(iKeyLength);
+ m_crypto->getKey(pKey, iKeyLength);
+
+ DBCONTACTWRITESETTING dbcws = { "CryptoEngine", "StoredKey" };
+ dbcws.value.type = DBVT_BLOB;
+ dbcws.value.cpbVal = (WORD)iKeyLength;
+ dbcws.value.pbVal = pKey;
+ WriteContactSetting(0, &dbcws);
+
+ SecureZeroMemory(pKey, iKeyLength);
+ return TRUE;
+}
+
+/////////////////////////////////////////////////////////////////////////////////////////
+// Saving encryption flag
+
+STDMETHODIMP_(BOOL) CDbxSQLite::ReadEncryption()
+{
+ DBVARIANT dbv = {};
+ dbv.type = DBVT_BYTE;
+ return (GetContactSetting(0, "CryptoEngine", "DatabaseEncryption", &dbv)) ? false : dbv.bVal != 0;
+}
+
+/////////////////////////////////////////////////////////////////////////////////////////
+// Saving provider in a database
+
+STDMETHODIMP_(CRYPTO_PROVIDER*) CDbxSQLite::ReadProvider()
+{
+ DBVARIANT dbv = {};
+ dbv.type = DBVT_BLOB;
+ if (GetContactSetting(0, "CryptoEngine", "Provider", &dbv))
+ return nullptr;
+
+ if (dbv.type != DBVT_BLOB)
+ return nullptr;
+
+ auto *pProvider = Crypto_GetProvider(LPCSTR(dbv.pbVal));
+ FreeVariant(&dbv);
+ return pProvider;
+}
+
STDMETHODIMP_(BOOL) CDbxSQLite::StoreProvider(CRYPTO_PROVIDER *pProvider)
{
DBCONTACTWRITESETTING dbcws = { "CryptoEngine", "Provider" };
diff --git a/plugins/Dbx_sqlite/src/dbintf.cpp b/plugins/Dbx_sqlite/src/dbintf.cpp
index 862ecdae75..9cc637103d 100755
--- a/plugins/Dbx_sqlite/src/dbintf.cpp
+++ b/plugins/Dbx_sqlite/src/dbintf.cpp
@@ -142,6 +142,11 @@ MDatabaseCommon* CDbxSQLite::Load(const wchar_t *profile, int readonly)
CDbxSQLite *db = new CDbxSQLite(database);
+ if (!db->InitCrypt()) {
+ delete db;
+ return nullptr;
+ }
+
db->InitContacts();
db->InitSettings();
db->InitEvents();
diff --git a/plugins/Dbx_sqlite/src/dbintf.h b/plugins/Dbx_sqlite/src/dbintf.h
index ca220289c0..88638623a5 100755
--- a/plugins/Dbx_sqlite/src/dbintf.h
+++ b/plugins/Dbx_sqlite/src/dbintf.h
@@ -91,7 +91,13 @@ public:
STDMETHODIMP_(BOOL) EnumModuleNames(DBMODULEENUMPROC pFunc, void *pParam) override;
- STDMETHODIMP_(BOOL) StoreProvider(CRYPTO_PROVIDER*);
+ STDMETHODIMP_(BOOL) ReadCryptoKey(MBinBuffer&) override;
+ STDMETHODIMP_(BOOL) StoreCryptoKey() override;
+
+ STDMETHODIMP_(CRYPTO_PROVIDER*) ReadProvider() override;
+ STDMETHODIMP_(BOOL) StoreProvider(CRYPTO_PROVIDER*) override;
+
+ STDMETHODIMP_(BOOL) ReadEncryption() override;
STDMETHODIMP_(BOOL) WriteContactSettingWorker(MCONTACT contactID, DBCONTACTWRITESETTING &dbcws) override;
STDMETHODIMP_(BOOL) DeleteContactSetting(MCONTACT contactID, LPCSTR szModule, LPCSTR szSetting) override;
diff --git a/plugins/Dbx_sqlite/src/resource.h b/plugins/Dbx_sqlite/src/resource.h
index 135f6899d0..9e2976c927 100644
--- a/plugins/Dbx_sqlite/src/resource.h
+++ b/plugins/Dbx_sqlite/src/resource.h
@@ -5,7 +5,6 @@
#define IDREMOVE 3
#define IDI_ICONPASS 100
#define IDI_LOGO 101
-#define IDD_LOGIN 102
#define IDD_NEWPASS 103
#define IDD_CHANGEPASS 104
#define IDD_OPTIONS 105
diff --git a/plugins/Import/src/dbrw/dbintf.h b/plugins/Import/src/dbrw/dbintf.h
index 4749e8146d..cca3998246 100644
--- a/plugins/Import/src/dbrw/dbintf.h
+++ b/plugins/Import/src/dbrw/dbintf.h
@@ -90,7 +90,7 @@ struct TSqlMessage {
HANDLE hDoneEvent;
};
-struct CDbxSQLite : public MDatabaseCommon, public MZeroedObject
+struct CDbxSQLite : public MDatabaseReadonly, public MZeroedObject
{
private:
sqlite3 *m_sqlite;
@@ -148,8 +148,6 @@ public:
STDMETHODIMP_(BOOL) EnumModuleNames(DBMODULEENUMPROC pFunc, void *pParam) override;
STDMETHODIMP_(BOOL) GetContactSettingWorker(MCONTACT contactID, LPCSTR szModule, LPCSTR szSetting, DBVARIANT *dbv, int isStatic) override;
- STDMETHODIMP_(BOOL) WriteContactSettingWorker(MCONTACT contactID, DBCONTACTWRITESETTING &dbcws) override;
- STDMETHODIMP_(BOOL) DeleteContactSetting(MCONTACT contactID, LPCSTR szModule, LPCSTR szSetting) override;
STDMETHODIMP_(BOOL) EnumContactSettings(MCONTACT hContact, DBSETTINGENUMPROC pfnEnumProc, const char *szModule, void *param) override;
STDMETHODIMP_(BOOL) MetaMergeHistory(DBCachedContact *ccMeta, DBCachedContact *ccSub) override;
diff --git a/plugins/Import/src/dbrw/dbsettings.cpp b/plugins/Import/src/dbrw/dbsettings.cpp
index b09f1675b6..615415dec6 100644
--- a/plugins/Import/src/dbrw/dbsettings.cpp
+++ b/plugins/Import/src/dbrw/dbsettings.cpp
@@ -148,16 +148,6 @@ STDMETHODIMP_(BOOL) CDbxSQLite::GetContactSettingWorker(MCONTACT contactID, LPCS
return 0;
}
-STDMETHODIMP_(BOOL) CDbxSQLite::WriteContactSettingWorker(MCONTACT, DBCONTACTWRITESETTING&)
-{
- return 1;
-}
-
-STDMETHODIMP_(BOOL) CDbxSQLite::DeleteContactSetting(MCONTACT, LPCSTR, LPCSTR)
-{
- return 1;
-}
-
STDMETHODIMP_(BOOL) CDbxSQLite::EnumContactSettings(MCONTACT hContact, DBSETTINGENUMPROC pfnEnumProc, const char *szModule, void *param)
{
if (szModule == nullptr)