Path: blob/main/crypto/heimdal/appl/test/gssapi_client.c
34870 views
/*1* Copyright (c) 1997 - 2000 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 the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include "test_locl.h"34#include <gssapi/gssapi.h>35#include <gssapi/gssapi_krb5.h>36#include <gssapi/gssapi_spnego.h>37#include "gss_common.h"38RCSID("$Id$");3940static int41do_trans (int sock, gss_ctx_id_t context_hdl)42{43OM_uint32 maj_stat, min_stat;44gss_buffer_desc real_input_token, real_output_token;45gss_buffer_t input_token = &real_input_token,46output_token = &real_output_token;4748/* get_mic */4950input_token->length = 3;51input_token->value = strdup("hej");5253maj_stat = gss_get_mic(&min_stat,54context_hdl,55GSS_C_QOP_DEFAULT,56input_token,57output_token);58if (GSS_ERROR(maj_stat))59gss_err (1, min_stat, "gss_get_mic");6061write_token (sock, input_token);62write_token (sock, output_token);6364/* wrap */6566input_token->length = 7;67input_token->value = "hemligt";6869maj_stat = gss_wrap (&min_stat,70context_hdl,710,72GSS_C_QOP_DEFAULT,73input_token,74NULL,75output_token);76if (GSS_ERROR(maj_stat))77gss_err (1, min_stat, "gss_wrap");7879write_token (sock, output_token);8081maj_stat = gss_wrap (&min_stat,82context_hdl,831,84GSS_C_QOP_DEFAULT,85input_token,86NULL,87output_token);88if (GSS_ERROR(maj_stat))89gss_err (1, min_stat, "gss_wrap");9091write_token (sock, output_token);9293return 0;94}9596extern char *password;9798static int99proto (int sock, const char *hostname, const char *service)100{101struct sockaddr_storage remote, local;102socklen_t addrlen;103104int context_established = 0;105gss_ctx_id_t context_hdl = GSS_C_NO_CONTEXT;106gss_cred_id_t cred = GSS_C_NO_CREDENTIAL;107gss_buffer_desc real_input_token, real_output_token;108gss_buffer_t input_token = &real_input_token,109output_token = &real_output_token;110OM_uint32 maj_stat, min_stat;111gss_name_t server;112gss_buffer_desc name_token;113u_char init_buf[4];114u_char acct_buf[4];115gss_OID mech_oid;116char *str;117118mech_oid = select_mech(mech);119120name_token.length = asprintf (&str,121"%s@%s", service, hostname);122if (str == NULL)123errx(1, "malloc - out of memory");124name_token.value = str;125126maj_stat = gss_import_name (&min_stat,127&name_token,128GSS_C_NT_HOSTBASED_SERVICE,129&server);130if (GSS_ERROR(maj_stat))131gss_err (1, min_stat,132"Error importing name `%s@%s':\n", service, hostname);133134if (password) {135gss_buffer_desc pw;136137pw.value = password;138pw.length = strlen(password);139140maj_stat = gss_acquire_cred_with_password(&min_stat,141GSS_C_NO_NAME,142&pw,143GSS_C_INDEFINITE,144GSS_C_NO_OID_SET,145GSS_C_INITIATE,146&cred,147NULL,148NULL);149if (GSS_ERROR(maj_stat))150gss_err (1, min_stat,151"Error acquiring default initiator credentials");152}153154addrlen = sizeof(local);155if (getsockname (sock, (struct sockaddr *)&local, &addrlen) < 0156|| addrlen > sizeof(local))157err (1, "getsockname(%s)", hostname);158159addrlen = sizeof(remote);160if (getpeername (sock, (struct sockaddr *)&remote, &addrlen) < 0161|| addrlen > sizeof(remote))162err (1, "getpeername(%s)", hostname);163164input_token->length = 0;165output_token->length = 0;166167#if 0168struct gss_channel_bindings_struct input_chan_bindings;169170input_chan_bindings.initiator_addrtype = GSS_C_AF_INET;171input_chan_bindings.initiator_address.length = 4;172init_buf[0] = (local.sin_addr.s_addr >> 24) & 0xFF;173init_buf[1] = (local.sin_addr.s_addr >> 16) & 0xFF;174init_buf[2] = (local.sin_addr.s_addr >> 8) & 0xFF;175init_buf[3] = (local.sin_addr.s_addr >> 0) & 0xFF;176input_chan_bindings.initiator_address.value = init_buf;177178input_chan_bindings.acceptor_addrtype = GSS_C_AF_INET;179input_chan_bindings.acceptor_address.length = 4;180acct_buf[0] = (remote.sin_addr.s_addr >> 24) & 0xFF;181acct_buf[1] = (remote.sin_addr.s_addr >> 16) & 0xFF;182acct_buf[2] = (remote.sin_addr.s_addr >> 8) & 0xFF;183acct_buf[3] = (remote.sin_addr.s_addr >> 0) & 0xFF;184input_chan_bindings.acceptor_address.value = acct_buf;185186input_chan_bindings.application_data.value = emalloc(4);187* (unsigned short*)input_chan_bindings.application_data.value = local.sin_port;188* ((unsigned short *)input_chan_bindings.application_data.value + 1) = remote.sin_port;189input_chan_bindings.application_data.length = 4;190191input_chan_bindings.application_data.length = 0;192input_chan_bindings.application_data.value = NULL;193#endif194195while(!context_established) {196maj_stat =197gss_init_sec_context(&min_stat,198cred,199&context_hdl,200server,201mech_oid,202GSS_C_MUTUAL_FLAG | GSS_C_SEQUENCE_FLAG,2030,204NULL,205input_token,206NULL,207output_token,208NULL,209NULL);210if (GSS_ERROR(maj_stat))211gss_err (1, min_stat, "gss_init_sec_context");212if (output_token->length != 0)213write_token (sock, output_token);214if (GSS_ERROR(maj_stat)) {215if (context_hdl != GSS_C_NO_CONTEXT)216gss_delete_sec_context (&min_stat,217&context_hdl,218GSS_C_NO_BUFFER);219break;220}221if (maj_stat & GSS_S_CONTINUE_NEEDED) {222read_token (sock, input_token);223} else {224context_established = 1;225}226227}228if (fork_flag) {229pid_t pid;230int pipefd[2];231232if (pipe (pipefd) < 0)233err (1, "pipe");234235pid = fork ();236if (pid < 0)237err (1, "fork");238if (pid != 0) {239gss_buffer_desc buf;240241maj_stat = gss_export_sec_context (&min_stat,242&context_hdl,243&buf);244if (GSS_ERROR(maj_stat))245gss_err (1, min_stat, "gss_export_sec_context");246write_token (pipefd[1], &buf);247exit (0);248} else {249gss_ctx_id_t context_hdl;250gss_buffer_desc buf;251252close (pipefd[1]);253read_token (pipefd[0], &buf);254close (pipefd[0]);255maj_stat = gss_import_sec_context (&min_stat, &buf, &context_hdl);256if (GSS_ERROR(maj_stat))257gss_err (1, min_stat, "gss_import_sec_context");258gss_release_buffer (&min_stat, &buf);259return do_trans (sock, context_hdl);260}261} else {262return do_trans (sock, context_hdl);263}264}265266int267main(int argc, char **argv)268{269krb5_context context; /* XXX */270int port = client_setup(&context, &argc, argv);271return client_doit (argv[argc], port, service, proto);272}273274275