Path: blob/main/crypto/heimdal/appl/test/nt_gss_client.c
34870 views
/*1* Copyright (c) 1997 - 1999 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.h>35#include "nt_gss_common.h"3637RCSID("$Id$");3839/*40* This program tries to act as a client for the sample in `Sample41* SSPI Code' in Windows 2000 RC1 SDK.42*/4344static int45proto (int sock, const char *hostname, const char *service)46{47struct sockaddr_in remote, local;48socklen_t addrlen;4950int context_established = 0;51gss_ctx_id_t context_hdl = GSS_C_NO_CONTEXT;52gss_buffer_t input_token, output_token;53gss_buffer_desc real_input_token, real_output_token;54OM_uint32 maj_stat, min_stat;55gss_name_t server;56gss_buffer_desc name_token;57char *str;5859name_token.length = asprintf (&str,60"%s@%s", service, hostname);61if (str == NULL)62errx(1, "out of memory");63name_token.value = str;6465maj_stat = gss_import_name (&min_stat,66&name_token,67GSS_C_NT_HOSTBASED_SERVICE,68&server);69if (GSS_ERROR(maj_stat))70gss_err (1, min_stat,71"Error importing name `%s@%s':\n", service, hostname);7273addrlen = sizeof(local);74if (getsockname (sock, (struct sockaddr *)&local, &addrlen) < 075|| addrlen != sizeof(local))76err (1, "getsockname(%s)", hostname);7778addrlen = sizeof(remote);79if (getpeername (sock, (struct sockaddr *)&remote, &addrlen) < 080|| addrlen != sizeof(remote))81err (1, "getpeername(%s)", hostname);8283input_token = &real_input_token;84output_token = &real_output_token;8586input_token->length = 0;87output_token->length = 0;8889while(!context_established) {90maj_stat =91gss_init_sec_context(&min_stat,92GSS_C_NO_CREDENTIAL,93&context_hdl,94server,95GSS_C_NO_OID,96GSS_C_MUTUAL_FLAG | GSS_C_SEQUENCE_FLAG,970,98GSS_C_NO_CHANNEL_BINDINGS,99input_token,100NULL,101output_token,102NULL,103NULL);104if (GSS_ERROR(maj_stat))105gss_err (1, min_stat, "gss_init_sec_context");106if (output_token->length != 0)107nt_write_token (sock, output_token);108if (GSS_ERROR(maj_stat)) {109if (context_hdl != GSS_C_NO_CONTEXT)110gss_delete_sec_context (&min_stat,111&context_hdl,112GSS_C_NO_BUFFER);113break;114}115if (maj_stat & GSS_S_CONTINUE_NEEDED) {116nt_read_token (sock, input_token);117} else {118context_established = 1;119}120121}122123/* get_mic */124125input_token->length = 3;126input_token->value = strdup("hej");127128maj_stat = gss_get_mic(&min_stat,129context_hdl,130GSS_C_QOP_DEFAULT,131input_token,132output_token);133if (GSS_ERROR(maj_stat))134gss_err (1, min_stat, "gss_get_mic");135136nt_write_token (sock, input_token);137nt_write_token (sock, output_token);138139/* wrap */140141input_token->length = 7;142input_token->value = "hemligt";143144145maj_stat = gss_wrap (&min_stat,146context_hdl,1471,148GSS_C_QOP_DEFAULT,149input_token,150NULL,151output_token);152if (GSS_ERROR(maj_stat))153gss_err (1, min_stat, "gss_wrap");154155nt_write_token (sock, output_token);156157return 0;158}159160int161main(int argc, char **argv)162{163krb5_context context; /* XXX */164int port = client_setup(&context, &argc, argv);165return client_doit (argv[argc], port, service, proto);166}167168169