Path: blob/main/crypto/krb5/src/tests/gssapi/t_s4u.c
34889 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/*2* Copyright 2009 by the Massachusetts Institute of Technology.3* All Rights Reserved.4*5* Export of this software from the United States of America may6* require a specific license from the United States Government.7* It is the responsibility of any person or organization contemplating8* export to obtain such a license before exporting.9*10* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and11* distribute this software and its documentation for any purpose and12* without fee is hereby granted, provided that the above copyright13* notice appear in all copies and that both that copyright notice and14* this permission notice appear in supporting documentation, and that15* the name of M.I.T. not be used in advertising or publicity pertaining16* to distribution of the software without specific, written prior17* permission. Furthermore if you modify this software you must label18* your software as modified software and not distribute it in such a19* fashion that it might be confused with the original M.I.T. software.20* M.I.T. makes no representations about the suitability of21* this software for any purpose. It is provided "as is" without express22* or implied warranty.23*/2425/*26* Test program for protocol transition (S4U2Self) and constrained delegation27* (S4U2Proxy)28*29* Note: because of name canonicalization, the following tips may help30* when configuring with Active Directory:31*32* - Create a computer account FOO$33* - Set the UPN to host/foo.domain (no suffix); this is necessary to34* be able to send an AS-REQ as this principal, otherwise you would35* need to use the canonical name (FOO$), which will cause principal36* comparison errors in gss_accept_sec_context().37* - Add a SPN of host/foo.domain38* - Configure the computer account to support constrained delegation with39* protocol transition (Trust this computer for delegation to specified40* services only / Use any authentication protocol)41* - Add host/foo.domain to the keytab (possibly easiest to do this42* with ktadd)43*44* For S4U2Proxy to work the TGT must be forwardable too.45*46* Usage eg:47*48* kinit -k -t test.keytab -f 'host/[email protected]'49* ./t_s4u p:[email protected] p:HOST/[email protected] test.keytab50*/5152#include <stdio.h>53#include <stdlib.h>54#include <string.h>5556#include "common.h"5758static int use_spnego = 0;5960static void61test_greet_authz_data(gss_name_t *name)62{63OM_uint32 major, minor;64gss_buffer_desc attr;65gss_buffer_desc value;66gss_name_t canon;6768major = gss_canonicalize_name(&minor, *name, &mech_krb5, &canon);69check_gsserr("gss_canonicalize_name", major, minor);7071attr.value = "greet:greeting";72attr.length = strlen((char *)attr.value);7374value.value = "Hello, acceptor world!";75value.length = strlen((char *)value.value);7677major = gss_set_name_attribute(&minor, canon, 1, &attr, &value);78if (major == GSS_S_UNAVAILABLE) {79(void)gss_release_name(&minor, &canon);80return;81}82check_gsserr("gss_set_name_attribute", major, minor);83gss_release_name(&minor, name);84*name = canon;85}8687static void88init_accept_sec_context(gss_cred_id_t claimant_cred_handle,89gss_cred_id_t verifier_cred_handle,90gss_cred_id_t *deleg_cred_handle)91{92OM_uint32 major, minor, flags;93gss_name_t source_name = GSS_C_NO_NAME, target_name = GSS_C_NO_NAME;94gss_ctx_id_t initiator_context, acceptor_context;95gss_OID mech = GSS_C_NO_OID;9697*deleg_cred_handle = GSS_C_NO_CREDENTIAL;9899major = gss_inquire_cred(&minor, verifier_cred_handle, &target_name, NULL,100NULL, NULL);101check_gsserr("gss_inquire_cred", major, minor);102103display_canon_name("Target name", target_name, &mech_krb5);104105mech = use_spnego ? &mech_spnego : &mech_krb5;106display_oid("Target mech", mech);107108flags = GSS_C_REPLAY_FLAG | GSS_C_SEQUENCE_FLAG;109establish_contexts(mech, claimant_cred_handle, verifier_cred_handle,110target_name, flags, &initiator_context,111&acceptor_context, &source_name, &mech,112deleg_cred_handle);113114display_canon_name("Source name", source_name, &mech_krb5);115display_oid("Source mech", mech);116enumerate_attributes(source_name, 1);117118(void)gss_release_name(&minor, &source_name);119(void)gss_release_name(&minor, &target_name);120(void)gss_delete_sec_context(&minor, &initiator_context, NULL);121(void)gss_delete_sec_context(&minor, &acceptor_context, NULL);122}123124static void125check_ticket_count(gss_cred_id_t cred, int expected)126{127krb5_error_code ret;128krb5_context context = NULL;129krb5_creds kcred;130krb5_cc_cursor cur;131krb5_ccache ccache;132int count = 0;133gss_key_value_set_desc store;134gss_key_value_element_desc elem;135OM_uint32 major, minor;136const char *ccname = "MEMORY:count";137138store.count = 1;139store.elements = &elem;140elem.key = "ccache";141elem.value = ccname;142major = gss_store_cred_into(&minor, cred, GSS_C_INITIATE, &mech_krb5, 1, 0,143&store, NULL, NULL);144check_gsserr("gss_store_cred_into", major, minor);145146ret = krb5_init_context(&context);147check_k5err(context, "krb5_init_context", ret);148149ret = krb5_cc_resolve(context, ccname, &ccache);150check_k5err(context, "krb5_cc_resolve", ret);151152ret = krb5_cc_start_seq_get(context, ccache, &cur);153check_k5err(context, "krb5_cc_start_seq_get", ret);154155while (!krb5_cc_next_cred(context, ccache, &cur, &kcred)) {156if (!krb5_is_config_principal(context, kcred.server))157count++;158krb5_free_cred_contents(context, &kcred);159}160161ret = krb5_cc_end_seq_get(context, ccache, &cur);162check_k5err(context, "krb5_cc_end_seq_get", ret);163164if (expected != count) {165printf("Expected %d tickets but got %d\n", expected, count);166exit(1);167}168169krb5_cc_destroy(context, ccache);170krb5_free_context(context);171}172173static void174constrained_delegate(gss_OID_set desired_mechs, gss_name_t target,175gss_cred_id_t delegated_cred_handle,176gss_cred_id_t verifier_cred_handle)177{178OM_uint32 major, minor;179gss_ctx_id_t initiator_context = GSS_C_NO_CONTEXT;180gss_name_t cred_name = GSS_C_NO_NAME;181OM_uint32 time_rec, lifetime;182gss_cred_usage_t usage;183gss_buffer_desc token;184gss_OID_set mechs;185186printf("Constrained delegation tests follow\n");187printf("-----------------------------------\n\n");188189if (gss_inquire_cred(&minor, verifier_cred_handle, &cred_name,190&lifetime, &usage, NULL) == GSS_S_COMPLETE) {191display_canon_name("Proxy name", cred_name, &mech_krb5);192(void)gss_release_name(&minor, &cred_name);193}194display_canon_name("Target name", target, &mech_krb5);195if (gss_inquire_cred(&minor, delegated_cred_handle, &cred_name,196&lifetime, &usage, &mechs) == GSS_S_COMPLETE) {197display_canon_name("Delegated name", cred_name, &mech_krb5);198display_oid("Delegated mech", &mechs->elements[0]);199(void)gss_release_name(&minor, &cred_name);200}201202printf("\n");203204major = gss_init_sec_context(&minor, delegated_cred_handle,205&initiator_context, target,206mechs ? &mechs->elements[0] : &mech_krb5,207GSS_C_REPLAY_FLAG | GSS_C_SEQUENCE_FLAG,208GSS_C_INDEFINITE, GSS_C_NO_CHANNEL_BINDINGS,209GSS_C_NO_BUFFER, NULL, &token, NULL,210&time_rec);211check_gsserr("gss_init_sec_context", major, minor);212213(void)gss_release_buffer(&minor, &token);214(void)gss_delete_sec_context(&minor, &initiator_context, NULL);215216/* Ensure a second call does not acquire new ticket. */217major = gss_init_sec_context(&minor, delegated_cred_handle,218&initiator_context, target,219mechs ? &mechs->elements[0] : &mech_krb5,220GSS_C_REPLAY_FLAG | GSS_C_SEQUENCE_FLAG,221GSS_C_INDEFINITE, GSS_C_NO_CHANNEL_BINDINGS,222GSS_C_NO_BUFFER, NULL, &token, NULL,223&time_rec);224check_gsserr("gss_init_sec_context", major, minor);225226(void)gss_release_buffer(&minor, &token);227(void)gss_delete_sec_context(&minor, &initiator_context, NULL);228(void)gss_release_oid_set(&minor, &mechs);229230/* We expect three tickets: our TGT, the evidence ticket, and the ticket to231* the target service. */232check_ticket_count(delegated_cred_handle, 3);233}234235int236main(int argc, char *argv[])237{238OM_uint32 minor, major;239gss_cred_id_t impersonator_cred_handle = GSS_C_NO_CREDENTIAL;240gss_cred_id_t user_cred_handle = GSS_C_NO_CREDENTIAL;241gss_cred_id_t delegated_cred_handle = GSS_C_NO_CREDENTIAL;242gss_name_t user = GSS_C_NO_NAME, target = GSS_C_NO_NAME;243gss_OID_set mechs;244gss_buffer_set_t bufset = GSS_C_NO_BUFFER_SET;245246if (argc < 2 || argc > 5) {247fprintf(stderr, "Usage: %s [--spnego] [user] "248"[proxy-target] [keytab]\n", argv[0]);249fprintf(stderr, " proxy-target and keytab are optional\n");250exit(1);251}252253if (strcmp(argv[1], "--spnego") == 0) {254use_spnego++;255argc--;256argv++;257}258259user = import_name(argv[1]);260261if (argc > 2 && strcmp(argv[2], "-"))262target = import_name(argv[2]);263264if (argc > 3) {265major = krb5_gss_register_acceptor_identity(argv[3]);266check_gsserr("krb5_gss_register_acceptor_identity", major, 0);267}268269/* Get default cred. */270mechs = use_spnego ? &mechset_spnego : &mechset_krb5;271major = gss_acquire_cred(&minor, GSS_C_NO_NAME, GSS_C_INDEFINITE, mechs,272GSS_C_BOTH, &impersonator_cred_handle, NULL,273NULL);274check_gsserr("gss_acquire_cred", major, minor);275276printf("Protocol transition tests follow\n");277printf("-----------------------------------\n\n");278279test_greet_authz_data(&user);280281/* Get S4U2Self cred. */282major = gss_acquire_cred_impersonate_name(&minor, impersonator_cred_handle,283user, GSS_C_INDEFINITE, mechs,284GSS_C_INITIATE,285&user_cred_handle, NULL, NULL);286check_gsserr("gss_acquire_cred_impersonate_name", major, minor);287288init_accept_sec_context(user_cred_handle, impersonator_cred_handle,289&delegated_cred_handle);290printf("\n");291292if (target != GSS_C_NO_NAME &&293delegated_cred_handle != GSS_C_NO_CREDENTIAL) {294constrained_delegate(mechs, target, delegated_cred_handle,295impersonator_cred_handle);296} else if (target != GSS_C_NO_NAME) {297fprintf(stderr, "Warning: no delegated cred handle returned\n\n");298fprintf(stderr, "Verify:\n\n");299fprintf(stderr, " - The TGT for the impersonating service is "300"forwardable\n");301fprintf(stderr, " - The T2A4D flag set on the impersonating service's "302"UAC\n");303fprintf(stderr, " - The user is not marked sensitive and cannot be "304"delegated\n");305fprintf(stderr, "\n");306}307308if (delegated_cred_handle != GSS_C_NO_CREDENTIAL) {309/* Inquire impersonator status. */310major = gss_inquire_cred_by_oid(&minor, user_cred_handle,311GSS_KRB5_GET_CRED_IMPERSONATOR,312&bufset);313check_gsserr("gss_inquire_cred_by_oid", major, minor);314if (bufset->count == 0)315errout("gss_inquire_cred_by_oid(user) returned NO impersonator");316(void)gss_release_buffer_set(&minor, &bufset);317318major = gss_inquire_cred_by_oid(&minor, impersonator_cred_handle,319GSS_KRB5_GET_CRED_IMPERSONATOR,320&bufset);321check_gsserr("gss_inquire_cred_by_oid", major, minor);322if (bufset->count != 0)323errout("gss_inquire_cred_by_oid(svc) returned an impersonator");324(void)gss_release_buffer_set(&minor, &bufset);325}326327(void)gss_release_name(&minor, &user);328(void)gss_release_name(&minor, &target);329(void)gss_release_cred(&minor, &delegated_cred_handle);330(void)gss_release_cred(&minor, &impersonator_cred_handle);331(void)gss_release_cred(&minor, &user_cred_handle);332return 0;333}334335336