From cef1c5304d49ccb5a9ab00e2b6097441a79e271b Mon Sep 17 00:00:00 2001 From: MikalaiR Date: Sat, 27 Feb 2016 11:09:45 +0000 Subject: dbx_lmdb: encryption settings to other dbi git-svn-id: http://svn.miranda-ng.org/main/trunk@16356 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/Dbx_mdb/src/dbcrypt.cpp | 140 +++++++++++++++++++++------------------ plugins/Dbx_mdb/src/dbevents.cpp | 9 ++- plugins/Dbx_mdb/src/dbintf.cpp | 1 + plugins/Dbx_mdb/src/dbintf.h | 6 ++ 4 files changed, 88 insertions(+), 68 deletions(-) diff --git a/plugins/Dbx_mdb/src/dbcrypt.cpp b/plugins/Dbx_mdb/src/dbcrypt.cpp index 0ff3be3817..1a4fe39276 100644 --- a/plugins/Dbx_mdb/src/dbcrypt.cpp +++ b/plugins/Dbx_mdb/src/dbcrypt.cpp @@ -25,75 +25,86 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. ///////////////////////////////////////////////////////////////////////////////////////// +#define DBKEY_PROVIDER "Provider" +#define DBKEY_KEY "Key" +#define DBKEY_IS_ENCRYPTED "EncryptedDB" + +CRYPTO_PROVIDER* CDbxMdb::SelectProvider() +{ + CRYPTO_PROVIDER **ppProvs, *pProv; + int iNumProvs; + Crypto_EnumProviders(&iNumProvs, &ppProvs); + if (iNumProvs == 0) + return nullptr; + + if (iNumProvs > 1) + { + CSelectCryptoDialog dlg(ppProvs, iNumProvs); + dlg.DoModal(); + pProv = dlg.GetSelected(); + } + else pProv = ppProvs[0]; + + txn_ptr txn(m_pMdbEnv); + MDB_val key = { sizeof(DBKEY_PROVIDER), DBKEY_PROVIDER }, value = { mir_strlen(pProv->pszName) + 1, pProv->pszName }; + mdb_put(txn, m_dbCrypto, &key, &value, 0); + txn.commit(); + + return pProv; +} int CDbxMdb::InitCrypt() { CRYPTO_PROVIDER *pProvider; - bool bMissingKey = false; - - DBVARIANT dbv = { 0 }; - dbv.type = DBVT_BLOB; - if (GetContactSetting(NULL, "CryptoEngine", "Provider", &dbv)) { - LBL_CreateProvider: - CRYPTO_PROVIDER **ppProvs; - int iNumProvs; - Crypto_EnumProviders(&iNumProvs, &ppProvs); - if (iNumProvs == 0) - return 1; - - if (iNumProvs > 1) - { - CSelectCryptoDialog dlg(ppProvs, iNumProvs); - dlg.DoModal(); - pProvider = dlg.GetSelected(); - } - else pProvider = ppProvs[0]; - DBCONTACTWRITESETTING dbcws = { "CryptoEngine", "Provider" }; - dbcws.value.type = DBVT_BLOB; - dbcws.value.pbVal = (PBYTE)pProvider->pszName; - dbcws.value.cpbVal = (int)strlen(pProvider->pszName) + 1; - WriteContactSetting(NULL, &dbcws); + txn_ptr_ro txn(m_txn); + + MDB_val key = { sizeof(DBKEY_PROVIDER), DBKEY_PROVIDER }, value; + if (mdb_get(txn, m_dbCrypto, &key, &value) == MDB_SUCCESS) + { + pProvider = Crypto_GetProvider((const char*)value.mv_data); + if (pProvider == nullptr) + pProvider = SelectProvider(); } - else + 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 == NULL) - goto LBL_CreateProvider; + pProvider = SelectProvider(); } + if (pProvider == nullptr) + return 1; - if ((m_crypto = pProvider->pFactory()) == NULL) + if ((m_crypto = pProvider->pFactory()) == nullptr) return 3; - dbv.type = DBVT_BLOB; - if (GetContactSetting(NULL, "CryptoEngine", "StoredKey", &dbv)) { - bMissingKey = true; - - LBL_SetNewKey: - m_crypto->generateKey(); // unencrypted key - StoreKey(); + key.mv_size = sizeof(DBKEY_KEY); key.mv_data = DBKEY_KEY; + if (mdb_get(txn, m_dbCrypto, &key, &value) == MDB_SUCCESS) + { + if (value.mv_size != m_crypto->getKeyLength()) + { + if (!m_crypto->generateKey()) + return 6; + StoreKey(); + } + else + { + if (!m_crypto->setKey((const BYTE*)value.mv_data, value.mv_size)) + if (!EnterPassword((const BYTE*)value.mv_data, value.mv_size)) // password protected? + return 4; + } } - else { - size_t iKeyLength = m_crypto->getKeyLength(); - if (dbv.cpbVal != (WORD)iKeyLength) - goto LBL_SetNewKey; - - if (!m_crypto->setKey(dbv.pbVal, iKeyLength)) - if (!EnterPassword(dbv.pbVal, iKeyLength)) // password protected? - return 4; - - FreeVariant(&dbv); + else + { + if (!m_crypto->generateKey()) + return 6; + StoreKey(); } - dbv.type = DBVT_BYTE; - if (!GetContactSetting(NULL, "CryptoEngine", "DatabaseEncryption", &dbv)) - m_bEncrypted = dbv.bVal != 0; + key.mv_size = sizeof(DBKEY_IS_ENCRYPTED); key.mv_data = DBKEY_IS_ENCRYPTED; + + if (mdb_get(txn, m_dbCrypto, &key, &value) == MDB_SUCCESS) + m_bEncrypted = *(const BYTE*)value.mv_data != 0; + else + m_bEncrypted = false; InitDialogs(); return 0; @@ -105,11 +116,10 @@ void CDbxMdb::StoreKey() 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(NULL, &dbcws); + txn_ptr txn(m_pMdbEnv); + MDB_val key = { sizeof(DBKEY_KEY), DBKEY_KEY }, value = { iKeyLength, pKey }; + mdb_put(txn, m_dbCrypto, &key, &value, 0); + txn.commit(); SecureZeroMemory(pKey, iKeyLength); } @@ -122,7 +132,7 @@ void CDbxMdb::SetPassword(LPCTSTR ptszPassword) } else { m_bUsesPassword = true; - m_crypto->setPassword(ptrA(mir_utf8encodeT(ptszPassword))); + m_crypto->setPassword(pass_ptrA(mir_utf8encodeT(ptszPassword))); } UpdateMenuItem(); } @@ -147,10 +157,10 @@ void CDbxMdb::ToggleEncryption() m_bEncrypted = !m_bEncrypted; - DBCONTACTWRITESETTING dbcws = { "CryptoEngine", "DatabaseEncryption" }; - dbcws.value.type = DBVT_BYTE; - dbcws.value.bVal = m_bEncrypted; - WriteContactSetting(NULL, &dbcws); + txn_ptr txn(m_pMdbEnv); + MDB_val key = { sizeof(DBKEY_IS_ENCRYPTED), DBKEY_IS_ENCRYPTED }, value = { sizeof(BYTE), &m_bEncrypted }; + mdb_put(txn, m_dbCrypto, &key, &value, 0); + txn.commit(); hSettingChangeEvent = hSave1; hEventAddedEvent = hSave2; diff --git a/plugins/Dbx_mdb/src/dbevents.cpp b/plugins/Dbx_mdb/src/dbevents.cpp index 50b5f2a8b8..bee0cffcc8 100644 --- a/plugins/Dbx_mdb/src/dbevents.cpp +++ b/plugins/Dbx_mdb/src/dbevents.cpp @@ -298,7 +298,8 @@ STDMETHODIMP_(BOOL) CDbxMdb::MarkEventRead(MCONTACT contactID, MEVENT hDbEvent) STDMETHODIMP_(MCONTACT) CDbxMdb::GetEventContact(MEVENT hDbEvent) { - if (hDbEvent == 0) return INVALID_CONTACT_ID; + if (hDbEvent == 0) + return INVALID_CONTACT_ID; mir_cslock lck(m_csDbAccess); txn_ptr_ro txn(m_txn); @@ -355,7 +356,8 @@ STDMETHODIMP_(MEVENT) CDbxMdb::FindLastEvent(MCONTACT contactID) STDMETHODIMP_(MEVENT) CDbxMdb::FindNextEvent(MCONTACT contactID, MEVENT hDbEvent) { - if (hDbEvent == 0) return m_evLast = 0; + if (hDbEvent == 0) + return m_evLast = 0; MDB_val data; DWORD ts; @@ -388,7 +390,8 @@ STDMETHODIMP_(MEVENT) CDbxMdb::FindNextEvent(MCONTACT contactID, MEVENT hDbEvent STDMETHODIMP_(MEVENT) CDbxMdb::FindPrevEvent(MCONTACT contactID, MEVENT hDbEvent) { - if (hDbEvent == 0) return m_evLast = 0; + if (hDbEvent == 0) + return m_evLast = 0; MDB_val data; DWORD ts; diff --git a/plugins/Dbx_mdb/src/dbintf.cpp b/plugins/Dbx_mdb/src/dbintf.cpp index 93606df78c..f6026e92e3 100644 --- a/plugins/Dbx_mdb/src/dbintf.cpp +++ b/plugins/Dbx_mdb/src/dbintf.cpp @@ -86,6 +86,7 @@ int CDbxMdb::Load(bool bSkipInit) if (!bSkipInit) { txn_ptr trnlck(m_pMdbEnv); + mdb_open(trnlck, "crypto", MDB_CREATE, &m_dbCrypto); mdb_open(trnlck, "global", MDB_CREATE | MDB_INTEGERKEY, &m_dbGlobal); mdb_open(trnlck, "contacts", MDB_CREATE | MDB_INTEGERKEY, &m_dbContacts); mdb_open(trnlck, "modules", MDB_CREATE | MDB_INTEGERKEY, &m_dbModules); diff --git a/plugins/Dbx_mdb/src/dbintf.h b/plugins/Dbx_mdb/src/dbintf.h index c1a8569a2c..2514431260 100644 --- a/plugins/Dbx_mdb/src/dbintf.h +++ b/plugins/Dbx_mdb/src/dbintf.h @@ -322,10 +322,16 @@ protected: //////////////////////////////////////////////////////////////////////////// // encryption + MDB_dbi m_dbCrypto; + + int InitCrypt(void); void ToggleEventsEncryption(MCONTACT contactID); void ToggleSettingsEncryption(MCONTACT contactID); + CRYPTO_PROVIDER* SelectProvider(); + void GenerateNewKey(); + void InitDialogs(); bool EnterPassword(const BYTE *pKey, const size_t keyLen); }; -- cgit v1.2.3