22#include <freerdp/config.h>
24#include <winpr/windows.h>
25#include <winpr/library.h>
28#include <winpr/assert.h>
35#include <winpr/assert.h>
39#ifdef WITH_PROGRESS_BAR
43#ifdef WITH_WINDOWS_CERT_STORE
47#include <freerdp/log.h>
48#include <freerdp/freerdp.h>
49#include <freerdp/constants.h>
50#include <freerdp/settings.h>
52#include <freerdp/locale/locale.h>
53#include <freerdp/locale/keyboard.h>
54#include <freerdp/codec/region.h>
55#include <freerdp/client/cmdline.h>
56#include <freerdp/client/channels.h>
57#include <freerdp/channels/channels.h>
58#include <freerdp/utils/signal.h>
62#include "wf_channels.h"
63#include "wf_graphics.h"
65#include "resource/resource.h"
67#define TAG CLIENT_TAG("windows")
69#define WM_FREERDP_SHOWWINDOW (WM_USER + 100)
71static BOOL wf_has_console(
void)
73#ifdef WITH_WIN_CONSOLE
74 int file = _fileno(stdin);
75 int tty = _isatty(file);
76 DWORD processes[2] = WINPR_C_ARRAY_INIT;
77 const DWORD count = GetConsoleProcessList(processes, ARRAYSIZE(processes));
78 const BOOL inherited = (count > 1);
82 BOOL inherited = FALSE;
85 WLog_INFO(TAG,
"Detected stdin=%d -> %s mode", file, (tty && inherited) ?
"console" :
"gui");
86 return tty && inherited;
89static BOOL wf_end_paint(rdpContext* context)
91 RECT updateRect = WINPR_C_ARRAY_INIT;
92 REGION16 invalidRegion = WINPR_C_ARRAY_INIT;
96 WINPR_ASSERT(context);
98 wfContext* wfc = (wfContext*)context;
99 rdpGdi* gdi = context->gdi;
102 HGDI_DC hdc = gdi->primary->hdc;
108 WINPR_ASSERT(hwnd->invalid || (hwnd->ninvalid == 0));
110 if (hwnd->invalid->null)
113 const int ninvalid = hwnd->ninvalid;
114 const HGDI_RGN cinvalid = hwnd->cinvalid;
119 region16_init(&invalidRegion);
121 for (
int i = 0; i < ninvalid; i++)
123 invalidRect.left = cinvalid[i].x;
124 invalidRect.top = cinvalid[i].y;
125 invalidRect.right = cinvalid[i].x + cinvalid[i].w;
126 invalidRect.bottom = cinvalid[i].y + cinvalid[i].h;
127 region16_union_rect(&invalidRegion, &invalidRegion, &invalidRect);
130 if (!region16_is_empty(&invalidRegion))
132 extents = region16_extents(&invalidRegion);
133 updateRect.left = extents->left;
134 updateRect.top = extents->top;
135 updateRect.right = extents->right;
136 updateRect.bottom = extents->bottom;
138 wf_scale_rect(wfc, &updateRect);
140 InvalidateRect(wfc->hwnd, &updateRect, FALSE);
143 wf_rail_invalidate_region(wfc, &invalidRegion);
146 region16_uninit(&invalidRegion);
150 wfc->is_shown = TRUE;
152#ifdef WITH_PROGRESS_BAR
153 if (wfc->taskBarList)
155 wfc->taskBarList->lpVtbl->SetProgressState(wfc->taskBarList, wfc->hwnd,
160 PostMessage(wfc->hwnd, WM_FREERDP_SHOWWINDOW, 0, 0);
161 WLog_INFO(TAG,
"Window is shown!");
166static BOOL wf_begin_paint(rdpContext* context)
170 if (!context || !context->gdi || !context->gdi->primary || !context->gdi->primary->hdc)
173 hdc = context->gdi->primary->hdc;
175 if (!hdc || !hdc->hwnd || !hdc->hwnd->invalid)
178 hdc->hwnd->invalid->null = TRUE;
179 hdc->hwnd->ninvalid = 0;
183static BOOL wf_desktop_resize(rdpContext* context)
187 rdpSettings* settings;
188 wfContext* wfc = (wfContext*)context;
190 if (!context || !context->settings)
193 settings = context->settings;
197 same = (wfc->primary == wfc->drawing) ? TRUE : FALSE;
198 wf_image_free(wfc->primary);
202 context->gdi->dstFormat,
nullptr);
207 context->gdi->dstFormat, wfc->primary->pdata,
nullptr))
211 wfc->drawing = wfc->primary;
213 if (wfc->fullscreen != TRUE)
216 SetWindowPos(wfc->hwnd, HWND_TOP, -1, -1,
223 wf_update_offset(wfc);
224 GetWindowRect(wfc->hwnd, &rect);
225 InvalidateRect(wfc->hwnd, &rect, TRUE);
231static BOOL wf_pre_connect(freerdp* instance)
233 WINPR_ASSERT(instance);
234 WINPR_ASSERT(instance->context);
235 WINPR_ASSERT(instance->context->settings);
237 rdpContext* context = instance->context;
238 wfContext* wfc = (wfContext*)instance->context;
239 rdpSettings* settings = context->settings;
249 if (wfc->percentscreen > 0)
251 desktopWidth = (GetSystemMetrics(SM_CXSCREEN) * wfc->percentscreen) / 100;
254 desktopHeight = (GetSystemMetrics(SM_CYSCREEN) * wfc->percentscreen) / 100;
263 desktopWidth = GetSystemMetrics(SM_CXVIRTUALSCREEN);
264 desktopHeight = GetSystemMetrics(SM_CYVIRTUALSCREEN);
268 desktopWidth = GetSystemMetrics(SM_CXSCREEN);
269 desktopHeight = GetSystemMetrics(SM_CYSCREEN);
275 desktopWidth = (desktopWidth + 3) & (~3);
292 CHAR name[KL_NAMELENGTH + 1] = WINPR_C_ARRAY_INIT;
293 if (GetKeyboardLayoutNameA(name))
298 rc = strtoul(name,
nullptr, 16);
300 keyboardLayoutId = rc;
303 if (keyboardLayoutId == 0)
305 const HKL layout = GetKeyboardLayout(0);
306 const uint32_t masked = (uint32_t)(((uintptr_t)layout >> 16) & 0xFFFF);
307 keyboardLayoutId = masked;
311 if (keyboardLayoutId == 0)
312 freerdp_detect_keyboard_layout_from_system_locale(&keyboardLayoutId);
313 if (keyboardLayoutId == 0)
314 keyboardLayoutId = ENGLISH_UNITED_STATES;
317 PubSub_SubscribeChannelConnected(instance->context->pubSub, wf_OnChannelConnectedEventHandler);
318 PubSub_SubscribeChannelDisconnected(instance->context->pubSub,
319 wf_OnChannelDisconnectedEventHandler);
323static void wf_append_item_to_system_menu(HMENU hMenu, UINT fMask, UINT wID,
const wchar_t* text,
326 MENUITEMINFO item_info = WINPR_C_ARRAY_INIT;
327 item_info.fMask = fMask;
328 item_info.cbSize =
sizeof(MENUITEMINFO);
330 item_info.fType = MFT_STRING;
331 item_info.dwTypeData = _wcsdup(text);
332 item_info.cch = (UINT)_wcslen(text);
334 item_info.dwItemData = (ULONG_PTR)wfc;
335 InsertMenuItem(hMenu, wfc->systemMenuInsertPosition++, TRUE, &item_info);
338static void wf_add_system_menu(wfContext* wfc)
342 if (wfc->fullscreen && !wfc->fullscreen_toggle)
352 hMenu = GetSystemMenu(wfc->hwnd, FALSE);
354 wf_append_item_to_system_menu(hMenu,
355 MIIM_CHECKMARKS | MIIM_FTYPE | MIIM_ID | MIIM_STRING | MIIM_DATA,
356 SYSCOMMAND_ID_SMARTSIZING, L
"Smart sizing", wfc);
360 CheckMenuItem(hMenu, SYSCOMMAND_ID_SMARTSIZING, MF_CHECKED);
364 wf_append_item_to_system_menu(hMenu, MIIM_FTYPE | MIIM_ID | MIIM_STRING,
365 SYSCOMMAND_ID_REQUEST_CONTROL, L
"Request control", wfc);
368static WCHAR* wf_window_get_title(rdpSettings* settings)
371 WCHAR* windowTitle =
nullptr;
373 WCHAR prefix[] = L
"FreeRDP:";
385 size = strlen(name) + 16 + wcslen(prefix);
386 windowTitle = calloc(size,
sizeof(WCHAR));
392 _snwprintf_s(windowTitle, size, _TRUNCATE, L
"%s %S", prefix, name);
394 _snwprintf_s(windowTitle, size, _TRUNCATE, L
"%s %S:%u", prefix, name,
400static BOOL wf_post_connect(freerdp* instance)
407 rdpSettings* settings;
408 EmbedWindowEventArgs e;
409 const UINT32 format = PIXEL_FORMAT_BGRX32;
411 WINPR_ASSERT(instance);
413 context = instance->context;
414 WINPR_ASSERT(context);
416 settings = context->settings;
417 WINPR_ASSERT(settings);
419 wfc = (wfContext*)instance->context;
428 WLog_ERR(TAG,
"Failed to allocate primary surface");
432 if (!gdi_init_ex(instance, format, 0, wfc->primary->pdata,
nullptr))
435 cache = instance->context->cache;
438 gdi = instance->context->gdi;
442 wf_gdi_register_update_callbacks(context->update);
445 wfc->window_title = wf_window_get_title(settings);
447 if (!wfc->window_title)
459 dwStyle = WS_CHILD | WS_BORDER;
462 WS_CAPTION | WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX | WS_SIZEBOX | WS_MAXIMIZEBOX;
466 wfc->hwnd = CreateWindowEx(0, wfc->wndClassName, wfc->window_title, dwStyle, 0, 0, 0, 0,
467 wfc->hWndParent,
nullptr, wfc->hInstance,
nullptr);
470 WLog_ERR(TAG,
"CreateWindowEx failed with error: %lu", GetLastError());
473 SetWindowLongPtr(wfc->hwnd, GWLP_USERDATA, (LONG_PTR)wfc);
476 wf_resize_window(wfc);
477 wf_add_system_menu(wfc);
480 wfc->drawing = wfc->primary;
481 EventArgsInit(&e,
"wfreerdp");
483 e.handle = (
void*)wfc->hwnd;
484 if (PubSub_OnEmbedWindow(context->pubSub, context, &e) < 0)
487#ifdef WITH_PROGRESS_BAR
488 if (wfc->taskBarList)
490 ShowWindow(wfc->hwnd, SW_SHOWMINIMIZED);
491 wfc->taskBarList->lpVtbl->SetProgressState(wfc->taskBarList, wfc->hwnd, TBPF_INDETERMINATE);
494 UpdateWindow(wfc->hwnd);
495 context->update->BeginPaint = wf_begin_paint;
496 context->update->DesktopResize = wf_desktop_resize;
497 context->update->EndPaint = wf_end_paint;
498 context->update->SetKeyboardIndicators = wf_keyboard_set_indicators;
499 wf_register_pointer(context->graphics);
501 wfc->floatbar = wf_floatbar_new(wfc, wfc->hInstance,
504 wf_event_focus_in(wfc);
509static void wf_post_disconnect(freerdp* instance)
513 if (!instance || !instance->context)
516 wfc = (wfContext*)instance->context;
517 free(wfc->window_title);
520static CREDUI_INFOW wfUiInfo = {
sizeof(CREDUI_INFOW),
nullptr, L
"Enter your credentials",
521 L
"Remote Desktop Security",
nullptr };
523static BOOL wf_authenticate_ex(freerdp* instance,
char** username,
char** password,
char** domain,
524 rdp_auth_reason reason)
530 WCHAR UserNameW[CREDUI_MAX_USERNAME_LENGTH + 1] = WINPR_C_ARRAY_INIT;
531 WCHAR UserW[CREDUI_MAX_USERNAME_LENGTH + 1] = WINPR_C_ARRAY_INIT;
532 WCHAR DomainW[CREDUI_MAX_DOMAIN_TARGET_LENGTH + 1] = WINPR_C_ARRAY_INIT;
533 WCHAR PasswordW[CREDUI_MAX_PASSWORD_LENGTH + 1] = WINPR_C_ARRAY_INIT;
535 WINPR_ASSERT(instance);
536 WINPR_ASSERT(instance->context);
537 WINPR_ASSERT(instance->context->settings);
539 wfc = (wfContext*)instance->context;
542 WINPR_ASSERT(username);
543 WINPR_ASSERT(domain);
544 WINPR_ASSERT(password);
546 const WCHAR auth[] = L
"Target credentials requested";
547 const WCHAR authPin[] = L
"PIN requested";
548 const WCHAR gwAuth[] = L
"Gateway credentials requested";
549 const WCHAR* titleW = auth;
552 dwFlags = CREDUI_FLAGS_DO_NOT_PERSIST | CREDUI_FLAGS_EXCLUDE_CERTIFICATES |
553 CREDUI_FLAGS_USERNAME_TARGET_CREDENTIALS;
560 if ((*username) && (*password))
563 case AUTH_SMARTCARD_PIN:
564 dwFlags &= ~CREDUI_FLAGS_USERNAME_TARGET_CREDENTIALS;
565 dwFlags |= CREDUI_FLAGS_PASSWORD_ONLY_OK | CREDUI_FLAGS_KEEP_USERNAME;
570 *username = _strdup(
"PIN");
583 (void)ConvertUtf8ToWChar(*username, UserNameW, ARRAYSIZE(UserNameW));
584 (void)ConvertUtf8ToWChar(*username, UserW, ARRAYSIZE(UserW));
588 (void)ConvertUtf8ToWChar(*password, PasswordW, ARRAYSIZE(PasswordW));
591 (void)ConvertUtf8ToWChar(*domain, DomainW, ARRAYSIZE(DomainW));
593 if (_wcsnlen(PasswordW, ARRAYSIZE(PasswordW)) == 0)
595 if (!wfc->isConsole &&
597 WLog_ERR(TAG,
"Flag for stdin read present but stdin is redirected; using GUI");
598 if (wfc->isConsole &&
600 status = CredUICmdLinePromptForCredentialsW(titleW,
nullptr, 0, UserNameW,
601 ARRAYSIZE(UserNameW), PasswordW,
602 ARRAYSIZE(PasswordW), &fSave, dwFlags);
604 status = CredUIPromptForCredentialsW(&wfUiInfo, titleW,
nullptr, 0, UserNameW,
605 ARRAYSIZE(UserNameW), PasswordW,
606 ARRAYSIZE(PasswordW), &fSave, dwFlags);
607 if (status != NO_ERROR)
609 WLog_ERR(TAG,
"CredUIPromptForCredentials unexpected status: 0x%08lX", status);
613 if ((dwFlags & CREDUI_FLAGS_KEEP_USERNAME) == 0)
615 status = CredUIParseUserNameW(UserNameW, UserW, ARRAYSIZE(UserW), DomainW,
617 if (status != NO_ERROR)
619 CHAR User[CREDUI_MAX_USERNAME_LENGTH + 1] = WINPR_C_ARRAY_INIT;
620 CHAR UserName[CREDUI_MAX_USERNAME_LENGTH + 1] = WINPR_C_ARRAY_INIT;
621 CHAR Domain[CREDUI_MAX_DOMAIN_TARGET_LENGTH + 1] = WINPR_C_ARRAY_INIT;
623 (void)ConvertWCharNToUtf8(UserNameW, ARRAYSIZE(UserNameW), UserName,
624 ARRAYSIZE(UserName));
625 (void)ConvertWCharNToUtf8(UserW, ARRAYSIZE(UserW), User, ARRAYSIZE(User));
626 (void)ConvertWCharNToUtf8(DomainW, ARRAYSIZE(DomainW), Domain, ARRAYSIZE(Domain));
627 WLog_ERR(TAG,
"Failed to parse UserName: %s into User: %s Domain: %s", UserName,
634 *username = ConvertWCharNToUtf8Alloc(UserW, ARRAYSIZE(UserW),
nullptr);
637 WLog_ERR(TAG,
"ConvertWCharNToUtf8Alloc failed", status);
641 if (_wcsnlen(DomainW, ARRAYSIZE(DomainW)) > 0)
642 *domain = ConvertWCharNToUtf8Alloc(DomainW, ARRAYSIZE(DomainW),
nullptr);
644 *domain = _strdup(
"\0");
649 WLog_ERR(TAG,
"strdup failed", status);
653 *password = ConvertWCharNToUtf8Alloc(PasswordW, ARRAYSIZE(PasswordW),
nullptr);
664static WCHAR* wf_format_text(
const WCHAR* fmt, ...)
668 WCHAR* buffer =
nullptr;
672 WCHAR* tmp =
nullptr;
673 va_list ap = WINPR_C_ARRAY_INIT;
675 rc = _vsnwprintf(buffer, size, fmt, ap);
680 if ((
size_t)rc < size)
683 size = (size_t)rc + 1;
684 tmp = realloc(buffer, size *
sizeof(WCHAR));
696#ifdef WITH_WINDOWS_CERT_STORE
704static void wf_report_error(
char* wszMessage, DWORD dwErrCode)
706 LPSTR pwszMsgBuf =
nullptr;
708 if (
nullptr != wszMessage && 0 != *wszMessage)
710 WLog_ERR(TAG,
"%s", wszMessage);
713 FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
718 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
728 if (
nullptr != pwszMsgBuf)
730 WLog_ERR(TAG,
"Error: 0x%08x (%d) %s", dwErrCode, dwErrCode, pwszMsgBuf);
731 LocalFree(pwszMsgBuf);
735 WLog_ERR(TAG,
"Error: 0x%08x (%d)", dwErrCode, dwErrCode);
739static DWORD wf_is_x509_certificate_trusted(
const char* common_name,
const char* subject,
740 const char* issuer,
const char* fingerprint)
742 HRESULT hr = CRYPT_E_NOT_FOUND;
744 DWORD dwChainFlags = CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT;
746 HCERTCHAINENGINE hChainEngine =
nullptr;
747 PCCERT_CHAIN_CONTEXT pChainContext =
nullptr;
749 CERT_ENHKEY_USAGE EnhkeyUsage = WINPR_C_ARRAY_INIT;
750 CERT_USAGE_MATCH CertUsage = WINPR_C_ARRAY_INIT;
751 CERT_CHAIN_PARA ChainPara = WINPR_C_ARRAY_INIT;
752 CERT_CHAIN_POLICY_PARA ChainPolicy = WINPR_C_ARRAY_INIT;
753 CERT_CHAIN_POLICY_STATUS PolicyStatus = WINPR_C_ARRAY_INIT;
754 CERT_CHAIN_ENGINE_CONFIG EngineConfig = WINPR_C_ARRAY_INIT;
756 DWORD derPubKeyLen = WINPR_ASSERTING_INT_CAST(uint32_t, strlen(fingerprint));
757 char* derPubKey = calloc(derPubKeyLen,
sizeof(
char));
758 if (
nullptr == derPubKey)
760 WLog_ERR(TAG,
"Could not allocate derPubKey");
767 if (!CryptStringToBinaryA(fingerprint, 0, CRYPT_STRING_BASE64HEADER, derPubKey, &derPubKeyLen,
770 WLog_ERR(TAG,
"CryptStringToBinary failed. Err: %d", GetLastError());
777 EnhkeyUsage.cUsageIdentifier = 0;
778 EnhkeyUsage.rgpszUsageIdentifier =
nullptr;
780 CertUsage.dwType = USAGE_MATCH_TYPE_AND;
781 CertUsage.Usage = EnhkeyUsage;
783 ChainPara.cbSize =
sizeof(ChainPara);
784 ChainPara.RequestedUsage = CertUsage;
786 ChainPolicy.cbSize =
sizeof(ChainPolicy);
788 PolicyStatus.cbSize =
sizeof(PolicyStatus);
790 EngineConfig.cbSize =
sizeof(EngineConfig);
791 EngineConfig.dwUrlRetrievalTimeout = 0;
793 pCert = CertCreateCertificateContext(X509_ASN_ENCODING, derPubKey, derPubKeyLen);
794 if (
nullptr == pCert)
796 WLog_ERR(TAG,
"FAILED: Certificate could not be parsed.");
800 dwChainFlags |= CERT_CHAIN_ENABLE_PEER_TRUST;
809 if (!CertCreateCertificateChainEngine(&EngineConfig, &hChainEngine))
811 hr = HRESULT_FROM_WIN32(GetLastError());
818 if (!CertGetCertificateChain(hChainEngine,
829 hr = HRESULT_FROM_WIN32(GetLastError());
836 if (!CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_BASE,
841 hr = HRESULT_FROM_WIN32(GetLastError());
845 if (PolicyStatus.dwError != S_OK)
847 wf_report_error(
"CertVerifyCertificateChainPolicy: Chain Status", PolicyStatus.dwError);
848 hr = PolicyStatus.dwError;
854 if (PolicyStatus.dwError == CRYPT_E_NO_REVOCATION_CHECK ||
855 PolicyStatus.dwError == CRYPT_E_REVOCATION_OFFLINE)
863 WLog_INFO(TAG,
"CertVerifyCertificateChainPolicy succeeded for %s (%s) issued by %s",
864 common_name, subject, issuer);
871 WLog_INFO(TAG,
"CertVerifyCertificateChainPolicy failed for %s (%s) issued by %s",
872 common_name, subject, issuer);
873 wf_report_error(
nullptr, hr);
878 if (
nullptr != pChainContext)
880 CertFreeCertificateChain(pChainContext);
883 if (
nullptr != hChainEngine)
885 CertFreeCertificateChainEngine(hChainEngine);
888 if (
nullptr != pCert)
890 CertFreeCertificateContext(pCert);
897static DWORD wf_cli_verify_certificate_ex(freerdp* instance,
const char* host, UINT16 port,
898 const char* common_name,
const char* subject,
899 const char* issuer,
const char* fingerprint, DWORD flags)
901#ifdef WITH_WINDOWS_CERT_STORE
902 if (flags & VERIFY_CERT_FLAG_FP_IS_PEM && !(flags & VERIFY_CERT_FLAG_MISMATCH))
904 if (wf_is_x509_certificate_trusted(common_name, subject, issuer, fingerprint) == S_OK)
911 return client_cli_verify_certificate_ex(instance, host, port, common_name, subject, issuer,
915static DWORD wf_verify_certificate_ex(freerdp* instance,
const char* host, UINT16 port,
916 const char* common_name,
const char* subject,
917 const char* issuer,
const char* fingerprint, DWORD flags)
923#ifdef WITH_WINDOWS_CERT_STORE
924 if (flags & VERIFY_CERT_FLAG_FP_IS_PEM && !(flags & VERIFY_CERT_FLAG_MISMATCH))
926 if (wf_is_x509_certificate_trusted(common_name, subject, issuer, fingerprint) == S_OK)
933 buffer = wf_format_text(
934 L
"Certificate details:\n"
935 L
"\tCommonName: %S\n"
938 L
"\tThumbprint: %S\n"
939 L
"\tHostMismatch: %S\n"
941 L
"The above X.509 certificate could not be verified, possibly because you do not have "
942 L
"the CA certificate in your certificate store, or the certificate has expired. "
943 L
"Please look at the OpenSSL documentation on how to add a private CA to the store.\n"
945 L
"YES\tAccept permanently\n"
946 L
"NO\tAccept for this session only\n"
947 L
"CANCEL\tAbort connection\n",
948 common_name, subject, issuer, fingerprint,
949 flags & VERIFY_CERT_FLAG_MISMATCH ?
"Yes" :
"No");
950 caption = wf_format_text(L
"Verify certificate for %S:%hu", host, port);
952 WINPR_UNUSED(instance);
954 if (!buffer || !caption)
957 what = MessageBoxW(
nullptr, buffer, caption, MB_YESNOCANCEL);
975static DWORD wf_verify_changed_certificate_ex(freerdp* instance,
const char* host, UINT16 port,
976 const char* common_name,
const char* subject,
977 const char* issuer,
const char* new_fingerprint,
978 const char* old_subject,
const char* old_issuer,
979 const char* old_fingerprint, DWORD flags)
985 buffer = wf_format_text(
986 L
"New Certificate details:\n"
987 L
"\tCommonName: %S\n"
990 L
"\tThumbprint: %S\n"
991 L
"\tHostMismatch: %S\n"
993 L
"Old Certificate details:\n"
997 L
"The above X.509 certificate could not be verified, possibly because you do not have "
998 L
"the CA certificate in your certificate store, or the certificate has expired. "
999 L
"Please look at the OpenSSL documentation on how to add a private CA to the store.\n"
1001 L
"YES\tAccept permanently\n"
1002 L
"NO\tAccept for this session only\n"
1003 L
"CANCEL\tAbort connection\n",
1004 common_name, subject, issuer, new_fingerprint,
1005 flags & VERIFY_CERT_FLAG_MISMATCH ?
"Yes" :
"No", old_subject, old_issuer, old_fingerprint);
1006 caption = wf_format_text(L
"Verify certificate change for %S:%hu", host, port);
1008 WINPR_UNUSED(instance);
1009 if (!buffer || !caption)
1012 what = MessageBoxW(
nullptr, buffer, caption, MB_YESNOCANCEL);
1030static BOOL wf_present_gateway_message(freerdp* instance, UINT32 type, BOOL isDisplayMandatory,
1031 BOOL isConsentMandatory,
size_t length,
const WCHAR* message)
1033 if (!isDisplayMandatory && !isConsentMandatory)
1037 if (type == GATEWAY_MESSAGE_CONSENT && isConsentMandatory)
1042 msg = wf_format_text(L
"%.*s\n\nI understand and agree to the terms of this policy", length,
1044 mbRes = MessageBoxW(
nullptr, msg, L
"Consent Message", MB_YESNO);
1051 return client_cli_present_gateway_message(instance, type, isDisplayMandatory,
1052 isConsentMandatory, length, message);
1057static DWORD WINAPI wf_client_thread(LPVOID lpParam)
1059 MSG msg = WINPR_C_ARRAY_INIT;
1062 BOOL msg_ret = FALSE;
1066 freerdp* instance = (freerdp*)lpParam;
1067 WINPR_ASSERT(instance);
1069 if (!freerdp_connect(instance))
1072 rdpContext* context = instance->context;
1073 WINPR_ASSERT(context);
1075 wfContext* wfc = (wfContext*)instance->context;
1078 rdpChannels* channels = context->channels;
1079 WINPR_ASSERT(channels);
1081 rdpSettings* settings = context->settings;
1082 WINPR_ASSERT(settings);
1084 while (!freerdp_shall_disconnect_context(instance->context))
1086 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = WINPR_C_ARRAY_INIT;
1089 if (freerdp_focus_required(instance))
1091 wf_event_focus_in(wfc);
1092 wf_event_focus_in(wfc);
1096 DWORD tmp = freerdp_get_event_handles(context, &handles[nCount], 64 - nCount);
1100 WLog_ERR(TAG,
"freerdp_get_event_handles failed");
1107 DWORD status = MsgWaitForMultipleObjectsEx(nCount, handles, 5 * 1000, QS_ALLINPUT,
1108 MWMO_ALERTABLE | MWMO_INPUTAVAILABLE);
1109 if (status == WAIT_FAILED)
1111 WLog_ERR(TAG,
"wfreerdp_run: WaitForMultipleObjects failed: 0x%08lX", GetLastError());
1116 if (!freerdp_check_event_handles(context))
1118 if (client_auto_reconnect(instance))
1121 WLog_ERR(TAG,
"Failed to check FreeRDP file descriptor");
1126 if (freerdp_shall_disconnect_context(instance->context))
1131 while (PeekMessage(&msg,
nullptr, 0, 0, PM_NOREMOVE))
1133 msg_ret = GetMessage(&msg,
nullptr, 0, 0);
1137 if ((msg.message == WM_SETFOCUS) && (msg.lParam == 1))
1139 PostMessage(wfc->hwnd, WM_SETFOCUS, 0, 0);
1141 else if ((msg.message == WM_KILLFOCUS) && (msg.lParam == 1))
1143 PostMessage(wfc->hwnd, WM_KILLFOCUS, 0, 0);
1147 switch (msg.message)
1151 width = LOWORD(msg.lParam);
1152 height = HIWORD(msg.lParam);
1153 SetWindowPos(wfc->hwnd, HWND_TOP, 0, 0, width, height, SWP_FRAMECHANGED);
1156 case WM_FREERDP_SHOWWINDOW:
1158 ShowWindow(wfc->hwnd, SW_NORMAL);
1161 wf_floatbar_reset_position(wfc->floatbar);
1169 if ((msg_ret == 0) || (msg_ret == -1))
1175 TranslateMessage(&msg);
1176 DispatchMessage(&msg);
1184 freerdp_disconnect(instance);
1187 error = freerdp_get_last_error(instance->context);
1188 WLog_DBG(TAG,
"Main thread exited with %" PRIu32, error);
1193static DWORD WINAPI wf_keyboard_thread(LPVOID lpParam)
1199 wfc = (wfContext*)lpParam;
1200 WINPR_ASSERT(
nullptr != wfc);
1201 hook_handle = SetWindowsHookEx(WH_KEYBOARD_LL, wf_ll_kbd_proc, wfc->hInstance, 0);
1205 while ((status = GetMessage(&msg,
nullptr, 0, 0)) != 0)
1209 WLog_ERR(TAG,
"keyboard thread error getting message");
1214 TranslateMessage(&msg);
1215 DispatchMessage(&msg);
1219 UnhookWindowsHookEx(hook_handle);
1223 WLog_ERR(TAG,
"failed to install keyboard hook");
1226 WLog_DBG(TAG,
"Keyboard thread exited.");
1231int freerdp_client_set_window_size(wfContext* wfc,
int width,
int height)
1233 WLog_DBG(TAG,
"freerdp_client_set_window_size %d, %d", width, height);
1235 if ((width != wfc->client_width) || (height != wfc->client_height))
1237 PostThreadMessage(wfc->mainThreadId, WM_SIZE, SIZE_RESTORED,
1238 ((UINT)height << 16) | (UINT)width);
1244void wf_size_scrollbars(wfContext* wfc, UINT32 client_width, UINT32 client_height)
1246 const rdpSettings* settings;
1249 settings = wfc->common.context.settings;
1250 WINPR_ASSERT(settings);
1252 if (wfc->disablewindowtracking)
1256 wfc->disablewindowtracking = TRUE;
1261 wfc->xCurrentScroll = 0;
1262 wfc->yCurrentScroll = 0;
1264 if (wfc->xScrollVisible || wfc->yScrollVisible)
1266 if (ShowScrollBar(wfc->hwnd, SB_BOTH, FALSE))
1268 wfc->xScrollVisible = FALSE;
1269 wfc->yScrollVisible = FALSE;
1276 BOOL horiz = wfc->xScrollVisible;
1277 BOOL vert = wfc->yScrollVisible;
1286 settings, FreeRDP_DesktopWidth) )
1298 settings, FreeRDP_DesktopHeight) )
1303 if (horiz == vert && (horiz != wfc->xScrollVisible && vert != wfc->yScrollVisible))
1305 if (ShowScrollBar(wfc->hwnd, SB_BOTH, horiz))
1307 wfc->xScrollVisible = horiz;
1308 wfc->yScrollVisible = vert;
1312 if (horiz != wfc->xScrollVisible)
1314 if (ShowScrollBar(wfc->hwnd, SB_HORZ, horiz))
1316 wfc->xScrollVisible = horiz;
1320 if (vert != wfc->yScrollVisible)
1322 if (ShowScrollBar(wfc->hwnd, SB_VERT, vert))
1324 wfc->yScrollVisible = vert;
1335 wfc->xCurrentScroll = MIN(wfc->xCurrentScroll, wfc->xMaxScroll);
1336 si.cbSize =
sizeof(si);
1337 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
1338 si.nMin = wfc->xMinScroll;
1340 si.nPage = client_width;
1341 si.nPos = wfc->xCurrentScroll;
1342 SetScrollInfo(wfc->hwnd, SB_HORZ, &si, TRUE);
1350 wfc->yMaxScroll = MAX(
1352 wfc->yCurrentScroll = MIN(wfc->yCurrentScroll, wfc->yMaxScroll);
1353 si.cbSize =
sizeof(si);
1354 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
1355 si.nMin = wfc->yMinScroll;
1357 si.nPage = client_height;
1358 si.nPos = wfc->yCurrentScroll;
1359 SetScrollInfo(wfc->hwnd, SB_VERT, &si, TRUE);
1363 wfc->disablewindowtracking = FALSE;
1364 wf_update_canvas_diff(wfc);
1367static BOOL wfreerdp_client_global_init(
void)
1371 WSAStartup(0x101, &wsaData);
1372 if (freerdp_handle_signals() != 0)
1375 if (freerdp_register_addin_provider(freerdp_channels_load_static_addin_entry, 0) !=
1382static void wfreerdp_client_global_uninit(
void)
1387static BOOL wfreerdp_client_new(freerdp* instance, rdpContext* context)
1389 wfContext* wfc = (wfContext*)context;
1395 wfc->isConsole = wf_has_console();
1397#ifdef WITH_WIN_CONSOLE
1398 if (!wfc->isConsole)
1400 HWND hwndConsole = GetConsoleWindow();
1402 ShowWindow(hwndConsole, SW_HIDE);
1403 (void)FreeConsole();
1407 if (!(wfreerdp_client_global_init()))
1410 WINPR_ASSERT(instance);
1411 instance->PreConnect = wf_pre_connect;
1412 instance->PostConnect = wf_post_connect;
1413 instance->PostDisconnect = wf_post_disconnect;
1414 instance->AuthenticateEx = wf_authenticate_ex;
1416#ifdef WITH_WINDOWS_CERT_STORE
1421 if (!wfc->isConsole)
1423 instance->VerifyCertificateEx = wf_verify_certificate_ex;
1424 instance->VerifyChangedCertificateEx = wf_verify_changed_certificate_ex;
1425 instance->PresentGatewayMessage = wf_present_gateway_message;
1428#ifdef WITH_PROGRESS_BAR
1429 CoInitializeEx(
nullptr, COINIT_APARTMENTTHREADED);
1430 CoCreateInstance(&CLSID_TaskbarList,
nullptr, CLSCTX_ALL, &IID_ITaskbarList3,
1431 (
void**)&wfc->taskBarList);
1437static void wfreerdp_client_free(freerdp* instance, rdpContext* context)
1439 WINPR_UNUSED(instance);
1443#ifdef WITH_PROGRESS_BAR
1448static int wfreerdp_client_start(rdpContext* context)
1450 wfContext* wfc = (wfContext*)context;
1452 WINPR_ASSERT(context);
1453 WINPR_ASSERT(context->settings);
1455 freerdp* instance = context->instance;
1456 WINPR_ASSERT(instance);
1458 rdpSettings* settings = context->settings;
1459 WINPR_ASSERT(settings);
1461 HINSTANCE hInstance = GetModuleHandle(
nullptr);
1466 wfc->hWndParent = hWndParent;
1470 typedef UINT(WINAPI * GetDpiForWindow_t)(HWND hwnd);
1471 typedef BOOL(WINAPI * SetProcessDPIAware_t)(void);
1473 HMODULE
module = GetModuleHandle(_T("User32"));
1476 GetDpiForWindow_t pGetDpiForWindow =
1477 GetProcAddressAs(module,
"GetDpiForWindow", GetDpiForWindow_t);
1478 SetProcessDPIAware_t pSetProcessDPIAware =
1479 GetProcAddressAs(module,
"SetProcessDPIAware", SetProcessDPIAware_t);
1480 if (pGetDpiForWindow && pSetProcessDPIAware)
1482 const UINT dpiAwareness = pGetDpiForWindow(hWndParent);
1483 if (dpiAwareness != USER_DEFAULT_SCREEN_DPI)
1484 pSetProcessDPIAware();
1486 FreeLibrary(module);
1494 wfc->systemMenuInsertPosition = 6;
1496 wfc->hInstance = hInstance;
1497 wfc->cursor = LoadCursor(
nullptr, IDC_ARROW);
1498 wfc->icon = LoadIcon(GetModuleHandle(
nullptr), MAKEINTRESOURCE(IDI_ICON1));
1499 wfc->wndClassName = _tcsdup(_T(
"FreeRDP"));
1500 wfc->wndClass.cbSize =
sizeof(WNDCLASSEX);
1501 wfc->wndClass.style = CS_HREDRAW | CS_VREDRAW;
1502 wfc->wndClass.lpfnWndProc = wf_event_proc;
1503 wfc->wndClass.cbClsExtra = 0;
1504 wfc->wndClass.cbWndExtra = 0;
1505 wfc->wndClass.hCursor =
nullptr;
1506 wfc->wndClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
1507 wfc->wndClass.lpszMenuName =
nullptr;
1508 wfc->wndClass.lpszClassName = wfc->wndClassName;
1509 wfc->wndClass.hInstance = hInstance;
1510 wfc->wndClass.hIcon = wfc->icon;
1511 wfc->wndClass.hIconSm = wfc->icon;
1512 RegisterClassEx(&(wfc->wndClass));
1513 wfc->keyboardThread =
1514 CreateThread(
nullptr, 0, wf_keyboard_thread, (
void*)wfc, 0, &wfc->keyboardThreadId);
1516 if (!wfc->keyboardThread)
1519 wfc->common.thread =
1520 CreateThread(
nullptr, 0, wf_client_thread, (
void*)instance, 0, &wfc->mainThreadId);
1522 if (!wfc->common.thread)
1528static int wfreerdp_client_stop(rdpContext* context)
1531 wfContext* wfc = (wfContext*)context;
1534 PostThreadMessage(wfc->mainThreadId, WM_QUIT, 0, 0);
1535 rc = freerdp_client_common_stop(context);
1536 wfc->mainThreadId = 0;
1538 if (wfc->keyboardThread)
1540 PostThreadMessage(wfc->keyboardThreadId, WM_QUIT, 0, 0);
1541 (void)WaitForSingleObject(wfc->keyboardThread, INFINITE);
1542 (void)CloseHandle(wfc->keyboardThread);
1543 wfc->keyboardThread =
nullptr;
1544 wfc->keyboardThreadId = 0;
1550int RdpClientEntry(RDP_CLIENT_ENTRY_POINTS* pEntryPoints)
1552 pEntryPoints->Version = 1;
1553 pEntryPoints->Size =
sizeof(RDP_CLIENT_ENTRY_POINTS_V1);
1554 pEntryPoints->GlobalInit = wfreerdp_client_global_init;
1555 pEntryPoints->GlobalUninit = wfreerdp_client_global_uninit;
1556 pEntryPoints->ContextSize =
sizeof(wfContext);
1557 pEntryPoints->ClientNew = wfreerdp_client_new;
1558 pEntryPoints->ClientFree = wfreerdp_client_free;
1559 pEntryPoints->ClientStart = wfreerdp_client_start;
1560 pEntryPoints->ClientStop = wfreerdp_client_stop;
WINPR_ATTR_NODISCARD FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_bool(rdpSettings *settings, FreeRDP_Settings_Keys_Bool id, BOOL val)
Sets a BOOL settings value.
WINPR_ATTR_NODISCARD FREERDP_API UINT64 freerdp_settings_get_uint64(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt64 id)
Returns a UINT64 settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 val)
Sets a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.