summaryrefslogtreecommitdiff
path: root/plugins/Dbx_mdbx/src/dbcrypt.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/Dbx_mdbx/src/dbcrypt.cpp')
-rw-r--r--plugins/Dbx_mdbx/src/dbcrypt.cpp185
1 files changed, 43 insertions, 142 deletions
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) {