From 0f3d6c04c20a21238a23102b0f3d01724eb3ac07 Mon Sep 17 00:00:00 2001 From: mikalair Date: Fri, 1 Feb 2019 18:18:19 +0300 Subject: Initialize OpenSSL locks --- src/mir_app/mir_app.vcxproj | 2 ++ src/mir_app/src/miranda.cpp | 5 ++++ src/mir_app/src/openssl.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++++ src/mir_app/src/openssl.h | 30 ++++++++++++++++++++ 4 files changed, 104 insertions(+) create mode 100644 src/mir_app/src/openssl.cpp create mode 100644 src/mir_app/src/openssl.h (limited to 'src/mir_app') diff --git a/src/mir_app/mir_app.vcxproj b/src/mir_app/mir_app.vcxproj index 7ef633707c..720b8c50ba 100644 --- a/src/mir_app/mir_app.vcxproj +++ b/src/mir_app/mir_app.vcxproj @@ -21,6 +21,7 @@ {538E451F-E667-4D07-BCE6-976ECC7BB8D1} mir_app + 10.0.17763.0 @@ -35,6 +36,7 @@ src/mir_app64.def /ignore:4197 %(AdditionalOptions) type=%27win32%27 name=%27Microsoft.Windows.Common-Controls%27 version=%276.0.0.0%27 processorArchitecture=%27*%27 publicKeyToken=%276595b64144ccf1df%27 language=%27*%27;type=%27win32%27 name=%27Microsoft.Windows.Gdiplus%27 version=%271.0.0.0%27 processorArchitecture=%27amd64%27 publicKeyToken=%276595b64144ccf1df%27 language=%27*%27;%(AdditionalManifestDependencies) + libeay32.lib;%(AdditionalDependencies) res/miranda32.exe.manifest diff --git a/src/mir_app/src/miranda.cpp b/src/mir_app/src/miranda.cpp index 23e99d1643..423f709d75 100644 --- a/src/mir_app/src/miranda.cpp +++ b/src/mir_app/src/miranda.cpp @@ -24,6 +24,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "stdafx.h" +#include "openssl.h" #if defined(VLD_ENABLED) #include "msapi\vld.h" @@ -325,6 +326,8 @@ int WINAPI mir_main(LPTSTR cmdLine) if (IsWinVer7Plus()) CoCreateInstance(CLSID_TaskbarList, nullptr, CLSCTX_ALL, IID_ITaskbarList3, (void**)&pTaskbarInterface); + OpenSSL_Init(); + int result = 0; if (LoadDefaultModules()) { g_bMirandaTerminated = true; @@ -403,6 +406,8 @@ int WINAPI mir_main(LPTSTR cmdLine) if (pTaskbarInterface) pTaskbarInterface->Release(); + OpenSSL_Cleanup(); + OleUninitialize(); if (bufferedPaintUninit) diff --git a/src/mir_app/src/openssl.cpp b/src/mir_app/src/openssl.cpp new file mode 100644 index 0000000000..2a41e01437 --- /dev/null +++ b/src/mir_app/src/openssl.cpp @@ -0,0 +1,67 @@ +/* + +Miranda NG: the free IM client for Microsoft* Windows* + +Copyright (C) 2012-19 Miranda NG team (https://miranda-ng.org), +Copyright (c) 2000-12 Miranda IM project, +all portions of this codebase are copyrighted to the people +listed in contributors.txt. + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#include "stdafx.h" +#include "openssl.h" + + +static HANDLE *mutexes; + +unsigned long OpenSSL_thread_id() +{ + return static_cast(GetCurrentThreadId()); +} + +void OpenSSL_lock(int mode, int n, const char *, int) +{ + if (mode & CRYPTO_LOCK) { + WaitForSingleObject(mutexes[n], INFINITE); + } else { + ReleaseMutex(mutexes[n]); + } +} + +void OpenSSL_Init() { + int num_locks = CRYPTO_num_locks(); + + mutexes = new HANDLE[num_locks]; + + for (int i = 0; i < num_locks; ++i) { + mutexes[i] = CreateMutex(nullptr, FALSE, nullptr); + } + + CRYPTO_set_id_callback(OpenSSL_thread_id); + CRYPTO_set_locking_callback(OpenSSL_lock); +} + +void OpenSSL_Cleanup(){ + CRYPTO_set_id_callback(nullptr); + CRYPTO_set_locking_callback(nullptr); + + for (int i = 0; i < CRYPTO_num_locks(); ++i) { + CloseHandle(mutexes[i]); + } + + delete[] mutexes; +} \ No newline at end of file diff --git a/src/mir_app/src/openssl.h b/src/mir_app/src/openssl.h new file mode 100644 index 0000000000..8791ce28d0 --- /dev/null +++ b/src/mir_app/src/openssl.h @@ -0,0 +1,30 @@ +/* + +Miranda NG: the free IM client for Microsoft* Windows* + +Copyright (C) 2012-19 Miranda NG team (https://miranda-ng.org), +Copyright (c) 2000-12 Miranda IM project, +all portions of this codebase are copyrighted to the people +listed in contributors.txt. + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#pragma once + +#include + +void OpenSSL_Init(); +void OpenSSL_Cleanup(); -- cgit v1.2.3