Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/krb5/auto/BasicProc.java
38853 views
/*1* Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8009977 8186884 820162726* @summary A test to launch multiple Java processes using either Java GSS27* or native GSS28* @library ../../../../java/security/testlibrary /lib/testlibrary29* @compile -XDignore.symbol.file BasicProc.java30* @run main/othervm -Dsun.net.spi.nameservice.provider.1=ns,mock BasicProc launcher31*/3233import java.nio.file.Files;34import java.nio.file.Paths;35import java.nio.file.attribute.PosixFilePermission;36import java.util.Arrays;37import java.util.Collections;38import java.util.HashSet;39import java.util.PropertyPermission;40import java.util.Set;4142import jdk.testlibrary.Asserts;43import org.ietf.jgss.Oid;44import sun.security.krb5.Config;4546import javax.security.auth.PrivateCredentialPermission;4748/**49* Run this test automatically and test Java GSS with embedded KDC.50*51* Run with customized native.krb5.libs to test interop between Java GSS52* and native GSS, and native.kdc.path with a native KDC. For example,53* run the following command to test interop among Java, default native,54* MIT, and Heimdal krb5 libraries with the Heimdal KDC:55*56* jtreg -Dnative.krb5.libs=j=,57* n=,58* k=/usr/local/krb5/lib/libgssapi_krb5.so,59* h=/space/install/heimdal/lib/libgssapi.so \60* -Dnative.kdc.path=/usr/local/heimdal \61* BasicProc.java62*63* Note: The first 4 lines should be concatenated to make a long system64* property value with no blank around ",". This comma-separated value65* has each element being name=libpath. The special name "j" means the66* Java library and libpath is ignored. Otherwise it means a native library,67* and libpath (can be empty) will be the value for the sun.security.jgss.lib68* system property. If this system property is not set, only the Java69* library will be tested.70*/7172public class BasicProc {7374private static final String CONF = "krb5.conf";75private static final String KTAB_S = "server.ktab";76private static final String KTAB_B = "backend.ktab";7778private static final String HOST = "localhost";79private static final String SERVER = "server/" + HOST;80private static final String BACKEND = "backend/" + HOST;81private static final String USER = "user";82private static final char[] PASS = "password".toCharArray();83private static final String REALM = "REALM";8485private static final int MSGSIZE = 1024;86private static final byte[] MSG = new byte[MSGSIZE];8788public static void main(String[] args) throws Exception {8990Oid oid = new Oid("1.2.840.113554.1.2.2");91byte[] token, msg;9293switch (args[0]) {94case "launcher":95KDC kdc = KDC.create(REALM, HOST, 0, true);96try {97kdc.addPrincipal(USER, PASS);98kdc.addPrincipalRandKey("krbtgt/" + REALM);99kdc.addPrincipalRandKey(SERVER);100kdc.addPrincipalRandKey(BACKEND);101102// Native lib might do some name lookup103KDC.saveConfig(CONF, kdc,104"dns_lookup_kdc = no",105"ticket_lifetime = 1h",106"dns_lookup_realm = no",107"dns_canonicalize_hostname = false",108"forwardable = true");109System.setProperty("java.security.krb5.conf", CONF);110Config.refresh();111kdc.writeKtab(KTAB_S, false, SERVER);112kdc.writeKtab(KTAB_B, false, BACKEND);113114String[] tmp = System.getProperty("native.krb5.libs", "j=")115.split(",");116117// Library paths. The 1st one is always null which means118// Java, "" means the default native lib.119String[] libs = new String[tmp.length];120121// Names for each lib above. Use in file names.122String[] names = new String[tmp.length];123124boolean hasNative = false;125126for (int i = 0; i < tmp.length; i++) {127if (tmp[i].isEmpty()) {128throw new Exception("Invalid native.krb5.libs");129}130String[] pair = tmp[i].split("=", 2);131names[i] = pair[0];132if (!pair[0].equals("j")) {133libs[i] = pair.length > 1 ? pair[1] : "";134hasNative = true;135}136}137138if (hasNative) {139kdc.kinit(USER, "base.ccache");140}141142// Try the same lib first143for (int i = 0; i < libs.length; i++) {144once(names[i] + names[i] + names[i],145libs[i], libs[i], libs[i]);146}147148for (int i = 0; i < libs.length; i++) {149for (int j = 0; j < libs.length; j++) {150for (int k = 0; k < libs.length; k++) {151if (i != j || i != k) {152once(names[i] + names[j] + names[k],153libs[i], libs[j], libs[k]);154}155}156}157}158} finally {159kdc.terminate();160}161break;162case "client":163Context c = args[1].equals("n") ?164Context.fromThinAir() :165Context.fromUserPass(USER, PASS, false);166c.startAsClient(SERVER, oid);167c.x().requestCredDeleg(true);168c.x().requestMutualAuth(true);169Proc.binOut(c.take(new byte[0])); // AP-REQ170c.take(Proc.binIn()); // AP-REP171Proc.binOut(c.wrap(MSG, true));172Proc.binOut(c.getMic(MSG));173break;174case "server":175Context s = args[1].equals("n") ?176Context.fromThinAir() :177Context.fromUserKtab(SERVER, KTAB_S, true);178s.startAsServer(oid);179token = Proc.binIn(); // AP-REQ180Proc.binOut(s.take(token)); // AP-REP181msg = s.unwrap(Proc.binIn(), true);182Asserts.assertTrue(Arrays.equals(msg, MSG));183s.verifyMic(Proc.binIn(), msg);184Context s2 = s.delegated();185s2.startAsClient(BACKEND, oid);186s2.x().requestMutualAuth(false);187Proc.binOut(s2.take(new byte[0])); // AP-REQ188msg = s2.unwrap(Proc.binIn(), true);189Asserts.assertTrue(Arrays.equals(msg, MSG));190s2.verifyMic(Proc.binIn(), msg);191break;192case "backend":193Context b = args[1].equals("n") ?194Context.fromThinAir() :195Context.fromUserKtab(BACKEND, KTAB_B, true);196b.startAsServer(oid);197token = b.take(Proc.binIn()); // AP-REQ198Asserts.assertTrue(token == null);199Proc.binOut(b.wrap(MSG, true));200Proc.binOut(b.getMic(MSG));201break;202}203}204205/**206* One test run.207*208* @param label test label209* @param lc lib of client210* @param ls lib of server211* @param lb lib of backend212*/213private static void once(String label, String lc, String ls, String lb)214throws Exception {215216Proc pc = proc(lc)217.args("client", lc == null ? "j" : "n")218.perm(new javax.security.auth.kerberos.ServicePermission(219"krbtgt/" + REALM + "@" + REALM, "initiate"))220.perm(new javax.security.auth.kerberos.ServicePermission(221SERVER + "@" + REALM, "initiate"))222.perm(new javax.security.auth.kerberos.DelegationPermission(223"\"" + SERVER + "@" + REALM + "\" " +224"\"krbtgt/" + REALM + "@" + REALM + "\""))225.debug(label + "-C");226if (lc == null) {227// for Krb5LoginModule::promptForName228pc.perm(new PropertyPermission("user.name", "read"));229} else {230Files.copy(Paths.get("base.ccache"), Paths.get(label + ".ccache"));231Set<PosixFilePermission> perms = new HashSet<>();232perms.add(PosixFilePermission.OWNER_READ);233perms.add(PosixFilePermission.OWNER_WRITE);234Files.setPosixFilePermissions(Paths.get(label + ".ccache"),235Collections.unmodifiableSet(perms));236pc.env("KRB5CCNAME", label + ".ccache");237// Do not try system ktab if ccache fails238pc.env("KRB5_KTNAME", "none");239}240pc.start();241242Proc ps = proc(ls)243.args("server", ls == null ? "j" : "n")244.perm(new javax.security.auth.kerberos.ServicePermission(245SERVER + "@" + REALM, "accept"))246.perm(new javax.security.auth.kerberos.ServicePermission(247BACKEND + "@" + REALM, "initiate"))248.debug(label + "-S");249if (ls == null) {250ps.perm(new PrivateCredentialPermission(251"javax.security.auth.kerberos.KeyTab * \"*\"", "read"))252.perm(new java.io.FilePermission(KTAB_S, "read"));253} else {254ps.env("KRB5_KTNAME", KTAB_S);255}256ps.start();257258Proc pb = proc(lb)259.args("backend", lb == null ? "j" : "n")260.perm(new javax.security.auth.kerberos.ServicePermission(261BACKEND + "@" + REALM, "accept"))262.debug(label + "-B");263if (lb == null) {264pb.perm(new PrivateCredentialPermission(265"javax.security.auth.kerberos.KeyTab * \"*\"", "read"))266.perm(new java.io.FilePermission(KTAB_B, "read"));267} else {268pb.env("KRB5_KTNAME", KTAB_B);269}270pb.start();271272// Client and server273ps.println(pc.readData()); // AP-REQ274pc.println(ps.readData()); // AP-REP275276ps.println(pc.readData()); // KRB-PRIV277ps.println(pc.readData()); // KRB-SAFE278279// Server and backend280pb.println(ps.readData()); // AP-REQ281282ps.println(pb.readData()); // KRB-PRIV283ps.println(pb.readData()); // KRB-SAFE284285if ((pc.waitFor() | ps.waitFor() | pb.waitFor()) != 0) {286throw new Exception("Process failed");287}288}289290/**291* A Proc for a child process.292*293* @param lib the library. Null is Java. "" is default native lib.294*/295private static Proc proc(String lib) throws Exception {296Proc p = Proc.create("BasicProc")297.prop("java.security.manager", "")298.prop("sun.net.spi.nameservice.provider.1", "ns,mock")299.perm(new javax.security.auth.AuthPermission("doAs"));300if (lib != null) {301p.env("KRB5_CONFIG", CONF)302.env("KRB5_TRACE", "/dev/stderr")303.prop("sun.security.jgss.native", "true")304.prop("sun.security.jgss.lib", lib)305.prop("javax.security.auth.useSubjectCredsOnly", "false")306.prop("sun.security.nativegss.debug", "true");307int pos = lib.lastIndexOf('/');308if (pos > 0) {309p.env("LD_LIBRARY_PATH", lib.substring(0, pos));310p.env("DYLD_LIBRARY_PATH", lib.substring(0, pos));311}312} else {313p.perm(new java.util.PropertyPermission(314"sun.security.krb5.principal", "read"))315// For Krb5LoginModule::login.316.perm(new java.lang.RuntimePermission(317"accessClassInPackage.sun.net.spi.nameservice"))318.perm(new javax.security.auth.AuthPermission(319"modifyPrincipals"))320.perm(new javax.security.auth.AuthPermission(321"modifyPrivateCredentials"))322.prop("sun.security.krb5.debug", "true")323.prop("java.security.krb5.conf", CONF);324}325return p;326}327}328329330