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