1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8009977
  27  * @summary A test library to launch multiple Java processes
  28  * @library ../../../../java/security/testlibrary/
  29  * @modules java.base/sun.net.spi.nameservice
  30  *          java.base/sun.security.util
  31  *          java.security.jgss/sun.security.krb5
  32  *          java.security.jgss/sun.security.krb5.internal
  33  *          java.security.jgss/sun.security.krb5.internal.ccache
  34  *          java.security.jgss/sun.security.krb5.internal.crypto
  35  *          java.security.jgss/sun.security.krb5.internal.ktab
  36  * @compile -XDignore.symbol.file BasicProc.java
  37  * @run main/othervm BasicProc
  38  */
  39 
  40 import java.io.File;
  41 import org.ietf.jgss.Oid;
  42 
  43 import javax.security.auth.PrivateCredentialPermission;
  44 
  45 public class BasicProc {
  46 
  47     static String CONF = "krb5.conf";
  48     static String KTAB = "ktab";
  49     public static void main(String[] args) throws Exception {
  50         String HOST = "localhost";
  51         String SERVER = "server/" + HOST;
  52         String BACKEND = "backend/" + HOST;
  53         String USER = "user";
  54         char[] PASS = "password".toCharArray();
  55         String REALM = "REALM";
  56 
  57         Oid oid = new Oid("1.2.840.113554.1.2.2");
  58 
  59         if (args.length == 0) {
  60             System.setProperty("java.security.krb5.conf", CONF);
  61             KDC kdc = KDC.create(REALM, HOST, 0, true);
  62             kdc.addPrincipal(USER, PASS);
  63             kdc.addPrincipalRandKey("krbtgt/" + REALM);
  64             kdc.addPrincipalRandKey(SERVER);
  65             kdc.addPrincipalRandKey(BACKEND);
  66 
  67             String cwd = System.getProperty("user.dir");
  68             kdc.writeKtab(KTAB);
  69             KDC.saveConfig(CONF, kdc, "forwardable = true");
  70 
  71             Proc pc = Proc.create("BasicProc")
  72                     .args("client")
  73                     .prop("java.security.krb5.conf", CONF)
  74                     .prop("java.security.manager", "")
  75                     .perm(new java.util.PropertyPermission(
  76                             "sun.security.krb5.principal", "read"))
  77                     .perm(new javax.security.auth.AuthPermission(
  78                             "modifyPrincipals"))
  79                     .perm(new javax.security.auth.AuthPermission(
  80                             "modifyPrivateCredentials"))
  81                     .perm(new javax.security.auth.AuthPermission("doAs"))
  82                     .perm(new javax.security.auth.kerberos.ServicePermission(
  83                             "krbtgt/" + REALM + "@" + REALM, "initiate"))
  84                     .perm(new javax.security.auth.kerberos.ServicePermission(
  85                             "server/localhost@" + REALM, "initiate"))
  86                     .perm(new javax.security.auth.kerberos.DelegationPermission(
  87                             "\"server/localhost@" + REALM + "\" " +
  88                                     "\"krbtgt/" + REALM + "@" + REALM + "\""))
  89                     .debug("C")
  90                     .start();
  91             Proc ps = Proc.create("BasicProc")
  92                     .args("server")
  93                     .prop("java.security.krb5.conf", CONF)
  94                     .prop("java.security.manager", "")
  95                     .perm(new java.util.PropertyPermission(
  96                             "sun.security.krb5.principal", "read"))
  97                     .perm(new javax.security.auth.AuthPermission(
  98                             "modifyPrincipals"))
  99                     .perm(new javax.security.auth.AuthPermission(
 100                             "modifyPrivateCredentials"))
 101                     .perm(new javax.security.auth.AuthPermission("doAs"))
 102                     .perm(new PrivateCredentialPermission(
 103                             "javax.security.auth.kerberos.KeyTab * \"*\"",
 104                             "read"))
 105                     .perm(new javax.security.auth.kerberos.ServicePermission(
 106                             "server/localhost@" + REALM, "accept"))
 107                     .perm(new java.io.FilePermission(
 108                             cwd + File.separator + KTAB, "read"))
 109                     .perm(new javax.security.auth.kerberos.ServicePermission(
 110                             "backend/localhost@" + REALM, "initiate"))
 111                     .debug("S")
 112                     .start();
 113             Proc pb = Proc.create("BasicProc")
 114                     .args("backend")
 115                     .prop("java.security.krb5.conf", CONF)
 116                     .prop("java.security.manager", "")
 117                     .perm(new java.util.PropertyPermission(
 118                             "sun.security.krb5.principal", "read"))
 119                     .perm(new javax.security.auth.AuthPermission(
 120                             "modifyPrincipals"))
 121                     .perm(new javax.security.auth.AuthPermission(
 122                             "modifyPrivateCredentials"))
 123                     .perm(new javax.security.auth.AuthPermission("doAs"))
 124                     .perm(new PrivateCredentialPermission(
 125                             "javax.security.auth.kerberos.KeyTab * \"*\"",
 126                             "read"))
 127                     .perm(new javax.security.auth.kerberos.ServicePermission(
 128                             "backend/localhost@" + REALM, "accept"))
 129                     .perm(new java.io.FilePermission(
 130                             cwd + File.separator + KTAB, "read"))
 131                     .debug("B")
 132                     .start();
 133 
 134             // Client and server handshake
 135             String token = pc.readData();
 136             ps.println(token);
 137             token = ps.readData();
 138             pc.println(token);
 139             // Server and backend handshake
 140             token = ps.readData();
 141             pb.println(token);
 142             token = pb.readData();
 143             ps.println(token);
 144             // wrap/unwrap/getMic/verifyMic and plain text
 145             token = ps.readData();
 146             pb.println(token);
 147             token = pb.readData();
 148             ps.println(token);
 149             token = pb.readData();
 150             ps.println(token);
 151 
 152             if ((pc.waitFor() | ps.waitFor() | pb.waitFor()) != 0) {
 153                 throw new Exception();
 154             }
 155         } else if (args[0].equals("client")) {
 156             Context c = Context.fromUserPass(USER, PASS, false);
 157             c.startAsClient(SERVER, oid);
 158             c.x().requestCredDeleg(true);
 159             Proc.binOut(c.take(new byte[0]));
 160             byte[] token = Proc.binIn();
 161             c.take(token);
 162         } else if (args[0].equals("server")) {
 163             Context s = Context.fromUserKtab(SERVER, KTAB, true);
 164             s.startAsServer(oid);
 165             byte[] token = Proc.binIn();
 166             token = s.take(token);
 167             Proc.binOut(token);
 168             Context s2 = s.delegated();
 169             s2.startAsClient(BACKEND, oid);
 170             Proc.binOut(s2.take(new byte[0]));
 171             token = Proc.binIn();
 172             s2.take(token);
 173             byte[] msg = "Hello".getBytes();
 174             Proc.binOut(s2.wrap(msg, true));
 175             s2.verifyMic(Proc.binIn(), msg);
 176             String in = Proc.textIn();
 177             if (!in.equals("Hello")) {
 178                 throw new Exception();
 179             }
 180         } else if (args[0].equals("backend")) {
 181             Context b = Context.fromUserKtab(BACKEND, KTAB, true);
 182             b.startAsServer(oid);
 183             byte[] token = Proc.binIn();
 184             Proc.binOut(b.take(token));
 185             byte[] msg = b.unwrap(Proc.binIn(), true);
 186             Proc.binOut(b.getMic(msg));
 187             Proc.textOut(new String(msg));
 188         }
 189     }
 190     // create a native server
 191     private static Proc ns(Proc p) throws Exception {
 192         return p
 193             .env("KRB5_CONFIG", CONF)
 194             .env("KRB5_KTNAME", KTAB)
 195             .prop("sun.security.jgss.native", "true")
 196             .prop("javax.security.auth.useSubjectCredsOnly", "false")
 197             .prop("sun.security.nativegss.debug", "true");
 198     }
 199 }