Path: blob/main/crypto/krb5/src/tests/gssapi/t_gssexts.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#include <stdio.h>26#include <stdlib.h>27#include <string.h>2829#include "common.h"3031/*32* Test program for protocol transition (S4U2Self) and constrained delegation33* (S4U2Proxy)34*35* Note: because of name canonicalization, the following tips may help36* when configuring with Active Directory:37*38* - Create a computer account FOO$39* - Set the UPN to host/foo.domain (no suffix); this is necessary to40* be able to send an AS-REQ as this principal, otherwise you would41* need to use the canonical name (FOO$), which will cause principal42* comparison errors in gss_accept_sec_context().43* - Add a SPN of host/foo.domain44* - Configure the computer account to support constrained delegation with45* protocol transition (Trust this computer for delegation to specified46* services only / Use any authentication protocol)47* - Add host/foo.domain to the keytab (possibly easiest to do this48* with ktadd)49*50* For S4U2Proxy to work the TGT must be forwardable too.51*52* Usage eg:53*54* kinit -k -t test.keytab -f 'host/[email protected]'55* ./t_s4u p:[email protected] p:HOST/[email protected] test.keytab56*/5758static int use_spnego = 0;5960static void61test_prf(gss_ctx_id_t initiatorContext, gss_ctx_id_t acceptorContext,62int flags)63{64gss_buffer_desc constant;65OM_uint32 major, minor;66unsigned int i;67gss_buffer_desc initiatorPrf;68gss_buffer_desc acceptorPrf;6970constant.value = "gss prf test";71constant.length = strlen((char *)constant.value);7273initiatorPrf.value = NULL;74acceptorPrf.value = NULL;7576major = gss_pseudo_random(&minor, initiatorContext, flags, &constant, 19,77&initiatorPrf);78check_gsserr("gss_pseudo_random", major, minor);7980printf("%s\n", flags == GSS_C_PRF_KEY_FULL ?81"PRF_KEY_FULL" : "PRF_KEY_PARTIAL");8283printf("Initiator PRF: ");84for (i = 0; i < initiatorPrf.length; i++)85printf("%02x ", ((char *)initiatorPrf.value)[i] & 0xFF);86printf("\n");8788major = gss_pseudo_random(&minor, acceptorContext, flags, &constant, 19,89&acceptorPrf);90check_gsserr("gss_pseudo_random", major, minor);9192printf("Acceptor PRF: ");93for (i = 0; i < acceptorPrf.length; i++)94printf("%02x ", ((char *)acceptorPrf.value)[i] & 0xFF);95printf("\n");9697if (acceptorPrf.length != initiatorPrf.length ||98memcmp(acceptorPrf.value, initiatorPrf.value, initiatorPrf.length)) {99fprintf(stderr, "Initiator and acceptor PRF output does not match\n");100exit(1);101}102103(void)gss_release_buffer(&minor, &initiatorPrf);104(void)gss_release_buffer(&minor, &acceptorPrf);105}106107static void108init_accept_sec_context(gss_cred_id_t claimant_cred_handle,109gss_cred_id_t verifier_cred_handle,110gss_cred_id_t *deleg_cred_handle)111{112OM_uint32 major, minor, flags;113gss_name_t source_name = GSS_C_NO_NAME, target_name = GSS_C_NO_NAME;114gss_ctx_id_t initiator_context, acceptor_context;115gss_OID mech;116117*deleg_cred_handle = GSS_C_NO_CREDENTIAL;118119major = gss_inquire_cred(&minor, verifier_cred_handle, &target_name, NULL,120NULL, NULL);121check_gsserr("gss_inquire_cred", major, minor);122display_canon_name("Target name", target_name, &mech_krb5);123124mech = use_spnego ? &mech_spnego : &mech_krb5;125display_oid("Target mech", mech);126127flags = GSS_C_REPLAY_FLAG | GSS_C_SEQUENCE_FLAG;128establish_contexts(mech, claimant_cred_handle, verifier_cred_handle,129target_name, flags, &initiator_context,130&acceptor_context, &source_name, NULL,131deleg_cred_handle);132133test_prf(initiator_context, acceptor_context, GSS_C_PRF_KEY_FULL);134test_prf(initiator_context, acceptor_context, GSS_C_PRF_KEY_PARTIAL);135136(void)gss_release_name(&minor, &source_name);137(void)gss_delete_sec_context(&minor, &acceptor_context, NULL);138(void)gss_delete_sec_context(&minor, &initiator_context, NULL);139}140141static void142get_default_cred(const char *keytab_name, gss_OID_set mechs,143gss_cred_id_t *impersonator_cred_handle)144{145OM_uint32 major = GSS_S_FAILURE, minor;146krb5_error_code ret;147krb5_context context = NULL;148krb5_keytab keytab = NULL;149krb5_principal keytab_principal = NULL;150krb5_ccache ccache = NULL;151152if (keytab_name != NULL) {153ret = krb5_init_context(&context);154check_k5err(context, "krb5_init_context", ret);155156ret = krb5_kt_resolve(context, keytab_name, &keytab);157check_k5err(context, "krb5_kt_resolve", ret);158159ret = krb5_cc_default(context, &ccache);160check_k5err(context, "krb5_cc_default", ret);161162ret = krb5_cc_get_principal(context, ccache, &keytab_principal);163check_k5err(context, "krb5_cc_get_principal", ret);164165major = gss_krb5_import_cred(&minor, ccache, keytab_principal, keytab,166impersonator_cred_handle);167check_gsserr("gss_krb5_import_cred", major, minor);168169krb5_free_principal(context, keytab_principal);170krb5_cc_close(context, ccache);171krb5_kt_close(context, keytab);172krb5_free_context(context);173} else {174major = gss_acquire_cred(&minor, GSS_C_NO_NAME, GSS_C_INDEFINITE,175mechs, GSS_C_BOTH, impersonator_cred_handle,176NULL, NULL);177check_gsserr("gss_acquire_cred", major, minor);178}179}180181int182main(int argc, char *argv[])183{184OM_uint32 minor, major;185gss_cred_id_t impersonator_cred_handle = GSS_C_NO_CREDENTIAL;186gss_cred_id_t user_cred_handle = GSS_C_NO_CREDENTIAL;187gss_cred_id_t delegated_cred_handle = GSS_C_NO_CREDENTIAL;188gss_name_t user = GSS_C_NO_NAME, target = GSS_C_NO_NAME;189gss_OID_set mechs, actual_mechs = GSS_C_NO_OID_SET;190uid_t uid;191192if (argc < 2 || argc > 5) {193fprintf(stderr, "Usage: %s [--spnego] [user] "194"[proxy-target] [keytab]\n", argv[0]);195fprintf(stderr, " proxy-target and keytab are optional\n");196exit(1);197}198199if (strcmp(argv[1], "--spnego") == 0) {200use_spnego++;201argc--;202argv++;203}204205user = import_name(argv[1]);206207major = gss_pname_to_uid(&minor, user, NULL, &uid);208check_gsserr("gss_pname_to_uid(user)", major, minor);209210if (argc > 2 && strcmp(argv[2], "-") != 0)211target = import_name(argv[2]);212213mechs = use_spnego ? &mechset_spnego : &mechset_krb5;214215get_default_cred((argc > 3) ? argv[3] : NULL, mechs,216&impersonator_cred_handle);217218printf("Protocol transition tests follow\n");219printf("-----------------------------------\n\n");220221/* get S4U2Self cred */222major = gss_acquire_cred_impersonate_name(&minor, impersonator_cred_handle,223user, GSS_C_INDEFINITE, mechs,224GSS_C_INITIATE,225&user_cred_handle, &actual_mechs,226NULL);227check_gsserr("gss_acquire_cred_impersonate_name", major, minor);228229/* Try to store it in default ccache */230major = gss_store_cred(&minor, user_cred_handle, GSS_C_INITIATE,231&mechs->elements[0], 1, 1, NULL, NULL);232check_gsserr("gss_store_cred", major, minor);233234init_accept_sec_context(user_cred_handle, impersonator_cred_handle,235&delegated_cred_handle);236237printf("\n");238239(void)gss_release_name(&minor, &user);240(void)gss_release_name(&minor, &target);241(void)gss_release_cred(&minor, &delegated_cred_handle);242(void)gss_release_cred(&minor, &impersonator_cred_handle);243(void)gss_release_cred(&minor, &user_cred_handle);244(void)gss_release_oid_set(&minor, &actual_mechs);245return 0;246}247248249