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