24#include <freerdp/config.h>
26#include <freerdp/build-config.h>
27#include <freerdp/peer.h>
30#include <winpr/wtypes.h>
31#include <winpr/assert.h>
32#include <winpr/library.h>
33#include <winpr/registry.h>
34#include <winpr/sspi.h>
36#include <freerdp/log.h>
39#include "credssp_auth.h"
41#define TAG FREERDP_TAG("core.auth")
43#define SERVER_KEY "Software\\%s\\Server"
49 AUTH_STATE_IN_PROGRESS,
53struct rdp_credssp_auth
55 const rdpContext* rdp_ctx;
56 SecurityFunctionTable* table;
58 SEC_WINNT_AUTH_IDENTITY identity;
71 SECURITY_STATUS sspi_error;
72 enum AUTH_STATE state;
76static const char* credssp_auth_state_string(
const rdpCredsspAuth* auth)
81 case AUTH_STATE_INITIAL:
82 return "AUTH_STATE_INITIAL";
83 case AUTH_STATE_CREDS:
84 return "AUTH_STATE_CREDS";
85 case AUTH_STATE_IN_PROGRESS:
86 return "AUTH_STATE_IN_PROGRESS";
87 case AUTH_STATE_FINAL:
88 return "AUTH_STATE_FINAL";
90 return "AUTH_STATE_UNKNOWN";
93static BOOL parseKerberosDeltat(
const char* value, INT32* dest,
const char* message);
94static BOOL credssp_auth_setup_identity(rdpCredsspAuth* auth);
95static SecurityFunctionTable* auth_resolve_sspi_table(
const rdpSettings* settings);
97#define log_status(status, level, ...) \
98 log_status_((status), (level), __FILE__, __func__, __LINE__, __VA_ARGS__)
100WINPR_ATTR_FORMAT_ARG(6, 7)
101static BOOL log_status_(SECURITY_STATUS status, DWORD level, const
char* file, const
char* fkt,
102 size_t line, WINPR_FORMAT_ARG const
char* what, ...)
104 static wLog* log =
nullptr;
108 if (WLog_IsLevelActive(log, level))
110 char fwhat[128] = WINPR_C_ARRAY_INIT;
111 va_list ap = WINPR_C_ARRAY_INIT;
113 (void)vsnprintf(fwhat,
sizeof(fwhat) - 1, what, ap);
116 WLog_PrintTextMessage(log, level, line, file, fkt,
"%s status %s [0x%08" PRIx32
"]", fwhat,
117 GetSecurityStatusString(status),
118 WINPR_CXX_COMPAT_CAST(uint32_t, status));
124static BOOL credssp_auth_update_name_cache(rdpCredsspAuth* auth, TCHAR* name)
128 free(auth->pkgNameA);
129 auth->pkgNameA =
nullptr;
132 auth->pkgNameA = ConvertWCharToUtf8Alloc(name,
nullptr);
134 auth->pkgNameA = _strdup(name);
138rdpCredsspAuth* credssp_auth_new(
const rdpContext* context)
140 WINPR_ASSERT(context);
141 rdpCredsspAuth* auth = calloc(1,
sizeof(rdpCredsspAuth));
144 auth->rdp_ctx = context;
152 WINPR_ASSERT(auth->rdp_ctx);
154 const rdpSettings* settings = auth->rdp_ctx->settings;
155 WINPR_ASSERT(settings);
157 if (!credssp_auth_update_name_cache(auth, pkg_name))
160 auth->table = auth_resolve_sspi_table(settings);
163 WLog_ERR(TAG,
"Unable to initialize sspi table");
168 WINPR_ASSERT(auth->table->QuerySecurityPackageInfo);
169 const SECURITY_STATUS status = auth->table->QuerySecurityPackageInfo(pkg_name, &auth->info);
170 if (status != SEC_E_OK)
171 return log_status(status, WLOG_ERROR,
"QuerySecurityPackageInfo (%s)",
172 credssp_auth_pkg_name(auth));
174 if (!credssp_auth_update_name_cache(auth, auth->info->Name))
177 WLog_DBG(TAG,
"Using package: %s (cbMaxToken: %u bytes)", credssp_auth_pkg_name(auth),
178 auth->info->cbMaxToken);
181 if (!credssp_auth_setup_identity(auth))
184 auth->bindings = bindings;
189static BOOL credssp_auth_setup_auth_data(rdpCredsspAuth* auth,
190 const SEC_WINNT_AUTH_IDENTITY* identity,
193 WINPR_ASSERT(pAuthData);
197 identityEx->Version = SEC_WINNT_AUTH_IDENTITY_VERSION;
198 identityEx->Length =
sizeof(SEC_WINNT_AUTH_IDENTITY_EX);
199 identityEx->User = identity->User;
200 identityEx->UserLength = identity->UserLength;
201 identityEx->Domain = identity->Domain;
202 identityEx->DomainLength = identity->DomainLength;
203 identityEx->Password = identity->Password;
204 identityEx->PasswordLength = identity->PasswordLength;
205 identityEx->Flags = identity->Flags;
206 identityEx->Flags |= SEC_WINNT_AUTH_IDENTITY_UNICODE;
207 identityEx->Flags |= SEC_WINNT_AUTH_IDENTITY_EXTENDED;
209 if (auth->package_list)
211 const size_t len = _wcslen(auth->package_list);
212 if (len > UINT32_MAX)
215 identityEx->PackageList = (UINT16*)auth->package_list;
216 identityEx->PackageListLength = (UINT32)len;
219 pAuthData->ntlmSettings = &auth->ntlmSettings;
220 pAuthData->kerberosSettings = &auth->kerberosSettings;
225static BOOL credssp_auth_client_init_cred_attributes(rdpCredsspAuth* auth)
229 if (!utils_str_is_empty(auth->kerberosSettings.kdcUrl))
231 SECURITY_STATUS status = ERROR_INTERNAL_ERROR;
232 SSIZE_T str_size = 0;
234 str_size = ConvertUtf8ToWChar(auth->kerberosSettings.kdcUrl,
nullptr, 0);
235 if ((str_size <= 0) || (str_size >= UINT16_MAX / 2))
239 const size_t buffer_size =
241 if (buffer_size > UINT32_MAX)
247 secAttr->Version = KDC_PROXY_SETTINGS_V1;
248 secAttr->ProxyServerLength = (UINT16)((
size_t)str_size *
sizeof(WCHAR));
251 if (ConvertUtf8ToWChar(auth->kerberosSettings.kdcUrl, (WCHAR*)(secAttr + 1),
252 (
size_t)str_size) <= 0)
259 if (auth->table->SetCredentialsAttributesW)
260 status = auth->table->SetCredentialsAttributesW(&auth->credentials,
261 SECPKG_CRED_ATTR_KDC_PROXY_SETTINGS,
262 (
void*)secAttr, (UINT32)buffer_size);
264 status = SEC_E_UNSUPPORTED_FUNCTION;
266 if (auth->table->SetCredentialsAttributesA)
267 status = auth->table->SetCredentialsAttributesA(&auth->credentials,
268 SECPKG_CRED_ATTR_KDC_PROXY_SETTINGS,
269 (
void*)secAttr, (UINT32)buffer_size);
271 status = SEC_E_UNSUPPORTED_FUNCTION;
274 if (status != SEC_E_OK)
276 WLog_WARN(TAG,
"Explicit Kerberos KDC URL (%s) injection is not supported",
277 auth->kerberosSettings.kdcUrl);
286BOOL credssp_auth_setup_client(rdpCredsspAuth* auth,
const char* target_service,
287 const char* target_hostname,
const SEC_WINNT_AUTH_IDENTITY* identity,
290 void* pAuthData =
nullptr;
294 WINPR_ASSERT(auth->table);
295 WINPR_ASSERT(auth->info);
297 WINPR_ASSERT(auth->state == AUTH_STATE_INITIAL);
300 if (!credssp_auth_set_spn(auth, target_service, target_hostname))
305 credssp_auth_setup_auth_data(auth, identity, &winprAuthData);
309 auth->kerberosSettings.pkinitX509Identity = _strdup(pkinit);
310 if (!auth->kerberosSettings.pkinitX509Identity)
312 WLog_ERR(TAG,
"unable to copy pkinitArgs");
317 pAuthData = (
void*)&winprAuthData;
320 WINPR_ASSERT(auth->table->AcquireCredentialsHandle);
321 const SECURITY_STATUS status = auth->table->AcquireCredentialsHandle(
322 nullptr, auth->info->Name, SECPKG_CRED_OUTBOUND,
nullptr, pAuthData,
nullptr,
nullptr,
323 &auth->credentials,
nullptr);
325 if (status != SEC_E_OK)
326 return log_status(status, WLOG_ERROR,
"AcquireCredentialsHandleA");
328 if (!credssp_auth_client_init_cred_attributes(auth))
330 WLog_ERR(TAG,
"Fatal error setting credential attributes");
334 auth->state = AUTH_STATE_CREDS;
335 WLog_DBG(TAG,
"Acquired client credentials");
340BOOL credssp_auth_setup_server(rdpCredsspAuth* auth)
342 void* pAuthData =
nullptr;
346 WINPR_ASSERT(auth->table);
348 WINPR_ASSERT(auth->state == AUTH_STATE_INITIAL);
350 if (auth->ntlmSettings.samFile || auth->ntlmSettings.hashCallback ||
351 auth->kerberosSettings.keytab)
353 credssp_auth_setup_auth_data(auth, &auth->identity, &winprAuthData);
354 pAuthData = &winprAuthData;
357 WINPR_ASSERT(auth->table->AcquireCredentialsHandle);
358 const SECURITY_STATUS status = auth->table->AcquireCredentialsHandle(
359 nullptr, auth->info->Name, SECPKG_CRED_INBOUND,
nullptr, pAuthData,
nullptr,
nullptr,
360 &auth->credentials,
nullptr);
361 if (status != SEC_E_OK)
362 return log_status(status, WLOG_ERROR,
"AcquireCredentialsHandleA");
364 auth->state = AUTH_STATE_CREDS;
365 WLog_DBG(TAG,
"Acquired server credentials");
372void credssp_auth_set_flags(rdpCredsspAuth* auth, ULONG flags)
416#define query_logged(auth, ulAttribute, pBuffer) \
417 query_logged_((auth), (ulAttribute), (pBuffer), __FILE__, __func__, __LINE__)
418static SECURITY_STATUS query_logged_(rdpCredsspAuth* auth, ULONG ulAttribute,
void* pBuffer,
419 const char* file,
const char* fkt,
size_t line)
422 WINPR_ASSERT(auth->table);
423 WINPR_ASSERT(auth->table->QueryContextAttributes);
425 SECURITY_STATUS status =
426 auth->table->QueryContextAttributes(&auth->context, ulAttribute, pBuffer);
427 (void)log_status_(status, WLOG_DEBUG, file, fkt, line,
428 "QueryContextAttributes(0x%08" PRIx32
")", ulAttribute);
432int credssp_auth_authenticate(rdpCredsspAuth* auth)
434 SECURITY_STATUS status = ERROR_INTERNAL_ERROR;
435 SecBuffer input_buffers[2] = WINPR_C_ARRAY_INIT;
436 SecBufferDesc input_buffer_desc = { SECBUFFER_VERSION, 1, input_buffers };
440 WINPR_ASSERT(auth->table);
442 SecBufferDesc output_buffer_desc = { SECBUFFER_VERSION, 1, &auth->output_buffer };
446 case AUTH_STATE_CREDS:
447 case AUTH_STATE_IN_PROGRESS:
449 case AUTH_STATE_INITIAL:
450 case AUTH_STATE_FINAL:
451 WLog_ERR(TAG,
"context in invalid state!");
459 context = &auth->context;
460 if (!auth->context.dwLower && !auth->context.dwUpper)
463 input_buffers[0] = auth->input_buffer;
467 input_buffer_desc.cBuffers = 2;
469 input_buffers[1].BufferType = SECBUFFER_CHANNEL_BINDINGS;
470 input_buffers[1].cbBuffer = auth->bindings->BindingsLength;
471 input_buffers[1].pvBuffer = auth->bindings->Bindings;
475 sspi_SecBufferFree(&auth->output_buffer);
476 auth->output_buffer.BufferType = SECBUFFER_TOKEN;
477 if (!sspi_SecBufferAlloc(&auth->output_buffer, auth->info->cbMaxToken))
482 WINPR_ASSERT(auth->table->AcceptSecurityContext);
483 status = auth->table->AcceptSecurityContext(
484 &auth->credentials, context, &input_buffer_desc, auth->flags, SECURITY_NATIVE_DREP,
485 &auth->context, &output_buffer_desc, &auth->flags,
nullptr);
489 WINPR_ASSERT(auth->table->InitializeSecurityContext);
490 status = auth->table->InitializeSecurityContext(
491 &auth->credentials, context, auth->spn, auth->flags, 0, SECURITY_NATIVE_DREP,
492 &input_buffer_desc, 0, &auth->context, &output_buffer_desc, &auth->flags,
nullptr);
495 const rdpSettings* settings = auth->rdp_ctx->settings;
496 WINPR_ASSERT(settings);
500 const size_t len = strlen(name);
502 status = SEC_E_INVALID_PARAMETER;
505 const SECURITY_STATUS sca = auth->table->SetContextAttributesA(
506 &auth->context, SECPKG_ATTR_AUTH_NTLM_HOSTNAME,
507 WINPR_CAST_CONST_PTR_AWAY(name,
char*), WINPR_ASSERTING_INT_CAST(ULONG, len));
508 (void)log_status(sca, WLOG_DEBUG,
"SetContextAttributesA(0x%08" PRIx32
")",
509 SECPKG_ATTR_AUTH_NTLM_HOSTNAME);
515 if (status == SEC_E_OK)
517 WLog_DBG(TAG,
"Authentication complete (output token size: %" PRIu32
" bytes)",
518 auth->output_buffer.cbBuffer);
519 auth->state = AUTH_STATE_FINAL;
523 (void)query_logged(auth, SECPKG_ATTR_SIZES, &auth->sizes);
524 WLog_DBG(TAG,
"Context sizes: cbMaxSignature=%" PRIu32
", cbSecurityTrailer=%" PRIu32
"",
525 auth->sizes.cbMaxSignature, auth->sizes.cbSecurityTrailer);
529 rdpSettings* settings = auth->rdp_ctx->settings;
534 SECURITY_STATUS qstatus = query_logged(auth, SECPKG_ATTR_AUTH_NTLM_HOSTNAME_LEN, &len);
536 if ((qstatus == SEC_E_OK) && (len > 0))
538 void* WorkstationName = calloc(len + 1,
sizeof(WCHAR));
539 if (!WorkstationName)
542 qstatus = query_logged(auth, SECPKG_ATTR_AUTH_NTLM_HOSTNAME, WorkstationName);
543 if (qstatus == SEC_E_OK)
547 WorkstationName, len))
551 WorkstationName, len))
555 free(WorkstationName);
560 else if (status == SEC_I_CONTINUE_NEEDED)
562 WLog_DBG(TAG,
"Authentication in progress... (output token size: %" PRIu32
")",
563 auth->output_buffer.cbBuffer);
564 auth->state = AUTH_STATE_IN_PROGRESS;
569 (void)log_status(status, WLOG_ERROR,
570 auth->server ?
"AcceptSecurityContext" :
"InitializeSecurityContext");
571 auth->sspi_error = status;
577BOOL credssp_auth_encrypt(rdpCredsspAuth* auth,
const SecBuffer* plaintext,
SecBuffer* ciphertext,
578 size_t* signature_length, ULONG sequence)
580 SECURITY_STATUS status = ERROR_INTERNAL_ERROR;
581 SecBuffer buffers[2] = WINPR_C_ARRAY_INIT;
582 SecBufferDesc buffer_desc = { SECBUFFER_VERSION, 2, buffers };
585 WINPR_ASSERT(auth && auth->table);
586 WINPR_ASSERT(plaintext);
587 WINPR_ASSERT(ciphertext);
591 case AUTH_STATE_INITIAL:
592 WLog_ERR(TAG,
"Invalid state %s", credssp_auth_state_string(auth));
599 buf = calloc(1, plaintext->cbBuffer + auth->sizes.cbSecurityTrailer);
603 buffers[0].BufferType = SECBUFFER_TOKEN;
604 buffers[0].cbBuffer = auth->sizes.cbSecurityTrailer;
605 buffers[0].pvBuffer = buf;
607 buffers[1].BufferType = SECBUFFER_DATA;
608 if (plaintext->BufferType & SECBUFFER_READONLY)
609 buffers[1].BufferType |= SECBUFFER_READONLY;
610 buffers[1].pvBuffer = buf + auth->sizes.cbSecurityTrailer;
611 buffers[1].cbBuffer = plaintext->cbBuffer;
612 CopyMemory(buffers[1].pvBuffer, plaintext->pvBuffer, plaintext->cbBuffer);
614 WINPR_ASSERT(auth->table->EncryptMessage);
615 status = auth->table->EncryptMessage(&auth->context, 0, &buffer_desc, sequence);
616 if (status != SEC_E_OK)
619 return log_status(status, WLOG_ERROR,
"EncryptMessage");
622 if (buffers[0].cbBuffer < auth->sizes.cbSecurityTrailer)
625 MoveMemory(((BYTE*)buffers[0].pvBuffer) + buffers[0].cbBuffer, buffers[1].pvBuffer,
626 buffers[1].cbBuffer);
628 auth->sizes.cbSecurityTrailer = buffers[0].cbBuffer;
631 ciphertext->cbBuffer = buffers[0].cbBuffer + buffers[1].cbBuffer;
632 ciphertext->pvBuffer = buf;
634 if (signature_length)
635 *signature_length = buffers[0].cbBuffer;
641BOOL credssp_auth_decrypt(rdpCredsspAuth* auth,
const SecBuffer* ciphertext,
SecBuffer* plaintext,
645 SecBufferDesc buffer_desc = { SECBUFFER_VERSION, 2, buffers };
648 WINPR_ASSERT(auth && auth->table);
649 WINPR_ASSERT(ciphertext);
650 WINPR_ASSERT(plaintext);
654 case AUTH_STATE_INITIAL:
655 WLog_ERR(TAG,
"Invalid state %s", credssp_auth_state_string(auth));
662 if (ciphertext->cbBuffer < auth->sizes.cbSecurityTrailer)
664 WLog_ERR(TAG,
"Encrypted message buffer too small");
670 buffers[0].BufferType = SECBUFFER_TOKEN;
671 buffers[0].pvBuffer = ciphertext->pvBuffer;
672 buffers[0].cbBuffer = auth->sizes.cbSecurityTrailer;
674 buffers[1].BufferType = SECBUFFER_DATA;
675 if (!sspi_SecBufferAlloc(&buffers[1], ciphertext->cbBuffer - auth->sizes.cbSecurityTrailer))
677 CopyMemory(buffers[1].pvBuffer, (BYTE*)ciphertext->pvBuffer + auth->sizes.cbSecurityTrailer,
678 buffers[1].cbBuffer);
680 WINPR_ASSERT(auth->table->DecryptMessage);
681 const SECURITY_STATUS status =
682 auth->table->DecryptMessage(&auth->context, &buffer_desc, sequence, &fqop);
683 if (status != SEC_E_OK)
685 WLog_ERR(TAG,
"DecryptMessage failed with %s [0x%08" PRIx32
"]",
686 GetSecurityStatusString(status), WINPR_CXX_COMPAT_CAST(uint32_t, status));
687 sspi_SecBufferFree(&buffers[1]);
691 *plaintext = buffers[1];
696BOOL credssp_auth_impersonate(rdpCredsspAuth* auth)
698 WINPR_ASSERT(auth && auth->table);
700 WINPR_ASSERT(auth->table->ImpersonateSecurityContext);
701 const SECURITY_STATUS status = auth->table->ImpersonateSecurityContext(&auth->context);
703 if (status != SEC_E_OK)
705 WLog_ERR(TAG,
"ImpersonateSecurityContext failed with %s [0x%08" PRIx32
"]",
706 GetSecurityStatusString(status), WINPR_CXX_COMPAT_CAST(uint32_t, status));
713BOOL credssp_auth_revert_to_self(rdpCredsspAuth* auth)
715 WINPR_ASSERT(auth && auth->table);
717 WINPR_ASSERT(auth->table->RevertSecurityContext);
718 const SECURITY_STATUS status = auth->table->RevertSecurityContext(&auth->context);
720 if (status != SEC_E_OK)
722 WLog_ERR(TAG,
"RevertSecurityContext failed with %s [0x%08" PRIx32
"]",
723 GetSecurityStatusString(status), WINPR_CXX_COMPAT_CAST(uint32_t, status));
730void credssp_auth_take_input_buffer(rdpCredsspAuth* auth,
SecBuffer* buffer)
733 WINPR_ASSERT(buffer);
735 sspi_SecBufferFree(&auth->input_buffer);
737 auth->input_buffer = *buffer;
738 auth->input_buffer.BufferType = SECBUFFER_TOKEN;
745const SecBuffer* credssp_auth_get_output_buffer(
const rdpCredsspAuth* auth)
748 return &auth->output_buffer;
751BOOL credssp_auth_have_output_token(rdpCredsspAuth* auth)
754 return (auth->output_buffer.cbBuffer != 0);
757BOOL credssp_auth_is_complete(
const rdpCredsspAuth* auth)
760 return auth->state == AUTH_STATE_FINAL;
763size_t credssp_auth_trailer_size(
const rdpCredsspAuth* auth)
766 return auth->sizes.cbSecurityTrailer;
769const char* credssp_auth_pkg_name(
const rdpCredsspAuth* auth)
771 WINPR_ASSERT(auth && auth->info);
772 return auth->pkgNameA;
775INT32 credssp_auth_sspi_error(
const rdpCredsspAuth* auth)
778 return auth->sspi_error;
781void credssp_auth_tableAndContext(rdpCredsspAuth* auth, SecurityFunctionTable** ptable,
785 WINPR_ASSERT(ptable);
786 WINPR_ASSERT(pcontext);
788 *ptable = auth->table;
789 *pcontext = auth->context;
792void credssp_auth_free(rdpCredsspAuth* auth)
804 case AUTH_STATE_IN_PROGRESS:
805 case AUTH_STATE_FINAL:
806 WINPR_ASSERT(auth->table->DeleteSecurityContext);
807 auth->table->DeleteSecurityContext(&auth->context);
810 case AUTH_STATE_CREDS:
811 WINPR_ASSERT(auth->table->FreeCredentialsHandle);
812 auth->table->FreeCredentialsHandle(&auth->credentials);
814 case AUTH_STATE_INITIAL:
821 WINPR_ASSERT(auth->table->FreeContextBuffer);
822 auth->table->FreeContextBuffer(auth->info);
826 sspi_FreeAuthIdentity(&auth->identity);
828 krb_settings = &auth->kerberosSettings;
829 ntlm_settings = &auth->ntlmSettings;
831 free(krb_settings->kdcUrl);
832 free(krb_settings->cache);
833 free(krb_settings->keytab);
834 free(krb_settings->armorCache);
835 free(krb_settings->pkinitX509Anchors);
836 free(krb_settings->pkinitX509Identity);
839 free(auth->package_list);
841 sspi_SecBufferFree(&auth->input_buffer);
842 sspi_SecBufferFree(&auth->output_buffer);
843 credssp_auth_update_name_cache(auth,
nullptr);
847static void auth_get_sspi_module_from_reg(
char** sspi_module)
853 WINPR_ASSERT(sspi_module);
854 *sspi_module =
nullptr;
856 char* key = freerdp_getApplicatonDetailsRegKey(SERVER_KEY);
860 const LONG rc = RegOpenKeyExA(HKEY_LOCAL_MACHINE, key, 0, KEY_READ | KEY_WOW64_64KEY, &hKey);
863 if (rc != ERROR_SUCCESS)
866 if (RegQueryValueExA(hKey,
"SspiModule",
nullptr, &dwType,
nullptr, &dwSize) != ERROR_SUCCESS)
872 char*
module = (LPSTR)calloc(dwSize + sizeof(CHAR), sizeof(char));
879 if (RegQueryValueExA(hKey,
"SspiModule",
nullptr, &dwType, (BYTE*)module, &dwSize) !=
888 *sspi_module =
module;
891static SecurityFunctionTable* auth_resolve_sspi_table(
const rdpSettings* settings)
893 char* sspi_module =
nullptr;
895 WINPR_ASSERT(settings);
897 if (settings->ServerMode)
898 auth_get_sspi_module_from_reg(&sspi_module);
900 if (sspi_module || settings->SspiModule)
902 const char* module_name = sspi_module ? sspi_module : settings->SspiModule;
904 const char* proc_name =
"InitSecurityInterfaceW";
906 const char* proc_name =
"InitSecurityInterfaceA";
909 HMODULE hSSPI = LoadLibraryX(module_name);
913 WLog_ERR(TAG,
"Failed to load SSPI module: %s", module_name);
918 WLog_INFO(TAG,
"Using SSPI Module: %s", module_name);
920 INIT_SECURITY_INTERFACE InitSecurityInterface_ptr =
921 GetProcAddressAs(hSSPI, proc_name, INIT_SECURITY_INTERFACE);
922 if (!InitSecurityInterface_ptr)
924 WLog_ERR(TAG,
"Failed to load SSPI module: %s, no function %s", module_name, proc_name);
929 return InitSecurityInterface_ptr();
932 return InitSecurityInterfaceEx(0);
935static BOOL credssp_auth_setup_identity(rdpCredsspAuth* auth)
937 const rdpSettings* settings =
nullptr;
940 freerdp_peer* peer =
nullptr;
943 WINPR_ASSERT(auth->rdp_ctx);
945 peer = auth->rdp_ctx->peer;
946 settings = auth->rdp_ctx->settings;
947 WINPR_ASSERT(settings);
949 krb_settings = &auth->kerberosSettings;
950 ntlm_settings = &auth->ntlmSettings;
952 if (settings->KerberosLifeTime)
953 parseKerberosDeltat(settings->KerberosLifeTime, &krb_settings->lifeTime,
"lifetime");
954 if (settings->KerberosStartTime)
955 parseKerberosDeltat(settings->KerberosStartTime, &krb_settings->startTime,
"starttime");
956 if (settings->KerberosRenewableLifeTime)
957 parseKerberosDeltat(settings->KerberosRenewableLifeTime, &krb_settings->renewLifeTime,
960 if (settings->KerberosKdcUrl)
962 krb_settings->kdcUrl = _strdup(settings->KerberosKdcUrl);
963 if (!krb_settings->kdcUrl)
965 WLog_ERR(TAG,
"unable to copy kdcUrl");
970 if (settings->KerberosCache)
972 krb_settings->cache = _strdup(settings->KerberosCache);
973 if (!krb_settings->cache)
975 WLog_ERR(TAG,
"unable to copy cache name");
980 if (settings->KerberosKeytab)
982 krb_settings->keytab = _strdup(settings->KerberosKeytab);
983 if (!krb_settings->keytab)
985 WLog_ERR(TAG,
"unable to copy keytab name");
990 if (settings->KerberosArmor)
992 krb_settings->armorCache = _strdup(settings->KerberosArmor);
993 if (!krb_settings->armorCache)
995 WLog_ERR(TAG,
"unable to copy armorCache");
1000 if (settings->PkinitAnchors)
1002 krb_settings->pkinitX509Anchors = _strdup(settings->PkinitAnchors);
1003 if (!krb_settings->pkinitX509Anchors)
1005 WLog_ERR(TAG,
"unable to copy pkinitX509Anchors");
1010 if (settings->NtlmSamFile)
1012 ntlm_settings->
samFile = _strdup(settings->NtlmSamFile);
1015 WLog_ERR(TAG,
"unable to copy samFile");
1020 if (peer && peer->SspiNtlmHashCallback)
1022 ntlm_settings->
hashCallback = peer->SspiNtlmHashCallback;
1026 if (settings->AuthenticationPackageList)
1028 auth->package_list = ConvertUtf8ToWCharAlloc(settings->AuthenticationPackageList,
nullptr);
1029 if (!auth->package_list)
1033 auth->identity.Flags |= SEC_WINNT_AUTH_IDENTITY_UNICODE;
1034 auth->identity.Flags |= SEC_WINNT_AUTH_IDENTITY_EXTENDED;
1039BOOL credssp_auth_set_spn(rdpCredsspAuth* auth,
const char* service,
const char* hostname)
1042 char* spn =
nullptr;
1050 spn = _strdup(hostname);
1053 length = strlen(service) + strlen(hostname) + 2;
1054 spn = calloc(length + 1,
sizeof(
char));
1058 (void)sprintf_s(spn, length,
"%s/%s", service, hostname);
1064 auth->spn = ConvertUtf8ToWCharAlloc(spn,
nullptr);
1073static const char* parseInt(
const char* v, INT32* r)
1078 if (!*v || !((*v >=
'0') && (*v <=
'9')))
1081 for (; *v && (*v >=
'0') && (*v <=
'9'); v++)
1083 *r = (*r * 10) + (*v -
'0');
1089static BOOL parseKerberosDeltat(
const char* value, INT32* dest,
const char* message)
1092 const char* ptr =
nullptr;
1094 WINPR_ASSERT(value);
1096 WINPR_ASSERT(message);
1103 ptr = strchr(value,
':');
1108 value = parseInt(value, &v);
1109 if (!value || *value !=
':')
1111 WLog_ERR(TAG,
"Invalid value for %s", message);
1117 value = parseInt(value + 1, &v);
1118 if (!value || (*value != 0 && *value !=
':') || (v > 60))
1120 WLog_ERR(TAG,
"Invalid value for %s", message);
1128 value = parseInt(value + 1, &v);
1129 if (!value || (*value != 0) || (v > 60))
1131 WLog_ERR(TAG,
"Invalid value for %s", message);
1140 value = parseInt(value, &v);
1143 WLog_ERR(TAG,
"Invalid value for %s", message);
1147 if (!*value || isspace(*value))
1179 WLog_ERR(TAG,
"invalid value for unit %c when parsing %s", *value, message);
1183 if ((maxValue > 0) && (v > maxValue))
1185 WLog_ERR(TAG,
"invalid value for unit %c when parsing %s", *value, message);
1189 *dest += (v * factor);
1194 value = parseInt(value, &v);
1195 if (!value || !*value)
1197 WLog_ERR(TAG,
"Invalid value for %s", message);
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.
WINPR_ATTR_NODISCARD psSspiNtlmHashCallback hashCallback