24#include <freerdp/config.h>
26#include <freerdp/build-config.h>
27#include <freerdp/peer.h>
28#include <freerdp/utils/helpers.h>
29#include <winpr/config-readers.h>
32#include <winpr/wtypes.h>
33#include <winpr/assert.h>
34#include <winpr/library.h>
35#if !defined(WITHOUT_FREERDP_3x_DEPRECATED)
36#include <winpr/registry.h>
38#include <winpr/sspi.h>
40#include <freerdp/log.h>
43#include "credssp_auth.h"
45#define TAG FREERDP_TAG("core.auth")
47#if !defined(WITHOUT_FREERDP_3x_DEPRECATED)
48#define SERVER_KEY "Software\\%s\\Server"
55 AUTH_STATE_IN_PROGRESS,
59struct rdp_credssp_auth
61 const rdpContext* rdp_ctx;
62 SecurityFunctionTable* table;
64 SEC_WINNT_AUTH_IDENTITY identity;
65 SEC_WINPR_NTLM_SETTINGS_V2* ntlmSettingsV2;
66 SEC_WINPR_KERBEROS_SETTINGS_V2* kerberosSettingsV2;
77 SECURITY_STATUS sspi_error;
78 enum AUTH_STATE state;
82static const char credssp_config_file_name[] =
"credssp.json";
84static const char* credssp_auth_state_string(
const rdpCredsspAuth* auth)
89 case AUTH_STATE_INITIAL:
90 return "AUTH_STATE_INITIAL";
91 case AUTH_STATE_CREDS:
92 return "AUTH_STATE_CREDS";
93 case AUTH_STATE_IN_PROGRESS:
94 return "AUTH_STATE_IN_PROGRESS";
95 case AUTH_STATE_FINAL:
96 return "AUTH_STATE_FINAL";
98 return "AUTH_STATE_UNKNOWN";
101static BOOL parseKerberosDeltat(
const char* value, INT32* dest,
const char* message);
102static BOOL credssp_auth_setup_identity(rdpCredsspAuth* auth);
103static SecurityFunctionTable* auth_resolve_sspi_table(
const rdpSettings* settings);
105#define log_status(status, level, ...) \
106 log_status_((status), (level), __FILE__, __func__, __LINE__, __VA_ARGS__)
108WINPR_ATTR_FORMAT_ARG(6, 7)
109static BOOL log_status_(SECURITY_STATUS status, DWORD level, const
char* file, const
char* fkt,
110 size_t line, WINPR_FORMAT_ARG const
char* what, ...)
112 static wLog* log =
nullptr;
116 if (WLog_IsLevelActive(log, level))
118 char fwhat[128] = WINPR_C_ARRAY_INIT;
119 va_list ap = WINPR_C_ARRAY_INIT;
121 (void)vsnprintf(fwhat,
sizeof(fwhat) - 1, what, ap);
124 WLog_PrintTextMessage(log, level, line, file, fkt,
"%s status %s [0x%08" PRIx32
"]", fwhat,
125 GetSecurityStatusString(status),
126 WINPR_CXX_COMPAT_CAST(uint32_t, status));
132static BOOL credssp_auth_update_name_cache(rdpCredsspAuth* auth, TCHAR* name)
136 free(auth->pkgNameA);
137 auth->pkgNameA =
nullptr;
140 auth->pkgNameA = ConvertWCharToUtf8Alloc(name,
nullptr);
142 auth->pkgNameA = _strdup(name);
146rdpCredsspAuth* credssp_auth_new(
const rdpContext* context)
148 WINPR_ASSERT(context);
149 rdpCredsspAuth* auth = calloc(1,
sizeof(rdpCredsspAuth));
153 auth->ntlmSettingsV2 = sspi_AllocSecNtlmSettings();
154 if (!auth->ntlmSettingsV2)
157 auth->kerberosSettingsV2 = sspi_AllocSecKerberosSettings();
158 if (!auth->kerberosSettingsV2)
161 auth->rdp_ctx = context;
166 credssp_auth_free(auth);
173 WINPR_ASSERT(auth->rdp_ctx);
175 const rdpSettings* settings = auth->rdp_ctx->settings;
176 WINPR_ASSERT(settings);
178 if (!credssp_auth_update_name_cache(auth, pkg_name))
181 auth->table = auth_resolve_sspi_table(settings);
184 WLog_ERR(TAG,
"Unable to initialize sspi table");
189 WINPR_ASSERT(auth->table->QuerySecurityPackageInfo);
190 const SECURITY_STATUS status = auth->table->QuerySecurityPackageInfo(pkg_name, &auth->info);
191 if (status != SEC_E_OK)
192 return log_status(status, WLOG_ERROR,
"QuerySecurityPackageInfo (%s)",
193 credssp_auth_pkg_name(auth));
195 if (!credssp_auth_update_name_cache(auth, auth->info->Name))
198 WLog_DBG(TAG,
"Using package: %s (cbMaxToken: %u bytes)", credssp_auth_pkg_name(auth),
199 auth->info->cbMaxToken);
202 if (!credssp_auth_setup_identity(auth))
205 auth->bindings = bindings;
210static BOOL credssp_auth_setup_auth_data(rdpCredsspAuth* auth,
211 const SEC_WINNT_AUTH_IDENTITY* identity,
212 SEC_WINNT_AUTH_IDENTITY_WINPR_V2* pAuthData)
214 WINPR_ASSERT(pAuthData);
215 ZeroMemory(pAuthData,
sizeof(SEC_WINNT_AUTH_IDENTITY_WINPR_V2));
218 identityEx->Version = SEC_WINNT_AUTH_IDENTITY_VERSION;
219 identityEx->Length =
sizeof(SEC_WINNT_AUTH_IDENTITY_EX);
220 identityEx->User = identity->User;
221 identityEx->UserLength = identity->UserLength;
222 identityEx->Domain = identity->Domain;
223 identityEx->DomainLength = identity->DomainLength;
224 identityEx->Password = identity->Password;
225 identityEx->PasswordLength = identity->PasswordLength;
226 identityEx->Flags = identity->Flags;
227 identityEx->Flags |= SEC_WINNT_AUTH_IDENTITY_UNICODE;
228 identityEx->Flags |= SEC_WINNT_AUTH_IDENTITY_EXTENDED_v2;
230 if (auth->package_list)
232 const size_t len = _wcslen(auth->package_list);
233 if (len > UINT32_MAX)
236 identityEx->PackageList = (UINT16*)auth->package_list;
237 identityEx->PackageListLength = (UINT32)len;
240 pAuthData->version = SEC_WINNT_AUTH_IDENTITY_WINPR_V2_REVISION_1;
241 pAuthData->ntlmSettingsV2 = auth->ntlmSettingsV2;
242 WINPR_ASSERT(pAuthData->ntlmSettingsV2);
244 pAuthData->kerberosSettingsV2 = auth->kerberosSettingsV2;
245 WINPR_ASSERT(pAuthData->kerberosSettingsV2);
250static BOOL credssp_auth_client_init_cred_attributes(rdpCredsspAuth* auth)
254 if (!utils_str_is_empty(auth->kerberosSettingsV2->kdcUrl))
256 SECURITY_STATUS status = ERROR_INTERNAL_ERROR;
257 SSIZE_T str_size = 0;
259 str_size = ConvertUtf8ToWChar(auth->kerberosSettingsV2->kdcUrl,
nullptr, 0);
260 if ((str_size <= 0) || (str_size >= UINT16_MAX / 2))
264 const size_t buffer_size =
266 if (buffer_size > UINT32_MAX)
272 secAttr->Version = KDC_PROXY_SETTINGS_V1;
273 secAttr->ProxyServerLength = (UINT16)((
size_t)str_size *
sizeof(WCHAR));
276 if (ConvertUtf8ToWChar(auth->kerberosSettingsV2->kdcUrl, (WCHAR*)(secAttr + 1),
277 (
size_t)str_size) <= 0)
284 if (auth->table->SetCredentialsAttributesW)
285 status = auth->table->SetCredentialsAttributesW(&auth->credentials,
286 SECPKG_CRED_ATTR_KDC_PROXY_SETTINGS,
287 (
void*)secAttr, (UINT32)buffer_size);
289 status = SEC_E_UNSUPPORTED_FUNCTION;
291 if (auth->table->SetCredentialsAttributesA)
292 status = auth->table->SetCredentialsAttributesA(&auth->credentials,
293 SECPKG_CRED_ATTR_KDC_PROXY_SETTINGS,
294 (
void*)secAttr, (UINT32)buffer_size);
296 status = SEC_E_UNSUPPORTED_FUNCTION;
299 if (status != SEC_E_OK)
301 WLog_WARN(TAG,
"Explicit Kerberos KDC URL (%s) injection is not supported",
302 auth->kerberosSettingsV2->kdcUrl);
311BOOL credssp_auth_setup_client(rdpCredsspAuth* auth,
const char* target_service,
312 const char* target_hostname,
const SEC_WINNT_AUTH_IDENTITY* identity,
315 void* pAuthData =
nullptr;
316 SEC_WINNT_AUTH_IDENTITY_WINPR_V2 winprAuthData = WINPR_C_ARRAY_INIT;
319 WINPR_ASSERT(auth->table);
320 WINPR_ASSERT(auth->info);
322 WINPR_ASSERT(auth->state == AUTH_STATE_INITIAL);
325 if (!credssp_auth_set_spn(auth, target_service, target_hostname))
330 credssp_auth_setup_auth_data(auth, identity, &winprAuthData);
334 if (!sspi_CloneSecSettingsString(&auth->kerberosSettingsV2->pkinitX509Identity, pkinit))
336 WLog_ERR(TAG,
"unable to copy pkinitArgs");
341 pAuthData = (
void*)&winprAuthData;
344 WINPR_ASSERT(auth->table->AcquireCredentialsHandle);
345 const SECURITY_STATUS status = auth->table->AcquireCredentialsHandle(
346 nullptr, auth->info->Name, SECPKG_CRED_OUTBOUND,
nullptr, pAuthData,
nullptr,
nullptr,
347 &auth->credentials,
nullptr);
349 if (status != SEC_E_OK)
350 return log_status(status, WLOG_ERROR,
"AcquireCredentialsHandleA");
352 if (!credssp_auth_client_init_cred_attributes(auth))
354 WLog_ERR(TAG,
"Fatal error setting credential attributes");
358 auth->state = AUTH_STATE_CREDS;
359 WLog_DBG(TAG,
"Acquired client credentials");
364BOOL credssp_auth_setup_server(rdpCredsspAuth* auth)
366 void* pAuthData =
nullptr;
367 SEC_WINNT_AUTH_IDENTITY_WINPR_V2 winprAuthData = WINPR_C_ARRAY_INIT;
370 WINPR_ASSERT(auth->table);
372 WINPR_ASSERT(auth->state == AUTH_STATE_INITIAL);
374 if (auth->ntlmSettingsV2->samFile || auth->ntlmSettingsV2->hashCallback ||
375 auth->kerberosSettingsV2->keytab)
377 credssp_auth_setup_auth_data(auth, &auth->identity, &winprAuthData);
378 pAuthData = &winprAuthData;
381 WINPR_ASSERT(auth->table->AcquireCredentialsHandle);
382 const SECURITY_STATUS status = auth->table->AcquireCredentialsHandle(
383 nullptr, auth->info->Name, SECPKG_CRED_INBOUND,
nullptr, pAuthData,
nullptr,
nullptr,
384 &auth->credentials,
nullptr);
385 if (status != SEC_E_OK)
386 return log_status(status, WLOG_ERROR,
"AcquireCredentialsHandleA");
388 auth->state = AUTH_STATE_CREDS;
389 WLog_DBG(TAG,
"Acquired server credentials");
396void credssp_auth_set_flags(rdpCredsspAuth* auth, ULONG flags)
440#define query_logged(auth, ulAttribute, pBuffer) \
441 query_logged_((auth), (ulAttribute), (pBuffer), __FILE__, __func__, __LINE__)
442static SECURITY_STATUS query_logged_(rdpCredsspAuth* auth, ULONG ulAttribute,
void* pBuffer,
443 const char* file,
const char* fkt,
size_t line)
446 WINPR_ASSERT(auth->table);
447 WINPR_ASSERT(auth->table->QueryContextAttributes);
449 SECURITY_STATUS status =
450 auth->table->QueryContextAttributes(&auth->context, ulAttribute, pBuffer);
451 (void)log_status_(status, WLOG_DEBUG, file, fkt, line,
452 "QueryContextAttributes(0x%08" PRIx32
")", ulAttribute);
456int credssp_auth_authenticate(rdpCredsspAuth* auth)
458 SECURITY_STATUS status = ERROR_INTERNAL_ERROR;
459 SecBuffer input_buffers[2] = WINPR_C_ARRAY_INIT;
460 SecBufferDesc input_buffer_desc = { SECBUFFER_VERSION, 1, input_buffers };
464 WINPR_ASSERT(auth->table);
466 SecBufferDesc output_buffer_desc = { SECBUFFER_VERSION, 1, &auth->output_buffer };
470 case AUTH_STATE_CREDS:
471 case AUTH_STATE_IN_PROGRESS:
473 case AUTH_STATE_INITIAL:
474 case AUTH_STATE_FINAL:
475 WLog_ERR(TAG,
"context in invalid state!");
483 context = &auth->context;
484 if (!auth->context.dwLower && !auth->context.dwUpper)
487 input_buffers[0] = auth->input_buffer;
491 input_buffer_desc.cBuffers = 2;
493 input_buffers[1].BufferType = SECBUFFER_CHANNEL_BINDINGS;
494 input_buffers[1].cbBuffer = auth->bindings->BindingsLength;
495 input_buffers[1].pvBuffer = auth->bindings->Bindings;
499 sspi_SecBufferFree(&auth->output_buffer);
500 auth->output_buffer.BufferType = SECBUFFER_TOKEN;
501 if (!sspi_SecBufferAlloc(&auth->output_buffer, auth->info->cbMaxToken))
506 WINPR_ASSERT(auth->table->AcceptSecurityContext);
507 status = auth->table->AcceptSecurityContext(
508 &auth->credentials, context, &input_buffer_desc, auth->flags, SECURITY_NATIVE_DREP,
509 &auth->context, &output_buffer_desc, &auth->flags,
nullptr);
513 WINPR_ASSERT(auth->table->InitializeSecurityContext);
514 status = auth->table->InitializeSecurityContext(
515 &auth->credentials, context, auth->spn, auth->flags, 0, SECURITY_NATIVE_DREP,
516 &input_buffer_desc, 0, &auth->context, &output_buffer_desc, &auth->flags,
nullptr);
519 const rdpSettings* settings = auth->rdp_ctx->settings;
520 WINPR_ASSERT(settings);
524 const size_t len = strlen(name);
526 status = SEC_E_INVALID_PARAMETER;
529 const SECURITY_STATUS sca = auth->table->SetContextAttributesA(
530 &auth->context, SECPKG_ATTR_AUTH_NTLM_HOSTNAME,
531 WINPR_CAST_CONST_PTR_AWAY(name,
char*), WINPR_ASSERTING_INT_CAST(ULONG, len));
532 (void)log_status(sca, WLOG_DEBUG,
"SetContextAttributesA(0x%08" PRIx32
")",
533 SECPKG_ATTR_AUTH_NTLM_HOSTNAME);
539 if (status == SEC_E_OK)
541 WLog_DBG(TAG,
"Authentication complete (output token size: %" PRIu32
" bytes)",
542 auth->output_buffer.cbBuffer);
543 auth->state = AUTH_STATE_FINAL;
547 (void)query_logged(auth, SECPKG_ATTR_SIZES, &auth->sizes);
548 WLog_DBG(TAG,
"Context sizes: cbMaxSignature=%" PRIu32
", cbSecurityTrailer=%" PRIu32
"",
549 auth->sizes.cbMaxSignature, auth->sizes.cbSecurityTrailer);
553 rdpSettings* settings = auth->rdp_ctx->settings;
558 SECURITY_STATUS qstatus = query_logged(auth, SECPKG_ATTR_AUTH_NTLM_HOSTNAME_LEN, &len);
560 if ((qstatus == SEC_E_OK) && (len > 0))
562 void* WorkstationName = calloc(len + 1,
sizeof(WCHAR));
563 if (!WorkstationName)
566 qstatus = query_logged(auth, SECPKG_ATTR_AUTH_NTLM_HOSTNAME, WorkstationName);
567 if (qstatus == SEC_E_OK)
571 WorkstationName, len))
575 WorkstationName, len))
579 free(WorkstationName);
584 else if (status == SEC_I_CONTINUE_NEEDED)
586 WLog_DBG(TAG,
"Authentication in progress... (output token size: %" PRIu32
")",
587 auth->output_buffer.cbBuffer);
588 auth->state = AUTH_STATE_IN_PROGRESS;
593 (void)log_status(status, WLOG_ERROR,
594 auth->server ?
"AcceptSecurityContext" :
"InitializeSecurityContext");
595 auth->sspi_error = status;
601BOOL credssp_auth_encrypt(rdpCredsspAuth* auth,
const SecBuffer* plaintext,
SecBuffer* ciphertext,
602 size_t* signature_length, ULONG sequence)
604 SECURITY_STATUS status = ERROR_INTERNAL_ERROR;
605 SecBuffer buffers[2] = WINPR_C_ARRAY_INIT;
606 SecBufferDesc buffer_desc = { SECBUFFER_VERSION, 2, buffers };
609 WINPR_ASSERT(auth && auth->table);
610 WINPR_ASSERT(plaintext);
611 WINPR_ASSERT(ciphertext);
615 case AUTH_STATE_INITIAL:
616 WLog_ERR(TAG,
"Invalid state %s", credssp_auth_state_string(auth));
623 buf = calloc(1, plaintext->cbBuffer + auth->sizes.cbSecurityTrailer);
627 buffers[0].BufferType = SECBUFFER_TOKEN;
628 buffers[0].cbBuffer = auth->sizes.cbSecurityTrailer;
629 buffers[0].pvBuffer = buf;
631 buffers[1].BufferType = SECBUFFER_DATA;
632 if (plaintext->BufferType & SECBUFFER_READONLY)
633 buffers[1].BufferType |= SECBUFFER_READONLY;
634 buffers[1].pvBuffer = buf + auth->sizes.cbSecurityTrailer;
635 buffers[1].cbBuffer = plaintext->cbBuffer;
636 CopyMemory(buffers[1].pvBuffer, plaintext->pvBuffer, plaintext->cbBuffer);
638 WINPR_ASSERT(auth->table->EncryptMessage);
639 status = auth->table->EncryptMessage(&auth->context, 0, &buffer_desc, sequence);
640 if (status != SEC_E_OK)
643 return log_status(status, WLOG_ERROR,
"EncryptMessage");
646 if (buffers[0].cbBuffer < auth->sizes.cbSecurityTrailer)
649 MoveMemory(((BYTE*)buffers[0].pvBuffer) + buffers[0].cbBuffer, buffers[1].pvBuffer,
650 buffers[1].cbBuffer);
652 auth->sizes.cbSecurityTrailer = buffers[0].cbBuffer;
655 ciphertext->cbBuffer = buffers[0].cbBuffer + buffers[1].cbBuffer;
656 ciphertext->pvBuffer = buf;
658 if (signature_length)
659 *signature_length = buffers[0].cbBuffer;
665BOOL credssp_auth_decrypt(rdpCredsspAuth* auth,
const SecBuffer* ciphertext,
SecBuffer* plaintext,
669 SecBufferDesc buffer_desc = { SECBUFFER_VERSION, 2, buffers };
672 WINPR_ASSERT(auth && auth->table);
673 WINPR_ASSERT(ciphertext);
674 WINPR_ASSERT(plaintext);
678 case AUTH_STATE_INITIAL:
679 WLog_ERR(TAG,
"Invalid state %s", credssp_auth_state_string(auth));
686 if (ciphertext->cbBuffer < auth->sizes.cbSecurityTrailer)
688 WLog_ERR(TAG,
"Encrypted message buffer too small");
694 buffers[0].BufferType = SECBUFFER_TOKEN;
695 buffers[0].pvBuffer = ciphertext->pvBuffer;
696 buffers[0].cbBuffer = auth->sizes.cbSecurityTrailer;
698 buffers[1].BufferType = SECBUFFER_DATA;
699 if (!sspi_SecBufferAlloc(&buffers[1], ciphertext->cbBuffer - auth->sizes.cbSecurityTrailer))
701 CopyMemory(buffers[1].pvBuffer, (BYTE*)ciphertext->pvBuffer + auth->sizes.cbSecurityTrailer,
702 buffers[1].cbBuffer);
704 WINPR_ASSERT(auth->table->DecryptMessage);
705 const SECURITY_STATUS status =
706 auth->table->DecryptMessage(&auth->context, &buffer_desc, sequence, &fqop);
707 if (status != SEC_E_OK)
709 WLog_ERR(TAG,
"DecryptMessage failed with %s [0x%08" PRIx32
"]",
710 GetSecurityStatusString(status), WINPR_CXX_COMPAT_CAST(uint32_t, status));
711 sspi_SecBufferFree(&buffers[1]);
715 *plaintext = buffers[1];
720BOOL credssp_auth_impersonate(rdpCredsspAuth* auth)
722 WINPR_ASSERT(auth && auth->table);
724 WINPR_ASSERT(auth->table->ImpersonateSecurityContext);
725 const SECURITY_STATUS status = auth->table->ImpersonateSecurityContext(&auth->context);
727 if (status != SEC_E_OK)
729 WLog_ERR(TAG,
"ImpersonateSecurityContext failed with %s [0x%08" PRIx32
"]",
730 GetSecurityStatusString(status), WINPR_CXX_COMPAT_CAST(uint32_t, status));
737BOOL credssp_auth_revert_to_self(rdpCredsspAuth* auth)
739 WINPR_ASSERT(auth && auth->table);
741 WINPR_ASSERT(auth->table->RevertSecurityContext);
742 const SECURITY_STATUS status = auth->table->RevertSecurityContext(&auth->context);
744 if (status != SEC_E_OK)
746 WLog_ERR(TAG,
"RevertSecurityContext failed with %s [0x%08" PRIx32
"]",
747 GetSecurityStatusString(status), WINPR_CXX_COMPAT_CAST(uint32_t, status));
754void credssp_auth_take_input_buffer(rdpCredsspAuth* auth,
SecBuffer* buffer)
757 WINPR_ASSERT(buffer);
759 sspi_SecBufferFree(&auth->input_buffer);
761 auth->input_buffer = *buffer;
762 auth->input_buffer.BufferType = SECBUFFER_TOKEN;
769const SecBuffer* credssp_auth_get_output_buffer(
const rdpCredsspAuth* auth)
772 return &auth->output_buffer;
775BOOL credssp_auth_have_output_token(rdpCredsspAuth* auth)
778 return (auth->output_buffer.cbBuffer != 0);
781BOOL credssp_auth_is_complete(
const rdpCredsspAuth* auth)
784 return auth->state == AUTH_STATE_FINAL;
787size_t credssp_auth_trailer_size(
const rdpCredsspAuth* auth)
790 return auth->sizes.cbSecurityTrailer;
793const char* credssp_auth_pkg_name(
const rdpCredsspAuth* auth)
795 WINPR_ASSERT(auth && auth->info);
796 return auth->pkgNameA;
799INT32 credssp_auth_sspi_error(
const rdpCredsspAuth* auth)
802 return auth->sspi_error;
805void credssp_auth_tableAndContext(rdpCredsspAuth* auth, SecurityFunctionTable** ptable,
809 WINPR_ASSERT(ptable);
810 WINPR_ASSERT(pcontext);
812 *ptable = auth->table;
813 *pcontext = auth->context;
816void credssp_auth_free(rdpCredsspAuth* auth)
825 case AUTH_STATE_IN_PROGRESS:
826 case AUTH_STATE_FINAL:
827 WINPR_ASSERT(auth->table->DeleteSecurityContext);
828 auth->table->DeleteSecurityContext(&auth->context);
831 case AUTH_STATE_CREDS:
832 WINPR_ASSERT(auth->table->FreeCredentialsHandle);
833 auth->table->FreeCredentialsHandle(&auth->credentials);
835 case AUTH_STATE_INITIAL:
842 WINPR_ASSERT(auth->table->FreeContextBuffer);
843 auth->table->FreeContextBuffer(auth->info);
847 sspi_FreeAuthIdentity(&auth->identity);
848 sspi_FreeSecKerberosSettings(auth->kerberosSettingsV2);
849 sspi_FreeSecNtlmSettings(auth->ntlmSettingsV2);
851 free(auth->package_list);
853 sspi_SecBufferFree(&auth->input_buffer);
854 sspi_SecBufferFree(&auth->output_buffer);
855 credssp_auth_update_name_cache(auth,
nullptr);
859static void auth_get_sspi_module_from_config(
char** sspi_module)
861 WINPR_ASSERT(sspi_module);
863 WINPR_JSON* json = freerdp_GetJSONConfigFile(TRUE, credssp_config_file_name);
867 winpr_config_apply_string(credssp_config_file_name, json,
"SspiModule", sspi_module);
871#if !defined(WITHOUT_FREERDP_3x_DEPRECATED)
872static void auth_get_sspi_module_from_reg(
char** sspi_module)
878 WINPR_ASSERT(sspi_module);
880 char* key = freerdp_getApplicatonDetailsRegKey(SERVER_KEY);
884 const LONG rc = RegOpenKeyExA(HKEY_LOCAL_MACHINE, key, 0, KEY_READ | KEY_WOW64_64KEY, &hKey);
887 if (rc != ERROR_SUCCESS)
890 WLog_WARN(TAG,
"HKLM.reg is deprecated since 3.33.0. ATTENTION: Use %s instead!",
891 credssp_config_file_name);
892 if (RegQueryValueExA(hKey,
"SspiModule",
nullptr, &dwType,
nullptr, &dwSize) != ERROR_SUCCESS)
898 char*
module = (LPSTR)calloc(dwSize + sizeof(CHAR), sizeof(char));
905 if (RegQueryValueExA(hKey,
"SspiModule",
nullptr, &dwType, (BYTE*)module, &dwSize) !=
916 *sspi_module =
module;
920static SecurityFunctionTable* auth_resolve_sspi_table(
const rdpSettings* settings)
922 char* sspi_module =
nullptr;
924 WINPR_ASSERT(settings);
926 if (settings->ServerMode)
928#if !defined(WITHOUT_FREERDP_3x_DEPRECATED)
929 auth_get_sspi_module_from_reg(&sspi_module);
931 auth_get_sspi_module_from_config(&sspi_module);
934 if (sspi_module || settings->SspiModule)
936 const char* module_name = sspi_module ? sspi_module : settings->SspiModule;
938 const char* proc_name =
"InitSecurityInterfaceW";
940 const char* proc_name =
"InitSecurityInterfaceA";
943 HMODULE hSSPI = LoadLibraryX(module_name);
947 WLog_ERR(TAG,
"Failed to load SSPI module: %s", module_name);
952 WLog_INFO(TAG,
"Using SSPI Module: %s", module_name);
954 INIT_SECURITY_INTERFACE InitSecurityInterface_ptr =
955 GetProcAddressAs(hSSPI, proc_name, INIT_SECURITY_INTERFACE);
956 if (!InitSecurityInterface_ptr)
958 WLog_ERR(TAG,
"Failed to load SSPI module: %s, no function %s", module_name, proc_name);
963 return InitSecurityInterface_ptr();
966 return InitSecurityInterfaceEx(0);
969static BOOL credssp_auth_setup_identity(rdpCredsspAuth* auth)
972 WINPR_ASSERT(auth->rdp_ctx);
974 freerdp_peer* peer = auth->rdp_ctx->peer;
975 const rdpSettings* settings = auth->rdp_ctx->settings;
976 WINPR_ASSERT(settings);
978 SEC_WINPR_KERBEROS_SETTINGS_V2* krb_settings = auth->kerberosSettingsV2;
979 WINPR_ASSERT(krb_settings);
981 SEC_WINPR_NTLM_SETTINGS_V2* ntlm_settings = auth->ntlmSettingsV2;
982 WINPR_ASSERT(ntlm_settings);
984 if (settings->KerberosLifeTime)
985 parseKerberosDeltat(settings->KerberosLifeTime, &krb_settings->lifeTime,
"lifetime");
986 if (settings->KerberosStartTime)
987 parseKerberosDeltat(settings->KerberosStartTime, &krb_settings->startTime,
"starttime");
988 if (settings->KerberosRenewableLifeTime)
989 parseKerberosDeltat(settings->KerberosRenewableLifeTime, &krb_settings->renewLifeTime,
992 if (settings->KerberosKdcUrl)
994 if (!sspi_CloneSecSettingsString(&krb_settings->kdcUrl, settings->KerberosKdcUrl))
996 WLog_ERR(TAG,
"unable to copy kdcUrl");
1001 if (settings->KerberosCache)
1003 if (!sspi_CloneSecSettingsString(&krb_settings->cache, settings->KerberosCache))
1005 WLog_ERR(TAG,
"unable to copy cache name");
1010 if (settings->KerberosKeytab)
1012 if (!sspi_CloneSecSettingsString(&krb_settings->keytab, settings->KerberosKeytab))
1013 if (!krb_settings->keytab)
1015 WLog_ERR(TAG,
"unable to copy keytab name");
1020 if (settings->KerberosArmor)
1022 if (!sspi_CloneSecSettingsString(&krb_settings->armorCache, settings->KerberosArmor))
1024 WLog_ERR(TAG,
"unable to copy armorCache");
1029 if (settings->PkinitAnchors)
1031 if (!sspi_CloneSecSettingsString(&krb_settings->pkinitX509Anchors, settings->PkinitAnchors))
1033 WLog_ERR(TAG,
"unable to copy pkinitX509Anchors");
1038 if (settings->NtlmSamFile)
1040 if (!sspi_CloneSecSettingsString(&ntlm_settings->samFile, settings->NtlmSamFile))
1042 WLog_ERR(TAG,
"unable to copy samFile");
1047 if (peer && peer->SspiNtlmHashCallback)
1049 ntlm_settings->hashCallback = peer->SspiNtlmHashCallback;
1050 ntlm_settings->hashCallbackArg = peer;
1053 if (settings->AuthenticationPackageList)
1055 auth->package_list = ConvertUtf8ToWCharAlloc(settings->AuthenticationPackageList,
nullptr);
1056 if (!auth->package_list)
1060 auth->identity.Flags |= SEC_WINNT_AUTH_IDENTITY_UNICODE;
1061 auth->identity.Flags |= SEC_WINNT_AUTH_IDENTITY_EXTENDED_v2;
1066BOOL credssp_auth_set_spn(rdpCredsspAuth* auth,
const char* service,
const char* hostname)
1069 char* spn =
nullptr;
1077 spn = _strdup(hostname);
1080 length = strlen(service) + strlen(hostname) + 2;
1081 spn = calloc(length + 1,
sizeof(
char));
1085 (void)sprintf_s(spn, length,
"%s/%s", service, hostname);
1091 auth->spn = ConvertUtf8ToWCharAlloc(spn,
nullptr);
1100static const char* parseInt(
const char* v, INT32* r)
1105 if (!*v || !((*v >=
'0') && (*v <=
'9')))
1108 for (; *v && (*v >=
'0') && (*v <=
'9'); v++)
1110 *r = (*r * 10) + (*v -
'0');
1116static BOOL parseKerberosDeltat(
const char* value, INT32* dest,
const char* message)
1119 const char* ptr =
nullptr;
1121 WINPR_ASSERT(value);
1123 WINPR_ASSERT(message);
1130 ptr = strchr(value,
':');
1135 value = parseInt(value, &v);
1136 if (!value || *value !=
':')
1138 WLog_ERR(TAG,
"Invalid value for %s", message);
1144 value = parseInt(value + 1, &v);
1145 if (!value || (*value != 0 && *value !=
':') || (v > 60))
1147 WLog_ERR(TAG,
"Invalid value for %s", message);
1155 value = parseInt(value + 1, &v);
1156 if (!value || (*value != 0) || (v > 60))
1158 WLog_ERR(TAG,
"Invalid value for %s", message);
1167 value = parseInt(value, &v);
1170 WLog_ERR(TAG,
"Invalid value for %s", message);
1174 if (!*value || isspace(*value))
1206 WLog_ERR(TAG,
"invalid value for unit %c when parsing %s", *value, message);
1210 if ((maxValue > 0) && (v > maxValue))
1212 WLog_ERR(TAG,
"invalid value for unit %c when parsing %s", *value, message);
1216 *dest += (v * factor);
1221 value = parseInt(value, &v);
1222 if (!value || !*value)
1224 WLog_ERR(TAG,
"Invalid value for %s", message);
WINPR_API void WINPR_JSON_Delete(WINPR_JSON *item)
Delete a WinPR JSON wrapper object.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_string_from_utf16N(rdpSettings *settings, FreeRDP_Settings_Keys_String id, const WCHAR *param, size_t length)
Sets a string settings value. The param is converted to UTF-8 and the copy stored.
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_string_len(rdpSettings *settings, FreeRDP_Settings_Keys_String id, const char *val, size_t len)
Sets a string settings value. The val is copied.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_string(rdpSettings *settings, FreeRDP_Settings_Keys_String id, const char *val)
Sets a string settings value. The param is copied.