20 #ifndef WITH_KRB5_HEIMDAL
21 #error "This file must ony be included with HEIMDAL kerberos"
24 #include <winpr/endian.h>
25 #include <winpr/wlog.h>
26 #include <winpr/assert.h>
29 void krb5glue_keys_free(krb5_context ctx,
struct krb5glue_keyset* keyset)
33 if (keyset->session_key)
34 krb5_crypto_destroy(ctx, keyset->session_key);
35 if (keyset->initiator_key)
36 krb5_crypto_destroy(ctx, keyset->initiator_key);
37 if (keyset->acceptor_key)
38 krb5_crypto_destroy(ctx, keyset->acceptor_key);
41 krb5_error_code krb5glue_update_keyset(krb5_context ctx, krb5_auth_context auth_ctx, BOOL acceptor,
44 krb5_keyblock* keyblock = NULL;
45 krb5_error_code rv = 0;
48 WINPR_ASSERT(auth_ctx);
51 krb5glue_keys_free(ctx, keyset);
53 if (!(rv = krb5_auth_con_getkey(ctx, auth_ctx, &keyblock)))
55 krb5_crypto_init(ctx, keyblock, ENCTYPE_NULL, &keyset->session_key);
56 krb5_free_keyblock(ctx, keyblock);
61 rv = krb5_auth_con_getremotesubkey(ctx, auth_ctx, &keyblock);
63 rv = krb5_auth_con_getlocalsubkey(ctx, auth_ctx, &keyblock);
67 krb5_crypto_init(ctx, keyblock, ENCTYPE_NULL, &keyset->initiator_key);
68 krb5_free_keyblock(ctx, keyblock);
73 rv = krb5_auth_con_getlocalsubkey(ctx, auth_ctx, &keyblock);
75 rv = krb5_auth_con_getremotesubkey(ctx, auth_ctx, &keyblock);
79 krb5_crypto_init(ctx, keyblock, ENCTYPE_NULL, &keyset->acceptor_key);
80 krb5_free_keyblock(ctx, keyblock);
86 krb5_error_code krb5glue_verify_checksum_iov(krb5_context ctx, krb5glue_key key,
unsigned usage,
87 krb5_crypto_iov* iov,
unsigned int iov_size,
88 krb5_boolean* is_valid)
90 krb5_error_code rv = 0;
94 WINPR_ASSERT(is_valid);
96 rv = krb5_verify_checksum_iov(ctx, key, usage, iov, iov_size, NULL);
97 *is_valid = (rv == 0);
101 krb5_error_code krb5glue_crypto_length(krb5_context ctx, krb5glue_key key,
int type,
104 krb5_error_code rv = 0;
111 rv = krb5_crypto_length(ctx, key, type, &s);
116 krb5_error_code krb5glue_log_error(krb5_context ctx, krb5_data* msg,
const char* tag)
118 krb5_error error = { 0 };
119 krb5_error_code rv = 0;
125 if (!(rv = krb5_rd_error(ctx, msg, &error)))
127 WLog_ERR(tag,
"KRB_ERROR: %" PRIx32, error.error_code);
128 krb5_free_error_contents(ctx, &error);
133 BOOL krb5glue_authenticator_validate_chksum(krb5glue_authenticator authenticator,
int cksumtype,
138 if (!authenticator || !authenticator->cksum || authenticator->cksum->cksumtype != cksumtype ||
139 authenticator->cksum->checksum.length < 24)
142 const BYTE* data = authenticator->cksum->checksum.data;
143 Data_Read_UINT32((data + 20), (*flags));
147 krb5_error_code krb5glue_get_init_creds(krb5_context ctx, krb5_principal princ, krb5_ccache ccache,
148 krb5_prompter_fct prompter,
char* password,
151 krb5_error_code rv = 0;
152 krb5_deltat start_time = 0;
153 krb5_get_init_creds_opt* gic_opt = NULL;
154 krb5_init_creds_context creds_ctx = NULL;
155 krb5_creds creds = { 0 };
161 if ((rv = krb5_get_init_creds_opt_alloc(ctx, &gic_opt)) != 0)
164 krb5_get_init_creds_opt_set_forwardable(gic_opt, 0);
165 krb5_get_init_creds_opt_set_proxiable(gic_opt, 0);
169 if (krb_settings->startTime)
170 start_time = krb_settings->startTime;
171 if (krb_settings->lifeTime)
172 krb5_get_init_creds_opt_set_tkt_life(gic_opt, krb_settings->lifeTime);
173 if (krb_settings->renewLifeTime)
174 krb5_get_init_creds_opt_set_renew_life(gic_opt, krb_settings->renewLifeTime);
175 if (krb_settings->withPac)
176 krb5_get_init_creds_opt_set_pac_request(ctx, gic_opt, TRUE);
177 if (krb_settings->pkinitX509Anchors || krb_settings->pkinitX509Identity)
179 if ((rv = krb5_get_init_creds_opt_set_pkinit(
180 ctx, gic_opt, princ, krb_settings->pkinitX509Identity,
181 krb_settings->pkinitX509Anchors, NULL, NULL, 0, prompter, password,
187 if ((rv = krb5_init_creds_init(ctx, princ, prompter, password, start_time, gic_opt,
190 if ((rv = krb5_init_creds_set_password(ctx, creds_ctx, password)) != 0)
192 if (krb_settings && krb_settings->armorCache)
194 krb5_ccache armor_cc = NULL;
195 if ((rv = krb5_cc_resolve(ctx, krb_settings->armorCache, &armor_cc)) != 0)
197 if ((rv = krb5_init_creds_set_fast_ccache(ctx, creds_ctx, armor_cc)) != 0)
199 krb5_cc_close(ctx, armor_cc);
201 if ((rv = krb5_init_creds_get(ctx, creds_ctx)) != 0)
203 if ((rv = krb5_init_creds_get_creds(ctx, creds_ctx, &creds)) != 0)
205 if ((rv = krb5_cc_store_cred(ctx, ccache, &creds)) != 0)
209 krb5_free_cred_contents(ctx, &creds);
210 krb5_init_creds_free(ctx, creds_ctx);
211 krb5_get_init_creds_opt_free(ctx, gic_opt);