Path: blob/main/crypto/krb5/src/tests/gssapi/t_s4u2proxy_krb5.c
34907 views
/* -*- mode: c; indent-tabs-mode: nil -*- */1/* tests/gssapi/t_s4u2proxy_deleg.c - Test S4U2Proxy after krb5 auth */2/*3* Copyright 2011 by the Massachusetts Institute of Technology.4* All Rights Reserved.5*6* Export of this software from the United States of America may7* require a specific license from the United States Government.8* It is the responsibility of any person or organization contemplating9* export to obtain such a license before exporting.10*11* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and12* distribute this software and its documentation for any purpose and13* without fee is hereby granted, provided that the above copyright14* notice appear in all copies and that both that copyright notice and15* this permission notice appear in supporting documentation, and that16* the name of M.I.T. not be used in advertising or publicity pertaining17* to distribution of the software without specific, written prior18* permission. Furthermore if you modify this software you must label19* your software as modified software and not distribute it in such a20* fashion that it might be confused with the original M.I.T. software.21* M.I.T. makes no representations about the suitability of22* this software for any purpose. It is provided "as is" without express23* or implied warranty.24*/2526#include <stdio.h>27#include <stdlib.h>28#include <string.h>2930#include "common.h"3132/*33* Usage: ./t_s4u2proxy_krb5 [--spnego] client_cache storage_cache34* [accname|-] service1 service235*36* This program performs a regular Kerberos or SPNEGO authentication from the37* default principal of client_cache to service1. If that authentication38* yields delegated credentials, the program stores those credentials in39* sorage_ccache and uses that cache to perform a second authentication to40* service2 using S4U2Proxy.41*42* The default keytab must contain keys for service1 and service2. The default43* ccache must contain a TGT for service1. This program assumes that krb5 or44* SPNEGO authentication requires only one token exchange.45*/4647int48main(int argc, char *argv[])49{50const char *client_ccname, *storage_ccname, *accname, *service1, *service2;51krb5_context context = NULL;52krb5_error_code ret;53krb5_boolean use_spnego = FALSE;54krb5_ccache storage_ccache = NULL;55krb5_principal client_princ = NULL;56OM_uint32 minor, major, flags;57gss_buffer_desc buf = GSS_C_EMPTY_BUFFER;58gss_OID mech;59gss_OID_set mechs;60gss_name_t acceptor_name = GSS_C_NO_NAME, client_name = GSS_C_NO_NAME;61gss_name_t service1_name = GSS_C_NO_NAME, service2_name = GSS_C_NO_NAME;62gss_cred_id_t service1_cred = GSS_C_NO_CREDENTIAL;63gss_cred_id_t deleg_cred = GSS_C_NO_CREDENTIAL;64gss_ctx_id_t initiator_context, acceptor_context;6566/* Parse arguments. */67if (argc >= 2 && strcmp(argv[1], "--spnego") == 0) {68use_spnego = TRUE;69argc--;70argv++;71}72if (argc != 6) {73fprintf(stderr, "./t_s4u2proxy_krb5 [--spnego] client_ccache "74"storage_ccache [accname|-] service1 service2\n");75return 1;76}77client_ccname = argv[1];78storage_ccname = argv[2];79accname = argv[3];80service1 = argv[4];81service2 = argv[5];8283mech = use_spnego ? &mech_spnego : &mech_krb5;84mechs = use_spnego ? &mechset_spnego : &mechset_krb5;85ret = krb5_init_context(&context);86check_k5err(context, "krb5_init_context", ret);8788/* Get GSS_C_BOTH acceptor credentials, using the default ccache. */89acceptor_name = GSS_C_NO_NAME;90if (strcmp(accname, "-") != 0)91acceptor_name = import_name(service1);92major = gss_acquire_cred(&minor, acceptor_name, GSS_C_INDEFINITE,93mechs, GSS_C_BOTH, &service1_cred, NULL, NULL);94check_gsserr("gss_acquire_cred(service1)", major, minor);9596/* Establish contexts using the client ccache. */97service1_name = import_name(service1);98major = gss_krb5_ccache_name(&minor, client_ccname, NULL);99check_gsserr("gss_krb5_ccache_name(1)", major, minor);100flags = GSS_C_REPLAY_FLAG | GSS_C_SEQUENCE_FLAG;101establish_contexts(mech, GSS_C_NO_CREDENTIAL, service1_cred, service1_name,102flags, &initiator_context, &acceptor_context,103&client_name, NULL, &deleg_cred);104105/* Display and remember the client principal. */106major = gss_display_name(&minor, client_name, &buf, NULL);107check_gsserr("gss_display_name(1)", major, minor);108printf("auth1: %.*s\n", (int)buf.length, (char *)buf.value);109/* Assumes buffer is null-terminated, which in our implementation it is. */110ret = krb5_parse_name(context, buf.value, &client_princ);111check_k5err(context, "krb5_parse_name", ret);112(void)gss_release_buffer(&minor, &buf);113114if (deleg_cred == GSS_C_NO_CREDENTIAL) {115printf("no credential delegated.\n");116goto cleanup;117}118119/* Take the opportunity to test cred export/import on the synthesized120* S4U2Proxy delegated cred. */121export_import_cred(&deleg_cred);122123/* Store the delegated credentials. */124ret = krb5_cc_resolve(context, storage_ccname, &storage_ccache);125check_k5err(context, "krb5_cc_resolve", ret);126ret = krb5_cc_initialize(context, storage_ccache, client_princ);127check_k5err(context, "krb5_cc_initialize", ret);128major = gss_krb5_copy_ccache(&minor, deleg_cred, storage_ccache);129check_gsserr("gss_krb5_copy_ccache", major, minor);130ret = krb5_cc_close(context, storage_ccache);131check_k5err(context, "krb5_cc_close", ret);132133(void)gss_delete_sec_context(&minor, &initiator_context, GSS_C_NO_BUFFER);134(void)gss_delete_sec_context(&minor, &acceptor_context, GSS_C_NO_BUFFER);135(void)gss_release_name(&minor, &client_name);136(void)gss_release_cred(&minor, &deleg_cred);137138/* Establish contexts using the storage ccache. */139service2_name = import_name(service2);140major = gss_krb5_ccache_name(&minor, storage_ccname, NULL);141check_gsserr("gss_krb5_ccache_name(2)", major, minor);142establish_contexts(mech, GSS_C_NO_CREDENTIAL, GSS_C_NO_CREDENTIAL,143service2_name, flags, &initiator_context,144&acceptor_context, &client_name, NULL, &deleg_cred);145146major = gss_display_name(&minor, client_name, &buf, NULL);147check_gsserr("gss_display_name(2)", major, minor);148printf("auth2: %.*s\n", (int)buf.length, (char *)buf.value);149(void)gss_release_buffer(&minor, &buf);150151cleanup:152(void)gss_release_name(&minor, &acceptor_name);153(void)gss_release_name(&minor, &client_name);154(void)gss_release_name(&minor, &service1_name);155(void)gss_release_name(&minor, &service2_name);156(void)gss_release_cred(&minor, &service1_cred);157(void)gss_release_cred(&minor, &deleg_cred);158(void)gss_delete_sec_context(&minor, &initiator_context, GSS_C_NO_BUFFER);159(void)gss_delete_sec_context(&minor, &acceptor_context, GSS_C_NO_BUFFER);160krb5_free_principal(context, client_princ);161krb5_free_context(context);162return 0;163}164165166