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  * @compile -XDignore.symbol.file BasicProc.java
  30  * @run main/othervm BasicProc
  31  */
  32 
  33 import java.io.File;
  34 import org.ietf.jgss.Oid;
  35 
  36 import javax.security.auth.PrivateCredentialPermission;
  37 
  38 public class BasicProc {
  39 
  40     static String CONF = "krb5.conf";
  41     static String KTAB = "ktab";
  42     public static void main(String[] args) throws Exception {
  43         String HOST = "localhost";
  44         String SERVER = "server/" + HOST;
  45         String BACKEND = "backend/" + HOST;
  46         String USER = "user";
  47         char[] PASS = "password".toCharArray();
  48         String REALM = "REALM";
  49 
  50         Oid oid = new Oid("1.2.840.113554.1.2.2");
  51 
  52         if (args.length == 0) {
  53             System.setProperty("java.security.krb5.conf", CONF);
  54             KDC kdc = KDC.create(REALM, HOST, 0, true);
  55             kdc.addPrincipal(USER, PASS);
  56             kdc.addPrincipalRandKey("krbtgt/" + REALM);
  57             kdc.addPrincipalRandKey(SERVER);
  58             kdc.addPrincipalRandKey(BACKEND);
  59 
  60             String cwd = System.getProperty("user.dir");
  61             kdc.writeKtab(KTAB);
  62             KDC.saveConfig(CONF, kdc, "forwardable = true");
  63 
  64             Proc pc = Proc.create("BasicProc")
  65                     .args("client")
  66                     .prop("java.security.krb5.conf", CONF)
  67                     .prop("java.security.manager", "")
  68                     .perm(new java.util.PropertyPermission(
  69                             "sun.security.krb5.principal", "read"))
  70                     .perm(new javax.security.auth.AuthPermission(
  71                             "modifyPrincipals"))
  72                     .perm(new javax.security.auth.AuthPermission(
  73                             "modifyPrivateCredentials"))
  74                     .perm(new javax.security.auth.AuthPermission("doAs"))
  75                     .perm(new javax.security.auth.kerberos.ServicePermission(
  76                             "krbtgt/" + REALM + "@" + REALM, "initiate"))
  77                     .perm(new javax.security.auth.kerberos.ServicePermission(
  78                             "server/localhost@" + REALM, "initiate"))
  79                     .perm(new javax.security.auth.kerberos.DelegationPermission(
  80                             "\"server/localhost@" + REALM + "\" " +
  81                                     "\"krbtgt/" + REALM + "@" + REALM + "\""))
  82                     .debug("C")
  83                     .start();
  84             Proc ps = Proc.create("BasicProc")
  85                     .args("server")
  86                     .prop("java.security.krb5.conf", CONF)
  87                     .prop("java.security.manager", "")
  88                     .perm(new java.util.PropertyPermission(
  89                             "sun.security.krb5.principal", "read"))
  90                     .perm(new javax.security.auth.AuthPermission(
  91                             "modifyPrincipals"))
  92                     .perm(new javax.security.auth.AuthPermission(
  93                             "modifyPrivateCredentials"))
  94                     .perm(new javax.security.auth.AuthPermission("doAs"))
  95                     .perm(new PrivateCredentialPermission(
  96                             "javax.security.auth.kerberos.KeyTab * \"*\"",
  97                             "read"))
  98                     .perm(new javax.security.auth.kerberos.ServicePermission(
  99                             "server/localhost@" + REALM, "accept"))
 100                     .perm(new java.io.FilePermission(
 101                             cwd + File.separator + KTAB, "read"))
 102                     .perm(new javax.security.auth.kerberos.ServicePermission(
 103                             "backend/localhost@" + REALM, "initiate"))
 104                     .debug("S")
 105                     .start();
 106             Proc pb = Proc.create("BasicProc")
 107                     .args("backend")
 108                     .prop("java.security.krb5.conf", CONF)
 109                     .prop("java.security.manager", "")
 110                     .perm(new java.util.PropertyPermission(
 111                             "sun.security.krb5.principal", "read"))
 112                     .perm(new javax.security.auth.AuthPermission(
 113                             "modifyPrincipals"))
 114                     .perm(new javax.security.auth.AuthPermission(
 115                             "modifyPrivateCredentials"))
 116                     .perm(new javax.security.auth.AuthPermission("doAs"))
 117                     .perm(new PrivateCredentialPermission(
 118                             "javax.security.auth.kerberos.KeyTab * \"*\"",
 119                             "read"))
 120                     .perm(new javax.security.auth.kerberos.ServicePermission(
 121                             "backend/localhost@" + REALM, "accept"))
 122                     .perm(new java.io.FilePermission(
 123                             cwd + File.separator + KTAB, "read"))
 124                     .debug("B")
 125                     .start();
 126 
 127             // Client and server handshake
 128             String token = pc.readData();
 129             ps.println(token);
 130             token = ps.readData();
 131             pc.println(token);
 132             // Server and backend handshake
 133             token = ps.readData();
 134             pb.println(token);
 135             token = pb.readData();
 136             ps.println(token);
 137             // wrap/unwrap/getMic/verifyMic and plain text
 138             token = ps.readData();
 139             pb.println(token);
 140             token = pb.readData();
 141             ps.println(token);
 142             token = pb.readData();
 143             ps.println(token);
 144 
 145             if ((pc.waitFor() | ps.waitFor() | pb.waitFor()) != 0) {
 146                 throw new Exception();
 147             }
 148         } else if (args[0].equals("client")) {
 149             Context c = Context.fromUserPass(USER, PASS, false);
 150             c.startAsClient(SERVER, oid);
 151             c.x().requestCredDeleg(true);
 152             Proc.binOut(c.take(new byte[0]));
 153             byte[] token = Proc.binIn();
 154             c.take(token);
 155         } else if (args[0].equals("server")) {
 156             Context s = Context.fromUserKtab(SERVER, KTAB, true);
 157             s.startAsServer(oid);
 158             byte[] token = Proc.binIn();
 159             token = s.take(token);
 160             Proc.binOut(token);
 161             Context s2 = s.delegated();
 162             s2.startAsClient(BACKEND, oid);
 163             Proc.binOut(s2.take(new byte[0]));
 164             token = Proc.binIn();
 165             s2.take(token);
 166             byte[] msg = "Hello".getBytes();
 167             Proc.binOut(s2.wrap(msg, true));
 168             s2.verifyMic(Proc.binIn(), msg);
 169             String in = Proc.textIn();
 170             if (!in.equals("Hello")) {
 171                 throw new Exception();
 172             }
 173         } else if (args[0].equals("backend")) {
 174             Context b = Context.fromUserKtab(BACKEND, KTAB, true);
 175             b.startAsServer(oid);
 176             byte[] token = Proc.binIn();
 177             Proc.binOut(b.take(token));
 178             byte[] msg = b.unwrap(Proc.binIn(), true);
 179             Proc.binOut(b.getMic(msg));
 180             Proc.textOut(new String(msg));
 181         }
 182     }
 183     // create a native server
 184     private static Proc ns(Proc p) throws Exception {
 185         return p
 186             .env("KRB5_CONFIG", CONF)
 187             .env("KRB5_KTNAME", KTAB)
 188             .prop("sun.security.jgss.native", "true")
 189             .prop("javax.security.auth.useSubjectCredsOnly", "false")
 190             .prop("sun.security.nativegss.debug", "true");
 191     }
 192 }