From 97254a71ba33edf4c829e4c4b0c4961c6348045f Mon Sep 17 00:00:00 2001 From: aunsane Date: Mon, 18 Dec 2017 14:51:36 +0300 Subject: Tox: enable profile encription (in test mode) --- protocols/Tox/res/resource.rc | 4 ++-- protocols/Tox/src/tox_profile.cpp | 49 +++++++++++++++++++++++++++------------ protocols/Tox/src/tox_profile.h | 9 ++++--- 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/protocols/Tox/res/resource.rc b/protocols/Tox/res/resource.rc index 4f40144849..25bdea7c5c 100644 --- a/protocols/Tox/res/resource.rc +++ b/protocols/Tox/res/resource.rc @@ -144,8 +144,8 @@ CAPTION "Enter password" FONT 8, "MS Shell Dlg", 0, 0, 0x1 BEGIN EDITTEXT IDC_PASSWORD,7,24,197,12,ES_PASSWORD | ES_AUTOHSCROLL - CONTROL "Save password",IDC_SAVEPERMANENTLY,"Button",BS_AUTOCHECKBOX | NOT WS_VISIBLE | WS_TABSTOP,7,40,197,12 - DEFPUSHBUTTON "OK",IDOK,101,56,50,14 + CONTROL "Save password",IDC_SAVEPERMANENTLY,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,40,197,12 + DEFPUSHBUTTON "OK",IDOK,101,56,50,14,WS_DISABLED PUSHBUTTON "Cancel",IDCANCEL,154,56,50,14 LTEXT "Tox profile is encrypted. Enter the password to continue.",IDC_STATIC,7,5,197,18 END diff --git a/protocols/Tox/src/tox_profile.cpp b/protocols/Tox/src/tox_profile.cpp index a144eebee2..3beeb2d91c 100644 --- a/protocols/Tox/src/tox_profile.cpp +++ b/protocols/Tox/src/tox_profile.cpp @@ -64,18 +64,21 @@ bool CToxProto::LoadToxProfile(Tox_Options *options) mir_free(data); return false; } + password = mir_utf8encodeW(pass_ptrT(passwordEditor.GetPassword())); } - uint8_t *encryptedData = (uint8_t*)mir_calloc(size - TOX_PASS_ENCRYPTION_EXTRA_LENGTH); + size_t decryptedSize = size - TOX_PASS_ENCRYPTION_EXTRA_LENGTH; + uint8_t *decryptedData = (uint8_t*)mir_calloc(decryptedSize); TOX_ERR_DECRYPTION coreDecryptError; - if (!tox_pass_decrypt(data, size, (uint8_t*)(char*)password, mir_strlen(password), encryptedData, &coreDecryptError)) { + if (!tox_pass_decrypt(data, size, (uint8_t*)(char*)password, mir_strlen(password), decryptedData, &coreDecryptError)) { ShowNotification(TranslateT("Unable to decrypt Tox profile"), MB_ICONERROR); debugLogA(__FUNCTION__": failed to decrypt tox profile (%d)", coreDecryptError); mir_free(data); return false; } mir_free(data); - data = encryptedData; - size -= TOX_PASS_ENCRYPTION_EXTRA_LENGTH; + data = decryptedData; + size = decryptedSize; + return true; } if (data) { @@ -93,21 +96,26 @@ void CToxProto::SaveToxProfile(Tox *tox) mir_cslock locker(profileLock); size_t size = tox_get_savedata_size(tox); - uint8_t *data = (uint8_t*)mir_calloc(size + TOX_PASS_ENCRYPTION_EXTRA_LENGTH); + uint8_t *data = (uint8_t*)mir_calloc(size); tox_get_savedata(tox, data); - /*pass_ptrA password(mir_utf8encodeW(pass_ptrT(getWStringA("Password")))); + pass_ptrA password(mir_utf8encodeW(pass_ptrT(getWStringA("Password")))); if (password && mir_strlen(password)) { TOX_ERR_ENCRYPTION coreEncryptError; - if (!tox_pass_encrypt(data, size, (uint8_t*)(char*)password, mir_strlen(password), data, &coreEncryptError)) + size_t encryptedSize = size + TOX_PASS_ENCRYPTION_EXTRA_LENGTH; + uint8_t *encryptedData = (uint8_t*)mir_calloc(encryptedSize); + if (!tox_pass_encrypt(data, size, (uint8_t*)(char*)password, mir_strlen(password), encryptedData, &coreEncryptError)) { debugLogA(__FUNCTION__": failed to encrypt tox profile"); mir_free(data); + mir_free(encryptedData); return; } - size += TOX_PASS_ENCRYPTION_EXTRA_LENGTH; - }*/ + mir_free(data); + data = encryptedData; + size = encryptedSize; + } ptrW profilePath(GetToxProfilePath()); FILE *profile = _wfopen(profilePath, L"wb"); @@ -141,17 +149,28 @@ INT_PTR CToxProto::OnCopyToxID(WPARAM, LPARAM) } CToxPasswordEditor::CToxPasswordEditor(CToxProto *proto) : - CToxDlgBase(proto, IDD_PASSWORD, false), ok(this, IDOK), - password(this, IDC_PASSWORD), savePermanently(this, IDC_SAVEPERMANENTLY) + CToxDlgBase(proto, IDD_PASSWORD, false), m_ok(this, IDOK), + m_password(this, IDC_PASSWORD), m_savePermanently(this, IDC_SAVEPERMANENTLY) { - ok.OnClick = Callback(this, &CToxPasswordEditor::OnOk); + m_password.OnChange = Callback(this, &CToxPasswordEditor::OnOk); + m_ok.OnClick = Callback(this, &CToxPasswordEditor::OnOk); + m_ok.Enable(GetWindowTextLength(m_password.GetHwnd())); +} + +void CToxPasswordEditor::OnChange(CCtrlBase*) +{ + //m_ok.Enable(GetWindowTextLength(m_password.GetHwnd())); } void CToxPasswordEditor::OnOk(CCtrlButton*) { - pass_ptrT tszPassword(password.GetText()); - if (savePermanently.Enabled()) + pass_ptrT tszPassword(m_password.GetText()); + if (m_savePermanently.Enabled()) m_proto->setWString("Password", tszPassword); - EndDialog(m_hwnd, 1); } + +wchar_t* CToxPasswordEditor::GetPassword() +{ + return m_password.GetText(); +} diff --git a/protocols/Tox/src/tox_profile.h b/protocols/Tox/src/tox_profile.h index 796f264d5b..6a0b31f1f5 100644 --- a/protocols/Tox/src/tox_profile.h +++ b/protocols/Tox/src/tox_profile.h @@ -4,16 +4,19 @@ class CToxPasswordEditor : public CToxDlgBase { private: - CCtrlEdit password; - CCtrlCheck savePermanently; + CCtrlEdit m_password; + CCtrlCheck m_savePermanently; - CCtrlButton ok; + CCtrlButton m_ok; protected: + void OnChange(CCtrlBase*); void OnOk(CCtrlButton*); public: CToxPasswordEditor(CToxProto *proto); + + wchar_t* GetPassword(); }; #endif //_TOX_PROFILE_H_ \ No newline at end of file -- cgit v1.2.3