test/jdk/sun/security/krb5/auto/SSLwithPerms.java
author ssahoo
Thu, 25 Jan 2018 05:57:22 -0800
changeset 48668 2da4a52715d8
parent 47227 8052fa06e1b7
permissions -rw-r--r--
8194486: Several krb5 tests failed in Mac. Summary: Several tests failed due to improper host service Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30905
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
     1
/*
48668
2da4a52715d8 8194486: Several krb5 tests failed in Mac.
ssahoo
parents: 47227
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
30905
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
     4
 *
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
     8
 *
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    13
 * accompanied this code).
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    14
 *
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    18
 *
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    21
 * questions.
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    22
 */
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    23
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    24
/*
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    25
 * @test
48668
2da4a52715d8 8194486: Several krb5 tests failed in Mac.
ssahoo
parents: 47227
diff changeset
    26
 * @bug 8038089 8194486
30905
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    27
 * @summary TLS optional support for Kerberos cipher suites needs to be re-examined
48668
2da4a52715d8 8194486: Several krb5 tests failed in Mac.
ssahoo
parents: 47227
diff changeset
    28
 * @library ../../../../java/security/testlibrary/ /test/lib
2da4a52715d8 8194486: Several krb5 tests failed in Mac.
ssahoo
parents: 47227
diff changeset
    29
 * @run main jdk.test.lib.FileInstaller TestHosts TestHosts
2da4a52715d8 8194486: Several krb5 tests failed in Mac.
ssahoo
parents: 47227
diff changeset
    30
 * @run main/othervm -Djdk.net.hosts.file=TestHosts SSLwithPerms
30905
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    31
 */
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    32
import java.io.*;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    33
import javax.net.ssl.*;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    34
import javax.security.auth.AuthPermission;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    35
import javax.security.auth.kerberos.ServicePermission;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    36
import java.net.SocketPermission;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    37
import java.nio.ByteBuffer;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    38
import java.nio.file.Files;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    39
import java.nio.file.Paths;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    40
import java.security.Principal;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    41
import java.security.Security;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    42
import java.security.SecurityPermission;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    43
import java.util.Collections;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    44
import java.util.Date;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    45
import java.util.List;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    46
import java.util.ArrayList;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    47
import java.util.Locale;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    48
import java.util.PropertyPermission;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    49
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    50
import sun.security.jgss.GSSUtil;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    51
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    52
public class SSLwithPerms {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    53
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    54
    static String KRB5_CONF = "krb5.conf";
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    55
    static String JAAS_CONF = "jaas.conf";
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    56
    static String REALM = "REALM";
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    57
    static String KTAB = "ktab";
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    58
    static String HOST = "host." + REALM.toLowerCase(Locale.US);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    59
    static String SERVER = "host/" + HOST;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    60
    static String USER = "user";
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    61
    static char[] PASS = "password".toCharArray();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    62
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    63
    public static void main(String[] args) throws Exception {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    64
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    65
        Security.setProperty("jdk.tls.disabledAlgorithms", "");
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    66
        if (args.length == 0) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    67
            KDC kdc = KDC.create(REALM, HOST, 0, true);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    68
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    69
            kdc.addPrincipal(USER, PASS);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    70
            kdc.addPrincipalRandKey("krbtgt/" + REALM);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    71
            kdc.addPrincipalRandKey(SERVER);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    72
            KDC.saveConfig(KRB5_CONF, kdc);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    73
            kdc.writeKtab(KTAB);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    74
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    75
            File f = new File(JAAS_CONF);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    76
            FileOutputStream fos = new FileOutputStream(f);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    77
            fos.write((
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    78
                    "ssl {\n" +
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    79
                            "    com.sun.security.auth.module.Krb5LoginModule required\n" +
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    80
                            "    principal=\"" + SERVER + "\"\n" +
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    81
                            "    useKeyTab=true\n" +
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    82
                            "    keyTab=" + KTAB + "\n" +
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    83
                            "    isInitiator=false\n" +
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    84
                            "    storeKey=true;\n};\n"
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    85
            ).getBytes());
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    86
            fos.close();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    87
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 30905
diff changeset
    88
            String hostsFileName = System.getProperty("test.src", ".") + "/TestHosts";
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 30905
diff changeset
    89
30905
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    90
            Proc pc = Proc.create("SSLwithPerms")
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    91
                    .args("client")
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    92
                    .inheritIO()
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    93
                    .prop("java.security.manager", "")
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    94
                    .prop("java.security.krb5.conf", KRB5_CONF)
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 30905
diff changeset
    95
                    .prop("jdk.net.hosts.file", hostsFileName)
30905
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    96
                    .prop("javax.net.ssl", "handshake")
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    97
                    .prop("sun.security.krb5.debug", "true")
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    98
                    .perm(new SecurityPermission("setProperty.jdk.tls.disabledAlgorithms"))
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47216
diff changeset
    99
                    .perm(new java.util.PropertyPermission("user.name", "read"))
30905
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   100
                    .perm(new PropertyPermission("sun.security.krb5.principal", "read"))
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   101
                    .perm(new FilePermission("port", "read"))
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 30905
diff changeset
   102
                    .perm(new FilePermission(hostsFileName, "read"))
30905
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   103
                    .perm(new FilePermission(KTAB, "read"))
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   104
                    .perm(new AuthPermission("modifyPrincipals"))
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   105
                    .perm(new AuthPermission("modifyPrivateCredentials"))
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   106
                    .perm(new AuthPermission("doAs"))
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   107
                    .perm(new SocketPermission("127.0.0.1", "connect"))
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   108
                    .perm(new ServicePermission("host/host.realm@REALM", "initiate"))
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   109
                    .start();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   110
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   111
            Proc ps = Proc.create("SSLwithPerms")
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   112
                    .args("server")
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   113
                    .inheritIO()
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   114
                    .prop("java.security.manager", "")
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   115
                    .prop("java.security.krb5.conf", KRB5_CONF)
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   116
                    .prop("java.security.auth.login.config", JAAS_CONF)
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 30905
diff changeset
   117
                    .prop("jdk.net.hosts.file", hostsFileName)
30905
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   118
                    .prop("javax.net.ssl", "handshake")
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   119
                    .prop("sun.security.krb5.debug", "true")
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   120
                    .perm(new SecurityPermission("setProperty.jdk.tls.disabledAlgorithms"))
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   121
                    .perm(new AuthPermission("createLoginContext.ssl"))
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   122
                    .perm(new AuthPermission("doAs"))
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 30905
diff changeset
   123
                    .perm(new FilePermission(hostsFileName, "read"))
30905
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   124
                    .perm(new FilePermission("port", "write"))
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   125
                    .perm(new SocketPermission("127.0.0.1", "accept"))
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   126
                    .perm(new ServicePermission("host/host.realm@REALM", "accept"))
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   127
                    .start();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   128
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   129
            if (pc.waitFor() != 0) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   130
                throw new Exception();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   131
            }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   132
            if (ps.waitFor() != 0) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   133
                throw new Exception();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   134
            }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   135
        } else if (args[0].equals("client")) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   136
            Context c;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   137
            c = Context.fromUserPass(USER, PASS, false);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   138
            c.doAs(new JsseClientAction(), null);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   139
        } else if (args[0].equals("server")) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   140
            final Context s = Context.fromJAAS("ssl");
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   141
            s.doAs(new JsseServerAction(), null);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   142
        }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   143
    }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   144
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   145
    private static class JsseClientAction implements Action {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   146
        public byte[] run(Context s, byte[] input) throws Exception {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   147
            SSLSocketFactory sslsf =
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   148
                (SSLSocketFactory) SSLSocketFactory.getDefault();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   149
            while (!Files.exists(Paths.get("port"))) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   150
                Thread.sleep(100);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   151
            }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   152
            int port = ByteBuffer.allocate(4)
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   153
                    .put(Files.readAllBytes(Paths.get("port"))).getInt(0);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   154
            System.out.println("Connecting " + SERVER + ":" + port);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   155
            SSLSocket sslSocket = (SSLSocket) sslsf.createSocket(HOST, port);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   156
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   157
            // Enable only a KRB5 cipher suite.
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   158
            String enabledSuites[] = {"TLS_KRB5_WITH_RC4_128_SHA"};
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   159
            sslSocket.setEnabledCipherSuites(enabledSuites);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   160
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   161
            SSLParameters params = sslSocket.getSSLParameters();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   162
            params.setServerNames(Collections.singletonList(new SNIHostName(HOST)));
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   163
            sslSocket.setSSLParameters(params);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   164
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   165
            BufferedReader in = new BufferedReader(new InputStreamReader(
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   166
                sslSocket.getInputStream()));
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   167
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   168
                sslSocket.getOutputStream()));
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   169
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   170
            String outStr = "Hello There!\n";
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   171
            out.write(outStr);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   172
            out.flush();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   173
            System.out.print("Sending " + outStr);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   174
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   175
            String inStr = in.readLine();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   176
            System.out.println("Received " + inStr);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   177
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   178
            String cipherSuiteChosen = sslSocket.getSession().getCipherSuite();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   179
            System.out.println("Cipher suite in use: " + cipherSuiteChosen);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   180
            Principal self = sslSocket.getSession().getLocalPrincipal();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   181
            System.out.println("I am: " + self.toString());
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   182
            Principal peer = sslSocket.getSession().getPeerPrincipal();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   183
            System.out.println("Server is: " + peer.toString());
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   184
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   185
            sslSocket.close();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   186
            return null;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   187
        }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   188
    }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   189
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   190
    private static class JsseServerAction implements Action {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   191
        public byte[] run(Context s, byte[] input) throws Exception {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   192
            SSLServerSocketFactory sslssf =
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   193
                (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   194
            SSLServerSocket sslServerSocket =
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   195
                (SSLServerSocket) sslssf.createServerSocket(0); // any port
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   196
            int port = sslServerSocket.getLocalPort();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   197
            System.out.println("Listening on " + port);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   198
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   199
            String enabledSuites[] = {"TLS_KRB5_WITH_RC4_128_SHA"};
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   200
            sslServerSocket.setEnabledCipherSuites(enabledSuites);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   201
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   202
            Files.write(Paths.get("port"), ByteBuffer.allocate(4).putInt(port).array());
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   203
            System.out.println("Waiting for incoming connection...");
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   204
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   205
            SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   206
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   207
            System.out.println("Got connection from client "
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   208
                + sslSocket.getInetAddress());
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   209
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   210
            BufferedReader in = new BufferedReader(new InputStreamReader(
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   211
                sslSocket.getInputStream()));
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   212
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   213
                sslSocket.getOutputStream()));
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   214
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   215
            String inStr = in.readLine();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   216
            System.out.println("Received " + inStr);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   217
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   218
            String outStr = inStr + " " + new Date().toString() + "\n";
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   219
            out.write(outStr);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   220
            System.out.println("Sending " + outStr);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   221
            out.flush();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   222
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   223
            String cipherSuiteChosen =
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   224
                sslSocket.getSession().getCipherSuite();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   225
            System.out.println("Cipher suite in use: " + cipherSuiteChosen);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   226
            Principal self = sslSocket.getSession().getLocalPrincipal();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   227
            System.out.println("I am: " + self.toString());
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   228
            Principal peer = sslSocket.getSession().getPeerPrincipal();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   229
            System.out.println("Client is: " + peer.toString());
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   230
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   231
            sslSocket.close();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   232
            return null;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   233
        }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   234
    }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   235
}