Path: blob/main/crypto/heimdal/lib/gssapi/test_acquire_cred.c
34889 views
/*1* Copyright (c) 2003-2007 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of KTH nor the names of its contributors may be17* used to endorse or promote products derived from this software without18* specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY21* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR23* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE24* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR25* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF26* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR27* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,28* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR29* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF30* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31*/3233#ifdef HAVE_CONFIG_H34#include <config.h>35#endif3637#include <roken.h>38#include <stdio.h>39#include <stdlib.h>40#include <string.h>41#include <stdarg.h>42#include <gssapi.h>43#include <gssapi_krb5.h>44#include <gssapi_spnego.h>45#include <err.h>46#include <getarg.h>4748#include "test_common.h"4950static void51print_time(OM_uint32 time_rec)52{53if (time_rec == GSS_C_INDEFINITE) {54printf("cred never expire\n");55} else {56time_t t = time_rec + time(NULL);57printf("expiration time: %s", ctime(&t));58}59}6061#if 06263static void64test_add(gss_cred_id_t cred_handle)65{66OM_uint32 major_status, minor_status;67gss_cred_id_t copy_cred;68OM_uint32 time_rec;6970major_status = gss_add_cred (&minor_status,71cred_handle,72GSS_C_NO_NAME,73GSS_KRB5_MECHANISM,74GSS_C_INITIATE,750,760,77©_cred,78NULL,79&time_rec,80NULL);8182if (GSS_ERROR(major_status))83errx(1, "add_cred failed");8485print_time(time_rec);8687major_status = gss_release_cred(&minor_status,88©_cred);89if (GSS_ERROR(major_status))90errx(1, "release_cred failed");91}9293static void94copy_cred(void)95{96OM_uint32 major_status, minor_status;97gss_cred_id_t cred_handle;98OM_uint32 time_rec;99100major_status = gss_acquire_cred(&minor_status,101GSS_C_NO_NAME,1020,103NULL,104GSS_C_INITIATE,105&cred_handle,106NULL,107&time_rec);108if (GSS_ERROR(major_status))109errx(1, "acquire_cred failed");110111print_time(time_rec);112113test_add(cred_handle);114test_add(cred_handle);115test_add(cred_handle);116117major_status = gss_release_cred(&minor_status,118&cred_handle);119if (GSS_ERROR(major_status))120errx(1, "release_cred failed");121}122#endif123124static gss_cred_id_t125acquire_cred_service(const char *service,126gss_OID nametype,127gss_OID_set oidset,128int flags)129{130OM_uint32 major_status, minor_status;131gss_cred_id_t cred_handle;132OM_uint32 time_rec;133gss_buffer_desc name_buffer;134gss_name_t name = GSS_C_NO_NAME;135136if (service) {137name_buffer.value = rk_UNCONST(service);138name_buffer.length = strlen(service);139140major_status = gss_import_name(&minor_status,141&name_buffer,142nametype,143&name);144if (GSS_ERROR(major_status))145errx(1, "import_name failed");146}147148major_status = gss_acquire_cred(&minor_status,149name,1500,151oidset,152flags,153&cred_handle,154NULL,155&time_rec);156if (GSS_ERROR(major_status)) {157warnx("acquire_cred failed: %s",158gssapi_err(major_status, minor_status, GSS_C_NO_OID));159} else {160print_time(time_rec);161gss_release_cred(&minor_status, &cred_handle);162}163164if (name != GSS_C_NO_NAME)165gss_release_name(&minor_status, &name);166167if (GSS_ERROR(major_status))168exit(1);169170return cred_handle;171}172173static int version_flag = 0;174static int help_flag = 0;175static int kerberos_flag = 0;176static int enctype = 0;177static char *acquire_name;178static char *acquire_type;179static char *target_name;180static char *name_type;181static char *ccache;182static int num_loops = 1;183184static struct getargs args[] = {185{"acquire-name", 0, arg_string, &acquire_name, "name", NULL },186{"acquire-type", 0, arg_string, &acquire_type, "type", NULL },187{"enctype", 0, arg_integer, &enctype, "enctype-num", NULL },188{"loops", 0, arg_integer, &num_loops, "enctype-num", NULL },189{"kerberos", 0, arg_flag, &kerberos_flag, "enctype-num", NULL },190{"target-name", 0, arg_string, &target_name, "name", NULL },191{"ccache", 0, arg_string, &ccache, "name", NULL },192{"name-type", 0, arg_string, &name_type, "type", NULL },193{"version", 0, arg_flag, &version_flag, "print version", NULL },194{"help", 0, arg_flag, &help_flag, NULL, NULL }195};196197static void198usage (int ret)199{200arg_printusage (args, sizeof(args)/sizeof(*args), NULL, "");201exit (ret);202}203204int205main(int argc, char **argv)206{207gss_OID_set oidset = GSS_C_NULL_OID_SET;208gss_OID mechoid = GSS_C_NO_OID;209OM_uint32 maj_stat, min_stat;210gss_cred_id_t cred;211gss_name_t target = GSS_C_NO_NAME;212int i, optidx = 0;213OM_uint32 flag;214gss_OID type;215216setprogname(argv[0]);217if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))218usage(1);219220if (help_flag)221usage (0);222223if(version_flag){224print_version(NULL);225exit(0);226}227228argc -= optidx;229argv += optidx;230231if (argc != 0)232usage(1);233234if (acquire_type) {235if (strcasecmp(acquire_type, "both") == 0)236flag = GSS_C_BOTH;237else if (strcasecmp(acquire_type, "accept") == 0)238flag = GSS_C_ACCEPT;239else if (strcasecmp(acquire_type, "initiate") == 0)240flag = GSS_C_INITIATE;241else242errx(1, "unknown type %s", acquire_type);243} else244flag = GSS_C_ACCEPT;245246if (name_type) {247if (strcasecmp("hostbased-service", name_type) == 0)248type = GSS_C_NT_HOSTBASED_SERVICE;249else if (strcasecmp("user-name", name_type) == 0)250type = GSS_C_NT_USER_NAME;251else252errx(1, "unknown name type %s", name_type);253} else254type = GSS_C_NT_HOSTBASED_SERVICE;255256if (ccache) {257maj_stat = gss_krb5_ccache_name(&min_stat, ccache, NULL);258if (GSS_ERROR(maj_stat))259errx(1, "gss_krb5_ccache_name %s",260gssapi_err(maj_stat, min_stat, GSS_C_NO_OID));261}262263if (kerberos_flag) {264mechoid = GSS_KRB5_MECHANISM;265266maj_stat = gss_create_empty_oid_set(&min_stat, &oidset);267if (maj_stat != GSS_S_COMPLETE)268errx(1, "gss_create_empty_oid_set: %s",269gssapi_err(maj_stat, min_stat, GSS_C_NO_OID));270271maj_stat = gss_add_oid_set_member(&min_stat, GSS_KRB5_MECHANISM, &oidset);272if (maj_stat != GSS_S_COMPLETE)273errx(1, "gss_add_oid_set_member: %s",274gssapi_err(maj_stat, min_stat, GSS_C_NO_OID));275}276277if (target_name) {278gss_buffer_desc name;279280name.value = target_name;281name.length = strlen(target_name);282maj_stat = gss_import_name(&min_stat, &name,283GSS_C_NT_HOSTBASED_SERVICE, &target);284if (maj_stat != GSS_S_COMPLETE)285errx(1, "gss_import_name: %s",286gssapi_err(maj_stat, min_stat, GSS_C_NO_OID));287}288289for (i = 0; i < num_loops; i++) {290291cred = acquire_cred_service(acquire_name, type, oidset, flag);292293if (enctype) {294int32_t enctypelist = enctype;295296maj_stat = gss_krb5_set_allowable_enctypes(&min_stat, cred,2971, &enctypelist);298if (maj_stat)299errx(1, "gss_krb5_set_allowable_enctypes: %s",300gssapi_err(maj_stat, min_stat, GSS_C_NO_OID));301}302303if (target) {304gss_ctx_id_t context = GSS_C_NO_CONTEXT;305gss_buffer_desc out;306307out.length = 0;308out.value = NULL;309310maj_stat = gss_init_sec_context(&min_stat,311cred, &context,312target, mechoid,313GSS_C_MUTUAL_FLAG, 0, NULL,314GSS_C_NO_BUFFER, NULL,315&out, NULL, NULL);316if (maj_stat != GSS_S_COMPLETE && maj_stat != GSS_S_CONTINUE_NEEDED)317errx(1, "init_sec_context failed: %s",318gssapi_err(maj_stat, min_stat, GSS_C_NO_OID));319320gss_release_buffer(&min_stat, &out);321gss_delete_sec_context(&min_stat, &context, NULL);322}323gss_release_cred(&min_stat, &cred);324}325326327return 0;328}329330331