2022-12-12 04:51:08 +03:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Client
|
|
|
|
* Kerberos Auth Protocol
|
|
|
|
*
|
|
|
|
* Copyright 2022 Isaac Klein <fifthdegree@protonmail.com>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2023-05-23 10:05:48 +03:00
|
|
|
#ifndef WITH_KRB5_MIT
|
|
|
|
#error "This file must only be included with MIT kerberos"
|
|
|
|
#endif
|
2022-12-12 04:51:08 +03:00
|
|
|
|
|
|
|
#include <winpr/path.h>
|
|
|
|
#include <winpr/wlog.h>
|
|
|
|
#include <winpr/endian.h>
|
|
|
|
#include <winpr/crypto.h>
|
|
|
|
#include <winpr/print.h>
|
2022-12-12 18:38:27 +03:00
|
|
|
#include <winpr/assert.h>
|
2022-12-12 04:51:08 +03:00
|
|
|
#include <errno.h>
|
|
|
|
#include "krb5glue.h"
|
|
|
|
#include <profile.h>
|
|
|
|
|
|
|
|
static char* create_temporary_file(void)
|
|
|
|
{
|
|
|
|
BYTE buffer[32];
|
|
|
|
char* hex;
|
|
|
|
char* path;
|
|
|
|
|
|
|
|
winpr_RAND(buffer, sizeof(buffer));
|
|
|
|
hex = winpr_BinToHexString(buffer, sizeof(buffer), FALSE);
|
|
|
|
path = GetKnownSubPath(KNOWN_PATH_TEMP, hex);
|
|
|
|
free(hex);
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
void krb5glue_keys_free(krb5_context ctx, struct krb5glue_keyset* keyset)
|
|
|
|
{
|
2022-12-12 18:38:27 +03:00
|
|
|
WINPR_ASSERT(ctx);
|
|
|
|
WINPR_ASSERT(keyset);
|
|
|
|
|
2022-12-12 04:51:08 +03:00
|
|
|
krb5_k_free_key(ctx, keyset->session_key);
|
|
|
|
krb5_k_free_key(ctx, keyset->initiator_key);
|
|
|
|
krb5_k_free_key(ctx, keyset->acceptor_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
krb5_error_code krb5glue_update_keyset(krb5_context ctx, krb5_auth_context auth_ctx, BOOL acceptor,
|
|
|
|
struct krb5glue_keyset* keyset)
|
|
|
|
{
|
2022-12-12 18:38:27 +03:00
|
|
|
WINPR_ASSERT(ctx);
|
|
|
|
WINPR_ASSERT(auth_ctx);
|
|
|
|
WINPR_ASSERT(keyset);
|
|
|
|
|
2022-12-12 04:51:08 +03:00
|
|
|
krb5glue_keys_free(ctx, keyset);
|
|
|
|
krb5_auth_con_getkey_k(ctx, auth_ctx, &keyset->session_key);
|
|
|
|
if (acceptor)
|
|
|
|
{
|
|
|
|
krb5_auth_con_getsendsubkey_k(ctx, auth_ctx, &keyset->acceptor_key);
|
|
|
|
krb5_auth_con_getrecvsubkey_k(ctx, auth_ctx, &keyset->initiator_key);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
krb5_auth_con_getsendsubkey_k(ctx, auth_ctx, &keyset->initiator_key);
|
|
|
|
krb5_auth_con_getrecvsubkey_k(ctx, auth_ctx, &keyset->acceptor_key);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
krb5_prompt_type krb5glue_get_prompt_type(krb5_context ctx, krb5_prompt prompts[], int index)
|
|
|
|
{
|
2022-12-12 18:38:27 +03:00
|
|
|
WINPR_ASSERT(ctx);
|
|
|
|
WINPR_ASSERT(prompts);
|
|
|
|
|
2022-12-12 04:51:08 +03:00
|
|
|
krb5_prompt_type* types = krb5_get_prompt_types(ctx);
|
|
|
|
return types ? types[index] : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
krb5_error_code krb5glue_log_error(krb5_context ctx, krb5_data* msg, const char* tag)
|
|
|
|
{
|
|
|
|
krb5_error* error = NULL;
|
|
|
|
krb5_error_code rv = 0;
|
|
|
|
|
2022-12-12 18:38:27 +03:00
|
|
|
WINPR_ASSERT(ctx);
|
|
|
|
WINPR_ASSERT(msg);
|
|
|
|
WINPR_ASSERT(tag);
|
|
|
|
|
2022-12-12 04:51:08 +03:00
|
|
|
if (!(rv = krb5_rd_error(ctx, msg, &error)))
|
|
|
|
{
|
|
|
|
WLog_ERR(tag, "KRB_ERROR: %s", error->text.data);
|
|
|
|
krb5_free_error(ctx, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL krb5glue_authenticator_validate_chksum(krb5glue_authenticator authenticator, int cksumtype,
|
|
|
|
uint32_t* flags)
|
|
|
|
{
|
2022-12-12 18:38:27 +03:00
|
|
|
WINPR_ASSERT(flags);
|
|
|
|
|
2022-12-12 04:51:08 +03:00
|
|
|
if (!authenticator || !authenticator->checksum ||
|
|
|
|
authenticator->checksum->checksum_type != cksumtype || authenticator->checksum->length < 24)
|
|
|
|
return FALSE;
|
|
|
|
Data_Read_UINT32((authenticator->checksum->contents + 20), (*flags));
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
krb5_error_code krb5glue_get_init_creds(krb5_context ctx, krb5_principal princ, krb5_ccache ccache,
|
|
|
|
krb5_prompter_fct prompter, char* password,
|
|
|
|
SEC_WINPR_KERBEROS_SETTINGS* krb_settings)
|
|
|
|
{
|
|
|
|
krb5_error_code rv = 0;
|
|
|
|
krb5_deltat start_time = 0;
|
|
|
|
krb5_get_init_creds_opt* gic_opt = NULL;
|
|
|
|
krb5_init_creds_context creds_ctx = NULL;
|
|
|
|
char* tmp_profile_path = create_temporary_file();
|
|
|
|
profile_t profile = NULL;
|
2023-04-26 18:43:49 +03:00
|
|
|
BOOL is_temp_ctx = FALSE;
|
2022-12-12 04:51:08 +03:00
|
|
|
|
2022-12-12 18:38:27 +03:00
|
|
|
WINPR_ASSERT(ctx);
|
|
|
|
|
2023-10-23 18:30:59 +03:00
|
|
|
rv = krb5_get_init_creds_opt_alloc(ctx, &gic_opt);
|
|
|
|
if (rv)
|
|
|
|
goto cleanup;
|
2022-12-12 04:51:08 +03:00
|
|
|
|
|
|
|
krb5_get_init_creds_opt_set_forwardable(gic_opt, 0);
|
|
|
|
krb5_get_init_creds_opt_set_proxiable(gic_opt, 0);
|
|
|
|
|
|
|
|
if (krb_settings)
|
|
|
|
{
|
|
|
|
if (krb_settings->startTime)
|
|
|
|
start_time = krb_settings->startTime;
|
|
|
|
if (krb_settings->lifeTime)
|
|
|
|
krb5_get_init_creds_opt_set_tkt_life(gic_opt, krb_settings->lifeTime);
|
|
|
|
if (krb_settings->renewLifeTime)
|
|
|
|
krb5_get_init_creds_opt_set_renew_life(gic_opt, krb_settings->renewLifeTime);
|
|
|
|
if (krb_settings->withPac)
|
2023-10-23 18:30:59 +03:00
|
|
|
{
|
|
|
|
rv = krb5_get_init_creds_opt_set_pac_request(ctx, gic_opt, TRUE);
|
|
|
|
if (rv)
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2022-12-12 04:51:08 +03:00
|
|
|
if (krb_settings->armorCache)
|
2023-10-23 18:30:59 +03:00
|
|
|
{
|
|
|
|
rv = krb5_get_init_creds_opt_set_fast_ccache_name(ctx, gic_opt,
|
|
|
|
krb_settings->armorCache);
|
|
|
|
if (rv)
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2022-12-12 04:51:08 +03:00
|
|
|
if (krb_settings->pkinitX509Identity)
|
2023-10-23 18:30:59 +03:00
|
|
|
{
|
|
|
|
rv = krb5_get_init_creds_opt_set_pa(ctx, gic_opt, "X509_user_identity",
|
|
|
|
krb_settings->pkinitX509Identity);
|
|
|
|
if (rv)
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2022-12-12 04:51:08 +03:00
|
|
|
if (krb_settings->pkinitX509Anchors)
|
2023-10-23 18:30:59 +03:00
|
|
|
{
|
|
|
|
rv = krb5_get_init_creds_opt_set_pa(ctx, gic_opt, "X509_anchors",
|
|
|
|
krb_settings->pkinitX509Anchors);
|
|
|
|
if (rv)
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2022-12-12 04:51:08 +03:00
|
|
|
if (krb_settings->kdcUrl)
|
|
|
|
{
|
|
|
|
const char* names[4] = { 0 };
|
|
|
|
char* realm = NULL;
|
2023-04-26 19:03:01 +03:00
|
|
|
char* kdc_url = NULL;
|
|
|
|
size_t size = 0;
|
2022-12-12 04:51:08 +03:00
|
|
|
|
|
|
|
if ((rv = krb5_get_profile(ctx, &profile)))
|
|
|
|
goto cleanup;
|
|
|
|
|
2023-04-26 19:03:01 +03:00
|
|
|
rv = ENOMEM;
|
2023-05-17 20:17:59 +03:00
|
|
|
if (winpr_asprintf(&kdc_url, &size, "https://%s/KdcProxy", krb_settings->kdcUrl) <= 0)
|
2023-04-26 19:03:01 +03:00
|
|
|
goto cleanup;
|
|
|
|
|
2022-12-12 04:51:08 +03:00
|
|
|
realm = calloc(princ->realm.length + 1, 1);
|
|
|
|
if (!realm)
|
|
|
|
{
|
2023-04-26 19:03:01 +03:00
|
|
|
free(kdc_url);
|
2022-12-12 04:51:08 +03:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
CopyMemory(realm, princ->realm.data, princ->realm.length);
|
2023-04-26 19:03:01 +03:00
|
|
|
|
|
|
|
names[0] = "realms";
|
2022-12-12 04:51:08 +03:00
|
|
|
names[1] = realm;
|
|
|
|
names[2] = "kdc";
|
|
|
|
|
|
|
|
profile_clear_relation(profile, names);
|
2023-04-26 19:03:01 +03:00
|
|
|
profile_add_relation(profile, names, kdc_url);
|
2022-12-12 04:51:08 +03:00
|
|
|
|
2023-04-21 20:11:08 +03:00
|
|
|
/* Since we know who the KDC is, tell krb5 that its certificate is valid for pkinit */
|
|
|
|
names[2] = "pkinit_kdc_hostname";
|
|
|
|
profile_add_relation(profile, names, krb_settings->kdcUrl);
|
|
|
|
|
2023-04-26 19:03:01 +03:00
|
|
|
free(kdc_url);
|
2022-12-12 04:51:08 +03:00
|
|
|
free(realm);
|
|
|
|
|
|
|
|
if ((rv = profile_flush_to_file(profile, tmp_profile_path)))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
profile_release(profile);
|
|
|
|
profile = NULL;
|
|
|
|
if ((rv = profile_init_path(tmp_profile_path, &profile)))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if ((rv = krb5_init_context_profile(profile, 0, &ctx)))
|
|
|
|
goto cleanup;
|
2023-04-26 18:43:49 +03:00
|
|
|
is_temp_ctx = TRUE;
|
2022-12-12 04:51:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-26 18:43:49 +03:00
|
|
|
if ((rv = krb5_get_init_creds_opt_set_in_ccache(ctx, gic_opt, ccache)))
|
|
|
|
goto cleanup;
|
2022-12-12 04:51:08 +03:00
|
|
|
|
2023-04-26 18:43:49 +03:00
|
|
|
if ((rv = krb5_get_init_creds_opt_set_out_ccache(ctx, gic_opt, ccache)))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if ((rv =
|
|
|
|
krb5_init_creds_init(ctx, princ, prompter, password, start_time, gic_opt, &creds_ctx)))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if ((rv = krb5_init_creds_get(ctx, creds_ctx)))
|
|
|
|
goto cleanup;
|
2022-12-12 04:51:08 +03:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
krb5_init_creds_free(ctx, creds_ctx);
|
|
|
|
krb5_get_init_creds_opt_free(ctx, gic_opt);
|
2023-04-26 18:43:49 +03:00
|
|
|
if (is_temp_ctx)
|
|
|
|
krb5_free_context(ctx);
|
|
|
|
profile_release(profile);
|
2022-12-12 04:51:08 +03:00
|
|
|
winpr_DeleteFile(tmp_profile_path);
|
|
|
|
free(tmp_profile_path);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|