1 /*
2  * Copyright (c) 2012, 2018, 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 6355584 8194486
27  * @summary Introduce constrained Kerberos delegation
28  * @library /test/lib
29  * @compile -XDignore.symbol.file S4U2proxyGSS.java
30  * @run main jdk.test.lib.FileInstaller TestHosts TestHosts
31  * @run main/othervm -Djdk.net.hosts.file=TestHosts
32  *      -Djavax.security.auth.useSubjectCredsOnly=false S4U2proxyGSS krb5
33  * @run main/othervm -Djdk.net.hosts.file=TestHosts
34  *      -Djavax.security.auth.useSubjectCredsOnly=false S4U2proxyGSS spnego
35  */
36 
37 import java.io.File;
38 import java.io.FileOutputStream;
39 import java.security.Security;
40 import java.util.Arrays;
41 import java.util.HashMap;
42 import java.util.List;
43 import java.util.Map;
44 import org.ietf.jgss.Oid;
45 import sun.security.jgss.GSSUtil;
46 
47 public class S4U2proxyGSS {
48 
main(String[] args)49     public static void main(String[] args) throws Exception {
50         Oid mech;
51         if (args[0].equals("spnego")) {
52             mech = GSSUtil.GSS_SPNEGO_MECH_OID;
53         } else if (args[0].contains("krb5")) {
54             mech = GSSUtil.GSS_KRB5_MECH_OID;
55         } else {
56             throw new Exception("Unknown mech");
57         }
58 
59         OneKDC kdc = new OneKDC(null);
60         kdc.writeJAASConf();
61         kdc.setOption(KDC.Option.PREAUTH_REQUIRED, false);
62         Map<String,List<String>> map = new HashMap<>();
63         map.put(OneKDC.SERVER + "@" + OneKDC.REALM, Arrays.asList(
64                 new String[]{OneKDC.SERVER + "@" + OneKDC.REALM}));
65         kdc.setOption(KDC.Option.ALLOW_S4U2PROXY, map);
66 
67         Context c, s, b;
68         System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
69         System.setProperty("java.security.auth.login.config", OneKDC.JAAS_CONF);
70         File f = new File(OneKDC.JAAS_CONF);
71         FileOutputStream fos = new FileOutputStream(f);
72         fos.write((
73                 "com.sun.security.jgss.krb5.initiate {\n" +
74                 "    com.sun.security.auth.module.Krb5LoginModule required;\n};\n" +
75                 "com.sun.security.jgss.krb5.accept {\n" +
76                 "    com.sun.security.auth.module.Krb5LoginModule required\n" +
77                 "    principal=\"" + OneKDC.SERVER + "\"\n" +
78                 "    useKeyTab=true\n" +
79                 "    storeKey=true;\n};\n"
80                 ).getBytes());
81         fos.close();
82         Security.setProperty("auth.login.defaultCallbackHandler", "OneKDC$CallbackForClient");
83         c = Context.fromThinAir();
84         s = Context.fromThinAir();
85         b = Context.fromThinAir();
86         c.startAsClient(OneKDC.SERVER, mech);
87         c.x().requestCredDeleg(false);
88         s.startAsServer(mech);
89 
90         Context.handshake(c, s);
91         Context p = s.delegated();
92         p.startAsClient(OneKDC.SERVER, mech);
93         b.startAsServer(mech);
94         Context.handshake(p, b);
95 
96         String n1 = p.x().getSrcName().toString().split("@")[0];
97         String n2 = b.x().getSrcName().toString().split("@")[0];
98         if (!n1.equals(OneKDC.USER) || !n2.equals(OneKDC.USER)) {
99             throw new Exception("Delegation failed");
100         }
101     }
102 }
103