summaryrefslogtreecommitdiff
path: root/plugins/Db3x_mmap/src/dbcrypt.cpp
diff options
context:
space:
mode:
authorGeorge Hazan <george.hazan@gmail.com>2013-11-27 19:09:16 +0000
committerGeorge Hazan <george.hazan@gmail.com>2013-11-27 19:09:16 +0000
commitcb80a37654ebc902769f87ffbadd38d161109333 (patch)
tree12b91f3d6920716b441c29aefa39dc858c264fe0 /plugins/Db3x_mmap/src/dbcrypt.cpp
parentab1971b510fb004fc23aa3a69565ce69d8d0743e (diff)
- strong cyphering for passwords in db3x_mmap;
- unique signature for the new mmap profiles; git-svn-id: http://svn.miranda-ng.org/main/trunk@6997 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'plugins/Db3x_mmap/src/dbcrypt.cpp')
-rw-r--r--plugins/Db3x_mmap/src/dbcrypt.cpp109
1 files changed, 107 insertions, 2 deletions
diff --git a/plugins/Db3x_mmap/src/dbcrypt.cpp b/plugins/Db3x_mmap/src/dbcrypt.cpp
index b0054bacbf..79a3744462 100644
--- a/plugins/Db3x_mmap/src/dbcrypt.cpp
+++ b/plugins/Db3x_mmap/src/dbcrypt.cpp
@@ -23,7 +23,101 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "commonheaders.h"
-int CDb3Base::InitCrypt()
+/////////////////////////////////////////////////////////////////////////////////////////
+
+bool isEncrypted(LPCSTR szModule, LPCSTR szSetting);
+
+//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) :
+ szVar(mir_strdup(var)),
+ szValue(mir_strdup(value))
+ {}
+
+ ptrA szVar, szValue;
+};
+
+struct SettingUgraderParam
+{
+ CDb3Mmap *db;
+ LPCSTR szModule;
+ HANDLE hContact;
+ OBJLIST<VarDescr>* pList;
+};
+
+int sttSettingUgrader(const char *szSetting, LPARAM lParam)
+{
+ SettingUgraderParam *param = (SettingUgraderParam*)lParam;
+ if (isEncrypted(param->szModule, szSetting)) {
+ DBVARIANT dbv = { DBVT_UTF8 };
+ DBCONTACTGETSETTING dbcgs = { param->szModule, szSetting, &dbv };
+ if (!param->db->GetContactSettingStr(param->hContact, &dbcgs)) {
+ if (dbv.type == DBVT_UTF8) {
+ DecodeString(dbv.pszVal);
+ param->pList->insert(new VarDescr(szSetting, dbv.pszVal));
+ }
+ param->db->FreeVariant(&dbv);
+ }
+ }
+ return 0;
+}
+
+void sttContactEnum(HANDLE hContact, const char *szModule, CDb3Mmap *db)
+{
+ OBJLIST<VarDescr> arSettings(1);
+ SettingUgraderParam param = { db, szModule, hContact, &arSettings };
+
+ DBCONTACTENUMSETTINGS dbces = { 0 };
+ dbces.pfnEnumProc = sttSettingUgrader;
+ dbces.szModule = szModule;
+ dbces.lParam = (LPARAM)&param;
+ db->EnumContactSettings(NULL, &dbces);
+
+ for (int i = 0; i < arSettings.getCount(); i++) {
+ VarDescr &p = arSettings[i];
+
+ size_t len;
+ BYTE *pResult = db->m_crypto->encodeString(p.szValue, &len);
+ if (pResult != NULL) {
+ DBCONTACTWRITESETTING dbcws = { szModule, p.szVar };
+ dbcws.value.type = DBVT_ENCRYPTED;
+ dbcws.value.pbVal = pResult;
+ dbcws.value.cpbVal = (WORD)len;
+ db->WriteContactSetting(hContact, &dbcws);
+
+ mir_free(pResult);
+ }
+ }
+}
+
+int sttModuleEnum(const char *szModule, DWORD, LPARAM lParam)
+{
+ CDb3Mmap *db = (CDb3Mmap*)lParam;
+ sttContactEnum(NULL, szModule, db);
+
+ for (HANDLE hContact = db->FindFirstContact(); hContact; hContact = db->FindNextContact(hContact))
+ sttContactEnum(hContact, szModule, db);
+
+ return 0;
+}
+
+/////////////////////////////////////////////////////////////////////////////////////////
+
+int CDb3Mmap::InitCrypt()
{
CRYPTO_PROVIDER *pProvider;
@@ -78,8 +172,19 @@ LBL_SetNewKey:
if (dbv.cpbVal != (WORD)iKeyLength)
goto LBL_SetNewKey;
- m_crypto->setKey(dbv.pbVal, iKeyLength);
+ if (!m_crypto->setKey(dbv.pbVal, iKeyLength))
+ goto LBL_SetNewKey;
+
FreeVariant(&dbv);
}
+
+ if (memcmp(&m_dbHeader.signature, &dbSignature, sizeof(m_dbHeader.signature))) {
+ EnumModuleNames(sttModuleEnum, this);
+
+ // upgrade signature
+ memcpy(&m_dbHeader.signature, &dbSignature, sizeof(dbSignature));
+ DBWrite(0, &dbSignature, sizeof(dbSignature));
+ }
+
return 0;
}