Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CLIENT:
- private void test() throws IOException, GSSException {
- String server = args[0];
- String hostName = args[1];
- int port = Integer.parseInt(args[2]);
- Socket socket = new Socket(hostName, port);
- DataInputStream inStream =
- new DataInputStream(socket.getInputStream());
- DataOutputStream outStream =
- new DataOutputStream(socket.getOutputStream());
- System.out.println("Connected to server "
- + socket.getInetAddress());
- /*
- * This Oid is used to represent the Kerberos version 5 GSS-API
- * mechanism. It is defined in RFC 1964. We will use this Oid
- * whenever we need to indicate to the GSS-API that it must
- * use Kerberos for some purpose.
- */
- Oid krb5Oid = new Oid("1.2.840.113554.1.2.2");
- GSSManager manager = GSSManager.getInstance();
- /*
- * Create a GSSName out of the server's name. The null
- * indicates that this application does not wish to make
- * any claims about the syntax of this name and that the
- * underlying mechanism should try to parse it as per whatever
- * default syntax it chooses.
- */
- GSSName serverName = manager.createName(server, null);
- /*
- * Create a GSSContext for mutual authentication with the
- * server.
- * - serverName is the GSSName that represents the server.
- * - krb5Oid is the Oid that represents the mechanism to
- * use. The client chooses the mechanism to use.
- * - null is passed in for client credentials
- * - DEFAULT_LIFETIME lets the mechanism decide how long the
- * context can remain valid.
- * Note: Passing in null for the credentials asks GSS-API to
- * use the default credentials. This means that the mechanism
- * will look among the credentials stored in the current Subject
- * to find the right kind of credentials that it needs.
- */
- GSSContext context = manager.createContext(serverName,
- krb5Oid,
- null,
- GSSContext.DEFAULT_LIFETIME);
- // Set the desired optional features on the context. The client
- // chooses these options.
- context.requestMutualAuth(true); // Mutual authentication
- context.requestConf(true); // Will use confidentiality later
- context.requestInteg(true); // Will use integrity later
- // Do the context eastablishment loop
- byte[] token = new byte[0];
- while (!context.isEstablished()) {
- // token is ignored on the first call
- token = context.initSecContext(token, 0, token.length);
- // Send a token to the server if one was generated by
- // initSecContext
- if (token != null) {
- System.out.println("Will send token of size "
- + token.length
- + " from initSecContext.");
- outStream.writeInt(token.length);
- outStream.write(token);
- outStream.flush();
- }
- // If the client is done with context establishment
- // then there will be no more tokens to read in this loop
- if (!context.isEstablished()) {
- token = new byte[inStream.readInt()];
- System.out.println("Will read input token of size "
- + token.length
- + " for processing by initSecContext");
- inStream.readFully(token);
- }
- }
- System.out.println("Context Established! ");
- System.out.println("Client is " + context.getSrcName());
- System.out.println("Server is " + context.getTargName());
- /*
- * If mutual authentication did not take place, then only the
- * client was authenticated to the server. Otherwise, both
- * client and server were authenticated to each other.
- */
- if (context.getMutualAuthState())
- System.out.println("Mutual authentication took place!");
- byte[] messageBytes = "Hello There!\0".getBytes();
- /*
- * The first MessageProp argument is 0 to request
- * the default Quality-of-Protection.
- * The second argument is true to request
- * privacy (encryption of the message).
- */
- MessageProp prop = new MessageProp(0, true);
- /*
- * Encrypt the data and send it across. Integrity protection
- * is always applied, irrespective of confidentiality
- * (i.e., encryption).
- * You can use the same token (byte array) as that used when
- * establishing the context.
- */
- token = context.wrap(messageBytes, 0, messageBytes.length, prop);
- System.out.println("Will send wrap token of size " + token.length);
- outStream.writeInt(token.length);
- outStream.write(token);
- outStream.flush();
- /*
- * Now we will allow the server to decrypt the message,
- * calculate a MIC on the decrypted message and send it back
- * to us for verification. This is unnecessary, but done here
- * for illustration.
- */
- token = new byte[inStream.readInt()];
- System.out.println("Will read token of size " + token.length);
- inStream.readFully(token);
- context.verifyMIC(token, 0, token.length,
- messageBytes, 0, messageBytes.length,
- prop);
- System.out.println("Verified received MIC for message.");
- System.out.println("Exiting...");
- context.dispose();
- socket.close();
- }
- };
- SERVER:
- private static void test(int localPort) throws IOException, GSSException {
- ServerSocket ss = new ServerSocket(localPort);
- GSSManager manager = GSSManager.getInstance();
- while (true) {
- System.out.println("Waiting for incoming connection...");
- Socket socket = ss.accept();
- DataInputStream inStream =
- new DataInputStream(socket.getInputStream());
- DataOutputStream outStream =
- new DataOutputStream(socket.getOutputStream());
- System.out.println("Got connection from client "
- + socket.getInetAddress());
- /*
- * Create a GSSContext to receive the incoming request
- * from the client. Use null for the server credentials
- * passed in. This tells the underlying mechanism
- * to use whatever credentials it has available that
- * can be used to accept this connection.
- */
- GSSContext context = manager.createContext((GSSCredential)null);
- // Do the context eastablishment loop
- byte[] token = null;
- while (!context.isEstablished()) {
- token = new byte[inStream.readInt()];
- System.out.println("Will read input token of size "
- + token.length
- + " for processing by acceptSecContext");
- inStream.readFully(token);
- token = context.acceptSecContext(token, 0, token.length);
- // Send a token to the peer if one was generated by
- // acceptSecContext
- if (token != null) {
- System.out.println("Will send token of size "
- + token.length
- + " from acceptSecContext.");
- outStream.writeInt(token.length);
- outStream.write(token);
- outStream.flush();
- }
- }
- System.out.print("Context Established! ");
- System.out.println("Client is " + context.getSrcName());
- System.out.println("Server is " + context.getTargName());
- /*
- * If mutual authentication did not take place, then
- * only the client was authenticated to the
- * server. Otherwise, both client and server were
- * authenticated to each other.
- */
- if (context.getMutualAuthState())
- System.out.println("Mutual authentication took place!");
- /*
- * Create a MessageProp which unwrap will use to return
- * information such as the Quality-of-Protection that was
- * applied to the wrapped token, whether or not it was
- * encrypted, etc. Since the initial MessageProp values
- * are ignored, just set them to the defaults of 0 and false.
- */
- MessageProp prop = new MessageProp(0, false);
- /*
- * Read the token. This uses the same token byte array
- * as that used during context establishment.
- */
- token = new byte[inStream.readInt()];
- System.out.println("Will read token of size "
- + token.length);
- inStream.readFully(token);
- byte[] bytes = context.unwrap(token, 0, token.length, prop);
- String str = new String(bytes);
- System.out.println("Received data \""
- + str + "\" of length " + str.length());
- System.out.println("Confidentiality applied: "
- + prop.getPrivacy());
- /*
- * Now generate a MIC and send it to the client. This is
- * just for illustration purposes. The integrity of the
- * incoming wrapped message is guaranteed irrespective of
- * the confidentiality (encryption) that was used.
- */
- /*
- * First reset the QOP of the MessageProp to 0
- * to ensure the default Quality-of-Protection
- * is applied.
- */
- prop.setQOP(0);
- token = context.getMIC(bytes, 0, bytes.length, prop);
- System.out.println("Will send MIC token of size "
- + token.length);
- outStream.writeInt(token.length);
- outStream.write(token);
- outStream.flush();
- System.out.println("Closing connection with client "
- + socket.getInetAddress());
- context.dispose();
- socket.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment