xref: /freebsd/crypto/heimdal/appl/test/gssapi_client.c (revision 884a2a699669ec61e2366e3e358342dbc94be24a)
1 /*
2  * Copyright (c) 1997 - 2000 Kungliga Tekniska H�gskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include "test_locl.h"
35 #include <gssapi.h>
36 #include "gss_common.h"
37 RCSID("$Id: gssapi_client.c 21521 2007-07-12 13:13:40Z lha $");
38 
39 static int
40 do_trans (int sock, gss_ctx_id_t context_hdl)
41 {
42     OM_uint32 maj_stat, min_stat;
43     gss_buffer_desc real_input_token, real_output_token;
44     gss_buffer_t input_token = &real_input_token,
45 	output_token = &real_output_token;
46 
47     /* get_mic */
48 
49     input_token->length = 3;
50     input_token->value  = strdup("hej");
51 
52     maj_stat = gss_get_mic(&min_stat,
53 			   context_hdl,
54 			   GSS_C_QOP_DEFAULT,
55 			   input_token,
56 			   output_token);
57     if (GSS_ERROR(maj_stat))
58 	gss_err (1, min_stat, "gss_get_mic");
59 
60     write_token (sock, input_token);
61     write_token (sock, output_token);
62 
63     /* wrap */
64 
65     input_token->length = 7;
66     input_token->value  = "hemligt";
67 
68     maj_stat = gss_wrap (&min_stat,
69 			 context_hdl,
70 			 0,
71 			 GSS_C_QOP_DEFAULT,
72 			 input_token,
73 			 NULL,
74 			 output_token);
75     if (GSS_ERROR(maj_stat))
76 	gss_err (1, min_stat, "gss_wrap");
77 
78     write_token (sock, output_token);
79 
80     maj_stat = gss_wrap (&min_stat,
81 			 context_hdl,
82 			 1,
83 			 GSS_C_QOP_DEFAULT,
84 			 input_token,
85 			 NULL,
86 			 output_token);
87     if (GSS_ERROR(maj_stat))
88 	gss_err (1, min_stat, "gss_wrap");
89 
90     write_token (sock, output_token);
91 
92     return 0;
93 }
94 
95 static int
96 proto (int sock, const char *hostname, const char *service)
97 {
98     struct sockaddr_in remote, local;
99     socklen_t addrlen;
100 
101     int context_established = 0;
102     gss_ctx_id_t context_hdl = GSS_C_NO_CONTEXT;
103     gss_buffer_desc real_input_token, real_output_token;
104     gss_buffer_t input_token = &real_input_token,
105 	output_token = &real_output_token;
106     OM_uint32 maj_stat, min_stat;
107     gss_name_t server;
108     gss_buffer_desc name_token;
109     struct gss_channel_bindings_struct input_chan_bindings;
110     u_char init_buf[4];
111     u_char acct_buf[4];
112     gss_OID mech_oid;
113     char *str;
114 
115     mech_oid = select_mech(mech);
116 
117     name_token.length = asprintf (&str,
118 				  "%s@%s", service, hostname);
119     if (str == NULL)
120 	errx(1, "malloc - out of memory");
121     name_token.value = str;
122 
123     maj_stat = gss_import_name (&min_stat,
124 				&name_token,
125 				GSS_C_NT_HOSTBASED_SERVICE,
126 				&server);
127     if (GSS_ERROR(maj_stat))
128 	gss_err (1, min_stat,
129 		 "Error importing name `%s@%s':\n", service, hostname);
130 
131     addrlen = sizeof(local);
132     if (getsockname (sock, (struct sockaddr *)&local, &addrlen) < 0
133 	|| addrlen != sizeof(local))
134 	err (1, "getsockname(%s)", hostname);
135 
136     addrlen = sizeof(remote);
137     if (getpeername (sock, (struct sockaddr *)&remote, &addrlen) < 0
138 	|| addrlen != sizeof(remote))
139 	err (1, "getpeername(%s)", hostname);
140 
141     input_token->length = 0;
142     output_token->length = 0;
143 
144     input_chan_bindings.initiator_addrtype = GSS_C_AF_INET;
145     input_chan_bindings.initiator_address.length = 4;
146     init_buf[0] = (local.sin_addr.s_addr >> 24) & 0xFF;
147     init_buf[1] = (local.sin_addr.s_addr >> 16) & 0xFF;
148     init_buf[2] = (local.sin_addr.s_addr >>  8) & 0xFF;
149     init_buf[3] = (local.sin_addr.s_addr >>  0) & 0xFF;
150     input_chan_bindings.initiator_address.value = init_buf;
151 
152     input_chan_bindings.acceptor_addrtype = GSS_C_AF_INET;
153     input_chan_bindings.acceptor_address.length = 4;
154     acct_buf[0] = (remote.sin_addr.s_addr >> 24) & 0xFF;
155     acct_buf[1] = (remote.sin_addr.s_addr >> 16) & 0xFF;
156     acct_buf[2] = (remote.sin_addr.s_addr >>  8) & 0xFF;
157     acct_buf[3] = (remote.sin_addr.s_addr >>  0) & 0xFF;
158     input_chan_bindings.acceptor_address.value = acct_buf;
159 
160 #if 0
161     input_chan_bindings.application_data.value = emalloc(4);
162     * (unsigned short*)input_chan_bindings.application_data.value = local.sin_port;
163     * ((unsigned short *)input_chan_bindings.application_data.value + 1) = remote.sin_port;
164     input_chan_bindings.application_data.length = 4;
165 #else
166     input_chan_bindings.application_data.length = 0;
167     input_chan_bindings.application_data.value = NULL;
168 #endif
169 
170     while(!context_established) {
171 	maj_stat =
172 	    gss_init_sec_context(&min_stat,
173 				 GSS_C_NO_CREDENTIAL,
174 				 &context_hdl,
175 				 server,
176 				 mech_oid,
177 				 GSS_C_MUTUAL_FLAG | GSS_C_SEQUENCE_FLAG
178 				 | GSS_C_DELEG_FLAG,
179 				 0,
180 				 &input_chan_bindings,
181 				 input_token,
182 				 NULL,
183 				 output_token,
184 				 NULL,
185 				 NULL);
186 	if (GSS_ERROR(maj_stat))
187 	    gss_err (1, min_stat, "gss_init_sec_context");
188 	if (output_token->length != 0)
189 	    write_token (sock, output_token);
190 	if (GSS_ERROR(maj_stat)) {
191 	    if (context_hdl != GSS_C_NO_CONTEXT)
192 		gss_delete_sec_context (&min_stat,
193 					&context_hdl,
194 					GSS_C_NO_BUFFER);
195 	    break;
196 	}
197 	if (maj_stat & GSS_S_CONTINUE_NEEDED) {
198 	    read_token (sock, input_token);
199 	} else {
200 	    context_established = 1;
201 	}
202 
203     }
204     if (fork_flag) {
205 	pid_t pid;
206 	int pipefd[2];
207 
208 	if (pipe (pipefd) < 0)
209 	    err (1, "pipe");
210 
211 	pid = fork ();
212 	if (pid < 0)
213 	    err (1, "fork");
214 	if (pid != 0) {
215 	    gss_buffer_desc buf;
216 
217 	    maj_stat = gss_export_sec_context (&min_stat,
218 					       &context_hdl,
219 					       &buf);
220 	    if (GSS_ERROR(maj_stat))
221 		gss_err (1, min_stat, "gss_export_sec_context");
222 	    write_token (pipefd[1], &buf);
223 	    exit (0);
224 	} else {
225 	    gss_ctx_id_t context_hdl;
226 	    gss_buffer_desc buf;
227 
228 	    close (pipefd[1]);
229 	    read_token (pipefd[0], &buf);
230 	    close (pipefd[0]);
231 	    maj_stat = gss_import_sec_context (&min_stat, &buf, &context_hdl);
232 	    if (GSS_ERROR(maj_stat))
233 		gss_err (1, min_stat, "gss_import_sec_context");
234 	    gss_release_buffer (&min_stat, &buf);
235 	    return do_trans (sock, context_hdl);
236 	}
237     } else {
238 	return do_trans (sock, context_hdl);
239     }
240 }
241 
242 int
243 main(int argc, char **argv)
244 {
245     krb5_context context; /* XXX */
246     int port = client_setup(&context, &argc, argv);
247     return client_doit (argv[argc], port, service, proto);
248 }
249