Path: blob/main/crypto/krb5/src/tests/gssapi/t_namingexts.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"3031static int use_spnego = 0;3233static void34display_name(const char *tag, gss_name_t name)35{36OM_uint32 major, minor;37gss_buffer_desc buf;3839major = gss_display_name(&minor, name, &buf, NULL);40check_gsserr("gss_display_name", major, minor);4142printf("%s:\t%.*s\n", tag, (int)buf.length, (char *)buf.value);4344(void)gss_release_buffer(&minor, &buf);45}4647static void48test_export_import_name(gss_name_t name)49{50OM_uint32 major, minor;51gss_buffer_desc exported_name = GSS_C_EMPTY_BUFFER;52gss_name_t imported_name = GSS_C_NO_NAME;53gss_name_t imported_name_comp = GSS_C_NO_NAME;54unsigned int i;5556major = gss_export_name_composite(&minor, name, &exported_name);57check_gsserr("gss_export_name_composite", major, minor);5859printf("Exported name:\n");60for (i = 0; i < exported_name.length; i++) {61if ((i % 32) == 0)62printf("\n");63printf("%02x", ((char *)exported_name.value)[i] & 0xFF);64}65printf("\n");6667major = gss_import_name(&minor, &exported_name, GSS_C_NT_EXPORT_NAME,68&imported_name);69check_gsserr("gss_import_name", major, minor);7071major = gss_import_name(&minor, &exported_name, GSS_C_NT_COMPOSITE_EXPORT,72&imported_name_comp);73check_gsserr("gss_import_name", major, minor);74(void)gss_release_buffer(&minor, &exported_name);7576printf("\n");77display_canon_name("Re-imported name", imported_name, &mech_krb5);78printf("Re-imported attributes:\n\n");79enumerate_attributes(imported_name, 0);8081display_name("Re-imported (as composite) name", imported_name_comp);82printf("Re-imported (as composite) attributes:\n\n");83enumerate_attributes(imported_name_comp, 0);8485(void)gss_release_name(&minor, &imported_name);86(void)gss_release_name(&minor, &imported_name_comp);87}8889static void90test_greet_authz_data(gss_name_t name)91{92OM_uint32 major, minor;93gss_buffer_desc attr;94gss_buffer_desc value;9596attr.value = "urn:greet:greeting";97attr.length = strlen((char *)attr.value);9899major = gss_delete_name_attribute(&minor, name, &attr);100if (major == GSS_S_UNAVAILABLE) {101fprintf(stderr, "Warning: greet_client plugin not installed\n");102exit(1);103}104check_gsserr("gss_delete_name_attribute", major, minor);105106value.value = "Hello, acceptor world!";107value.length = strlen((char *)value.value);108major = gss_set_name_attribute(&minor, name, 1, &attr, &value);109if (major == GSS_S_UNAVAILABLE)110return;111check_gsserr("gss_set_name_attribute", major, minor);112}113114static void115test_map_name_to_any(gss_name_t name)116{117OM_uint32 major, minor;118gss_buffer_desc type_id;119krb5_pac pac;120krb5_context context = NULL;121krb5_error_code ret;122size_t len, i;123krb5_ui_4 *types;124125type_id.value = "mspac";126type_id.length = strlen((char *)type_id.value);127128major = gss_map_name_to_any(&minor, name, 1, &type_id, (gss_any_t *)&pac);129if (major == GSS_S_UNAVAILABLE)130return;131check_gsserr("gss_map_name_to_any", major, minor);132133ret = krb5_init_context(&context);134check_k5err(context, "krb5_init_context", ret);135136if (krb5_pac_get_types(context, pac, &len, &types) == 0) {137printf("PAC buffer types:");138for (i = 0; i < len; i++)139printf(" %d", types[i]);140printf("\n");141free(types);142}143144(void)gss_release_any_name_mapping(&minor, name, &type_id,145(gss_any_t *)&pac);146}147148static void149init_accept_sec_context(gss_cred_id_t verifier_cred_handle)150{151OM_uint32 major, minor, flags;152gss_name_t source_name = GSS_C_NO_NAME, target_name = GSS_C_NO_NAME;153gss_ctx_id_t initiator_context, acceptor_context;154gss_OID mech = use_spnego ? &mech_spnego : &mech_krb5;155156major = gss_inquire_cred(&minor, verifier_cred_handle, &target_name, NULL,157NULL, NULL);158check_gsserr("gss_inquire_cred", major, minor);159160display_canon_name("Target name", target_name, &mech_krb5);161162flags = GSS_C_REPLAY_FLAG | GSS_C_SEQUENCE_FLAG;163establish_contexts(mech, verifier_cred_handle, verifier_cred_handle,164target_name, flags, &initiator_context,165&acceptor_context, &source_name, NULL, NULL);166167display_canon_name("Source name", source_name, &mech_krb5);168enumerate_attributes(source_name, 1);169test_export_import_name(source_name);170test_map_name_to_any(source_name);171172(void)gss_release_name(&minor, &source_name);173(void)gss_release_name(&minor, &target_name);174(void)gss_delete_sec_context(&minor, &initiator_context, NULL);175(void)gss_delete_sec_context(&minor, &acceptor_context, NULL);176}177178int179main(int argc, char *argv[])180{181OM_uint32 minor, major;182gss_cred_id_t cred_handle = GSS_C_NO_CREDENTIAL;183gss_OID_set mechs, actual_mechs = GSS_C_NO_OID_SET;184gss_name_t tmp_name, name;185186if (argc > 1 && strcmp(argv[1], "--spnego") == 0) {187use_spnego++;188argc--;189argv++;190}191192if (argc < 2) {193fprintf(stderr, "Usage: %s [--spnego] principal [keytab]\n", argv[0]);194exit(1);195}196197tmp_name = import_name(argv[1]);198major = gss_canonicalize_name(&minor, tmp_name, &mech_krb5, &name);199check_gsserr("gss_canonicalze_name", major, minor);200(void)gss_release_name(&minor, &tmp_name);201202test_greet_authz_data(name);203204if (argc >= 3) {205major = krb5_gss_register_acceptor_identity(argv[2]);206check_gsserr("krb5_gss_register_acceptor_identity", major, minor);207}208209mechs = use_spnego ? &mechset_spnego : &mechset_krb5;210211/* get default cred */212major = gss_acquire_cred(&minor, name, GSS_C_INDEFINITE, mechs, GSS_C_BOTH,213&cred_handle, &actual_mechs, NULL);214check_gsserr("gss_acquire_cred", major, minor);215216(void)gss_release_oid_set(&minor, &actual_mechs);217218init_accept_sec_context(cred_handle);219220printf("\n");221222(void)gss_release_cred(&minor, &cred_handle);223(void)gss_release_oid_set(&minor, &actual_mechs);224(void)gss_release_name(&minor, &name);225return 0;226}227228229