jdk/test/sun/security/krb5/auto/SSL.java
author vinnie
Wed, 02 Dec 2009 17:06:50 +0000
changeset 4344 ebd0b1f2e4a2
parent 4236 02f52c723b79
child 4346 aee26e5a53b0
permissions -rw-r--r--
6906510: Fix testcase for 6894643: Separate out dependency on Kerberos Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
     1
/*
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
     2
 * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
     4
 *
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
     7
 * published by the Free Software Foundation.
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
     8
 *
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    13
 * accompanied this code).
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    14
 *
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    18
 *
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    21
 * have any questions.
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    22
 */
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    23
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    24
/*
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    25
 * @test
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    26
 * @bug 6894643
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    27
 * @summary Test JSSE Kerberos ciphersuite
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    28
 */
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    29
import java.io.*;
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    30
import java.net.InetAddress;
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    31
import javax.net.ssl.*;
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    32
import java.security.Principal;
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    33
import java.util.Date;
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    34
import sun.security.jgss.GSSUtil;
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    35
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    36
public class SSL {
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    37
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    38
    private static final String KRB5_CIPHER = "TLS_KRB5_WITH_3DES_EDE_CBC_SHA";
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    39
    private static final int PORT = 4569;
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    40
    private static final int LOOP_LIMIT = 1;
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    41
    private static final char[] PASS = "secret".toCharArray();
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    42
    private static int loopCount = 0;
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    43
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    44
    private static String SERVER;
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    45
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    46
    public static void main(String[] args) throws Exception {
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    47
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    48
        KDC kdc = KDC.create(OneKDC.REALM);
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    49
        // Run this after KDC, so our own DNS service can be started
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    50
        try {
4344
ebd0b1f2e4a2 6906510: Fix testcase for 6894643: Separate out dependency on Kerberos
vinnie
parents: 4236
diff changeset
    51
            SERVER = InetAddress.getLocalHost().getHostName().toLowerCase();
4236
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    52
        } catch (java.net.UnknownHostException e) {
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    53
            SERVER = "localhost";
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    54
        }
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    55
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    56
        kdc.addPrincipal(OneKDC.USER, OneKDC.PASS);
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    57
        kdc.addPrincipalRandKey("krbtgt/" + OneKDC.REALM);
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    58
        kdc.addPrincipal("host/" + SERVER, PASS);
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    59
        KDC.saveConfig(OneKDC.KRB5_CONF, kdc);
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    60
        System.setProperty("java.security.krb5.conf", OneKDC.KRB5_CONF);
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    61
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    62
        final Context c = Context.fromUserPass(OneKDC.USER, OneKDC.PASS, false);
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    63
        final Context s = Context.fromUserPass("host/" + SERVER, PASS, true);
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    64
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    65
        c.startAsClient("host/" + SERVER, GSSUtil.GSS_KRB5_MECH_OID);
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    66
        s.startAsServer(GSSUtil.GSS_KRB5_MECH_OID);
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    67
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    68
        new Thread(new Runnable() {
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    69
            public void run() {
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    70
                try {
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    71
                    s.doAs(new JsseServerAction(), null);
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    72
                } catch (Exception e) {
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    73
                    e.printStackTrace();
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    74
                }
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    75
            }
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    76
        }).start();
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    77
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    78
        // Warm the server
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    79
        Thread.sleep(2000);
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    80
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    81
        c.doAs(new JsseClientAction(), null);
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    82
    }
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    83
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    84
    // Following codes copied from
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    85
    // http://java.sun.com/javase/6/docs/technotes/guides/security/jgss/lab/part2.html#JSSE
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    86
    private static class JsseClientAction implements Action {
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    87
        public byte[] run(Context s, byte[] input) throws Exception {
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    88
            SSLSocketFactory sslsf =
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    89
                (SSLSocketFactory) SSLSocketFactory.getDefault();
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    90
            SSLSocket sslSocket = (SSLSocket) sslsf.createSocket(SERVER, PORT);
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    91
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    92
            // Enable only a KRB5 cipher suite.
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    93
            String enabledSuites[] = {KRB5_CIPHER};
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    94
            sslSocket.setEnabledCipherSuites(enabledSuites);
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    95
            // Should check for exception if enabledSuites is not supported
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    96
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    97
            BufferedReader in = new BufferedReader(new InputStreamReader(
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    98
                sslSocket.getInputStream()));
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
    99
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   100
                sslSocket.getOutputStream()));
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   101
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   102
            String outStr = "Hello There!\n";
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   103
            out.write(outStr);
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   104
            out.flush();
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   105
            System.out.print("Sending " + outStr);
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   106
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   107
            String inStr = in.readLine();
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   108
            System.out.println("Received " + inStr);
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   109
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   110
            String cipherSuiteChosen = sslSocket.getSession().getCipherSuite();
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   111
            System.out.println("Cipher suite in use: " + cipherSuiteChosen);
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   112
            Principal self = sslSocket.getSession().getLocalPrincipal();
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   113
            System.out.println("I am: " + self.toString());
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   114
            Principal peer = sslSocket.getSession().getPeerPrincipal();
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   115
            System.out.println("Server is: " + peer.toString());
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   116
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   117
            sslSocket.close();
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   118
            return null;
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   119
        }
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   120
    }
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   121
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   122
    private static class JsseServerAction implements Action {
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   123
        public byte[] run(Context s, byte[] input) throws Exception {
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   124
            SSLServerSocketFactory sslssf =
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   125
                (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   126
            SSLServerSocket sslServerSocket =
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   127
                (SSLServerSocket) sslssf.createServerSocket(PORT);
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   128
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   129
            // Enable only a KRB5 cipher suite.
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   130
            String enabledSuites[] = {KRB5_CIPHER};
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   131
            sslServerSocket.setEnabledCipherSuites(enabledSuites);
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   132
            // Should check for exception if enabledSuites is not supported
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   133
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   134
            while (loopCount++ < LOOP_LIMIT) {
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   135
                System.out.println("Waiting for incoming connection...");
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   136
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   137
                SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   138
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   139
                System.out.println("Got connection from client "
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   140
                    + sslSocket.getInetAddress());
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   141
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   142
                BufferedReader in = new BufferedReader(new InputStreamReader(
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   143
                    sslSocket.getInputStream()));
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   144
                BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   145
                    sslSocket.getOutputStream()));
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   146
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   147
                String inStr = in.readLine();
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   148
                System.out.println("Received " + inStr);
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   149
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   150
                String outStr = inStr + " " + new Date().toString() + "\n";
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   151
                out.write(outStr);
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   152
                System.out.println("Sending " + outStr);
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   153
                out.flush();
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   154
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   155
                String cipherSuiteChosen =
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   156
                    sslSocket.getSession().getCipherSuite();
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   157
                System.out.println("Cipher suite in use: " + cipherSuiteChosen);
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   158
                Principal self = sslSocket.getSession().getLocalPrincipal();
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   159
                System.out.println("I am: " + self.toString());
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   160
                Principal peer = sslSocket.getSession().getPeerPrincipal();
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   161
                System.out.println("Client is: " + peer.toString());
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   162
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   163
                sslSocket.close();
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   164
            }
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   165
            return null;
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   166
        }
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   167
    }
02f52c723b79 6894643: Separate out dependency on Kerberos
vinnie
parents:
diff changeset
   168
}