gssapi_client.c revision 55682
1213237Sgonzo/*
2213237Sgonzo * Copyright (c) 1997 - 1999 Kungliga Tekniska H�gskolan
3213237Sgonzo * (Royal Institute of Technology, Stockholm, Sweden).
4213237Sgonzo * All rights reserved.
5213237Sgonzo *
6213237Sgonzo * Redistribution and use in source and binary forms, with or without
7213237Sgonzo * modification, are permitted provided that the following conditions
8213237Sgonzo * are met:
9213237Sgonzo *
10213237Sgonzo * 1. Redistributions of source code must retain the above copyright
11213237Sgonzo *    notice, this list of conditions and the following disclaimer.
12213237Sgonzo *
13213237Sgonzo * 2. Redistributions in binary form must reproduce the above copyright
14213277Sgonzo *    notice, this list of conditions and the following disclaimer in the
15213277Sgonzo *    documentation and/or other materials provided with the distribution.
16213277Sgonzo *
17213277Sgonzo * 3. Neither the name of the Institute nor the names of its contributors
18213277Sgonzo *    may be used to endorse or promote products derived from this software
19213277Sgonzo *    without specific prior written permission.
20213277Sgonzo *
21213277Sgonzo * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22213277Sgonzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23213277Sgonzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24213277Sgonzo * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25213237Sgonzo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26213237Sgonzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27213237Sgonzo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28213237Sgonzo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29213237Sgonzo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30213237Sgonzo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31213237Sgonzo * SUCH DAMAGE.
32266135Sloos */
33278786Sloos
34266135Sloos#include "test_locl.h"
35213237Sgonzo#include <gssapi.h>
36213237Sgonzo#include "gss_common.h"
37213237SgonzoRCSID("$Id: gssapi_client.c,v 1.10 1999/12/04 18:15:50 assar Exp $");
38266135Sloos
39213237Sgonzostatic int
40213237Sgonzoproto (int sock, const char *hostname, const char *service)
41213237Sgonzo{
42278784Sloos    struct sockaddr_in remote, local;
43278784Sloos    int addrlen;
44278784Sloos
45278784Sloos    int context_established = 0;
46278784Sloos    gss_ctx_id_t context_hdl = GSS_C_NO_CONTEXT;
47278784Sloos    gss_buffer_t input_token, output_token;
48278784Sloos    gss_buffer_desc real_input_token, real_output_token;
49278785Sloos    OM_uint32 maj_stat, min_stat;
50213237Sgonzo    gss_name_t server;
51213237Sgonzo    gss_buffer_desc name_token;
52213237Sgonzo
53213237Sgonzo    name_token.length = asprintf ((char **)&name_token.value,
54213237Sgonzo				  "%s@%s", service, hostname);
55213237Sgonzo
56213237Sgonzo    maj_stat = gss_import_name (&min_stat,
57213237Sgonzo				&name_token,
58213237Sgonzo				GSS_C_NT_HOSTBASED_SERVICE,
59213237Sgonzo				&server);
60213237Sgonzo    if (GSS_ERROR(maj_stat))
61213237Sgonzo	gss_err (1, min_stat,
62213237Sgonzo		 "Error importing name `%s@%s':\n", service, hostname);
63213237Sgonzo
64213237Sgonzo    addrlen = sizeof(local);
65278783Sloos    if (getsockname (sock, (struct sockaddr *)&local, &addrlen) < 0
66213237Sgonzo	|| addrlen != sizeof(local))
67213237Sgonzo	err (1, "getsockname(%s)", hostname);
68213237Sgonzo
69213237Sgonzo    addrlen = sizeof(remote);
70213237Sgonzo    if (getpeername (sock, (struct sockaddr *)&remote, &addrlen) < 0
71213237Sgonzo	|| addrlen != sizeof(remote))
72213237Sgonzo	err (1, "getpeername(%s)", hostname);
73213237Sgonzo
74278786Sloos    input_token = &real_input_token;
75278786Sloos    output_token = &real_output_token;
76278786Sloos
77278786Sloos    input_token->length = 0;
78278786Sloos    output_token->length = 0;
79278786Sloos
80278786Sloos    while(!context_established) {
81278786Sloos	maj_stat =
82278786Sloos	    gss_init_sec_context(&min_stat,
83278786Sloos				 GSS_C_NO_CREDENTIAL,
84278786Sloos				 &context_hdl,
85278786Sloos				 server,
86278786Sloos				 GSS_C_NO_OID,
87278786Sloos				 GSS_C_MUTUAL_FLAG | GSS_C_SEQUENCE_FLAG,
88278786Sloos				 0,
89278786Sloos				 GSS_C_NO_CHANNEL_BINDINGS,
90278786Sloos				 input_token,
91278785Sloos				 NULL,
92278785Sloos				 output_token,
93213237Sgonzo				 NULL,
94278785Sloos				 NULL);
95278785Sloos	if (GSS_ERROR(maj_stat))
96213237Sgonzo	    gss_err (1, min_stat, "gss_init_sec_context");
97213237Sgonzo	if (output_token->length != 0)
98213237Sgonzo	    write_token (sock, output_token);
99213237Sgonzo	if (GSS_ERROR(maj_stat)) {
100213237Sgonzo	    if (context_hdl != GSS_C_NO_CONTEXT)
101213237Sgonzo		gss_delete_sec_context (&min_stat,
102213237Sgonzo					&context_hdl,
103213237Sgonzo					GSS_C_NO_BUFFER);
104213237Sgonzo	    break;
105278785Sloos	}
106278785Sloos	if (maj_stat & GSS_S_CONTINUE_NEEDED) {
107213237Sgonzo	    read_token (sock, input_token);
108278785Sloos	} else {
109278785Sloos	    context_established = 1;
110213237Sgonzo	}
111278785Sloos
112278785Sloos    }
113278785Sloos
114213237Sgonzo    /* get_mic */
115213237Sgonzo
116213237Sgonzo    input_token->length = 3;
117213237Sgonzo    input_token->value  = strdup("hej");
118213237Sgonzo
119213237Sgonzo    maj_stat = gss_get_mic(&min_stat,
120213237Sgonzo			   context_hdl,
121213237Sgonzo			   GSS_C_QOP_DEFAULT,
122213237Sgonzo			   input_token,
123278785Sloos			   output_token);
124278785Sloos    if (GSS_ERROR(maj_stat))
125213237Sgonzo	gss_err (1, min_stat, "gss_get_mic");
126278785Sloos
127278785Sloos    write_token (sock, input_token);
128213237Sgonzo    write_token (sock, output_token);
129278785Sloos
130278785Sloos    /* wrap */
131278785Sloos
132213237Sgonzo    input_token->length = 7;
133213237Sgonzo    input_token->value  = "hemligt";
134278781Sloos
135278781Sloos
136278781Sloos    maj_stat = gss_wrap (&min_stat,
137278781Sloos			 context_hdl,
138278781Sloos			 1,
139278781Sloos			 GSS_C_QOP_DEFAULT,
140278781Sloos			 input_token,
141278781Sloos			 NULL,
142278784Sloos			 output_token);
143278784Sloos    if (GSS_ERROR(maj_stat))
144278784Sloos	gss_err (1, min_stat, "gss_wrap");
145278784Sloos
146278784Sloos    write_token (sock, output_token);
147278781Sloos
148278781Sloos    return 0;
149278781Sloos}
150278781Sloos
151278781Sloosint
152278781Sloosmain(int argc, char **argv)
153278781Sloos{
154278781Sloos    krb5_context context; /* XXX */
155278781Sloos    int port = client_setup(&context, &argc, argv);
156278781Sloos    return client_doit (argv[argc], port, service, proto);
157278781Sloos}
158278781Sloos