Path: blob/main/crypto/heimdal/lib/gssapi/test_kcred.c
34889 views
/*1* Copyright (c) 2003-2004 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 <krb5.h>46#include <err.h>47#include <getarg.h>4849static int version_flag = 0;50static int help_flag = 0;5152static void53copy_import(void)54{55gss_cred_id_t cred1, cred2;56OM_uint32 maj_stat, min_stat;57gss_name_t name1, name2;58OM_uint32 lifetime1, lifetime2;59gss_cred_usage_t usage1, usage2;60gss_OID_set mechs1, mechs2;61krb5_ccache id;62krb5_error_code ret;63krb5_context context;64int equal;6566maj_stat = gss_acquire_cred(&min_stat, GSS_C_NO_NAME, GSS_C_INDEFINITE,67GSS_C_NO_OID_SET, GSS_C_INITIATE,68&cred1, NULL, NULL);69if (maj_stat != GSS_S_COMPLETE)70errx(1, "gss_acquire_cred");7172maj_stat = gss_inquire_cred(&min_stat, cred1, &name1, &lifetime1,73&usage1, &mechs1);74if (maj_stat != GSS_S_COMPLETE)75errx(1, "gss_inquire_cred");7677ret = krb5_init_context(&context);78if (ret)79errx(1, "krb5_init_context");8081ret = krb5_cc_new_unique(context, krb5_cc_type_memory, NULL, &id);82if (ret)83krb5_err(context, 1, ret, "krb5_cc_new_unique");8485maj_stat = gss_krb5_copy_ccache(&min_stat, cred1, id);86if (maj_stat != GSS_S_COMPLETE)87errx(1, "gss_krb5_copy_ccache");8889maj_stat = gss_krb5_import_cred(&min_stat, id, NULL, NULL, &cred2);90if (maj_stat != GSS_S_COMPLETE)91errx(1, "gss_krb5_import_cred");9293maj_stat = gss_inquire_cred(&min_stat, cred2, &name2, &lifetime2,94&usage2, &mechs2);95if (maj_stat != GSS_S_COMPLETE)96errx(1, "gss_inquire_cred 2");9798maj_stat = gss_compare_name(&min_stat, name1, name2, &equal);99if (maj_stat != GSS_S_COMPLETE)100errx(1, "gss_compare_name");101if (!equal)102errx(1, "names not equal");103104if (lifetime1 != lifetime2)105errx(1, "lifetime not equal %lu != %lu",106(unsigned long)lifetime1, (unsigned long)lifetime2);107108if (usage1 != usage2) {109/* as long any of them is both are everything it ok */110if (usage1 != GSS_C_BOTH && usage2 != GSS_C_BOTH)111errx(1, "usages disjoined");112}113114gss_release_name(&min_stat, &name2);115gss_release_oid_set(&min_stat, &mechs2);116117maj_stat = gss_inquire_cred(&min_stat, cred2, &name2, &lifetime2,118&usage2, &mechs2);119if (maj_stat != GSS_S_COMPLETE)120errx(1, "gss_inquire_cred");121122maj_stat = gss_compare_name(&min_stat, name1, name2, &equal);123if (maj_stat != GSS_S_COMPLETE)124errx(1, "gss_compare_name");125if (!equal)126errx(1, "names not equal");127128if (lifetime1 != lifetime2)129errx(1, "lifetime not equal %lu != %lu",130(unsigned long)lifetime1, (unsigned long)lifetime2);131132gss_release_cred(&min_stat, &cred1);133gss_release_cred(&min_stat, &cred2);134135gss_release_name(&min_stat, &name1);136gss_release_name(&min_stat, &name2);137138#if 0139compare(mechs1, mechs2);140#endif141142gss_release_oid_set(&min_stat, &mechs1);143gss_release_oid_set(&min_stat, &mechs2);144145krb5_cc_destroy(context, id);146krb5_free_context(context);147}148149static struct getargs args[] = {150{"version", 0, arg_flag, &version_flag, "print version", NULL },151{"help", 0, arg_flag, &help_flag, NULL, NULL }152};153154static void155usage (int ret)156{157arg_printusage (args, sizeof(args)/sizeof(*args),158NULL, "");159exit (ret);160}161162int163main(int argc, char **argv)164{165int optidx = 0;166167setprogname(argv[0]);168if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))169usage(1);170171if (help_flag)172usage (0);173174if(version_flag){175print_version(NULL);176exit(0);177}178179argc -= optidx;180argv += optidx;181182copy_import();183184return 0;185}186187188