gss_accept_sec_context.c [plain text]
#include "mech_locl.h"
static OM_uint32
parse_header(const gss_buffer_t input_token, gss_OID *mech_oid)
{
gss_OID_desc mech;
unsigned char *p = input_token->value;
size_t len = input_token->length;
size_t a, b;
if (len == 0)
return (GSS_S_DEFECTIVE_TOKEN);
p++;
len--;
if (len == 0)
return (GSS_S_DEFECTIVE_TOKEN);
if ((*p & 0x80) == 0) {
a = *p;
p++;
len--;
} else {
b = *p & 0x7f;
p++;
len--;
if (len < b)
return (GSS_S_DEFECTIVE_TOKEN);
a = 0;
while (b) {
a = (a << 8) | *p;
p++;
len--;
b--;
}
}
if (a != len)
return (GSS_S_DEFECTIVE_TOKEN);
if (len < 2 || *p != 0x06)
return (GSS_S_DEFECTIVE_TOKEN);
if ((p[1] & 0x80) || p[1] > (len - 2))
return (GSS_S_DEFECTIVE_TOKEN);
mech.length = p[1];
p += 2;
mech.elements = p;
*mech_oid = _gss_mg_support_mechanism(&mech);
if (*mech_oid == GSS_C_NO_OID)
return GSS_S_BAD_MECH;
return GSS_S_COMPLETE;
}
static OM_uint32
choose_mech(const gss_buffer_t input, gss_OID *mech_oid)
{
OM_uint32 status;
status = parse_header(input, mech_oid);
if (status == GSS_S_COMPLETE)
return GSS_S_COMPLETE;
if (input->length > 8 &&
memcmp((const char *)input->value, "NTLMSSP\x00", 8) == 0)
{
*mech_oid = &__gss_ntlm_mechanism_oid_desc;
return GSS_S_COMPLETE;
} else if (input->length != 0 &&
((const char *)input->value)[0] == 0x6E)
{
*mech_oid = &__gss_krb5_mechanism_oid_desc;
return GSS_S_COMPLETE;
} else if (input->length == 0) {
*mech_oid = &__gss_spnego_mechanism_oid_desc;
return GSS_S_COMPLETE;
}
_gss_mg_log(10, "Don't have client request mech");
return status;
}
GSSAPI_LIB_FUNCTION OM_uint32 GSSAPI_LIB_CALL
gss_accept_sec_context( OM_uint32 * __nonnull minor_status,
__nonnull gss_ctx_id_t * __nullable context_handle,
__nullable const gss_cred_id_t acceptor_cred_handle,
__nullable const gss_buffer_t input_token,
__nullable const gss_channel_bindings_t input_chan_bindings,
__nullable gss_name_t * __nullable src_name,
__nullable gss_OID * __nullable mech_type,
__nonnull gss_buffer_t output_token,
OM_uint32 * __nullable ret_flags,
OM_uint32 * __nullable time_rec,
gss_cred_id_t __nullable * __nullable delegated_cred_handle)
{
OM_uint32 major_status, mech_ret_flags, junk;
gssapi_mech_interface m;
struct _gss_context *ctx = (struct _gss_context *) *context_handle;
struct _gss_cred *cred = (struct _gss_cred *) acceptor_cred_handle;
struct _gss_mechanism_cred *mc;
gss_cred_id_t acceptor_mc, delegated_mc;
gss_name_t src_mn;
gss_OID mech_ret_type = NULL;
*minor_status = 0;
if (src_name)
*src_name = GSS_C_NO_NAME;
if (mech_type)
*mech_type = GSS_C_NO_OID;
if (ret_flags)
*ret_flags = 0;
if (time_rec)
*time_rec = 0;
if (delegated_cred_handle)
*delegated_cred_handle = GSS_C_NO_CREDENTIAL;
_mg_buffer_zero(output_token);
_gss_mg_check_credential(acceptor_cred_handle);
if (*context_handle == GSS_C_NO_CONTEXT) {
gss_OID mech_oid;
major_status = choose_mech(input_token, &mech_oid);
if (major_status != GSS_S_COMPLETE)
return major_status;
ctx = malloc(sizeof(struct _gss_context));
if (!ctx) {
*minor_status = ENOMEM;
return (GSS_S_DEFECTIVE_TOKEN);
}
memset(ctx, 0, sizeof(struct _gss_context));
m = ctx->gc_mech = __gss_get_mechanism(mech_oid);
if (!m) {
free(ctx);
_gss_mg_log(10, "mechanism client used is unknown");
return (GSS_S_BAD_MECH);
}
*context_handle = (gss_ctx_id_t) ctx;
} else {
m = ctx->gc_mech;
}
if (m->gm_flags & GM_USE_MG_CRED) {
acceptor_mc = acceptor_cred_handle;
} else if (cred) {
HEIM_SLIST_FOREACH(mc, &cred->gc_mc, gmc_link)
if (mc->gmc_mech == m)
break;
if (!mc) {
gss_delete_sec_context(&junk, context_handle, NULL);
_gss_mg_log(10, "gss-asc: client sent mech %s "
"but no credential was matching",
m->gm_name);
HEIM_SLIST_FOREACH(mc, &cred->gc_mc, gmc_link)
_gss_mg_log(10, "gss-asc: available creds where %s", mc->gmc_mech->gm_name);
return (GSS_S_BAD_MECH);
}
acceptor_mc = mc->gmc_cred;
} else {
acceptor_mc = GSS_C_NO_CREDENTIAL;
}
delegated_mc = GSS_C_NO_CREDENTIAL;
mech_ret_flags = 0;
major_status = m->gm_accept_sec_context(minor_status,
&ctx->gc_ctx,
acceptor_mc,
input_token,
input_chan_bindings,
&src_mn,
&mech_ret_type,
output_token,
&mech_ret_flags,
time_rec,
&delegated_mc);
if (major_status != GSS_S_COMPLETE &&
major_status != GSS_S_CONTINUE_NEEDED)
{
_gss_mg_error(m, *minor_status);
gss_delete_sec_context(&junk, context_handle, NULL);
return (major_status);
}
if (mech_type)
*mech_type = mech_ret_type;
if (src_name && src_mn) {
struct _gss_name *name = _gss_create_name(src_mn, m);
if (!name) {
m->gm_release_name(minor_status, &src_mn);
gss_delete_sec_context(&junk, context_handle, NULL);
return (GSS_S_FAILURE);
}
*src_name = (gss_name_t) name;
} else if (src_mn) {
m->gm_release_name(minor_status, &src_mn);
}
if (mech_ret_flags & GSS_C_DELEG_FLAG) {
if (!delegated_cred_handle) {
if (m->gm_flags & GM_USE_MG_CRED)
gss_release_cred(minor_status, &delegated_mc);
else
m->gm_release_cred(minor_status, &delegated_mc);
mech_ret_flags &=
~(GSS_C_DELEG_FLAG|GSS_C_DELEG_POLICY_FLAG);
} else if ((m->gm_flags & GM_USE_MG_CRED) != 0) {
*delegated_cred_handle = delegated_mc;
} else if (gss_oid_equal(mech_ret_type, &m->gm_mech_oid) == 0) {
*delegated_cred_handle = delegated_mc;
} else if (delegated_mc) {
struct _gss_cred *dcred;
struct _gss_mechanism_cred *dmc;
dcred = _gss_mg_alloc_cred();
if (!dcred) {
*minor_status = ENOMEM;
gss_delete_sec_context(&junk, context_handle, NULL);
return (GSS_S_FAILURE);
}
dmc = malloc(sizeof(struct _gss_mechanism_cred));
if (!dmc) {
free(dcred);
*minor_status = ENOMEM;
gss_delete_sec_context(&junk, context_handle, NULL);
return (GSS_S_FAILURE);
}
dmc->gmc_mech = m;
dmc->gmc_mech_oid = &m->gm_mech_oid;
dmc->gmc_cred = delegated_mc;
HEIM_SLIST_INSERT_HEAD(&dcred->gc_mc, dmc, gmc_link);
*delegated_cred_handle = (gss_cred_id_t) dcred;
}
}
_gss_mg_log(10, "gss-asc: return %d/%d", (int)major_status, (int)*minor_status);
if (ret_flags)
*ret_flags = mech_ret_flags;
return (major_status);
}