summaryrefslogtreecommitdiff
path: root/plugins/Dbx_mdbx
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/Dbx_mdbx
parent64579d553e4089d55c136140bd0058696d2cf526 (diff)
encryption code unification, part 2: initialization & data storage
Diffstat (limited to 'plugins/Dbx_mdbx')
-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
6 files changed, 53 insertions, 164 deletions
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);
}