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;
67 CredHandle credentials;
69 SecPkgContext_Bindings* bindings;
73 SecBuffer input_buffer;
74 SecBuffer output_buffer;
76 SecPkgContext_Sizes sizes;
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);
314static BOOL credssp_auth_client_disable_ntlm(rdpCredsspAuth* auth)
317 WINPR_ASSERT(auth->rdp_ctx);
319 const rdpSettings* settings = auth->rdp_ctx->settings;
320 WINPR_ASSERT(settings);
322 const char* pkg = credssp_auth_pkg_name(auth);
323 if (pkg && (_stricmp(pkg, CREDSSP_AUTH_PKG_NTLM) == 0))
325 WLog_ERR(TAG,
"%s authentication is not supported with smartcard logon", pkg);
329 char* list =
nullptr;
331 const char* cur = settings->AuthenticationPackageList;
332 const BOOL empty = utils_str_is_empty(cur);
333 if (winpr_asprintf(&list, &len,
"%s%s!ntlm", empty ?
"" : cur, empty ?
"" :
",") < 0)
336 free(auth->package_list);
337 auth->package_list = ConvertUtf8ToWCharAlloc(list,
nullptr);
339 return auth->package_list !=
nullptr;
342BOOL credssp_auth_setup_client(rdpCredsspAuth* auth,
const char* target_service,
343 const char* target_hostname,
const SEC_WINNT_AUTH_IDENTITY* identity,
346 void* pAuthData =
nullptr;
347 SEC_WINNT_AUTH_IDENTITY_WINPR_V2 winprAuthData = WINPR_C_ARRAY_INIT;
350 WINPR_ASSERT(auth->table);
351 WINPR_ASSERT(auth->info);
352 WINPR_ASSERT(auth->rdp_ctx);
353 WINPR_ASSERT(auth->rdp_ctx->settings);
355 WINPR_ASSERT(auth->state == AUTH_STATE_INITIAL);
359 if (auth->rdp_ctx->settings->SmartcardLogon && !credssp_auth_client_disable_ntlm(auth))
363 if (!credssp_auth_set_spn(auth, target_service, target_hostname))
368 credssp_auth_setup_auth_data(auth, identity, &winprAuthData);
372 if (!sspi_CloneSecSettingsString(&auth->kerberosSettingsV2->pkinitX509Identity, pkinit))
374 WLog_ERR(TAG,
"unable to copy pkinitArgs");
379 pAuthData = (
void*)&winprAuthData;
382 WINPR_ASSERT(auth->table->AcquireCredentialsHandle);
383 const SECURITY_STATUS status = auth->table->AcquireCredentialsHandle(
384 nullptr, auth->info->Name, SECPKG_CRED_OUTBOUND,
nullptr, pAuthData,
nullptr,
nullptr,
385 &auth->credentials,
nullptr);
387 if (status != SEC_E_OK)
388 return log_status(status, WLOG_ERROR,
"AcquireCredentialsHandleA");
390 if (!credssp_auth_client_init_cred_attributes(auth))
392 WLog_ERR(TAG,
"Fatal error setting credential attributes");
396 auth->state = AUTH_STATE_CREDS;
397 WLog_DBG(TAG,
"Acquired client credentials");
402BOOL credssp_auth_setup_server(rdpCredsspAuth* auth)
404 void* pAuthData =
nullptr;
405 SEC_WINNT_AUTH_IDENTITY_WINPR_V2 winprAuthData = WINPR_C_ARRAY_INIT;
408 WINPR_ASSERT(auth->table);
410 WINPR_ASSERT(auth->state == AUTH_STATE_INITIAL);
412 if (auth->ntlmSettingsV2->samFile || auth->ntlmSettingsV2->hashCallback ||
413 auth->kerberosSettingsV2->keytab)
415 credssp_auth_setup_auth_data(auth, &auth->identity, &winprAuthData);
416 pAuthData = &winprAuthData;
419 WINPR_ASSERT(auth->table->AcquireCredentialsHandle);
420 const SECURITY_STATUS status = auth->table->AcquireCredentialsHandle(
421 nullptr, auth->info->Name, SECPKG_CRED_INBOUND,
nullptr, pAuthData,
nullptr,
nullptr,
422 &auth->credentials,
nullptr);
423 if (status != SEC_E_OK)
424 return log_status(status, WLOG_ERROR,
"AcquireCredentialsHandleA");
426 auth->state = AUTH_STATE_CREDS;
427 WLog_DBG(TAG,
"Acquired server credentials");
434void credssp_auth_set_flags(rdpCredsspAuth* auth, ULONG flags)
478#define query_logged(auth, ulAttribute, pBuffer) \
479 query_logged_((auth), (ulAttribute), (pBuffer), __FILE__, __func__, __LINE__)
480static SECURITY_STATUS query_logged_(rdpCredsspAuth* auth, ULONG ulAttribute,
void* pBuffer,
481 const char* file,
const char* fkt,
size_t line)
484 WINPR_ASSERT(auth->table);
485 WINPR_ASSERT(auth->table->QueryContextAttributes);
487 SECURITY_STATUS status =
488 auth->table->QueryContextAttributes(&auth->context, ulAttribute, pBuffer);
489 (void)log_status_(status, WLOG_DEBUG, file, fkt, line,
490 "QueryContextAttributes(0x%08" PRIx32
")", ulAttribute);
494int credssp_auth_authenticate(rdpCredsspAuth* auth)
496 SECURITY_STATUS status = ERROR_INTERNAL_ERROR;
497 SecBuffer input_buffers[2] = WINPR_C_ARRAY_INIT;
498 SecBufferDesc input_buffer_desc = { SECBUFFER_VERSION, 1, input_buffers };
499 CtxtHandle* context =
nullptr;
502 WINPR_ASSERT(auth->table);
504 SecBufferDesc output_buffer_desc = { SECBUFFER_VERSION, 1, &auth->output_buffer };
508 case AUTH_STATE_CREDS:
509 case AUTH_STATE_IN_PROGRESS:
511 case AUTH_STATE_INITIAL:
512 case AUTH_STATE_FINAL:
513 WLog_ERR(TAG,
"context in invalid state!");
521 context = &auth->context;
522 if (!auth->context.dwLower && !auth->context.dwUpper)
525 input_buffers[0] = auth->input_buffer;
529 input_buffer_desc.cBuffers = 2;
531 input_buffers[1].BufferType = SECBUFFER_CHANNEL_BINDINGS;
532 input_buffers[1].cbBuffer = auth->bindings->BindingsLength;
533 input_buffers[1].pvBuffer = auth->bindings->Bindings;
537 sspi_SecBufferFree(&auth->output_buffer);
538 auth->output_buffer.BufferType = SECBUFFER_TOKEN;
539 if (!sspi_SecBufferAlloc(&auth->output_buffer, auth->info->cbMaxToken))
544 WINPR_ASSERT(auth->table->AcceptSecurityContext);
545 status = auth->table->AcceptSecurityContext(
546 &auth->credentials, context, &input_buffer_desc, auth->flags, SECURITY_NATIVE_DREP,
547 &auth->context, &output_buffer_desc, &auth->flags,
nullptr);
551 WINPR_ASSERT(auth->table->InitializeSecurityContext);
552 status = auth->table->InitializeSecurityContext(
553 &auth->credentials, context, auth->spn, auth->flags, 0, SECURITY_NATIVE_DREP,
554 &input_buffer_desc, 0, &auth->context, &output_buffer_desc, &auth->flags,
nullptr);
557 const rdpSettings* settings = auth->rdp_ctx->settings;
558 WINPR_ASSERT(settings);
562 const size_t len = strlen(name);
564 status = SEC_E_INVALID_PARAMETER;
567 const SECURITY_STATUS sca = auth->table->SetContextAttributesA(
568 &auth->context, SECPKG_ATTR_AUTH_NTLM_HOSTNAME,
569 WINPR_CAST_CONST_PTR_AWAY(name,
char*), WINPR_ASSERTING_INT_CAST(ULONG, len));
570 (void)log_status(sca, WLOG_DEBUG,
"SetContextAttributesA(0x%08" PRIx32
")",
571 SECPKG_ATTR_AUTH_NTLM_HOSTNAME);
577 if (status == SEC_E_OK)
579 WLog_DBG(TAG,
"Authentication complete (output token size: %" PRIu32
" bytes)",
580 auth->output_buffer.cbBuffer);
581 auth->state = AUTH_STATE_FINAL;
585 (void)query_logged(auth, SECPKG_ATTR_SIZES, &auth->sizes);
586 WLog_DBG(TAG,
"Context sizes: cbMaxSignature=%" PRIu32
", cbSecurityTrailer=%" PRIu32
"",
587 auth->sizes.cbMaxSignature, auth->sizes.cbSecurityTrailer);
591 rdpSettings* settings = auth->rdp_ctx->settings;
596 SECURITY_STATUS qstatus = query_logged(auth, SECPKG_ATTR_AUTH_NTLM_HOSTNAME_LEN, &len);
598 if ((qstatus == SEC_E_OK) && (len > 0))
600 void* WorkstationName = calloc(len + 1,
sizeof(WCHAR));
601 if (!WorkstationName)
604 qstatus = query_logged(auth, SECPKG_ATTR_AUTH_NTLM_HOSTNAME, WorkstationName);
605 if (qstatus == SEC_E_OK)
609 WorkstationName, len))
613 WorkstationName, len))
617 free(WorkstationName);
622 else if (status == SEC_I_CONTINUE_NEEDED)
624 WLog_DBG(TAG,
"Authentication in progress... (output token size: %" PRIu32
")",
625 auth->output_buffer.cbBuffer);
626 auth->state = AUTH_STATE_IN_PROGRESS;
631 (void)log_status(status, WLOG_ERROR,
632 auth->server ?
"AcceptSecurityContext" :
"InitializeSecurityContext");
633 auth->sspi_error = status;
639BOOL credssp_auth_encrypt(rdpCredsspAuth* auth,
const SecBuffer* plaintext,
SecBuffer* ciphertext,
640 size_t* signature_length, ULONG sequence)
642 SECURITY_STATUS status = ERROR_INTERNAL_ERROR;
643 SecBuffer buffers[2] = WINPR_C_ARRAY_INIT;
644 SecBufferDesc buffer_desc = { SECBUFFER_VERSION, 2, buffers };
647 WINPR_ASSERT(auth && auth->table);
648 WINPR_ASSERT(plaintext);
649 WINPR_ASSERT(ciphertext);
653 case AUTH_STATE_INITIAL:
654 WLog_ERR(TAG,
"Invalid state %s", credssp_auth_state_string(auth));
660 if (plaintext->cbBuffer > SIZE_MAX - auth->sizes.cbSecurityTrailer)
662 const size_t len = 1ull * plaintext->cbBuffer + auth->sizes.cbSecurityTrailer;
665 buf = calloc(1, len);
669 buffers[0].BufferType = SECBUFFER_TOKEN;
670 buffers[0].cbBuffer = auth->sizes.cbSecurityTrailer;
671 buffers[0].pvBuffer = buf;
673 buffers[1].BufferType = SECBUFFER_DATA;
674 if (plaintext->BufferType & SECBUFFER_READONLY)
675 buffers[1].BufferType |= SECBUFFER_READONLY;
676 buffers[1].pvBuffer = buf + auth->sizes.cbSecurityTrailer;
677 buffers[1].cbBuffer = plaintext->cbBuffer;
678 CopyMemory(buffers[1].pvBuffer, plaintext->pvBuffer, plaintext->cbBuffer);
680 WINPR_ASSERT(auth->table->EncryptMessage);
681 status = auth->table->EncryptMessage(&auth->context, 0, &buffer_desc, sequence);
682 if (status != SEC_E_OK)
685 return log_status(status, WLOG_ERROR,
"EncryptMessage");
688 if (buffers[0].cbBuffer < auth->sizes.cbSecurityTrailer)
691 MoveMemory(((BYTE*)buffers[0].pvBuffer) + buffers[0].cbBuffer, buffers[1].pvBuffer,
692 buffers[1].cbBuffer);
694 auth->sizes.cbSecurityTrailer = buffers[0].cbBuffer;
697 ciphertext->cbBuffer = buffers[0].cbBuffer + buffers[1].cbBuffer;
698 ciphertext->pvBuffer = buf;
700 if (signature_length)
701 *signature_length = buffers[0].cbBuffer;
707BOOL credssp_auth_decrypt(rdpCredsspAuth* auth,
const SecBuffer* ciphertext,
SecBuffer* plaintext,
711 SecBufferDesc buffer_desc = { SECBUFFER_VERSION, 2, buffers };
714 WINPR_ASSERT(auth && auth->table);
715 WINPR_ASSERT(ciphertext);
716 WINPR_ASSERT(plaintext);
720 case AUTH_STATE_INITIAL:
721 WLog_ERR(TAG,
"Invalid state %s", credssp_auth_state_string(auth));
728 if (ciphertext->cbBuffer < auth->sizes.cbSecurityTrailer)
730 WLog_ERR(TAG,
"Encrypted message buffer too small");
736 buffers[0].BufferType = SECBUFFER_TOKEN;
737 buffers[0].pvBuffer = ciphertext->pvBuffer;
738 buffers[0].cbBuffer = auth->sizes.cbSecurityTrailer;
740 buffers[1].BufferType = SECBUFFER_DATA;
741 if (!sspi_SecBufferAlloc(&buffers[1], ciphertext->cbBuffer - auth->sizes.cbSecurityTrailer))
743 CopyMemory(buffers[1].pvBuffer, (BYTE*)ciphertext->pvBuffer + auth->sizes.cbSecurityTrailer,
744 buffers[1].cbBuffer);
746 WINPR_ASSERT(auth->table->DecryptMessage);
747 const SECURITY_STATUS status =
748 auth->table->DecryptMessage(&auth->context, &buffer_desc, sequence, &fqop);
749 if (status != SEC_E_OK)
751 WLog_ERR(TAG,
"DecryptMessage failed with %s [0x%08" PRIx32
"]",
752 GetSecurityStatusString(status), WINPR_CXX_COMPAT_CAST(uint32_t, status));
753 sspi_SecBufferFree(&buffers[1]);
757 *plaintext = buffers[1];
762BOOL credssp_auth_impersonate(rdpCredsspAuth* auth)
764 WINPR_ASSERT(auth && auth->table);
766 WINPR_ASSERT(auth->table->ImpersonateSecurityContext);
767 const SECURITY_STATUS status = auth->table->ImpersonateSecurityContext(&auth->context);
769 if (status != SEC_E_OK)
771 WLog_ERR(TAG,
"ImpersonateSecurityContext failed with %s [0x%08" PRIx32
"]",
772 GetSecurityStatusString(status), WINPR_CXX_COMPAT_CAST(uint32_t, status));
779BOOL credssp_auth_revert_to_self(rdpCredsspAuth* auth)
781 WINPR_ASSERT(auth && auth->table);
783 WINPR_ASSERT(auth->table->RevertSecurityContext);
784 const SECURITY_STATUS status = auth->table->RevertSecurityContext(&auth->context);
786 if (status != SEC_E_OK)
788 WLog_ERR(TAG,
"RevertSecurityContext failed with %s [0x%08" PRIx32
"]",
789 GetSecurityStatusString(status), WINPR_CXX_COMPAT_CAST(uint32_t, status));
796void credssp_auth_take_input_buffer(rdpCredsspAuth* auth,
SecBuffer* buffer)
799 WINPR_ASSERT(buffer);
801 sspi_SecBufferFree(&auth->input_buffer);
803 auth->input_buffer = *buffer;
804 auth->input_buffer.BufferType = SECBUFFER_TOKEN;
811const SecBuffer* credssp_auth_get_output_buffer(
const rdpCredsspAuth* auth)
814 return &auth->output_buffer;
817BOOL credssp_auth_have_output_token(rdpCredsspAuth* auth)
820 return (auth->output_buffer.cbBuffer != 0);
823BOOL credssp_auth_is_complete(
const rdpCredsspAuth* auth)
826 return auth->state == AUTH_STATE_FINAL;
829size_t credssp_auth_trailer_size(
const rdpCredsspAuth* auth)
832 return auth->sizes.cbSecurityTrailer;
835const char* credssp_auth_pkg_name(
const rdpCredsspAuth* auth)
837 WINPR_ASSERT(auth && auth->info);
838 return auth->pkgNameA;
841INT32 credssp_auth_sspi_error(
const rdpCredsspAuth* auth)
844 return auth->sspi_error;
847void credssp_auth_tableAndContext(rdpCredsspAuth* auth, SecurityFunctionTable** ptable,
848 CtxtHandle* pcontext)
851 WINPR_ASSERT(ptable);
852 WINPR_ASSERT(pcontext);
854 *ptable = auth->table;
855 *pcontext = auth->context;
858void credssp_auth_free(rdpCredsspAuth* auth)
867 case AUTH_STATE_IN_PROGRESS:
868 case AUTH_STATE_FINAL:
869 WINPR_ASSERT(auth->table->DeleteSecurityContext);
870 auth->table->DeleteSecurityContext(&auth->context);
873 case AUTH_STATE_CREDS:
874 WINPR_ASSERT(auth->table->FreeCredentialsHandle);
875 auth->table->FreeCredentialsHandle(&auth->credentials);
877 case AUTH_STATE_INITIAL:
884 WINPR_ASSERT(auth->table->FreeContextBuffer);
885 auth->table->FreeContextBuffer(auth->info);
889 sspi_FreeAuthIdentity(&auth->identity);
890 sspi_FreeSecKerberosSettings(auth->kerberosSettingsV2);
891 sspi_FreeSecNtlmSettings(auth->ntlmSettingsV2);
893 free(auth->package_list);
895 sspi_SecBufferFree(&auth->input_buffer);
896 sspi_SecBufferFree(&auth->output_buffer);
897 credssp_auth_update_name_cache(auth,
nullptr);
901static void auth_get_sspi_module_from_config(
char** sspi_module)
903 WINPR_ASSERT(sspi_module);
905 WINPR_JSON* json = freerdp_GetJSONConfigFile(TRUE, credssp_config_file_name);
909 winpr_config_apply_string(credssp_config_file_name, json,
"SspiModule", sspi_module);
913#if !defined(WITHOUT_FREERDP_3x_DEPRECATED)
914static void auth_get_sspi_module_from_reg(
char** sspi_module)
920 WINPR_ASSERT(sspi_module);
922 char* key = freerdp_getApplicatonDetailsRegKey(SERVER_KEY);
926 const LONG rc = RegOpenKeyExA(HKEY_LOCAL_MACHINE, key, 0, KEY_READ | KEY_WOW64_64KEY, &hKey);
929 if (rc != ERROR_SUCCESS)
932 WLog_WARN(TAG,
"HKLM.reg is deprecated since 3.33.0. ATTENTION: Use %s instead!",
933 credssp_config_file_name);
934 if (RegQueryValueExA(hKey,
"SspiModule",
nullptr, &dwType,
nullptr, &dwSize) != ERROR_SUCCESS)
940 char* module = (LPSTR)calloc(dwSize +
sizeof(CHAR),
sizeof(
char));
947 if (RegQueryValueExA(hKey,
"SspiModule",
nullptr, &dwType, (BYTE*)module, &dwSize) !=
958 *sspi_module =
module;
962static SecurityFunctionTable* auth_resolve_sspi_table(
const rdpSettings* settings)
964 char* sspi_module =
nullptr;
966 WINPR_ASSERT(settings);
968 if (settings->ServerMode)
970#if !defined(WITHOUT_FREERDP_3x_DEPRECATED)
971 auth_get_sspi_module_from_reg(&sspi_module);
973 auth_get_sspi_module_from_config(&sspi_module);
976 if (sspi_module || settings->SspiModule)
978 const char* module_name = sspi_module ? sspi_module : settings->SspiModule;
980 const char* proc_name =
"InitSecurityInterfaceW";
982 const char* proc_name =
"InitSecurityInterfaceA";
985 HMODULE hSSPI = LoadLibraryX(module_name);
989 WLog_ERR(TAG,
"Failed to load SSPI module: %s", module_name);
994 WLog_INFO(TAG,
"Using SSPI Module: %s", module_name);
996 INIT_SECURITY_INTERFACE InitSecurityInterface_ptr =
997 GetProcAddressAs(hSSPI, proc_name, INIT_SECURITY_INTERFACE);
998 if (!InitSecurityInterface_ptr)
1000 WLog_ERR(TAG,
"Failed to load SSPI module: %s, no function %s", module_name, proc_name);
1005 return InitSecurityInterface_ptr();
1008 return InitSecurityInterfaceEx(0);
1011static BOOL credssp_auth_setup_identity(rdpCredsspAuth* auth)
1014 WINPR_ASSERT(auth->rdp_ctx);
1016 freerdp_peer* peer = auth->rdp_ctx->peer;
1017 const rdpSettings* settings = auth->rdp_ctx->settings;
1018 WINPR_ASSERT(settings);
1020 SEC_WINPR_KERBEROS_SETTINGS_V2* krb_settings = auth->kerberosSettingsV2;
1021 WINPR_ASSERT(krb_settings);
1023 SEC_WINPR_NTLM_SETTINGS_V2* ntlm_settings = auth->ntlmSettingsV2;
1024 WINPR_ASSERT(ntlm_settings);
1026 if (settings->KerberosLifeTime)
1027 parseKerberosDeltat(settings->KerberosLifeTime, &krb_settings->lifeTime,
"lifetime");
1028 if (settings->KerberosStartTime)
1029 parseKerberosDeltat(settings->KerberosStartTime, &krb_settings->startTime,
"starttime");
1030 if (settings->KerberosRenewableLifeTime)
1031 parseKerberosDeltat(settings->KerberosRenewableLifeTime, &krb_settings->renewLifeTime,
1034 if (settings->KerberosKdcUrl)
1036 if (!sspi_CloneSecSettingsString(&krb_settings->kdcUrl, settings->KerberosKdcUrl))
1038 WLog_ERR(TAG,
"unable to copy kdcUrl");
1043 if (settings->KerberosCache)
1045 if (!sspi_CloneSecSettingsString(&krb_settings->cache, settings->KerberosCache))
1047 WLog_ERR(TAG,
"unable to copy cache name");
1052 if (settings->KerberosKeytab)
1054 if (!sspi_CloneSecSettingsString(&krb_settings->keytab, settings->KerberosKeytab))
1055 if (!krb_settings->keytab)
1057 WLog_ERR(TAG,
"unable to copy keytab name");
1062 if (settings->KerberosArmor)
1064 if (!sspi_CloneSecSettingsString(&krb_settings->armorCache, settings->KerberosArmor))
1066 WLog_ERR(TAG,
"unable to copy armorCache");
1071 if (settings->PkinitAnchors)
1073 if (!sspi_CloneSecSettingsString(&krb_settings->pkinitX509Anchors, settings->PkinitAnchors))
1075 WLog_ERR(TAG,
"unable to copy pkinitX509Anchors");
1080 if (settings->NtlmSamFile)
1082 if (!sspi_CloneSecSettingsString(&ntlm_settings->samFile, settings->NtlmSamFile))
1084 WLog_ERR(TAG,
"unable to copy samFile");
1089 if (peer && peer->SspiNtlmHashCallback)
1091 ntlm_settings->hashCallback = peer->SspiNtlmHashCallback;
1092 ntlm_settings->hashCallbackArg = peer;
1095 if (settings->AuthenticationPackageList)
1097 auth->package_list = ConvertUtf8ToWCharAlloc(settings->AuthenticationPackageList,
nullptr);
1098 if (!auth->package_list)
1102 auth->identity.Flags |= SEC_WINNT_AUTH_IDENTITY_UNICODE;
1103 auth->identity.Flags |= SEC_WINNT_AUTH_IDENTITY_EXTENDED_v2;
1108BOOL credssp_auth_set_spn(rdpCredsspAuth* auth,
const char* service,
const char* hostname)
1111 char* spn =
nullptr;
1119 spn = _strdup(hostname);
1122 length = strlen(service) + strlen(hostname) + 2;
1123 spn = calloc(length + 1,
sizeof(
char));
1127 (void)sprintf_s(spn, length,
"%s/%s", service, hostname);
1133 auth->spn = ConvertUtf8ToWCharAlloc(spn,
nullptr);
1142static const char* parseInt(
const char* v, INT32* r)
1147 if (!*v || !((*v >=
'0') && (*v <=
'9')))
1150 for (; *v && (*v >=
'0') && (*v <=
'9'); v++)
1152 *r = (*r * 10) + (*v -
'0');
1158static BOOL parseKerberosDeltat(
const char* value, INT32* dest,
const char* message)
1161 const char* ptr =
nullptr;
1163 WINPR_ASSERT(value);
1165 WINPR_ASSERT(message);
1172 ptr = strchr(value,
':');
1177 value = parseInt(value, &v);
1178 if (!value || *value !=
':')
1180 WLog_ERR(TAG,
"Invalid value for %s", message);
1186 value = parseInt(value + 1, &v);
1187 if (!value || (*value != 0 && *value !=
':') || (v > 60))
1189 WLog_ERR(TAG,
"Invalid value for %s", message);
1197 value = parseInt(value + 1, &v);
1198 if (!value || (*value != 0) || (v > 60))
1200 WLog_ERR(TAG,
"Invalid value for %s", message);
1209 value = parseInt(value, &v);
1212 WLog_ERR(TAG,
"Invalid value for %s", message);
1216 if (!*value || isspace(*value))
1248 WLog_ERR(TAG,
"invalid value for unit %c when parsing %s", *value, message);
1252 if ((maxValue > 0) && (v > maxValue))
1254 WLog_ERR(TAG,
"invalid value for unit %c when parsing %s", *value, message);
1258 *dest += (v * factor);
1263 value = parseInt(value, &v);
1264 if (!value || !*value)
1266 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.