BasicProc.java 13.4 KB
Newer Older
W
weijun 已提交
1
/*
2
 * Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved.
W
weijun 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
26
 * @bug 8009977 8186884 8201627
27 28
 * @summary A test to launch multiple Java processes using either Java GSS
 *          or native GSS
29
 * @library ../../../../java/security/testlibrary /lib/testlibrary
W
weijun 已提交
30
 * @compile -XDignore.symbol.file BasicProc.java
31
 * @run main/othervm -Dsun.net.spi.nameservice.provider.1=ns,mock BasicProc launcher
W
weijun 已提交
32 33
 */

34 35 36 37 38 39 40 41 42
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.PropertyPermission;
import java.util.Set;

43
import jdk.testlibrary.Asserts;
W
weijun 已提交
44
import org.ietf.jgss.Oid;
45
import sun.security.krb5.Config;
W
weijun 已提交
46 47 48

import javax.security.auth.PrivateCredentialPermission;

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
/**
 * Run this test automatically and test Java GSS with embedded KDC.
 *
 * Run with customized native.krb5.libs to test interop between Java GSS
 * and native GSS, and native.kdc.path with a native KDC. For example,
 * run the following command to test interop among Java, default native,
 * MIT, and Heimdal krb5 libraries with the Heimdal KDC:
 *
 *    jtreg -Dnative.krb5.libs=j=,
 *                             n=,
 *                             k=/usr/local/krb5/lib/libgssapi_krb5.so,
 *                             h=/space/install/heimdal/lib/libgssapi.so \
 *          -Dnative.kdc.path=/usr/local/heimdal \
 *          BasicProc.java
 *
 * Note: The first 4 lines should be concatenated to make a long system
 * property value with no blank around ",". This comma-separated value
 * has each element being name=libpath. The special name "j" means the
 * Java library and libpath is ignored. Otherwise it means a native library,
 * and libpath (can be empty) will be the value for the sun.security.jgss.lib
 * system property. If this system property is not set, only the Java
 * library will be tested.
 */

W
weijun 已提交
73 74
public class BasicProc {

75 76 77 78 79 80 81 82 83 84 85 86
    private static final String CONF = "krb5.conf";
    private static final String KTAB_S = "server.ktab";
    private static final String KTAB_B = "backend.ktab";

    private static final String HOST = "localhost";
    private static final String SERVER = "server/" + HOST;
    private static final String BACKEND = "backend/" + HOST;
    private static final String USER = "user";
    private static final char[] PASS = "password".toCharArray();
    private static final String REALM = "REALM";

    private static final int MSGSIZE = 1024;
87
    private static final byte[] MSG = new byte[MSGSIZE];
88

W
weijun 已提交
89 90 91
    public static void main(String[] args) throws Exception {

        Oid oid = new Oid("1.2.840.113554.1.2.2");
92
        byte[] token, msg;
W
weijun 已提交
93

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
        switch (args[0]) {
            case "launcher":
                KDC kdc = KDC.create(REALM, HOST, 0, true);
                try {
                    kdc.addPrincipal(USER, PASS);
                    kdc.addPrincipalRandKey("krbtgt/" + REALM);
                    kdc.addPrincipalRandKey(SERVER);
                    kdc.addPrincipalRandKey(BACKEND);

                    // Native lib might do some name lookup
                    KDC.saveConfig(CONF, kdc,
                            "dns_lookup_kdc = no",
                            "ticket_lifetime = 1h",
                            "dns_lookup_realm = no",
                            "dns_canonicalize_hostname = false",
                            "forwardable = true");
                    System.setProperty("java.security.krb5.conf", CONF);
                    Config.refresh();
                    kdc.writeKtab(KTAB_S, false, SERVER);
                    kdc.writeKtab(KTAB_B, false, BACKEND);

                    String[] tmp = System.getProperty("native.krb5.libs", "j=")
                            .split(",");

                    // Library paths. The 1st one is always null which means
                    // Java, "" means the default native lib.
                    String[] libs = new String[tmp.length];

                    // Names for each lib above. Use in file names.
                    String[] names = new String[tmp.length];

                    boolean hasNative = false;

                    for (int i = 0; i < tmp.length; i++) {
                        if (tmp[i].isEmpty()) {
                            throw new Exception("Invalid native.krb5.libs");
                        }
                        String[] pair = tmp[i].split("=", 2);
                        names[i] = pair[0];
                        if (!pair[0].equals("j")) {
                            libs[i] = pair.length > 1 ? pair[1] : "";
                            hasNative = true;
                        }
                    }

                    if (hasNative) {
                        kdc.kinit(USER, "base.ccache");
                    }

                    // Try the same lib first
                    for (int i = 0; i < libs.length; i++) {
                        once(names[i] + names[i] + names[i],
                                libs[i], libs[i], libs[i]);
                    }

                    for (int i = 0; i < libs.length; i++) {
                        for (int j = 0; j < libs.length; j++) {
                            for (int k = 0; k < libs.length; k++) {
                                if (i != j || i != k) {
                                    once(names[i] + names[j] + names[k],
                                            libs[i], libs[j], libs[k]);
                                }
                            }
                        }
                    }
                } finally {
                    kdc.terminate();
                }
                break;
            case "client":
                Context c = args[1].equals("n") ?
                        Context.fromThinAir() :
                        Context.fromUserPass(USER, PASS, false);
                c.startAsClient(SERVER, oid);
                c.x().requestCredDeleg(true);
169
                c.x().requestMutualAuth(true);
170
                Proc.binOut(c.take(new byte[0])); // AP-REQ
171 172 173
                c.take(Proc.binIn()); // AP-REP
                Proc.binOut(c.wrap(MSG, true));
                Proc.binOut(c.getMic(MSG));
174 175 176 177 178 179 180
                break;
            case "server":
                Context s = args[1].equals("n") ?
                        Context.fromThinAir() :
                        Context.fromUserKtab(SERVER, KTAB_S, true);
                s.startAsServer(oid);
                token = Proc.binIn(); // AP-REQ
181 182 183 184
                Proc.binOut(s.take(token)); // AP-REP
                msg = s.unwrap(Proc.binIn(), true);
                Asserts.assertTrue(Arrays.equals(msg, MSG));
                s.verifyMic(Proc.binIn(), msg);
185 186
                Context s2 = s.delegated();
                s2.startAsClient(BACKEND, oid);
187
                s2.x().requestMutualAuth(false);
188
                Proc.binOut(s2.take(new byte[0])); // AP-REQ
189 190 191
                msg = s2.unwrap(Proc.binIn(), true);
                Asserts.assertTrue(Arrays.equals(msg, MSG));
                s2.verifyMic(Proc.binIn(), msg);
192 193 194 195 196 197
                break;
            case "backend":
                Context b = args[1].equals("n") ?
                        Context.fromThinAir() :
                        Context.fromUserKtab(BACKEND, KTAB_B, true);
                b.startAsServer(oid);
198 199 200 201
                token = b.take(Proc.binIn()); // AP-REQ
                Asserts.assertTrue(token == null);
                Proc.binOut(b.wrap(MSG, true));
                Proc.binOut(b.getMic(MSG));
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
                break;
        }
    }

    /**
     * One test run.
     *
     * @param label test label
     * @param lc lib of client
     * @param ls lib of server
     * @param lb lib of backend
     */
    private static void once(String label, String lc, String ls, String lb)
            throws Exception {

        Proc pc = proc(lc)
                .args("client", lc == null ? "j" : "n")
                .perm(new javax.security.auth.kerberos.ServicePermission(
                        "krbtgt/" + REALM + "@" + REALM, "initiate"))
                .perm(new javax.security.auth.kerberos.ServicePermission(
                        SERVER + "@" + REALM, "initiate"))
                .perm(new javax.security.auth.kerberos.DelegationPermission(
                        "\"" + SERVER + "@" + REALM + "\" " +
                                "\"krbtgt/" + REALM + "@" + REALM + "\""))
                .debug(label + "-C");
        if (lc == null) {
            // for Krb5LoginModule::promptForName
            pc.perm(new PropertyPermission("user.name", "read"));
        } else {
            Files.copy(Paths.get("base.ccache"), Paths.get(label + ".ccache"));
            Set<PosixFilePermission> perms = new HashSet<>();
            perms.add(PosixFilePermission.OWNER_READ);
            perms.add(PosixFilePermission.OWNER_WRITE);
            Files.setPosixFilePermissions(Paths.get(label + ".ccache"),
                                          Collections.unmodifiableSet(perms));
            pc.env("KRB5CCNAME", label + ".ccache");
            // Do not try system ktab if ccache fails
            pc.env("KRB5_KTNAME", "none");
        }
        pc.start();

        Proc ps = proc(ls)
                .args("server", ls == null ? "j" : "n")
                .perm(new javax.security.auth.kerberos.ServicePermission(
                        SERVER + "@" + REALM, "accept"))
                .perm(new javax.security.auth.kerberos.ServicePermission(
                        BACKEND + "@" + REALM, "initiate"))
                .debug(label + "-S");
        if (ls == null) {
            ps.perm(new PrivateCredentialPermission(
                    "javax.security.auth.kerberos.KeyTab * \"*\"", "read"))
                .perm(new java.io.FilePermission(KTAB_S, "read"));
        } else {
            ps.env("KRB5_KTNAME", KTAB_S);
        }
        ps.start();

        Proc pb = proc(lb)
                .args("backend", lb == null ? "j" : "n")
                .perm(new javax.security.auth.kerberos.ServicePermission(
                        BACKEND + "@" + REALM, "accept"))
                .debug(label + "-B");
        if (lb == null) {
            pb.perm(new PrivateCredentialPermission(
                    "javax.security.auth.kerberos.KeyTab * \"*\"", "read"))
                .perm(new java.io.FilePermission(KTAB_B, "read"));
        } else {
            pb.env("KRB5_KTNAME", KTAB_B);
        }
        pb.start();

273 274 275 276 277 278
        // Client and server
        ps.println(pc.readData()); // AP-REQ
        pc.println(ps.readData()); // AP-REP

        ps.println(pc.readData()); // KRB-PRIV
        ps.println(pc.readData()); // KRB-SAFE
279

280 281
        // Server and backend
        pb.println(ps.readData()); // AP-REQ
282

283 284
        ps.println(pb.readData()); // KRB-PRIV
        ps.println(pb.readData()); // KRB-SAFE
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314

        if ((pc.waitFor() | ps.waitFor() | pb.waitFor()) != 0) {
            throw new Exception("Process failed");
        }
    }

    /**
     * A Proc for a child process.
     *
     * @param lib the library. Null is Java. "" is default native lib.
     */
    private static Proc proc(String lib) throws Exception {
        Proc p = Proc.create("BasicProc")
                .prop("java.security.manager", "")
                .prop("sun.net.spi.nameservice.provider.1", "ns,mock")
                .perm(new javax.security.auth.AuthPermission("doAs"));
        if (lib != null) {
            p.env("KRB5_CONFIG", CONF)
                    .env("KRB5_TRACE", "/dev/stderr")
                    .prop("sun.security.jgss.native", "true")
                    .prop("sun.security.jgss.lib", lib)
                    .prop("javax.security.auth.useSubjectCredsOnly", "false")
                    .prop("sun.security.nativegss.debug", "true");
            int pos = lib.lastIndexOf('/');
            if (pos > 0) {
                p.env("LD_LIBRARY_PATH", lib.substring(0, pos));
                p.env("DYLD_LIBRARY_PATH", lib.substring(0, pos));
            }
        } else {
            p.perm(new java.util.PropertyPermission(
W
weijun 已提交
315
                            "sun.security.krb5.principal", "read"))
316
                            // For Krb5LoginModule::login.
317 318
                    .perm(new java.lang.RuntimePermission(
                            "accessClassInPackage.sun.net.spi.nameservice"))
W
weijun 已提交
319 320 321 322
                    .perm(new javax.security.auth.AuthPermission(
                            "modifyPrincipals"))
                    .perm(new javax.security.auth.AuthPermission(
                            "modifyPrivateCredentials"))
323 324
                    .prop("sun.security.krb5.debug", "true")
                    .prop("java.security.krb5.conf", CONF);
W
weijun 已提交
325
        }
326
        return p;
W
weijun 已提交
327 328
    }
}