1/*
2 * Copyright (c) 2012, 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
27 * @summary Introduce constrained Kerberos delegation
28 * @compile -XDignore.symbol.file S4U2self.java
29 * @run main/othervm -Dsun.security.krb5.debug=false S4U2self krb5 0
30 * @run main/othervm/fail -Dsun.security.krb5.debug=false S4U2self krb5 1
31 * @run main/othervm/fail -Dsun.security.krb5.debug=false S4U2self krb5 2
32 * @run main/othervm/fail -Dsun.security.krb5.debug=false S4U2self krb5 3
33 * @run main/othervm/fail -Dsun.security.krb5.debug=false S4U2self krb5 4
34 * @run main/othervm/fail -Dsun.security.krb5.debug=false S4U2self krb5 5
35 * @run main/othervm -Dsun.security.krb5.debug=false S4U2self spnego
36 */
37
38import java.util.Arrays;
39import java.util.HashMap;
40import java.util.List;
41import java.util.Map;
42import org.ietf.jgss.Oid;
43import sun.security.jgss.GSSUtil;
44
45public class S4U2self {
46
47    public static void main(String[] args) throws Exception {
48        // Test case, different policy settings in KDC:
49        //                   |     ALLOW_S4U2SELF on
50        //                   |   USER    USER2    none
51        // ALLOW_S4U2PORXY   |-------------------------
52        // USER to BACKEND   |   0       1        2
53        // USER2 to BACKEND  |   3
54        // USER to SERVER    |   4
55        //      none         |   5
56        //
57        // 0 should succeed, all other fail
58        int test = 0;
59        Oid mech;
60        if (args[0].equals("spnego")) {
61            mech = GSSUtil.GSS_SPNEGO_MECH_OID;
62        } else if (args[0].contains("krb5")) {
63            mech = GSSUtil.GSS_KRB5_MECH_OID;
64            test = Integer.parseInt(args[1]);
65        } else {
66            throw new Exception("Unknown mech");
67        }
68
69        OneKDC kdc = new OneKDC(null);
70        kdc.writeJAASConf();
71
72        switch (test) {
73            case 1:
74                kdc.setOption(KDC.Option.ALLOW_S4U2SELF, Arrays.asList(
75                        new String[]{OneKDC.USER2 + "@" + OneKDC.REALM}));
76                break;
77            case 2:
78                // No S4U2self
79                break;
80            default:
81                kdc.setOption(KDC.Option.ALLOW_S4U2SELF, Arrays.asList(
82                        new String[]{OneKDC.USER + "@" + OneKDC.REALM}));
83                break;
84        }
85
86        Map<String,List<String>> map = new HashMap<>();
87        switch (test) {
88            case 3:
89                map.put(OneKDC.USER2 + "@" + OneKDC.REALM, Arrays.asList(
90                        new String[]{OneKDC.BACKEND + "@" + OneKDC.REALM}));
91                kdc.setOption(KDC.Option.ALLOW_S4U2PROXY, map);
92                break;
93            case 4:
94                map.put(OneKDC.USER + "@" + OneKDC.REALM, Arrays.asList(
95                        new String[]{OneKDC.SERVER + "@" + OneKDC.REALM}));
96                kdc.setOption(KDC.Option.ALLOW_S4U2PROXY, map);
97                break;
98            case 5:
99                // No S4U2proxy set
100                break;
101            default:
102                map.put(OneKDC.USER + "@" + OneKDC.REALM, Arrays.asList(
103                        new String[]{OneKDC.BACKEND + "@" + OneKDC.REALM}));
104                kdc.setOption(KDC.Option.ALLOW_S4U2PROXY, map);
105                break;
106        }
107
108        Context c, s;
109        c = Context.fromJAAS("client");
110
111        c = c.impersonate(OneKDC.USER2);
112        c.status();
113
114        c.startAsClient(OneKDC.BACKEND, mech);
115
116        s = Context.fromJAAS("backend");
117        s.startAsServer(mech);
118
119        Context.handshake(c, s);
120
121        Context.transmit("i say high --", c, s);
122        Context.transmit("   you say low", s, c);
123
124        c.status();
125        s.status();
126
127        String n1 = c.x().getSrcName().toString().split("@")[0];
128        String n2 = s.x().getSrcName().toString().split("@")[0];
129        if (!n1.equals(OneKDC.USER2) || !n2.equals(OneKDC.USER2)) {
130            throw new Exception("Impersonate failed");
131        }
132
133        s.dispose();
134        c.dispose();
135    }
136}
137