forked from cconlon/kerberos-java-gssapi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.java
428 lines (357 loc) · 16.3 KB
/
server.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
* Copyright (C) 2012 by the Massachusetts Institute of Technology.
* All rights reserved.
*
* Export of this software from the United States of America may
* require a specific license from the United States Government.
* It is the responsibility of any person or organization contemplating
* export to obtain such a license before exporting.
*
* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
* distribute this software and its documentation for any purpose and
* without fee is hereby granted, provided that the above copyright
* notice appear in all copies and that both that copyright notice and
* this permission notice appear in supporting documentation, and that
* the name of M.I.T. not be used in advertising or publicity pertaining
* to distribution of the software without specific, written prior
* permission. Furthermore if you modify this software you must label
* your software as modified software and not distribute it in such a
* fashion that it might be confused with the original M.I.T. software.
* M.I.T. makes no representations about the suitability of
* this software for any purpose. It is provided "as is" without express
* or implied warranty.
*
* Original source developed by yaSSL (http://www.yassl.com)
*
* Description:
* A simple server application which uses the MIT Kerberos Java GSS-API
* SWIG wrapper. The following actions are taken by the server:
* a) Establish a GSS-API context with a client.
* b) Unwraps a signed and encrypted message that the client sends,
* using gss_unwrap.
* c) Generates and sends a signature block for the received message
* using gss_get_mic.
* d) Repeats steps b) and c) using gss_unseal / gss_sign.
*
*/
import java.io.*;
import java.net.*;
import edu.mit.kerberos.*;
class server implements gsswrapperConstants
{
public static gss_ctx_id_t_desc context = new gss_ctx_id_t_desc();
public static void main(String argv[]) throws Exception
{
int server_port = 11115;
String serviceName = "[email protected]";
int ret = 0;
int authorizationError = 0;
long maj_status = 0;
long[] min_status = {0};
String clientMsg;
gss_ctx_id_t_desc gssContext = GSS_C_NO_CONTEXT;
gss_cred_id_t_desc serverCreds = new gss_cred_id_t_desc();
/* create input and output streams */
ServerSocket serverSocket = null;
Socket clientSocket = null;
OutputStream clientOut = null;
InputStream clientIn = null;
/* create server socket */
try {
serverSocket = new ServerSocket(server_port);
} catch (IOException e) {
System.out.println("Error on port: " + server_port + ", " + e);
System.exit(1);
}
System.out.println("Started Java GSS-API server example, " +
"waiting for client connection...");
while(true)
{
try {
clientSocket = serverSocket.accept();
/* get input and output streams */
clientOut = clientSocket.getOutputStream();
clientIn = clientSocket.getInputStream();
System.out.println("Client connection received");
} catch (IOException e) {
System.out.println("Server did not accept connection: " + e);
}
/* Import service name and acquire creds for it,
* returns NULL on failure */
serverCreds = AcquireServerCreds(serviceName);
if (serverCreds != null) {
System.out.println("Finished acquiring server creds");
/* Stores created context in global static "context" */
ret = Authenticate(clientSocket, clientIn, clientOut);
if (ret == 0) {
System.out.println("Finished Authentication");
/* Using gss_unwrap / gss_get_mic */
ret = Communicate(clientSocket, clientIn, clientOut);
if (ret == 0) {
System.out.println("Finished first communication " +
"with server");
/* Using gss_unseal / gss_sign */
ret = AltCommunicate(clientSocket, clientIn,
clientOut);
if (ret == 0) {
System.out.println("Finished second " +
"communication with server");
} else {
System.out.println("Failed during second " +
"communication with server");
}
} else {
System.out.println("Failed during first " +
"communication with server");
}
} else {
System.out.println("Failed during Authentication");
}
} else {
System.out.println("Failed when acquiring server creds");
}
if (ret == 0) {
System.out.println("SUCCESS!");
gsswrapper.gss_release_cred(min_status, serverCreds);
/* Delete established GSSAPI context */
gss_buffer_desc output_token = GSS_C_NO_BUFFER;
gss_OID_desc tmp = context.getMech_type();
maj_status = gsswrapper.gss_delete_sec_context(min_status,
context, output_token);
if (maj_status != GSS_S_COMPLETE) {
Util.displayError("deleting security context",
min_status, maj_status);
}
} else {
System.out.println("FAILURE!");
continue;
}
clientOut.close();
clientIn.close();
clientSocket.close();
}
}
public static gss_cred_id_t_desc AcquireServerCreds(String serviceName)
{
gss_cred_id_t_desc server_creds = new gss_cred_id_t_desc();
gss_buffer_desc name_buf = new gss_buffer_desc(serviceName);
gss_name_t_desc server_name = new gss_name_t_desc();
gss_OID_set_desc actual_mechs = GSS_C_NO_OID_SET;
long maj_status = 0;
long[] min_status = {0};
long[] time_rec = {0};
maj_status = gsswrapper.gss_import_name(min_status, name_buf,
gsswrapper.getGSS_C_NT_HOSTBASED_SERVICE(), server_name);
if (maj_status != GSS_S_COMPLETE) {
Util.displayError("gss_import_name, serviceName",
min_status, maj_status);
return null;
}
maj_status = gsswrapper.gss_acquire_cred(min_status, server_name,
0, actual_mechs,
GSS_C_ACCEPT,
server_creds, null, time_rec);
if (maj_status != GSS_S_COMPLETE) {
Util.displayError("gss_acquire_cred(server_name)",
min_status, maj_status);
return null;
}
gsswrapper.gss_release_name(min_status, server_name);
return server_creds;
}
public static int Authenticate(Socket inSocket,
InputStream clientIn,
OutputStream clientOut)
{
int err = 0;
long maj_status = 0;
long[] min_status = {0};
gss_ctx_id_t_desc context_tmp = GSS_C_NO_CONTEXT;
byte[] inputTokenBuffer = null;
gss_buffer_desc inputToken = new gss_buffer_desc();
System.out.println("Authenticating...");
/* The main authentication loop. We need to loop reading "input
* tokens" from the client, calling gss_accept_sec_context on the
* "input tokens" and send the resulting "output tokens" back to
* the client until we get GSS_S_COMPLETE or an error */
maj_status = GSS_S_CONTINUE_NEEDED;
while ( (err == 0) &&
(maj_status != GSS_S_COMPLETE)) {
/* Clean up old input buffer */
if (inputTokenBuffer != null)
inputTokenBuffer = null;
inputTokenBuffer = Util.ReadToken(clientIn);
if (inputTokenBuffer != null) {
/* Set up input buffers for the next run through the loop */
gsswrapper.setDescArray(inputToken, inputTokenBuffer);
inputToken.setLength(inputTokenBuffer.length);
System.out.println("inputToken.value = "
+ inputToken.getValue());
System.out.println("inputToken.length = "
+ inputToken.getLength());
} else {
System.out.println("Got bad token from client, " +
"check client for error description.");
return -1;
}
if (inputTokenBuffer != null) {
gss_buffer_desc outputToken = new gss_buffer_desc();
outputToken.setValue(null);
outputToken.setLength(0);
/* gss_accept_sec_context takes the client request and
* generates an appropriate reply. Passing
* GSS_C_NO_CREDENTIAL for the service principal causes
* the server to accept any service princiapl in the
* server's keytab. */
System.out.println("Calling gss_accept_sec_context...");
long[] req_flags = {0};
long[] time_rec = {0};
gss_cred_id_t_desc acceptor_cred_handle = GSS_C_NO_CREDENTIAL;
gss_channel_bindings_struct input_chan_bindings =
GSS_C_NO_CHANNEL_BINDINGS;
maj_status = gsswrapper.gss_accept_sec_context(
min_status, context_tmp,
acceptor_cred_handle,
inputToken,
input_chan_bindings,
null, null, outputToken, req_flags, time_rec, null);
if ( (outputToken.getLength() > 0) &&
(outputToken.getValue() != null) ) {
/* Send the output token to the client */
byte[] temp_token = new byte[(int)outputToken.getLength()];
temp_token = gsswrapper.getDescArray(outputToken);
System.out.println("temp_token.length = "
+ temp_token.length);
err = Util.WriteToken(clientOut, temp_token);
if (err != 0) {
System.out.println("Error sending token to client");
}
System.out.println("outputToken.value = "
+ outputToken.getValue());
System.out.println("outputToken.length = "
+ outputToken.getLength());
/* free the output token */
gsswrapper.gss_release_buffer(min_status, outputToken);
}
}
if ( (maj_status != GSS_S_COMPLETE) &&
(maj_status != GSS_S_CONTINUE_NEEDED))
{
Util.displayError("gss_accept_sec_context",
min_status, maj_status);
return -1;
}
}
if (err == 0) {
context = context_tmp;
} else {
System.out.println("Authenticate failed!");
return -1;
}
return err;
} /* end Authorize() */
public static int Communicate(Socket inSocket,
InputStream clientIn,
OutputStream clientOut)
{
long maj_status = 0;
long[] min_status = {0};
int[] conf_state = {0};
long[] qop_state = {0};
int err = 0;
gss_buffer_desc in_buf = new gss_buffer_desc();
gss_buffer_desc out_buf = new gss_buffer_desc();
gss_buffer_desc mic_buf = new gss_buffer_desc();
byte[] messageBuffer = null;
/* Receive the message token */
messageBuffer = Util.ReadToken(clientIn);
if (messageBuffer != null) {
gsswrapper.setDescArray(in_buf, messageBuffer);
in_buf.setLength(messageBuffer.length);
}
/* Unwrap token */
maj_status = gsswrapper.gss_unwrap(min_status, context,
in_buf, out_buf, conf_state, qop_state);
if (maj_status != GSS_S_COMPLETE) {
Util.displayError("unwrapping token, gss_unwrap",
min_status, maj_status);
return -1;
} else if (conf_state[0] == 0) {
System.out.println("Warning! Message not encrypted.");
}
System.out.println("Received message: " + out_buf.getValue());
/* Produce a signature block for the message */
maj_status = gsswrapper.gss_get_mic(min_status, context,
GSS_C_QOP_DEFAULT,
out_buf, mic_buf);
if (maj_status != GSS_S_COMPLETE) {
Util.displayError("producing signature block, gss_get_mic",
min_status, maj_status);
return -1;
}
/* Send signature block to client */
byte[] temp_token = new byte[(int)mic_buf.getLength()];
temp_token = gsswrapper.getDescArray(mic_buf);
err = Util.WriteToken(clientOut, temp_token);
if (err != 0) {
System.out.println("Error sending signature block to client");
return -1;
}
gsswrapper.gss_release_buffer(min_status, in_buf);
gsswrapper.gss_release_buffer(min_status, out_buf);
gsswrapper.gss_release_buffer(min_status, mic_buf);
return 0;
} /* end Communicate() */
public static int AltCommunicate(Socket inSocket,
InputStream clientIn,
OutputStream clientOut)
{
long maj_status = 0;
long[] min_status = {0};
int[] conf_state = {0};
int[] qop_state = {0};
int err = 0;
gss_buffer_desc in_buf = new gss_buffer_desc();
gss_buffer_desc out_buf = new gss_buffer_desc();
gss_buffer_desc mic_buf = new gss_buffer_desc();
byte[] messageBuffer = null;
/* Receive the message token */
messageBuffer = Util.ReadToken(clientIn);
if (messageBuffer != null) {
gsswrapper.setDescArray(in_buf, messageBuffer);
in_buf.setLength(messageBuffer.length);
}
/* Unwrap token */
maj_status = gsswrapper.gss_unseal(min_status, context,
in_buf, out_buf, conf_state, qop_state);
if (maj_status != GSS_S_COMPLETE) {
Util.displayError("unwrapping token, gss_unseal",
min_status, maj_status);
return -1;
} else if (conf_state[0] == 0) {
System.out.println("Warning! Message not encrypted.");
}
System.out.println("Received message: " + out_buf.getValue());
/* Produce a signature block for the message */
maj_status = gsswrapper.gss_sign(min_status, context,
GSS_C_QOP_DEFAULT,
out_buf, mic_buf);
if (maj_status != GSS_S_COMPLETE) {
Util.displayError("producing signature block, gss_sign",
min_status, maj_status);
return -1;
}
/* Send signature block to client */
byte[] temp_token = new byte[(int)mic_buf.getLength()];
temp_token = gsswrapper.getDescArray(mic_buf);
err = Util.WriteToken(clientOut, temp_token);
if (err != 0) {
System.out.println("Error sending signature block to client");
return -1;
}
gsswrapper.gss_release_buffer(min_status, in_buf);
gsswrapper.gss_release_buffer(min_status, out_buf);
gsswrapper.gss_release_buffer(min_status, mic_buf);
return 0;
} /* end AltCommunicate() */
}