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;
59 SEC_WINPR_NTLM_SETTINGS_V2* ntlmSettingsV2;
60 SEC_WINPR_KERBEROS_SETTINGS_V2* kerberosSettingsV2;
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));
145 auth->ntlmSettingsV2 = sspi_AllocSecNtlmSettings();
146 if (!auth->ntlmSettingsV2)
149 auth->kerberosSettingsV2 = sspi_AllocSecKerberosSettings();
150 if (!auth->kerberosSettingsV2)
153 auth->rdp_ctx = context;
158 credssp_auth_free(auth);
165 WINPR_ASSERT(auth->rdp_ctx);
167 const rdpSettings* settings = auth->rdp_ctx->settings;
168 WINPR_ASSERT(settings);
170 if (!credssp_auth_update_name_cache(auth, pkg_name))
173 auth->table = auth_resolve_sspi_table(settings);
176 WLog_ERR(TAG,
"Unable to initialize sspi table");
181 WINPR_ASSERT(auth->table->QuerySecurityPackageInfo);
182 const SECURITY_STATUS status = auth->table->QuerySecurityPackageInfo(pkg_name, &auth->info);
183 if (status != SEC_E_OK)
184 return log_status(status, WLOG_ERROR,
"QuerySecurityPackageInfo (%s)",
185 credssp_auth_pkg_name(auth));
187 if (!credssp_auth_update_name_cache(auth, auth->info->Name))
190 WLog_DBG(TAG,
"Using package: %s (cbMaxToken: %u bytes)", credssp_auth_pkg_name(auth),
191 auth->info->cbMaxToken);
194 if (!credssp_auth_setup_identity(auth))
197 auth->bindings = bindings;
202static BOOL credssp_auth_setup_auth_data(rdpCredsspAuth* auth,
203 const SEC_WINNT_AUTH_IDENTITY* identity,
204 SEC_WINNT_AUTH_IDENTITY_WINPR_V2* pAuthData)
206 WINPR_ASSERT(pAuthData);
207 ZeroMemory(pAuthData,
sizeof(SEC_WINNT_AUTH_IDENTITY_WINPR_V2));
210 identityEx->Version = SEC_WINNT_AUTH_IDENTITY_VERSION;
211 identityEx->Length =
sizeof(SEC_WINNT_AUTH_IDENTITY_EX);
212 identityEx->User = identity->User;
213 identityEx->UserLength = identity->UserLength;
214 identityEx->Domain = identity->Domain;
215 identityEx->DomainLength = identity->DomainLength;
216 identityEx->Password = identity->Password;
217 identityEx->PasswordLength = identity->PasswordLength;
218 identityEx->Flags = identity->Flags;
219 identityEx->Flags |= SEC_WINNT_AUTH_IDENTITY_UNICODE;
220 identityEx->Flags |= SEC_WINNT_AUTH_IDENTITY_EXTENDED_v2;
222 if (auth->package_list)
224 const size_t len = _wcslen(auth->package_list);
225 if (len > UINT32_MAX)
228 identityEx->PackageList = (UINT16*)auth->package_list;
229 identityEx->PackageListLength = (UINT32)len;
232 pAuthData->version = SEC_WINNT_AUTH_IDENTITY_WINPR_V2_REVISION_1;
233 pAuthData->ntlmSettingsV2 = auth->ntlmSettingsV2;
234 WINPR_ASSERT(pAuthData->ntlmSettingsV2);
236 pAuthData->kerberosSettingsV2 = auth->kerberosSettingsV2;
237 WINPR_ASSERT(pAuthData->kerberosSettingsV2);
242static BOOL credssp_auth_client_init_cred_attributes(rdpCredsspAuth* auth)
246 if (!utils_str_is_empty(auth->kerberosSettingsV2->kdcUrl))
248 SECURITY_STATUS status = ERROR_INTERNAL_ERROR;
249 SSIZE_T str_size = 0;
251 str_size = ConvertUtf8ToWChar(auth->kerberosSettingsV2->kdcUrl,
nullptr, 0);
252 if ((str_size <= 0) || (str_size >= UINT16_MAX / 2))
256 const size_t buffer_size =
258 if (buffer_size > UINT32_MAX)
264 secAttr->Version = KDC_PROXY_SETTINGS_V1;
265 secAttr->ProxyServerLength = (UINT16)((
size_t)str_size *
sizeof(WCHAR));
268 if (ConvertUtf8ToWChar(auth->kerberosSettingsV2->kdcUrl, (WCHAR*)(secAttr + 1),
269 (
size_t)str_size) <= 0)
276 if (auth->table->SetCredentialsAttributesW)
277 status = auth->table->SetCredentialsAttributesW(&auth->credentials,
278 SECPKG_CRED_ATTR_KDC_PROXY_SETTINGS,
279 (
void*)secAttr, (UINT32)buffer_size);
281 status = SEC_E_UNSUPPORTED_FUNCTION;
283 if (auth->table->SetCredentialsAttributesA)
284 status = auth->table->SetCredentialsAttributesA(&auth->credentials,
285 SECPKG_CRED_ATTR_KDC_PROXY_SETTINGS,
286 (
void*)secAttr, (UINT32)buffer_size);
288 status = SEC_E_UNSUPPORTED_FUNCTION;
291 if (status != SEC_E_OK)
293 WLog_WARN(TAG,
"Explicit Kerberos KDC URL (%s) injection is not supported",
294 auth->kerberosSettingsV2->kdcUrl);
303BOOL credssp_auth_setup_client(rdpCredsspAuth* auth,
const char* target_service,
304 const char* target_hostname,
const SEC_WINNT_AUTH_IDENTITY* identity,
307 void* pAuthData =
nullptr;
308 SEC_WINNT_AUTH_IDENTITY_WINPR_V2 winprAuthData = WINPR_C_ARRAY_INIT;
311 WINPR_ASSERT(auth->table);
312 WINPR_ASSERT(auth->info);
314 WINPR_ASSERT(auth->state == AUTH_STATE_INITIAL);
317 if (!credssp_auth_set_spn(auth, target_service, target_hostname))
322 credssp_auth_setup_auth_data(auth, identity, &winprAuthData);
326 if (!sspi_CloneSecSettingsString(&auth->kerberosSettingsV2->pkinitX509Identity, pkinit))
328 WLog_ERR(TAG,
"unable to copy pkinitArgs");
333 pAuthData = (
void*)&winprAuthData;
336 WINPR_ASSERT(auth->table->AcquireCredentialsHandle);
337 const SECURITY_STATUS status = auth->table->AcquireCredentialsHandle(
338 nullptr, auth->info->Name, SECPKG_CRED_OUTBOUND,
nullptr, pAuthData,
nullptr,
nullptr,
339 &auth->credentials,
nullptr);
341 if (status != SEC_E_OK)
342 return log_status(status, WLOG_ERROR,
"AcquireCredentialsHandleA");
344 if (!credssp_auth_client_init_cred_attributes(auth))
346 WLog_ERR(TAG,
"Fatal error setting credential attributes");
350 auth->state = AUTH_STATE_CREDS;
351 WLog_DBG(TAG,
"Acquired client credentials");
356BOOL credssp_auth_setup_server(rdpCredsspAuth* auth)
358 void* pAuthData =
nullptr;
359 SEC_WINNT_AUTH_IDENTITY_WINPR_V2 winprAuthData = WINPR_C_ARRAY_INIT;
362 WINPR_ASSERT(auth->table);
364 WINPR_ASSERT(auth->state == AUTH_STATE_INITIAL);
366 if (auth->ntlmSettingsV2->samFile || auth->ntlmSettingsV2->hashCallback ||
367 auth->kerberosSettingsV2->keytab)
369 credssp_auth_setup_auth_data(auth, &auth->identity, &winprAuthData);
370 pAuthData = &winprAuthData;
373 WINPR_ASSERT(auth->table->AcquireCredentialsHandle);
374 const SECURITY_STATUS status = auth->table->AcquireCredentialsHandle(
375 nullptr, auth->info->Name, SECPKG_CRED_INBOUND,
nullptr, pAuthData,
nullptr,
nullptr,
376 &auth->credentials,
nullptr);
377 if (status != SEC_E_OK)
378 return log_status(status, WLOG_ERROR,
"AcquireCredentialsHandleA");
380 auth->state = AUTH_STATE_CREDS;
381 WLog_DBG(TAG,
"Acquired server credentials");
388void credssp_auth_set_flags(rdpCredsspAuth* auth, ULONG flags)
432#define query_logged(auth, ulAttribute, pBuffer) \
433 query_logged_((auth), (ulAttribute), (pBuffer), __FILE__, __func__, __LINE__)
434static SECURITY_STATUS query_logged_(rdpCredsspAuth* auth, ULONG ulAttribute,
void* pBuffer,
435 const char* file,
const char* fkt,
size_t line)
438 WINPR_ASSERT(auth->table);
439 WINPR_ASSERT(auth->table->QueryContextAttributes);
441 SECURITY_STATUS status =
442 auth->table->QueryContextAttributes(&auth->context, ulAttribute, pBuffer);
443 (void)log_status_(status, WLOG_DEBUG, file, fkt, line,
444 "QueryContextAttributes(0x%08" PRIx32
")", ulAttribute);
448int credssp_auth_authenticate(rdpCredsspAuth* auth)
450 SECURITY_STATUS status = ERROR_INTERNAL_ERROR;
451 SecBuffer input_buffers[2] = WINPR_C_ARRAY_INIT;
452 SecBufferDesc input_buffer_desc = { SECBUFFER_VERSION, 1, input_buffers };
456 WINPR_ASSERT(auth->table);
458 SecBufferDesc output_buffer_desc = { SECBUFFER_VERSION, 1, &auth->output_buffer };
462 case AUTH_STATE_CREDS:
463 case AUTH_STATE_IN_PROGRESS:
465 case AUTH_STATE_INITIAL:
466 case AUTH_STATE_FINAL:
467 WLog_ERR(TAG,
"context in invalid state!");
475 context = &auth->context;
476 if (!auth->context.dwLower && !auth->context.dwUpper)
479 input_buffers[0] = auth->input_buffer;
483 input_buffer_desc.cBuffers = 2;
485 input_buffers[1].BufferType = SECBUFFER_CHANNEL_BINDINGS;
486 input_buffers[1].cbBuffer = auth->bindings->BindingsLength;
487 input_buffers[1].pvBuffer = auth->bindings->Bindings;
491 sspi_SecBufferFree(&auth->output_buffer);
492 auth->output_buffer.BufferType = SECBUFFER_TOKEN;
493 if (!sspi_SecBufferAlloc(&auth->output_buffer, auth->info->cbMaxToken))
498 WINPR_ASSERT(auth->table->AcceptSecurityContext);
499 status = auth->table->AcceptSecurityContext(
500 &auth->credentials, context, &input_buffer_desc, auth->flags, SECURITY_NATIVE_DREP,
501 &auth->context, &output_buffer_desc, &auth->flags,
nullptr);
505 WINPR_ASSERT(auth->table->InitializeSecurityContext);
506 status = auth->table->InitializeSecurityContext(
507 &auth->credentials, context, auth->spn, auth->flags, 0, SECURITY_NATIVE_DREP,
508 &input_buffer_desc, 0, &auth->context, &output_buffer_desc, &auth->flags,
nullptr);
511 const rdpSettings* settings = auth->rdp_ctx->settings;
512 WINPR_ASSERT(settings);
516 const size_t len = strlen(name);
518 status = SEC_E_INVALID_PARAMETER;
521 const SECURITY_STATUS sca = auth->table->SetContextAttributesA(
522 &auth->context, SECPKG_ATTR_AUTH_NTLM_HOSTNAME,
523 WINPR_CAST_CONST_PTR_AWAY(name,
char*), WINPR_ASSERTING_INT_CAST(ULONG, len));
524 (void)log_status(sca, WLOG_DEBUG,
"SetContextAttributesA(0x%08" PRIx32
")",
525 SECPKG_ATTR_AUTH_NTLM_HOSTNAME);
531 if (status == SEC_E_OK)
533 WLog_DBG(TAG,
"Authentication complete (output token size: %" PRIu32
" bytes)",
534 auth->output_buffer.cbBuffer);
535 auth->state = AUTH_STATE_FINAL;
539 (void)query_logged(auth, SECPKG_ATTR_SIZES, &auth->sizes);
540 WLog_DBG(TAG,
"Context sizes: cbMaxSignature=%" PRIu32
", cbSecurityTrailer=%" PRIu32
"",
541 auth->sizes.cbMaxSignature, auth->sizes.cbSecurityTrailer);
545 rdpSettings* settings = auth->rdp_ctx->settings;
550 SECURITY_STATUS qstatus = query_logged(auth, SECPKG_ATTR_AUTH_NTLM_HOSTNAME_LEN, &len);
552 if ((qstatus == SEC_E_OK) && (len > 0))
554 void* WorkstationName = calloc(len + 1,
sizeof(WCHAR));
555 if (!WorkstationName)
558 qstatus = query_logged(auth, SECPKG_ATTR_AUTH_NTLM_HOSTNAME, WorkstationName);
559 if (qstatus == SEC_E_OK)
563 WorkstationName, len))
567 WorkstationName, len))
571 free(WorkstationName);
576 else if (status == SEC_I_CONTINUE_NEEDED)
578 WLog_DBG(TAG,
"Authentication in progress... (output token size: %" PRIu32
")",
579 auth->output_buffer.cbBuffer);
580 auth->state = AUTH_STATE_IN_PROGRESS;
585 (void)log_status(status, WLOG_ERROR,
586 auth->server ?
"AcceptSecurityContext" :
"InitializeSecurityContext");
587 auth->sspi_error = status;
593BOOL credssp_auth_encrypt(rdpCredsspAuth* auth,
const SecBuffer* plaintext,
SecBuffer* ciphertext,
594 size_t* signature_length, ULONG sequence)
596 SECURITY_STATUS status = ERROR_INTERNAL_ERROR;
597 SecBuffer buffers[2] = WINPR_C_ARRAY_INIT;
598 SecBufferDesc buffer_desc = { SECBUFFER_VERSION, 2, buffers };
601 WINPR_ASSERT(auth && auth->table);
602 WINPR_ASSERT(plaintext);
603 WINPR_ASSERT(ciphertext);
607 case AUTH_STATE_INITIAL:
608 WLog_ERR(TAG,
"Invalid state %s", credssp_auth_state_string(auth));
615 buf = calloc(1, plaintext->cbBuffer + auth->sizes.cbSecurityTrailer);
619 buffers[0].BufferType = SECBUFFER_TOKEN;
620 buffers[0].cbBuffer = auth->sizes.cbSecurityTrailer;
621 buffers[0].pvBuffer = buf;
623 buffers[1].BufferType = SECBUFFER_DATA;
624 if (plaintext->BufferType & SECBUFFER_READONLY)
625 buffers[1].BufferType |= SECBUFFER_READONLY;
626 buffers[1].pvBuffer = buf + auth->sizes.cbSecurityTrailer;
627 buffers[1].cbBuffer = plaintext->cbBuffer;
628 CopyMemory(buffers[1].pvBuffer, plaintext->pvBuffer, plaintext->cbBuffer);
630 WINPR_ASSERT(auth->table->EncryptMessage);
631 status = auth->table->EncryptMessage(&auth->context, 0, &buffer_desc, sequence);
632 if (status != SEC_E_OK)
635 return log_status(status, WLOG_ERROR,
"EncryptMessage");
638 if (buffers[0].cbBuffer < auth->sizes.cbSecurityTrailer)
641 MoveMemory(((BYTE*)buffers[0].pvBuffer) + buffers[0].cbBuffer, buffers[1].pvBuffer,
642 buffers[1].cbBuffer);
644 auth->sizes.cbSecurityTrailer = buffers[0].cbBuffer;
647 ciphertext->cbBuffer = buffers[0].cbBuffer + buffers[1].cbBuffer;
648 ciphertext->pvBuffer = buf;
650 if (signature_length)
651 *signature_length = buffers[0].cbBuffer;
657BOOL credssp_auth_decrypt(rdpCredsspAuth* auth,
const SecBuffer* ciphertext,
SecBuffer* plaintext,
661 SecBufferDesc buffer_desc = { SECBUFFER_VERSION, 2, buffers };
664 WINPR_ASSERT(auth && auth->table);
665 WINPR_ASSERT(ciphertext);
666 WINPR_ASSERT(plaintext);
670 case AUTH_STATE_INITIAL:
671 WLog_ERR(TAG,
"Invalid state %s", credssp_auth_state_string(auth));
678 if (ciphertext->cbBuffer < auth->sizes.cbSecurityTrailer)
680 WLog_ERR(TAG,
"Encrypted message buffer too small");
686 buffers[0].BufferType = SECBUFFER_TOKEN;
687 buffers[0].pvBuffer = ciphertext->pvBuffer;
688 buffers[0].cbBuffer = auth->sizes.cbSecurityTrailer;
690 buffers[1].BufferType = SECBUFFER_DATA;
691 if (!sspi_SecBufferAlloc(&buffers[1], ciphertext->cbBuffer - auth->sizes.cbSecurityTrailer))
693 CopyMemory(buffers[1].pvBuffer, (BYTE*)ciphertext->pvBuffer + auth->sizes.cbSecurityTrailer,
694 buffers[1].cbBuffer);
696 WINPR_ASSERT(auth->table->DecryptMessage);
697 const SECURITY_STATUS status =
698 auth->table->DecryptMessage(&auth->context, &buffer_desc, sequence, &fqop);
699 if (status != SEC_E_OK)
701 WLog_ERR(TAG,
"DecryptMessage failed with %s [0x%08" PRIx32
"]",
702 GetSecurityStatusString(status), WINPR_CXX_COMPAT_CAST(uint32_t, status));
703 sspi_SecBufferFree(&buffers[1]);
707 *plaintext = buffers[1];
712BOOL credssp_auth_impersonate(rdpCredsspAuth* auth)
714 WINPR_ASSERT(auth && auth->table);
716 WINPR_ASSERT(auth->table->ImpersonateSecurityContext);
717 const SECURITY_STATUS status = auth->table->ImpersonateSecurityContext(&auth->context);
719 if (status != SEC_E_OK)
721 WLog_ERR(TAG,
"ImpersonateSecurityContext failed with %s [0x%08" PRIx32
"]",
722 GetSecurityStatusString(status), WINPR_CXX_COMPAT_CAST(uint32_t, status));
729BOOL credssp_auth_revert_to_self(rdpCredsspAuth* auth)
731 WINPR_ASSERT(auth && auth->table);
733 WINPR_ASSERT(auth->table->RevertSecurityContext);
734 const SECURITY_STATUS status = auth->table->RevertSecurityContext(&auth->context);
736 if (status != SEC_E_OK)
738 WLog_ERR(TAG,
"RevertSecurityContext failed with %s [0x%08" PRIx32
"]",
739 GetSecurityStatusString(status), WINPR_CXX_COMPAT_CAST(uint32_t, status));
746void credssp_auth_take_input_buffer(rdpCredsspAuth* auth,
SecBuffer* buffer)
749 WINPR_ASSERT(buffer);
751 sspi_SecBufferFree(&auth->input_buffer);
753 auth->input_buffer = *buffer;
754 auth->input_buffer.BufferType = SECBUFFER_TOKEN;
761const SecBuffer* credssp_auth_get_output_buffer(
const rdpCredsspAuth* auth)
764 return &auth->output_buffer;
767BOOL credssp_auth_have_output_token(rdpCredsspAuth* auth)
770 return (auth->output_buffer.cbBuffer != 0);
773BOOL credssp_auth_is_complete(
const rdpCredsspAuth* auth)
776 return auth->state == AUTH_STATE_FINAL;
779size_t credssp_auth_trailer_size(
const rdpCredsspAuth* auth)
782 return auth->sizes.cbSecurityTrailer;
785const char* credssp_auth_pkg_name(
const rdpCredsspAuth* auth)
787 WINPR_ASSERT(auth && auth->info);
788 return auth->pkgNameA;
791INT32 credssp_auth_sspi_error(
const rdpCredsspAuth* auth)
794 return auth->sspi_error;
797void credssp_auth_tableAndContext(rdpCredsspAuth* auth, SecurityFunctionTable** ptable,
801 WINPR_ASSERT(ptable);
802 WINPR_ASSERT(pcontext);
804 *ptable = auth->table;
805 *pcontext = auth->context;
808void credssp_auth_free(rdpCredsspAuth* auth)
817 case AUTH_STATE_IN_PROGRESS:
818 case AUTH_STATE_FINAL:
819 WINPR_ASSERT(auth->table->DeleteSecurityContext);
820 auth->table->DeleteSecurityContext(&auth->context);
823 case AUTH_STATE_CREDS:
824 WINPR_ASSERT(auth->table->FreeCredentialsHandle);
825 auth->table->FreeCredentialsHandle(&auth->credentials);
827 case AUTH_STATE_INITIAL:
834 WINPR_ASSERT(auth->table->FreeContextBuffer);
835 auth->table->FreeContextBuffer(auth->info);
839 sspi_FreeAuthIdentity(&auth->identity);
840 sspi_FreeSecKerberosSettings(auth->kerberosSettingsV2);
841 sspi_FreeSecNtlmSettings(auth->ntlmSettingsV2);
843 free(auth->package_list);
845 sspi_SecBufferFree(&auth->input_buffer);
846 sspi_SecBufferFree(&auth->output_buffer);
847 credssp_auth_update_name_cache(auth,
nullptr);
851static void auth_get_sspi_module_from_reg(
char** sspi_module)
857 WINPR_ASSERT(sspi_module);
858 *sspi_module =
nullptr;
860 char* key = freerdp_getApplicatonDetailsRegKey(SERVER_KEY);
864 const LONG rc = RegOpenKeyExA(HKEY_LOCAL_MACHINE, key, 0, KEY_READ | KEY_WOW64_64KEY, &hKey);
867 if (rc != ERROR_SUCCESS)
870 if (RegQueryValueExA(hKey,
"SspiModule",
nullptr, &dwType,
nullptr, &dwSize) != ERROR_SUCCESS)
876 char*
module = (LPSTR)calloc(dwSize + sizeof(CHAR), sizeof(char));
883 if (RegQueryValueExA(hKey,
"SspiModule",
nullptr, &dwType, (BYTE*)module, &dwSize) !=
892 *sspi_module =
module;
895static SecurityFunctionTable* auth_resolve_sspi_table(
const rdpSettings* settings)
897 char* sspi_module =
nullptr;
899 WINPR_ASSERT(settings);
901 if (settings->ServerMode)
902 auth_get_sspi_module_from_reg(&sspi_module);
904 if (sspi_module || settings->SspiModule)
906 const char* module_name = sspi_module ? sspi_module : settings->SspiModule;
908 const char* proc_name =
"InitSecurityInterfaceW";
910 const char* proc_name =
"InitSecurityInterfaceA";
913 HMODULE hSSPI = LoadLibraryX(module_name);
917 WLog_ERR(TAG,
"Failed to load SSPI module: %s", module_name);
922 WLog_INFO(TAG,
"Using SSPI Module: %s", module_name);
924 INIT_SECURITY_INTERFACE InitSecurityInterface_ptr =
925 GetProcAddressAs(hSSPI, proc_name, INIT_SECURITY_INTERFACE);
926 if (!InitSecurityInterface_ptr)
928 WLog_ERR(TAG,
"Failed to load SSPI module: %s, no function %s", module_name, proc_name);
933 return InitSecurityInterface_ptr();
936 return InitSecurityInterfaceEx(0);
939static BOOL credssp_auth_setup_identity(rdpCredsspAuth* auth)
942 WINPR_ASSERT(auth->rdp_ctx);
944 freerdp_peer* peer = auth->rdp_ctx->peer;
945 const rdpSettings* settings = auth->rdp_ctx->settings;
946 WINPR_ASSERT(settings);
948 SEC_WINPR_KERBEROS_SETTINGS_V2* krb_settings = auth->kerberosSettingsV2;
949 WINPR_ASSERT(krb_settings);
951 SEC_WINPR_NTLM_SETTINGS_V2* ntlm_settings = auth->ntlmSettingsV2;
952 WINPR_ASSERT(ntlm_settings);
954 if (settings->KerberosLifeTime)
955 parseKerberosDeltat(settings->KerberosLifeTime, &krb_settings->lifeTime,
"lifetime");
956 if (settings->KerberosStartTime)
957 parseKerberosDeltat(settings->KerberosStartTime, &krb_settings->startTime,
"starttime");
958 if (settings->KerberosRenewableLifeTime)
959 parseKerberosDeltat(settings->KerberosRenewableLifeTime, &krb_settings->renewLifeTime,
962 if (settings->KerberosKdcUrl)
964 if (!sspi_CloneSecSettingsString(&krb_settings->kdcUrl, settings->KerberosKdcUrl))
966 WLog_ERR(TAG,
"unable to copy kdcUrl");
971 if (settings->KerberosCache)
973 if (!sspi_CloneSecSettingsString(&krb_settings->cache, settings->KerberosCache))
975 WLog_ERR(TAG,
"unable to copy cache name");
980 if (settings->KerberosKeytab)
982 if (!sspi_CloneSecSettingsString(&krb_settings->keytab, settings->KerberosKeytab))
983 if (!krb_settings->keytab)
985 WLog_ERR(TAG,
"unable to copy keytab name");
990 if (settings->KerberosArmor)
992 if (!sspi_CloneSecSettingsString(&krb_settings->armorCache, settings->KerberosArmor))
994 WLog_ERR(TAG,
"unable to copy armorCache");
999 if (settings->PkinitAnchors)
1001 if (!sspi_CloneSecSettingsString(&krb_settings->pkinitX509Anchors, settings->PkinitAnchors))
1003 WLog_ERR(TAG,
"unable to copy pkinitX509Anchors");
1008 if (settings->NtlmSamFile)
1010 if (!sspi_CloneSecSettingsString(&ntlm_settings->samFile, settings->NtlmSamFile))
1012 WLog_ERR(TAG,
"unable to copy samFile");
1017 if (peer && peer->SspiNtlmHashCallback)
1019 ntlm_settings->hashCallback = peer->SspiNtlmHashCallback;
1020 ntlm_settings->hashCallbackArg = peer;
1023 if (settings->AuthenticationPackageList)
1025 auth->package_list = ConvertUtf8ToWCharAlloc(settings->AuthenticationPackageList,
nullptr);
1026 if (!auth->package_list)
1030 auth->identity.Flags |= SEC_WINNT_AUTH_IDENTITY_UNICODE;
1031 auth->identity.Flags |= SEC_WINNT_AUTH_IDENTITY_EXTENDED_v2;
1036BOOL credssp_auth_set_spn(rdpCredsspAuth* auth,
const char* service,
const char* hostname)
1039 char* spn =
nullptr;
1047 spn = _strdup(hostname);
1050 length = strlen(service) + strlen(hostname) + 2;
1051 spn = calloc(length + 1,
sizeof(
char));
1055 (void)sprintf_s(spn, length,
"%s/%s", service, hostname);
1061 auth->spn = ConvertUtf8ToWCharAlloc(spn,
nullptr);
1070static const char* parseInt(
const char* v, INT32* r)
1075 if (!*v || !((*v >=
'0') && (*v <=
'9')))
1078 for (; *v && (*v >=
'0') && (*v <=
'9'); v++)
1080 *r = (*r * 10) + (*v -
'0');
1086static BOOL parseKerberosDeltat(
const char* value, INT32* dest,
const char* message)
1089 const char* ptr =
nullptr;
1091 WINPR_ASSERT(value);
1093 WINPR_ASSERT(message);
1100 ptr = strchr(value,
':');
1105 value = parseInt(value, &v);
1106 if (!value || *value !=
':')
1108 WLog_ERR(TAG,
"Invalid value for %s", message);
1114 value = parseInt(value + 1, &v);
1115 if (!value || (*value != 0 && *value !=
':') || (v > 60))
1117 WLog_ERR(TAG,
"Invalid value for %s", message);
1125 value = parseInt(value + 1, &v);
1126 if (!value || (*value != 0) || (v > 60))
1128 WLog_ERR(TAG,
"Invalid value for %s", message);
1137 value = parseInt(value, &v);
1140 WLog_ERR(TAG,
"Invalid value for %s", message);
1144 if (!*value || isspace(*value))
1176 WLog_ERR(TAG,
"invalid value for unit %c when parsing %s", *value, message);
1180 if ((maxValue > 0) && (v > maxValue))
1182 WLog_ERR(TAG,
"invalid value for unit %c when parsing %s", *value, message);
1186 *dest += (v * factor);
1191 value = parseInt(value, &v);
1192 if (!value || !*value)
1194 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.