Skip to content

Commit 642c8e7

Browse files
committed
Merge pull request #4399
674c070 [Qt] seed OpenSSL PNRG with Windows event data (Philip Kaufmann)
2 parents ebb37a4 + 674c070 commit 642c8e7

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/qt/winshutdownmonitor.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66

77
#if defined(Q_OS_WIN) && QT_VERSION >= 0x050000
88
#include "init.h"
9+
#include "util.h"
910

1011
#include <windows.h>
1112

1213
#include <QDebug>
1314

15+
#include <openssl/rand.h>
16+
1417
// If we don't want a message to be processed by Qt, return true and set result to
1518
// the value that the window procedure should return. Otherwise return false.
1619
bool WinShutdownMonitor::nativeEventFilter(const QByteArray &eventType, void *pMessage, long *pnResult)
@@ -19,6 +22,16 @@ bool WinShutdownMonitor::nativeEventFilter(const QByteArray &eventType, void *pM
1922

2023
MSG *pMsg = static_cast<MSG *>(pMessage);
2124

25+
// Seed OpenSSL PRNG with Windows event data (e.g. mouse movements and other user interactions)
26+
if (RAND_event(pMsg->message, pMsg->wParam, pMsg->lParam) == 0) {
27+
// Warn only once as this is performance-critical
28+
static bool warned = false;
29+
if (!warned) {
30+
LogPrint("%s: OpenSSL RAND_event() failed to seed OpenSSL PRNG with enough data.\n", __func__);
31+
warned = true;
32+
}
33+
}
34+
2235
switch(pMsg->message)
2336
{
2437
case WM_QUERYENDSESSION:
@@ -45,13 +58,13 @@ void WinShutdownMonitor::registerShutdownBlockReason(const QString& strReason, c
4558
typedef BOOL (WINAPI *PSHUTDOWNBRCREATE)(HWND, LPCWSTR);
4659
PSHUTDOWNBRCREATE shutdownBRCreate = (PSHUTDOWNBRCREATE)GetProcAddress(GetModuleHandleA("User32.dll"), "ShutdownBlockReasonCreate");
4760
if (shutdownBRCreate == NULL) {
48-
qWarning() << "registerShutdownBlockReason : GetProcAddress for ShutdownBlockReasonCreate failed";
61+
qWarning() << "registerShutdownBlockReason: GetProcAddress for ShutdownBlockReasonCreate failed";
4962
return;
5063
}
5164

5265
if (shutdownBRCreate(mainWinId, strReason.toStdWString().c_str()))
53-
qWarning() << "registerShutdownBlockReason : Successfully registered: " + strReason;
66+
qWarning() << "registerShutdownBlockReason: Successfully registered: " + strReason;
5467
else
55-
qWarning() << "registerShutdownBlockReason : Failed to register: " + strReason;
68+
qWarning() << "registerShutdownBlockReason: Failed to register: " + strReason;
5669
}
5770
#endif

0 commit comments

Comments
 (0)