2017-05-11 19:51:45 +03:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Client
|
|
|
|
* Kerberos Auth Protocol
|
|
|
|
*
|
|
|
|
* Copyright 2015 ANSSI, Author Thomas Calderon
|
|
|
|
* Copyright 2017 Dorian Ducournau <dorian.ducournau@gmail.com>
|
2022-02-05 01:59:16 +03:00
|
|
|
* Copyright 2022 David Fort <contact@hardening-consulting.com>
|
2017-05-11 19:51:45 +03:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2018-01-16 12:58:30 +03:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2017-05-11 19:51:45 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2022-02-02 01:23:34 +03:00
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
2017-05-11 19:51:45 +03:00
|
|
|
|
2022-01-19 13:58:00 +03:00
|
|
|
#include <winpr/assert.h>
|
2017-05-11 19:51:45 +03:00
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/sspi.h>
|
|
|
|
#include <winpr/print.h>
|
|
|
|
#include <winpr/sysinfo.h>
|
|
|
|
#include <winpr/registry.h>
|
|
|
|
|
|
|
|
#include "kerberos.h"
|
|
|
|
|
2017-07-03 13:47:56 +03:00
|
|
|
#ifdef WITH_GSSAPI_HEIMDAL
|
|
|
|
#include <krb5-protos.h>
|
|
|
|
#endif
|
|
|
|
|
2017-05-11 19:51:45 +03:00
|
|
|
#include "../sspi.h"
|
|
|
|
#include "../../log.h"
|
|
|
|
#define TAG WINPR_TAG("sspi.Kerberos")
|
|
|
|
|
2022-02-14 16:59:22 +03:00
|
|
|
struct s_KRB_CONTEXT
|
2018-01-16 12:58:30 +03:00
|
|
|
{
|
|
|
|
CtxtHandle context;
|
|
|
|
SSPI_CREDENTIALS* credentials;
|
|
|
|
SEC_WINNT_AUTH_IDENTITY identity;
|
|
|
|
|
|
|
|
/* GSSAPI */
|
|
|
|
UINT32 major_status;
|
|
|
|
UINT32 minor_status;
|
|
|
|
UINT32 actual_time;
|
|
|
|
sspi_gss_cred_id_t cred;
|
|
|
|
sspi_gss_ctx_id_t gss_ctx;
|
|
|
|
sspi_gss_name_t target_name;
|
2022-01-19 13:58:00 +03:00
|
|
|
UINT32 trailerSize;
|
2018-01-16 12:58:30 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static const char* KRB_PACKAGE_NAME = "Kerberos";
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
const SecPkgInfoA KERBEROS_SecPkgInfoA = {
|
|
|
|
0x000F3BBF, /* fCapabilities */
|
|
|
|
1, /* wVersion */
|
|
|
|
0x0010, /* wRPCID */
|
|
|
|
0x0000BB80, /* cbMaxToken : 48k bytes maximum for Windows Server 2012 */
|
|
|
|
"Kerberos", /* Name */
|
2018-01-16 12:58:30 +03:00
|
|
|
"Kerberos Security Package" /* Comment */
|
|
|
|
};
|
|
|
|
|
2018-06-06 17:43:09 +03:00
|
|
|
static WCHAR KERBEROS_SecPkgInfoW_Name[] = { 'K', 'e', 'r', 'b', 'e', 'r', 'o', 's', '\0' };
|
2018-01-16 12:58:30 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static WCHAR KERBEROS_SecPkgInfoW_Comment[] = { 'K', 'e', 'r', 'b', 'e', 'r', 'o', 's', ' ',
|
|
|
|
'S', 'e', 'c', 'u', 'r', 'i', 't', 'y', ' ',
|
|
|
|
'P', 'a', 'c', 'k', 'a', 'g', 'e', '\0' };
|
|
|
|
|
|
|
|
const SecPkgInfoW KERBEROS_SecPkgInfoW = {
|
|
|
|
0x000F3BBF, /* fCapabilities */
|
|
|
|
1, /* wVersion */
|
|
|
|
0x0010, /* wRPCID */
|
|
|
|
0x0000BB80, /* cbMaxToken : 48k bytes maximum for Windows Server 2012 */
|
|
|
|
KERBEROS_SecPkgInfoW_Name, /* Name */
|
|
|
|
KERBEROS_SecPkgInfoW_Comment /* Comment */
|
2018-01-16 12:58:30 +03:00
|
|
|
};
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static sspi_gss_OID_desc g_SSPI_GSS_C_SPNEGO_KRB5 = {
|
|
|
|
9, (void*)"\x2a\x86\x48\x86\xf7\x12\x01\x02\x02"
|
2018-01-16 12:58:30 +03:00
|
|
|
};
|
|
|
|
static sspi_gss_OID SSPI_GSS_C_SPNEGO_KRB5 = &g_SSPI_GSS_C_SPNEGO_KRB5;
|
2017-05-11 19:51:45 +03:00
|
|
|
|
2018-01-16 12:58:30 +03:00
|
|
|
static KRB_CONTEXT* kerberos_ContextNew(void)
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
|
|
|
KRB_CONTEXT* context;
|
|
|
|
|
2022-02-05 01:59:16 +03:00
|
|
|
context = (KRB_CONTEXT*)calloc(1, sizeof(KRB_CONTEXT));
|
2017-05-11 19:51:45 +03:00
|
|
|
if (!context)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
context->minor_status = 0;
|
|
|
|
context->major_status = 0;
|
|
|
|
context->gss_ctx = SSPI_GSS_C_NO_CONTEXT;
|
|
|
|
context->cred = SSPI_GSS_C_NO_CREDENTIAL;
|
2022-02-05 01:59:16 +03:00
|
|
|
|
2017-05-11 19:51:45 +03:00
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
2018-01-16 12:58:30 +03:00
|
|
|
static void kerberos_ContextFree(KRB_CONTEXT* context)
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
|
|
|
UINT32 minor_status;
|
|
|
|
|
|
|
|
if (!context)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (context->target_name)
|
|
|
|
{
|
|
|
|
sspi_gss_release_name(&minor_status, &context->target_name);
|
|
|
|
context->target_name = NULL;
|
|
|
|
}
|
|
|
|
|
2022-02-05 01:59:16 +03:00
|
|
|
sspi_gss_release_cred(&minor_status, &context->cred);
|
|
|
|
|
2017-05-11 19:51:45 +03:00
|
|
|
if (context->gss_ctx)
|
|
|
|
{
|
|
|
|
sspi_gss_delete_sec_context(&minor_status, &context->gss_ctx, SSPI_GSS_C_NO_BUFFER);
|
|
|
|
context->gss_ctx = SSPI_GSS_C_NO_CONTEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(context);
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static SECURITY_STATUS SEC_ENTRY kerberos_AcquireCredentialsHandleW(
|
|
|
|
SEC_WCHAR* pszPrincipal, SEC_WCHAR* pszPackage, ULONG fCredentialUse, void* pvLogonID,
|
|
|
|
void* pAuthData, SEC_GET_KEY_FN pGetKeyFn, void* pvGetKeyArgument, PCredHandle phCredential,
|
|
|
|
PTimeStamp ptsExpiry)
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static SECURITY_STATUS SEC_ENTRY kerberos_AcquireCredentialsHandleA(
|
|
|
|
SEC_CHAR* pszPrincipal, SEC_CHAR* pszPackage, ULONG fCredentialUse, void* pvLogonID,
|
|
|
|
void* pAuthData, SEC_GET_KEY_FN pGetKeyFn, void* pvGetKeyArgument, PCredHandle phCredential,
|
|
|
|
PTimeStamp ptsExpiry)
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
2018-01-16 12:58:30 +03:00
|
|
|
static SECURITY_STATUS SEC_ENTRY kerberos_FreeCredentialsHandle(PCredHandle phCredential)
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
|
|
|
SSPI_CREDENTIALS* credentials;
|
|
|
|
|
|
|
|
if (!phCredential)
|
|
|
|
return SEC_E_INVALID_HANDLE;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
credentials = (SSPI_CREDENTIALS*)sspi_SecureHandleGetLowerPointer(phCredential);
|
2017-05-11 19:51:45 +03:00
|
|
|
|
|
|
|
if (!credentials)
|
|
|
|
return SEC_E_INVALID_HANDLE;
|
|
|
|
|
|
|
|
sspi_CredentialsFree(credentials);
|
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
2018-01-16 12:58:30 +03:00
|
|
|
static SECURITY_STATUS SEC_ENTRY kerberos_QueryCredentialsAttributesW(PCredHandle phCredential,
|
2019-11-06 17:24:51 +03:00
|
|
|
ULONG ulAttribute,
|
|
|
|
void* pBuffer)
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
|
|
|
if (ulAttribute == SECPKG_CRED_ATTR_NAMES)
|
|
|
|
{
|
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
2020-08-10 15:43:37 +03:00
|
|
|
WLog_ERR(TAG, "[%s]: TODO: Implement ulAttribute=%08" PRIx32, __FUNCTION__, ulAttribute);
|
2017-05-11 19:51:45 +03:00
|
|
|
return SEC_E_UNSUPPORTED_FUNCTION;
|
|
|
|
}
|
|
|
|
|
2018-01-16 12:58:30 +03:00
|
|
|
static SECURITY_STATUS SEC_ENTRY kerberos_QueryCredentialsAttributesA(PCredHandle phCredential,
|
2019-11-06 17:24:51 +03:00
|
|
|
ULONG ulAttribute,
|
|
|
|
void* pBuffer)
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
|
|
|
return kerberos_QueryCredentialsAttributesW(phCredential, ulAttribute, pBuffer);
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static SECURITY_STATUS SEC_ENTRY kerberos_InitializeSecurityContextW(
|
|
|
|
PCredHandle phCredential, PCtxtHandle phContext, SEC_WCHAR* pszTargetName, ULONG fContextReq,
|
|
|
|
ULONG Reserved1, ULONG TargetDataRep, PSecBufferDesc pInput, ULONG Reserved2,
|
|
|
|
PCtxtHandle phNewContext, PSecBufferDesc pOutput, ULONG* pfContextAttr, PTimeStamp ptsExpiry)
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
2020-08-10 15:43:37 +03:00
|
|
|
WLog_ERR(TAG, "[%s]: TODO: Implement", __FUNCTION__);
|
2017-05-11 19:51:45 +03:00
|
|
|
return SEC_E_UNSUPPORTED_FUNCTION;
|
|
|
|
}
|
|
|
|
|
2018-01-16 12:58:30 +03:00
|
|
|
static int kerberos_SetContextServicePrincipalNameA(KRB_CONTEXT* context,
|
2019-11-06 17:24:51 +03:00
|
|
|
SEC_CHAR* ServicePrincipalName)
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
|
|
|
char* p;
|
|
|
|
UINT32 major_status;
|
|
|
|
UINT32 minor_status;
|
|
|
|
char* gss_name = NULL;
|
|
|
|
sspi_gss_buffer_desc name_buffer;
|
|
|
|
|
|
|
|
if (!ServicePrincipalName)
|
|
|
|
{
|
|
|
|
context->target_name = NULL;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* GSSAPI expects a SPN of type <service>@FQDN, let's construct it */
|
|
|
|
gss_name = _strdup(ServicePrincipalName);
|
|
|
|
|
|
|
|
if (!gss_name)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
p = strchr(gss_name, '/');
|
|
|
|
|
|
|
|
if (p)
|
|
|
|
*p = '@';
|
|
|
|
|
|
|
|
name_buffer.value = gss_name;
|
|
|
|
name_buffer.length = strlen(gss_name) + 1;
|
|
|
|
major_status = sspi_gss_import_name(&minor_status, &name_buffer,
|
|
|
|
SSPI_GSS_C_NT_HOSTBASED_SERVICE, &(context->target_name));
|
|
|
|
free(gss_name);
|
|
|
|
|
|
|
|
if (SSPI_GSS_ERROR(major_status))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "error: gss_import_name failed");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef WITH_GSSAPI
|
2022-02-02 01:23:34 +03:00
|
|
|
|
|
|
|
static krb5_error_code krb5_prompter(krb5_context context, void* data, const char* name,
|
|
|
|
const char* banner, int num_prompts, krb5_prompt prompts[])
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
krb5_prompt_type* ptypes = krb5_get_prompt_types(context);
|
|
|
|
|
|
|
|
for (i = 0; i < num_prompts; i++)
|
|
|
|
{
|
|
|
|
if (ptypes && ptypes[i] == KRB5_PROMPT_TYPE_PREAUTH)
|
|
|
|
{
|
2022-02-24 15:18:56 +03:00
|
|
|
prompts[i].reply->data = _strdup((const char*)data);
|
2022-02-02 01:23:34 +03:00
|
|
|
prompts[i].reply->length = strlen((const char*)data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-02-24 15:18:56 +03:00
|
|
|
static char* allocate_print(const char* fmt, va_list ap)
|
|
|
|
{
|
|
|
|
char* buffer = NULL;
|
|
|
|
size_t size = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (size > 0)
|
|
|
|
{
|
|
|
|
char* tmp = realloc(buffer, size);
|
|
|
|
if (!tmp)
|
|
|
|
break;
|
|
|
|
buffer = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rc = vsnprintf(buffer, size, fmt, ap);
|
|
|
|
if (rc <= 0)
|
|
|
|
break;
|
|
|
|
size = rc;
|
|
|
|
} while (!buffer);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void krb5_log_error_message(krb5_context ctx, krb5_error_code ret, DWORD level,
|
|
|
|
const char* fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char* buffer = NULL;
|
|
|
|
const char* msg = krb5_get_error_message(ctx, ret);
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
buffer = allocate_print(fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
WLog_Print_tag(TAG, level, "%s [%s]", buffer, msg);
|
|
|
|
free(buffer);
|
|
|
|
krb5_free_error_message(ctx, msg);
|
|
|
|
}
|
|
|
|
|
2022-02-02 01:23:34 +03:00
|
|
|
static krb5_error_code acquire_cred(krb5_context ctx, SEC_WINPR_KERBEROS_SETTINGS* optionsBlock,
|
|
|
|
krb5_principal client, const char* password)
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
|
|
|
krb5_error_code ret;
|
|
|
|
krb5_creds creds;
|
|
|
|
krb5_deltat starttime = 0;
|
|
|
|
krb5_get_init_creds_opt* options = NULL;
|
|
|
|
krb5_ccache ccache;
|
|
|
|
krb5_init_creds_context init_ctx = NULL;
|
2022-02-05 01:59:16 +03:00
|
|
|
const char* cacheType;
|
2017-05-11 19:51:45 +03:00
|
|
|
|
2022-02-05 01:59:16 +03:00
|
|
|
/* Get ccache */
|
|
|
|
if (optionsBlock && optionsBlock->cache)
|
|
|
|
{
|
|
|
|
ret = krb5_cc_resolve(ctx, optionsBlock->cache, &ccache);
|
|
|
|
cacheType = "user set cache";
|
|
|
|
}
|
|
|
|
else
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
2022-02-05 01:59:16 +03:00
|
|
|
ret = krb5_cc_default(ctx, &ccache);
|
|
|
|
cacheType = "default";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
{
|
2022-02-24 15:18:56 +03:00
|
|
|
krb5_log_error_message(ctx, ret, WLOG_ERROR, "error while getting %s ccache", cacheType);
|
2017-05-11 19:51:45 +03:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ret = krb5_cc_initialize(ctx, ccache, client)))
|
|
|
|
{
|
2022-02-24 15:18:56 +03:00
|
|
|
krb5_log_error_message(ctx, ret, WLOG_ERROR, "error: could not initialize ccache");
|
2017-05-11 19:51:45 +03:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&creds, 0, sizeof(creds));
|
|
|
|
|
|
|
|
if ((ret = krb5_get_init_creds_opt_alloc(ctx, &options)))
|
|
|
|
{
|
2022-02-24 15:18:56 +03:00
|
|
|
krb5_log_error_message(ctx, ret, WLOG_ERROR, "error while allocating options");
|
2017-05-11 19:51:45 +03:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set default options */
|
|
|
|
krb5_get_init_creds_opt_set_forwardable(options, 0);
|
|
|
|
krb5_get_init_creds_opt_set_proxiable(options, 0);
|
2022-02-02 01:23:34 +03:00
|
|
|
|
2022-02-05 01:59:16 +03:00
|
|
|
if (optionsBlock && optionsBlock->startTime)
|
|
|
|
starttime = optionsBlock->startTime;
|
|
|
|
|
|
|
|
if (optionsBlock && optionsBlock->lifeTime)
|
|
|
|
krb5_get_init_creds_opt_set_tkt_life(options, optionsBlock->lifeTime);
|
|
|
|
|
|
|
|
if (optionsBlock && optionsBlock->renewLifeTime)
|
|
|
|
krb5_get_init_creds_opt_set_renew_life(options, optionsBlock->renewLifeTime);
|
|
|
|
|
2022-02-02 01:23:34 +03:00
|
|
|
if (optionsBlock && optionsBlock->withPac)
|
|
|
|
krb5_get_init_creds_opt_set_pac_request(ctx, options, TRUE);
|
|
|
|
|
|
|
|
if (optionsBlock && optionsBlock->armorCache)
|
|
|
|
krb5_get_init_creds_opt_set_fast_ccache_name(ctx, options, optionsBlock->armorCache);
|
|
|
|
|
|
|
|
if (optionsBlock && optionsBlock->pkinitX509Identity)
|
|
|
|
krb5_get_init_creds_opt_set_pa(ctx, options, "X509_user_identity",
|
|
|
|
optionsBlock->pkinitX509Identity);
|
|
|
|
|
|
|
|
if (optionsBlock && optionsBlock->pkinitX509Anchors)
|
|
|
|
krb5_get_init_creds_opt_set_pa(ctx, options, "X509_anchors",
|
|
|
|
optionsBlock->pkinitX509Anchors);
|
|
|
|
|
2017-08-18 20:18:02 +03:00
|
|
|
#ifdef WITH_GSSAPI_MIT
|
2017-05-11 19:51:45 +03:00
|
|
|
/* for MIT we specify ccache output using an option */
|
|
|
|
if ((ret = krb5_get_init_creds_opt_set_out_ccache(ctx, options, ccache)))
|
|
|
|
{
|
2022-02-24 15:18:56 +03:00
|
|
|
krb5_log_error_message(ctx, ret, WLOG_ERROR, "error while setting ccache output");
|
2017-05-11 19:51:45 +03:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2018-01-16 12:58:30 +03:00
|
|
|
|
2017-05-11 19:51:45 +03:00
|
|
|
#endif
|
|
|
|
|
2022-02-02 01:23:34 +03:00
|
|
|
if ((ret = krb5_init_creds_init(ctx, client, krb5_prompter, (void*)password, starttime, options,
|
|
|
|
&init_ctx)))
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
2022-02-24 15:18:56 +03:00
|
|
|
krb5_log_error_message(ctx, ret, WLOG_ERROR, "error krb5_init_creds_init failed");
|
2017-05-11 19:51:45 +03:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ret = krb5_init_creds_set_password(ctx, init_ctx, password)))
|
|
|
|
{
|
2022-02-24 15:18:56 +03:00
|
|
|
krb5_log_error_message(ctx, ret, WLOG_ERROR, "error krb5_init_creds_set_password failed");
|
2017-05-11 19:51:45 +03:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get credentials */
|
|
|
|
if ((ret = krb5_init_creds_get(ctx, init_ctx)))
|
|
|
|
{
|
2022-02-24 15:18:56 +03:00
|
|
|
krb5_log_error_message(ctx, ret, WLOG_ERROR, "error while getting credentials");
|
2017-05-11 19:51:45 +03:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Retrieve credentials */
|
|
|
|
if ((ret = krb5_init_creds_get_creds(ctx, init_ctx, &creds)))
|
|
|
|
{
|
2022-02-24 15:18:56 +03:00
|
|
|
krb5_log_error_message(ctx, ret, WLOG_ERROR, "error while retrieving credentials");
|
2017-05-11 19:51:45 +03:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef WITH_GSSAPI_HEIMDAL
|
2018-01-16 12:58:30 +03:00
|
|
|
|
2017-05-11 19:51:45 +03:00
|
|
|
/* For Heimdal, we use this function to store credentials */
|
|
|
|
if ((ret = krb5_init_creds_store(ctx, init_ctx, ccache)))
|
|
|
|
{
|
2022-02-24 15:18:56 +03:00
|
|
|
krb5_log_error_message(ctx, ret, WLOG_ERROR, "error while storing credentials");
|
2017-05-11 19:51:45 +03:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2017-08-18 20:18:02 +03:00
|
|
|
|
2018-01-16 12:58:30 +03:00
|
|
|
#endif
|
2017-05-11 19:51:45 +03:00
|
|
|
cleanup:
|
|
|
|
krb5_free_cred_contents(ctx, &creds);
|
2017-08-18 20:18:02 +03:00
|
|
|
#ifdef HAVE_AT_LEAST_KRB_V1_13
|
2018-01-16 12:58:30 +03:00
|
|
|
|
2017-08-18 20:18:02 +03:00
|
|
|
/* MIT Kerberos version 1.13 at minimum.
|
|
|
|
* For releases 1.12 and previous, krb5_get_init_creds_opt structure
|
|
|
|
* is freed in krb5_init_creds_free() */
|
2017-05-11 19:51:45 +03:00
|
|
|
if (options)
|
|
|
|
krb5_get_init_creds_opt_free(ctx, options);
|
2018-01-16 12:58:30 +03:00
|
|
|
|
2017-08-18 20:18:02 +03:00
|
|
|
#endif
|
2017-05-11 19:51:45 +03:00
|
|
|
|
|
|
|
if (init_ctx)
|
|
|
|
krb5_init_creds_free(ctx, init_ctx);
|
|
|
|
|
|
|
|
if (ccache)
|
|
|
|
krb5_cc_close(ctx, ccache);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-02-02 01:23:34 +03:00
|
|
|
static int init_creds(SEC_WINPR_KERBEROS_SETTINGS* options, LPCWSTR username, size_t username_len,
|
|
|
|
LPCWSTR domain, size_t domain_len, LPCWSTR password, size_t password_len)
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
|
|
|
krb5_error_code ret = 0;
|
|
|
|
krb5_context ctx = NULL;
|
|
|
|
krb5_principal principal = NULL;
|
|
|
|
char* krb_name = NULL;
|
|
|
|
char* lusername = NULL;
|
|
|
|
char* lrealm = NULL;
|
|
|
|
char* lpassword = NULL;
|
2017-07-03 13:47:56 +03:00
|
|
|
int flags = 0;
|
|
|
|
char* pstr = NULL;
|
2017-05-11 19:51:45 +03:00
|
|
|
size_t krb_name_len = 0;
|
|
|
|
size_t lrealm_len = 0;
|
|
|
|
size_t lusername_len = 0;
|
|
|
|
int status = 0;
|
2022-02-02 01:23:34 +03:00
|
|
|
BOOL isDefaultRealm = FALSE;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
status = ConvertFromUnicode(CP_UTF8, 0, username, username_len, &lusername, 0, NULL, NULL);
|
2017-05-11 19:51:45 +03:00
|
|
|
|
|
|
|
if (status <= 0)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Failed to convert username");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
status = ConvertFromUnicode(CP_UTF8, 0, password, password_len, &lpassword, 0, NULL, NULL);
|
2017-05-11 19:51:45 +03:00
|
|
|
if (status <= 0)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Failed to convert password");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Could call krb5_init_secure_context, but it disallows user overrides */
|
|
|
|
ret = krb5_init_context(&ctx);
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
{
|
2022-02-24 15:18:56 +03:00
|
|
|
krb5_log_error_message(ctx, ret, WLOG_ERROR,
|
|
|
|
"error: while initializing Kerberos 5 library");
|
2017-05-11 19:51:45 +03:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2022-02-02 01:23:34 +03:00
|
|
|
if (domain && domain_len)
|
|
|
|
{
|
|
|
|
char* tmp;
|
|
|
|
if (ConvertFromUnicode(CP_UTF8, 0, domain, domain_len, &lrealm, 0, NULL, NULL) <= 0)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "error: converting domain");
|
|
|
|
}
|
2017-05-11 19:51:45 +03:00
|
|
|
|
2022-02-02 01:23:34 +03:00
|
|
|
/* convert to upper case */
|
|
|
|
for (tmp = lrealm; *tmp; tmp++)
|
|
|
|
*tmp = toupper(*tmp);
|
|
|
|
|
|
|
|
isDefaultRealm = FALSE;
|
|
|
|
}
|
|
|
|
else
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
2022-02-02 01:23:34 +03:00
|
|
|
ret = krb5_get_default_realm(ctx, &lrealm);
|
|
|
|
if (ret)
|
|
|
|
{
|
2022-02-24 15:18:56 +03:00
|
|
|
krb5_log_error_message(ctx, ret, WLOG_WARN, "could not get Kerberos default realm");
|
2022-02-02 01:23:34 +03:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
isDefaultRealm = TRUE;
|
2017-05-11 19:51:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
lrealm_len = strlen(lrealm);
|
|
|
|
lusername_len = strlen(lusername);
|
|
|
|
krb_name_len = lusername_len + lrealm_len + 1; // +1 for '@'
|
|
|
|
krb_name = calloc(krb_name_len + 1, sizeof(char));
|
|
|
|
|
|
|
|
if (!krb_name)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "could not allocate memory for string rep of principal\n");
|
|
|
|
ret = -1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set buffer */
|
|
|
|
_snprintf(krb_name, krb_name_len + 1, "%s@%s", lusername, lrealm);
|
|
|
|
#ifdef WITH_DEBUG_NLA
|
|
|
|
WLog_DBG(TAG, "copied string is %s\n", krb_name);
|
|
|
|
#endif
|
2017-07-03 13:47:56 +03:00
|
|
|
pstr = strchr(lusername, '@');
|
|
|
|
|
|
|
|
if (pstr != NULL)
|
|
|
|
flags = KRB5_PRINCIPAL_PARSE_ENTERPRISE;
|
|
|
|
|
|
|
|
/* Use the specified principal name. */
|
2019-11-06 17:24:51 +03:00
|
|
|
ret = krb5_parse_name_flags(ctx, krb_name, flags, &principal);
|
2017-05-11 19:51:45 +03:00
|
|
|
|
|
|
|
if (ret)
|
|
|
|
{
|
2022-02-24 15:18:56 +03:00
|
|
|
krb5_log_error_message(ctx, ret, WLOG_ERROR, "could not convert %s to principal", krb_name);
|
2017-05-11 19:51:45 +03:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2022-02-02 01:23:34 +03:00
|
|
|
ret = acquire_cred(ctx, options, principal, lpassword);
|
2017-05-11 19:51:45 +03:00
|
|
|
|
|
|
|
if (ret)
|
|
|
|
{
|
2022-02-24 15:18:56 +03:00
|
|
|
krb5_log_error_message(ctx, ret, WLOG_ERROR,
|
|
|
|
"Kerberos credentials not found and could not be acquired");
|
2017-05-11 19:51:45 +03:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
free(lusername);
|
|
|
|
free(lpassword);
|
|
|
|
|
|
|
|
if (krb_name)
|
|
|
|
free(krb_name);
|
|
|
|
|
|
|
|
if (lrealm)
|
2022-02-02 01:23:34 +03:00
|
|
|
{
|
|
|
|
if (isDefaultRealm)
|
|
|
|
krb5_free_default_realm(ctx, lrealm);
|
|
|
|
else
|
|
|
|
free(lrealm);
|
|
|
|
}
|
2017-05-11 19:51:45 +03:00
|
|
|
|
|
|
|
if (principal)
|
|
|
|
krb5_free_principal(ctx, principal);
|
|
|
|
|
|
|
|
if (ctx)
|
|
|
|
krb5_free_context(ctx);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2022-02-05 01:59:16 +03:00
|
|
|
|
|
|
|
static SECURITY_STATUS setupCreds(KRB_CONTEXT* context, SSPI_CREDENTIALS* credentials)
|
|
|
|
{
|
|
|
|
SEC_WINPR_KERBEROS_SETTINGS* kerbSettings = credentials->kerbSettings;
|
|
|
|
sspi_gss_OID_set_desc krb5_set = { 1, SSPI_GSS_C_SPNEGO_KRB5 };
|
|
|
|
sspi_gss_key_value_element_desc ccache_setting = { "ccache", NULL };
|
|
|
|
sspi_gss_key_value_set_desc store = { 0, &ccache_setting };
|
|
|
|
char* file_colon_cache = NULL;
|
|
|
|
OM_uint32 ret, minor;
|
|
|
|
|
|
|
|
if (kerbSettings && kerbSettings->cache)
|
|
|
|
{
|
|
|
|
size_t len = _snprintf(NULL, 0, "FILE:%s", kerbSettings->cache);
|
|
|
|
file_colon_cache = malloc(len + 1);
|
|
|
|
_snprintf(file_colon_cache, len + 1, "FILE:%s", kerbSettings->cache);
|
|
|
|
store.count = 1;
|
|
|
|
ccache_setting.value = file_colon_cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
WLog_DBG(TAG, "acquiring credentials");
|
|
|
|
ret = sspi_gss_acquire_cred_from(&minor, SSPI_GSS_C_NO_NAME, SSPI_GSS_C_INDEFINITE, &krb5_set,
|
|
|
|
SSPI_GSS_C_INITIATE, &store, &(context->cred), NULL, NULL);
|
|
|
|
if (SSPI_GSS_ERROR(ret))
|
|
|
|
{
|
|
|
|
|
|
|
|
BOOL isNoCreds = !!(ret & SSPI_GSS_S_NO_CRED);
|
|
|
|
if (isNoCreds)
|
|
|
|
{
|
|
|
|
/* Then let's try to acquire credentials using login and password,
|
|
|
|
* and only those two, means not with a smartcard.
|
|
|
|
* If we use smartcard-logon, the credentials have already
|
|
|
|
* been acquired by pkinit process. If not, returned error previously.
|
|
|
|
*/
|
|
|
|
if (init_creds(credentials->kerbSettings, credentials->identity.User,
|
|
|
|
credentials->identity.UserLength, credentials->identity.Domain,
|
|
|
|
credentials->identity.DomainLength, credentials->identity.Password,
|
|
|
|
credentials->identity.PasswordLength))
|
|
|
|
return SEC_E_NO_CREDENTIALS;
|
|
|
|
|
|
|
|
WLog_INFO(TAG, "Authenticated to Kerberos v5 via login/password");
|
|
|
|
ret = sspi_gss_acquire_cred_from(&minor, SSPI_GSS_C_NO_NAME, SSPI_GSS_C_INDEFINITE, &krb5_set,
|
|
|
|
SSPI_GSS_C_INITIATE, &store, &(context->cred), NULL, NULL);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
free(file_colon_cache);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-05-11 19:51:45 +03:00
|
|
|
#endif
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static SECURITY_STATUS SEC_ENTRY kerberos_InitializeSecurityContextA(
|
|
|
|
PCredHandle phCredential, PCtxtHandle phContext, SEC_CHAR* pszTargetName, ULONG fContextReq,
|
|
|
|
ULONG Reserved1, ULONG TargetDataRep, PSecBufferDesc pInput, ULONG Reserved2,
|
|
|
|
PCtxtHandle phNewContext, PSecBufferDesc pOutput, ULONG* pfContextAttr, PTimeStamp ptsExpiry)
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
|
|
|
KRB_CONTEXT* context;
|
|
|
|
SSPI_CREDENTIALS* credentials;
|
|
|
|
PSecBuffer input_buffer = NULL;
|
|
|
|
PSecBuffer output_buffer = NULL;
|
2018-02-13 13:00:56 +03:00
|
|
|
sspi_gss_buffer_desc input_tok = { 0 };
|
|
|
|
sspi_gss_buffer_desc output_tok = { 0 };
|
2017-05-11 19:51:45 +03:00
|
|
|
sspi_gss_OID actual_mech;
|
2022-02-05 01:59:16 +03:00
|
|
|
sspi_gss_OID desired_mech = SSPI_GSS_C_SPNEGO_KRB5;
|
2017-05-11 19:51:45 +03:00
|
|
|
UINT32 actual_services;
|
|
|
|
|
2022-02-05 01:59:16 +03:00
|
|
|
context = (KRB_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
|
2017-05-11 19:51:45 +03:00
|
|
|
if (!context)
|
|
|
|
{
|
|
|
|
context = kerberos_ContextNew();
|
|
|
|
|
|
|
|
if (!context)
|
|
|
|
return SEC_E_INSUFFICIENT_MEMORY;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
credentials = (SSPI_CREDENTIALS*)sspi_SecureHandleGetLowerPointer(phCredential);
|
2017-05-11 19:51:45 +03:00
|
|
|
context->credentials = credentials;
|
|
|
|
|
|
|
|
if (kerberos_SetContextServicePrincipalNameA(context, pszTargetName) < 0)
|
2017-07-18 12:38:59 +03:00
|
|
|
{
|
|
|
|
kerberos_ContextFree(context);
|
2017-05-11 19:51:45 +03:00
|
|
|
return SEC_E_INTERNAL_ERROR;
|
2017-07-18 12:38:59 +03:00
|
|
|
}
|
2017-05-11 19:51:45 +03:00
|
|
|
|
|
|
|
sspi_SecureHandleSetLowerPointer(phNewContext, context);
|
2019-11-06 17:24:51 +03:00
|
|
|
sspi_SecureHandleSetUpperPointer(phNewContext, (void*)KRB_PACKAGE_NAME);
|
2017-05-11 19:51:45 +03:00
|
|
|
}
|
|
|
|
else
|
2022-02-05 01:59:16 +03:00
|
|
|
credentials = context->credentials;
|
|
|
|
|
|
|
|
if (pInput)
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
|
|
|
input_buffer = sspi_FindSecBuffer(pInput, SECBUFFER_TOKEN);
|
|
|
|
|
|
|
|
if (!input_buffer)
|
|
|
|
return SEC_E_INVALID_TOKEN;
|
|
|
|
|
|
|
|
if (input_buffer->cbBuffer < 1)
|
|
|
|
return SEC_E_INVALID_TOKEN;
|
|
|
|
|
|
|
|
input_tok.value = input_buffer->pvBuffer;
|
|
|
|
input_tok.length = input_buffer->cbBuffer;
|
2022-02-05 01:59:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!pOutput)
|
|
|
|
return SEC_E_INVALID_TOKEN;
|
|
|
|
|
|
|
|
if (pOutput->cBuffers < 1)
|
|
|
|
return SEC_E_INVALID_TOKEN;
|
|
|
|
|
|
|
|
output_buffer = sspi_FindSecBuffer(pOutput, SECBUFFER_TOKEN);
|
|
|
|
if (!output_buffer)
|
|
|
|
return SEC_E_INVALID_TOKEN;
|
2017-05-11 19:51:45 +03:00
|
|
|
|
2022-02-05 01:59:16 +03:00
|
|
|
if (output_buffer->cbBuffer < 1)
|
|
|
|
return SEC_E_INVALID_TOKEN;
|
|
|
|
|
|
|
|
#if defined(WITH_GSSAPI)
|
|
|
|
if (context->cred == SSPI_GSS_C_NO_CREDENTIAL)
|
|
|
|
{
|
|
|
|
context->major_status = setupCreds(context, credentials);
|
2017-05-11 19:51:45 +03:00
|
|
|
if (SSPI_GSS_ERROR(context->major_status))
|
2022-02-05 01:59:16 +03:00
|
|
|
return context->major_status;
|
|
|
|
}
|
2017-05-11 19:51:45 +03:00
|
|
|
|
2022-02-05 01:59:16 +03:00
|
|
|
context->major_status = sspi_gss_init_sec_context(
|
|
|
|
&(context->minor_status), context->cred, &(context->gss_ctx), context->target_name,
|
|
|
|
desired_mech, SSPI_GSS_C_MUTUAL_FLAG | SSPI_GSS_C_DELEG_FLAG, SSPI_GSS_C_INDEFINITE,
|
|
|
|
SSPI_GSS_C_NO_CHANNEL_BINDINGS, &input_tok, &actual_mech, &output_tok, &actual_services,
|
|
|
|
&(context->actual_time));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (output_buffer->cbBuffer < output_tok.length)
|
|
|
|
return SEC_E_INVALID_TOKEN;
|
|
|
|
|
|
|
|
output_buffer->cbBuffer = output_tok.length;
|
|
|
|
if (output_tok.length != 0)
|
|
|
|
{
|
|
|
|
CopyMemory(output_buffer->pvBuffer, output_tok.value, output_tok.length);
|
|
|
|
sspi_gss_release_buffer(&(context->minor_status), &output_tok);
|
2017-05-11 19:51:45 +03:00
|
|
|
}
|
|
|
|
|
2022-02-05 01:59:16 +03:00
|
|
|
if (context->major_status & SSPI_GSS_S_CONTINUE_NEEDED)
|
|
|
|
return SEC_I_CONTINUE_NEEDED;
|
|
|
|
|
|
|
|
if (SSPI_GSS_ERROR(context->major_status))
|
|
|
|
return SEC_E_INTERNAL_ERROR;
|
|
|
|
|
|
|
|
return SEC_E_OK;
|
2017-05-11 19:51:45 +03:00
|
|
|
}
|
|
|
|
|
2018-01-16 12:58:30 +03:00
|
|
|
static SECURITY_STATUS SEC_ENTRY kerberos_DeleteSecurityContext(PCtxtHandle phContext)
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
|
|
|
KRB_CONTEXT* context;
|
2019-11-06 17:24:51 +03:00
|
|
|
context = (KRB_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
|
2017-05-11 19:51:45 +03:00
|
|
|
|
|
|
|
if (!context)
|
|
|
|
return SEC_E_INVALID_HANDLE;
|
|
|
|
|
|
|
|
kerberos_ContextFree(context);
|
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
2018-01-16 12:58:30 +03:00
|
|
|
static SECURITY_STATUS SEC_ENTRY kerberos_QueryContextAttributesW(PCtxtHandle phContext,
|
2019-11-06 17:24:51 +03:00
|
|
|
ULONG ulAttribute, void* pBuffer)
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
2018-01-16 12:58:30 +03:00
|
|
|
static SECURITY_STATUS SEC_ENTRY kerberos_QueryContextAttributesA(PCtxtHandle phContext,
|
2019-11-06 17:24:51 +03:00
|
|
|
ULONG ulAttribute, void* pBuffer)
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
|
|
|
if (!phContext)
|
|
|
|
return SEC_E_INVALID_HANDLE;
|
|
|
|
|
|
|
|
if (!pBuffer)
|
|
|
|
return SEC_E_INSUFFICIENT_MEMORY;
|
|
|
|
|
|
|
|
if (ulAttribute == SECPKG_ATTR_SIZES)
|
|
|
|
{
|
2022-01-19 13:58:00 +03:00
|
|
|
UINT32 major_status, minor_status, max_unwrapped;
|
|
|
|
KRB_CONTEXT* context = (KRB_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
SecPkgContext_Sizes* ContextSizes = (SecPkgContext_Sizes*)pBuffer;
|
2017-05-11 19:51:45 +03:00
|
|
|
/* The MaxTokenSize by default is 12,000 bytes. This has been the default value
|
|
|
|
* since Windows 2000 SP2 and still remains in Windows 7 and Windows 2008 R2.
|
|
|
|
* For Windows Server 2012, the default value of the MaxTokenSize registry
|
|
|
|
* entry is 48,000 bytes.*/
|
|
|
|
ContextSizes->cbMaxToken = KERBEROS_SecPkgInfoA.cbMaxToken;
|
|
|
|
ContextSizes->cbMaxSignature = 0; /* means verify not supported */
|
2019-11-06 17:24:51 +03:00
|
|
|
ContextSizes->cbBlockSize = 0; /* padding not used */
|
2022-01-19 13:58:00 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(context);
|
|
|
|
|
|
|
|
major_status = sspi_gss_wrap_size_limit(&minor_status, context->gss_ctx, TRUE,
|
|
|
|
SSPI_GSS_C_QOP_DEFAULT, 12000, &max_unwrapped);
|
|
|
|
if (SSPI_GSS_ERROR(major_status))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "unable to compute the trailer size with gss_wrap_size_limit()");
|
|
|
|
return SEC_E_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
context->trailerSize = ContextSizes->cbSecurityTrailer = (12000 - max_unwrapped);
|
2017-05-11 19:51:45 +03:00
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
2020-08-10 15:43:37 +03:00
|
|
|
WLog_ERR(TAG, "[%s]: TODO: Implement ulAttribute=%08" PRIx32, __FUNCTION__, ulAttribute);
|
2017-05-11 19:51:45 +03:00
|
|
|
return SEC_E_UNSUPPORTED_FUNCTION;
|
|
|
|
}
|
|
|
|
|
2018-01-16 12:58:30 +03:00
|
|
|
static SECURITY_STATUS SEC_ENTRY kerberos_EncryptMessage(PCtxtHandle phContext, ULONG fQOP,
|
2019-11-06 17:24:51 +03:00
|
|
|
PSecBufferDesc pMessage,
|
|
|
|
ULONG MessageSeqNo)
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
|
|
|
int index;
|
|
|
|
int conf_state;
|
|
|
|
UINT32 major_status;
|
|
|
|
UINT32 minor_status;
|
|
|
|
KRB_CONTEXT* context;
|
|
|
|
sspi_gss_buffer_desc input;
|
|
|
|
sspi_gss_buffer_desc output;
|
2022-01-19 13:58:00 +03:00
|
|
|
BYTE* ptr;
|
|
|
|
size_t toCopy;
|
2017-05-11 19:51:45 +03:00
|
|
|
PSecBuffer data_buffer = NULL;
|
2022-01-19 13:58:00 +03:00
|
|
|
PSecBuffer sig_buffer = NULL;
|
|
|
|
SECURITY_STATUS ret = SEC_E_OK;
|
2017-05-11 19:51:45 +03:00
|
|
|
|
2022-01-19 13:58:00 +03:00
|
|
|
context = (KRB_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
|
2017-05-11 19:51:45 +03:00
|
|
|
if (!context)
|
|
|
|
return SEC_E_INVALID_HANDLE;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
for (index = 0; index < (int)pMessage->cBuffers; index++)
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
|
|
|
if (pMessage->pBuffers[index].BufferType == SECBUFFER_DATA)
|
|
|
|
data_buffer = &pMessage->pBuffers[index];
|
2022-01-19 13:58:00 +03:00
|
|
|
else if (pMessage->pBuffers[index].BufferType == SECBUFFER_TOKEN)
|
|
|
|
sig_buffer = &pMessage->pBuffers[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!data_buffer || !sig_buffer)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "kerberos_EncryptMessage: our winPR emulation can only handle calls with a "
|
|
|
|
"token AND a data buffer");
|
|
|
|
return SEC_E_INVALID_TOKEN;
|
2017-05-11 19:51:45 +03:00
|
|
|
}
|
|
|
|
|
2022-01-19 13:58:00 +03:00
|
|
|
if ((BYTE*)sig_buffer->pvBuffer + sig_buffer->cbBuffer != data_buffer->pvBuffer)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "kerberos_EncryptMessage: our winPR emulation expects token and data buffer "
|
|
|
|
"to be contiguous");
|
2017-05-11 19:51:45 +03:00
|
|
|
return SEC_E_INVALID_TOKEN;
|
2022-01-19 13:58:00 +03:00
|
|
|
}
|
2017-05-11 19:51:45 +03:00
|
|
|
|
|
|
|
input.value = data_buffer->pvBuffer;
|
|
|
|
input.length = data_buffer->cbBuffer;
|
2019-11-06 17:24:51 +03:00
|
|
|
major_status = sspi_gss_wrap(&minor_status, context->gss_ctx, TRUE, SSPI_GSS_C_QOP_DEFAULT,
|
|
|
|
&input, &conf_state, &output);
|
2017-05-11 19:51:45 +03:00
|
|
|
|
|
|
|
if (SSPI_GSS_ERROR(major_status))
|
|
|
|
return SEC_E_INTERNAL_ERROR;
|
|
|
|
|
|
|
|
if (conf_state == 0)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "error: gss_wrap confidentiality was not applied");
|
2022-01-19 13:58:00 +03:00
|
|
|
ret = SEC_E_INTERNAL_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (output.length < context->trailerSize)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "error: output is smaller than expected trailerSize");
|
|
|
|
ret = SEC_E_INTERNAL_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* we artificially fill the sig buffer and data_buffer, even if gss_wrap() puts the
|
|
|
|
* mic and message in the same place
|
|
|
|
*/
|
|
|
|
ptr = output.value;
|
|
|
|
CopyMemory(sig_buffer->pvBuffer, ptr, context->trailerSize);
|
|
|
|
ptr += context->trailerSize;
|
|
|
|
|
|
|
|
toCopy = output.length - context->trailerSize;
|
|
|
|
if (data_buffer->cbBuffer < toCopy)
|
|
|
|
{
|
|
|
|
ret = SEC_E_INTERNAL_ERROR;
|
|
|
|
goto out;
|
2017-05-11 19:51:45 +03:00
|
|
|
}
|
2022-01-19 13:58:00 +03:00
|
|
|
CopyMemory(data_buffer->pvBuffer, ptr, toCopy);
|
2017-05-11 19:51:45 +03:00
|
|
|
|
2022-01-19 13:58:00 +03:00
|
|
|
out:
|
2017-05-11 19:51:45 +03:00
|
|
|
sspi_gss_release_buffer(&minor_status, &output);
|
2022-01-19 13:58:00 +03:00
|
|
|
return ret;
|
2017-05-11 19:51:45 +03:00
|
|
|
}
|
|
|
|
|
2018-01-16 12:58:30 +03:00
|
|
|
static SECURITY_STATUS SEC_ENTRY kerberos_DecryptMessage(PCtxtHandle phContext,
|
2019-11-06 17:24:51 +03:00
|
|
|
PSecBufferDesc pMessage,
|
|
|
|
ULONG MessageSeqNo, ULONG* pfQOP)
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
|
|
|
int index;
|
|
|
|
int conf_state;
|
|
|
|
UINT32 major_status;
|
|
|
|
UINT32 minor_status;
|
|
|
|
KRB_CONTEXT* context;
|
|
|
|
sspi_gss_buffer_desc input_data;
|
|
|
|
sspi_gss_buffer_desc output;
|
2022-01-19 13:58:00 +03:00
|
|
|
PSecBuffer data_buffer_to_unwrap = NULL, sig_buffer = NULL;
|
2019-11-06 17:24:51 +03:00
|
|
|
context = (KRB_CONTEXT*)sspi_SecureHandleGetLowerPointer(phContext);
|
2017-05-11 19:51:45 +03:00
|
|
|
|
|
|
|
if (!context)
|
|
|
|
return SEC_E_INVALID_HANDLE;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
for (index = 0; index < (int)pMessage->cBuffers; index++)
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
|
|
|
if (pMessage->pBuffers[index].BufferType == SECBUFFER_DATA)
|
|
|
|
data_buffer_to_unwrap = &pMessage->pBuffers[index];
|
2022-01-19 13:58:00 +03:00
|
|
|
else if (pMessage->pBuffers[index].BufferType == SECBUFFER_TOKEN)
|
|
|
|
sig_buffer = &pMessage->pBuffers[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!data_buffer_to_unwrap || !sig_buffer)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "kerberos_DecryptMessage: our winPR emulation can only handle calls with a "
|
|
|
|
"token AND a data buffer");
|
|
|
|
return SEC_E_INVALID_TOKEN;
|
2017-05-11 19:51:45 +03:00
|
|
|
}
|
|
|
|
|
2022-01-19 13:58:00 +03:00
|
|
|
if ((BYTE*)sig_buffer->pvBuffer + sig_buffer->cbBuffer != data_buffer_to_unwrap->pvBuffer)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "kerberos_DecryptMessage: our winPR emulation expects token and data buffer "
|
|
|
|
"to be contiguous");
|
2017-05-11 19:51:45 +03:00
|
|
|
return SEC_E_INVALID_TOKEN;
|
2022-01-19 13:58:00 +03:00
|
|
|
}
|
2017-05-11 19:51:45 +03:00
|
|
|
|
|
|
|
/* unwrap encrypted TLS key AND its signature */
|
2022-01-19 13:58:00 +03:00
|
|
|
input_data.value = sig_buffer->pvBuffer;
|
|
|
|
input_data.length = sig_buffer->cbBuffer + data_buffer_to_unwrap->cbBuffer;
|
2019-11-06 17:24:51 +03:00
|
|
|
major_status =
|
|
|
|
sspi_gss_unwrap(&minor_status, context->gss_ctx, &input_data, &output, &conf_state, NULL);
|
2017-05-11 19:51:45 +03:00
|
|
|
|
|
|
|
if (SSPI_GSS_ERROR(major_status))
|
|
|
|
return SEC_E_INTERNAL_ERROR;
|
|
|
|
|
|
|
|
if (conf_state == 0)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "error: gss_unwrap confidentiality was not applied");
|
|
|
|
sspi_gss_release_buffer(&minor_status, &output);
|
|
|
|
return SEC_E_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
CopyMemory(data_buffer_to_unwrap->pvBuffer, output.value, output.length);
|
|
|
|
sspi_gss_release_buffer(&minor_status, &output);
|
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static SECURITY_STATUS SEC_ENTRY kerberos_MakeSignature(PCtxtHandle phContext, ULONG fQOP,
|
|
|
|
PSecBufferDesc pMessage, ULONG MessageSeqNo)
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
2018-01-16 12:58:30 +03:00
|
|
|
static SECURITY_STATUS SEC_ENTRY kerberos_VerifySignature(PCtxtHandle phContext,
|
2019-11-06 17:24:51 +03:00
|
|
|
PSecBufferDesc pMessage,
|
|
|
|
ULONG MessageSeqNo, ULONG* pfQOP)
|
2017-05-11 19:51:45 +03:00
|
|
|
{
|
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
const SecurityFunctionTableA KERBEROS_SecurityFunctionTableA = {
|
|
|
|
1, /* dwVersion */
|
|
|
|
NULL, /* EnumerateSecurityPackages */
|
|
|
|
kerberos_QueryCredentialsAttributesA, /* QueryCredentialsAttributes */
|
|
|
|
kerberos_AcquireCredentialsHandleA, /* AcquireCredentialsHandle */
|
|
|
|
kerberos_FreeCredentialsHandle, /* FreeCredentialsHandle */
|
|
|
|
NULL, /* Reserved2 */
|
|
|
|
kerberos_InitializeSecurityContextA, /* InitializeSecurityContext */
|
|
|
|
NULL, /* AcceptSecurityContext */
|
|
|
|
NULL, /* CompleteAuthToken */
|
|
|
|
kerberos_DeleteSecurityContext, /* DeleteSecurityContext */
|
|
|
|
NULL, /* ApplyControlToken */
|
|
|
|
kerberos_QueryContextAttributesA, /* QueryContextAttributes */
|
|
|
|
NULL, /* ImpersonateSecurityContext */
|
|
|
|
NULL, /* RevertSecurityContext */
|
|
|
|
kerberos_MakeSignature, /* MakeSignature */
|
|
|
|
kerberos_VerifySignature, /* VerifySignature */
|
|
|
|
NULL, /* FreeContextBuffer */
|
|
|
|
NULL, /* QuerySecurityPackageInfo */
|
|
|
|
NULL, /* Reserved3 */
|
|
|
|
NULL, /* Reserved4 */
|
|
|
|
NULL, /* ExportSecurityContext */
|
|
|
|
NULL, /* ImportSecurityContext */
|
|
|
|
NULL, /* AddCredentials */
|
|
|
|
NULL, /* Reserved8 */
|
|
|
|
NULL, /* QuerySecurityContextToken */
|
|
|
|
kerberos_EncryptMessage, /* EncryptMessage */
|
|
|
|
kerberos_DecryptMessage, /* DecryptMessage */
|
|
|
|
NULL, /* SetContextAttributes */
|
2017-05-11 19:51:45 +03:00
|
|
|
};
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
const SecurityFunctionTableW KERBEROS_SecurityFunctionTableW = {
|
|
|
|
1, /* dwVersion */
|
|
|
|
NULL, /* EnumerateSecurityPackages */
|
|
|
|
kerberos_QueryCredentialsAttributesW, /* QueryCredentialsAttributes */
|
|
|
|
kerberos_AcquireCredentialsHandleW, /* AcquireCredentialsHandle */
|
|
|
|
kerberos_FreeCredentialsHandle, /* FreeCredentialsHandle */
|
|
|
|
NULL, /* Reserved2 */
|
|
|
|
kerberos_InitializeSecurityContextW, /* InitializeSecurityContext */
|
|
|
|
NULL, /* AcceptSecurityContext */
|
|
|
|
NULL, /* CompleteAuthToken */
|
|
|
|
kerberos_DeleteSecurityContext, /* DeleteSecurityContext */
|
|
|
|
NULL, /* ApplyControlToken */
|
|
|
|
kerberos_QueryContextAttributesW, /* QueryContextAttributes */
|
|
|
|
NULL, /* ImpersonateSecurityContext */
|
|
|
|
NULL, /* RevertSecurityContext */
|
|
|
|
kerberos_MakeSignature, /* MakeSignature */
|
|
|
|
kerberos_VerifySignature, /* VerifySignature */
|
|
|
|
NULL, /* FreeContextBuffer */
|
|
|
|
NULL, /* QuerySecurityPackageInfo */
|
|
|
|
NULL, /* Reserved3 */
|
|
|
|
NULL, /* Reserved4 */
|
|
|
|
NULL, /* ExportSecurityContext */
|
|
|
|
NULL, /* ImportSecurityContext */
|
|
|
|
NULL, /* AddCredentials */
|
|
|
|
NULL, /* Reserved8 */
|
|
|
|
NULL, /* QuerySecurityContextToken */
|
|
|
|
kerberos_EncryptMessage, /* EncryptMessage */
|
|
|
|
kerberos_DecryptMessage, /* DecryptMessage */
|
|
|
|
NULL, /* SetContextAttributes */
|
2017-05-11 19:51:45 +03:00
|
|
|
};
|