Path: blob/main/crypto/krb5/src/tests/gssapi/t_export_cred.c
34889 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/*2* Copyright 2011 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>2728#include "common.h"2930/* Display a usage error message and exit. */31static void32usage(void)33{34fprintf(stderr, "Usage: t_export_cred [-k|-s] [-i initiatorname] "35"[-a acceptorname] targetname\n");36exit(1);37}3839int40main(int argc, char *argv[])41{42OM_uint32 major, minor, flags;43gss_name_t initiator_name = GSS_C_NO_NAME, acceptor_name = GSS_C_NO_NAME;44gss_name_t target_name;45gss_cred_id_t initiator_cred, acceptor_cred, delegated_cred;46gss_ctx_id_t initiator_context = GSS_C_NO_CONTEXT;47gss_ctx_id_t acceptor_context = GSS_C_NO_CONTEXT;48gss_OID mech = GSS_C_NO_OID;49gss_OID_set mechs = GSS_C_NO_OID_SET;50char optchar;5152/* Parse arguments. */53argv++;54while (*argv != NULL && **argv == '-') {55optchar = (*argv)[1];56argv++;57if (optchar == 'i') {58if (*argv == NULL)59usage();60initiator_name = import_name(*argv++);61} else if (optchar == 'a') {62if (*argv == NULL)63usage();64acceptor_name = import_name(*argv++);65} else if (optchar == 'k') {66mech = &mech_krb5;67mechs = &mechset_krb5;68} else if (optchar == 's') {69mech = &mech_spnego;70mechs = &mechset_spnego;71} else {72usage();73}74}75if (*argv == NULL || *(argv + 1) != NULL)76usage();77target_name = import_name(argv[0]);7879/* Get initiator cred and export/import it. */80major = gss_acquire_cred(&minor, initiator_name, GSS_C_INDEFINITE, mechs,81GSS_C_INITIATE, &initiator_cred, NULL, NULL);82check_gsserr("gss_acquire_cred(initiator)", major, minor);83export_import_cred(&initiator_cred);8485/* Get acceptor cred and export/import it. */86major = gss_acquire_cred(&minor, acceptor_name, GSS_C_INDEFINITE, mechs,87GSS_C_ACCEPT, &acceptor_cred, NULL, NULL);88check_gsserr("gss_acquire_cred(acceptor)", major, minor);89export_import_cred(&acceptor_cred);9091/* Initiate and accept a security context (one-token exchange only),92* delegating credentials. */93flags = GSS_C_REPLAY_FLAG | GSS_C_SEQUENCE_FLAG | GSS_C_CONF_FLAG |94GSS_C_INTEG_FLAG | GSS_C_DELEG_FLAG;95establish_contexts(mech, initiator_cred, acceptor_cred, target_name, flags,96&initiator_context, &acceptor_context, NULL, NULL,97&delegated_cred);9899/* Import, release, export, and store delegated creds */100export_import_cred(&delegated_cred);101major = gss_store_cred(&minor, delegated_cred, GSS_C_INITIATE,102GSS_C_NULL_OID, 1, 1, NULL, NULL);103check_gsserr("gss_store_cred", major, minor);104105(void)gss_release_name(&minor, &initiator_name);106(void)gss_release_name(&minor, &acceptor_name);107(void)gss_release_name(&minor, &target_name);108(void)gss_release_cred(&minor, &initiator_cred);109(void)gss_release_cred(&minor, &acceptor_cred);110(void)gss_release_cred(&minor, &delegated_cred);111(void)gss_delete_sec_context(&minor, &initiator_context, NULL);112(void)gss_delete_sec_context(&minor, &acceptor_context, NULL);113return 0;114}115116117