jgssapi_server.java [plain text]
import org.ietf.jgss.*;
import java.io.*;
import java.net.Socket;
import java.net.ServerSocket;
public class jgssapi_server {
static byte [] getMessage(DataInputStream inStream)
throws IOException
{
byte[] token;
token = new byte[inStream.readInt()];
inStream.readFully(token);
return token;
}
static void putMessage(DataOutputStream outStream, byte [] token)
throws IOException
{
outStream.writeInt(token.length);
outStream.write(token);
}
public static void main(String[] args)
throws IOException, GSSException {
GSSManager manager = GSSManager.getInstance();
GSSContext context = manager.createContext((GSSCredential)null);
byte[] token = null;
int port = 4717;
System.out.println("listen on port " + port);
Socket s = new ServerSocket(port).accept();
DataInputStream inStream = new DataInputStream(s.getInputStream());
DataOutputStream outStream = new DataOutputStream(s.getOutputStream());
System.out.println("negotiate context");
while (!context.isEstablished()) {
token = getMessage(inStream);
token = context.acceptSecContext(token, 0, token.length);
if (token != null)
putMessage(outStream, token);
}
System.out.println("done");
System.out.println("mic test");
System.out.println(" verify mic");
byte[] intoken = getMessage(inStream);
byte[] outtoken = getMessage(inStream);
byte[] bytes = null;
context.verifyMIC(outtoken, 0, outtoken.length,
intoken, 0, intoken.length, new MessageProp(0, false));
System.out.println(" create mic");
bytes = new byte[] { 0x66, 0x6f, 0x6f };
outtoken = context.getMIC(bytes, 0, bytes.length, new MessageProp(0, false));
putMessage(outStream, bytes);
putMessage(outStream, outtoken);
System.out.println("warp int");
outtoken = getMessage(inStream);
bytes = context.unwrap(outtoken, 0, outtoken.length, new MessageProp(0, false));
if (bytes == null)
System.err.println("wrap int failed");
System.out.println("warp conf");
outtoken = getMessage(inStream);
bytes = context.unwrap(outtoken, 0, outtoken.length, new MessageProp(0, true));
if (bytes == null)
System.err.println("wrap conf failed");
System.out.println("warp conf");
intoken = new byte[] { 0x66, 0x6f, 0x6f };
outtoken = context.wrap(intoken, 0, intoken.length, new MessageProp(0, true));
putMessage(outStream, outtoken);
outtoken = getMessage(inStream);
context.dispose();
System.exit(0);
}
}