-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGSSTestServer.java
104 lines (87 loc) · 4.17 KB
/
GSSTestServer.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
package cz.cacek.kerberos.jgss;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.ietf.jgss.GSSContext;
import org.ietf.jgss.GSSCredential;
import org.ietf.jgss.GSSException;
import org.ietf.jgss.GSSManager;
import org.ietf.jgss.MessageProp;
/**
* Sample echo server with GSS-API protection.
*/
public class GSSTestServer {
public static void main(String[] args) throws Exception {
System.setProperty("java.security.auth.debug", "gssloginconfig");
// System.setProperty("sun.security.krb5.debug", "true");
// System.setProperty("sun.security.jgss.debug", "true");
System.setProperty("java.security.auth.login.config", "jaas.conf");
System.setProperty("java.security.krb5.conf", "krb5.conf");
System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
ExecutorService executorService = Executors.newFixedThreadPool(3);
try (ServerSocket serverSocket = new ServerSocket(10089)) {
System.out.println("GSSTestServer started");
while (true) {
Socket acceptedSocket = serverSocket.accept();
executorService.execute(new ClientConnectionHandler(acceptedSocket));
}
}
}
private static class ClientConnectionHandler implements Runnable {
private final Socket acceptedSocket;
public ClientConnectionHandler(Socket socket) {
this.acceptedSocket = socket;
}
@Override
public void run() {
GSSContext gssContext = null;
try (Socket socket = acceptedSocket) {
gssContext = GSSManager.getInstance().createContext((GSSCredential) null);
System.out.println("Client connected");
DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
System.out.println("Initializing GSS context");
while (!gssContext.isEstablished()) {
byte[] inToken = new byte[dataInputStream.readInt()];
dataInputStream.readFully(inToken);
// Files.write(Paths.get("/tmp/init.token"), inToken);
byte[] outToken = gssContext.acceptSecContext(inToken, 0, inToken.length);
if (outToken != null) {
dataOutputStream.writeInt(outToken.length);
dataOutputStream.write(outToken);
dataOutputStream.flush();
}
}
String clientName = gssContext.getSrcName().toString();
System.out.println("Context Established with Client " + clientName);
byte[] wrappedMsg = new byte[dataInputStream.readInt()];
dataInputStream.readFully(wrappedMsg);
// initial values in the MessageProp are ignored
MessageProp msgProp = new MessageProp(0, false);
String message = new String(gssContext.unwrap(wrappedMsg, 0, wrappedMsg.length, msgProp), UTF_8);
System.out.println("Message: " + message);
System.out.println("Message privacy used: " + msgProp.getPrivacy());
String replyMsg = message + ", " + message;
byte[] replyMsgBytes = replyMsg.getBytes(UTF_8);
wrappedMsg = gssContext.wrap(replyMsgBytes, 0, replyMsgBytes.length, msgProp);
dataOutputStream.writeInt(wrappedMsg.length);
dataOutputStream.write(wrappedMsg);
dataOutputStream.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (gssContext != null) {
try {
gssContext.dispose();
} catch (GSSException e) {
e.printStackTrace();
}
}
}
}
}
}