9499
|
1 |
/*
|
|
2 |
* Copyright (c) 2011, 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 6894072
|
|
27 |
* @run main/othervm DynamicKeytab
|
|
28 |
* @summary always refresh keytab
|
|
29 |
*/
|
|
30 |
|
|
31 |
import java.io.File;
|
|
32 |
import java.io.FileOutputStream;
|
9542
|
33 |
import java.nio.file.Files;
|
|
34 |
import java.nio.file.Paths;
|
9499
|
35 |
import org.ietf.jgss.GSSException;
|
|
36 |
import sun.security.jgss.GSSUtil;
|
|
37 |
import sun.security.krb5.KrbException;
|
|
38 |
import sun.security.krb5.internal.Krb5;
|
|
39 |
|
|
40 |
public class DynamicKeytab {
|
|
41 |
|
|
42 |
Context c, s;
|
|
43 |
public static void main(String[] args)
|
|
44 |
throws Exception {
|
|
45 |
new DynamicKeytab().go();
|
|
46 |
}
|
|
47 |
|
|
48 |
void go() throws Exception {
|
|
49 |
OneKDC k = new OneKDC(null);
|
|
50 |
k.writeJAASConf();
|
|
51 |
|
9542
|
52 |
Files.delete(Paths.get(OneKDC.KTAB));
|
9499
|
53 |
|
|
54 |
// Starts with no keytab
|
|
55 |
c = Context.fromJAAS("client");
|
|
56 |
s = Context.fromJAAS("com.sun.security.jgss.krb5.accept");
|
|
57 |
|
|
58 |
// Test 1: read new key 1 from keytab
|
|
59 |
k.addPrincipal(OneKDC.SERVER, "pass1".toCharArray());
|
|
60 |
k.writeKtab(OneKDC.KTAB);
|
|
61 |
connect();
|
|
62 |
|
|
63 |
// Test 2: service key cached, find 1 in keytab (now contains 1 and 2)
|
|
64 |
k.addPrincipal(OneKDC.SERVER, "pass2".toCharArray());
|
|
65 |
k.appendKtab(OneKDC.KTAB);
|
|
66 |
connect();
|
|
67 |
|
|
68 |
// Test 3: re-login. Now find 2 in keytab
|
|
69 |
c = Context.fromJAAS("client");
|
|
70 |
connect();
|
|
71 |
|
|
72 |
// Test 4: re-login, KDC use 3 this time.
|
|
73 |
c = Context.fromJAAS("client");
|
|
74 |
// Put 3 and 4 into keytab but keep the real key back to 3.
|
|
75 |
k.addPrincipal(OneKDC.SERVER, "pass3".toCharArray());
|
|
76 |
k.appendKtab(OneKDC.KTAB);
|
|
77 |
k.addPrincipal(OneKDC.SERVER, "pass4".toCharArray());
|
|
78 |
k.appendKtab(OneKDC.KTAB);
|
|
79 |
k.addPrincipal(OneKDC.SERVER, "pass3".toCharArray());
|
|
80 |
connect();
|
|
81 |
|
|
82 |
// Test 5: invalid keytab file, should ignore
|
9542
|
83 |
try (FileOutputStream fos = new FileOutputStream(OneKDC.KTAB)) {
|
|
84 |
fos.write("BADBADBAD".getBytes());
|
|
85 |
}
|
9499
|
86 |
connect();
|
|
87 |
|
|
88 |
// Test 6: delete keytab file, identical to revoke all
|
9542
|
89 |
Files.delete(Paths.get(OneKDC.KTAB));
|
9499
|
90 |
try {
|
|
91 |
connect();
|
|
92 |
throw new Exception("Should not success");
|
|
93 |
} catch (GSSException gsse) {
|
|
94 |
System.out.println(gsse);
|
|
95 |
KrbException ke = (KrbException)gsse.getCause();
|
|
96 |
// KrbApReq.authenticate(*) if (dkey == null)...
|
|
97 |
// This should have been Krb5.KRB_AP_ERR_NOKEY
|
|
98 |
if (ke.returnCode() != Krb5.API_INVALID_ARG) {
|
|
99 |
throw new Exception("Not expected failure code: " +
|
|
100 |
ke.returnCode());
|
|
101 |
}
|
|
102 |
}
|
|
103 |
|
|
104 |
// Test 7: 3 revoked, should fail (now contains only 5)
|
|
105 |
k.addPrincipal(OneKDC.SERVER, "pass5".toCharArray());
|
|
106 |
k.writeKtab(OneKDC.KTAB); // overwrite keytab, which means
|
|
107 |
// old key is revoked
|
|
108 |
try {
|
|
109 |
connect();
|
|
110 |
throw new Exception("Should not success");
|
|
111 |
} catch (GSSException gsse) {
|
|
112 |
System.out.println(gsse);
|
|
113 |
KrbException ke = (KrbException)gsse.getCause();
|
|
114 |
if (ke.returnCode() != Krb5.KRB_AP_ERR_BADKEYVER) {
|
|
115 |
throw new Exception("Not expected failure code: " +
|
|
116 |
ke.returnCode());
|
|
117 |
}
|
|
118 |
}
|
|
119 |
|
|
120 |
// Test 8: an empty KDC means revoke all
|
|
121 |
KDC.create("EMPTY.REALM").writeKtab(OneKDC.KTAB);
|
|
122 |
try {
|
|
123 |
connect();
|
|
124 |
throw new Exception("Should not success");
|
|
125 |
} catch (GSSException gsse) {
|
|
126 |
System.out.println(gsse);
|
|
127 |
KrbException ke = (KrbException)gsse.getCause();
|
|
128 |
// KrbApReq.authenticate(*) if (dkey == null)...
|
|
129 |
// This should have been Krb5.KRB_AP_ERR_NOKEY
|
|
130 |
if (ke.returnCode() != Krb5.API_INVALID_ARG) {
|
|
131 |
throw new Exception("Not expected failure code: " +
|
|
132 |
ke.returnCode());
|
|
133 |
}
|
|
134 |
}
|
|
135 |
}
|
|
136 |
|
|
137 |
void connect() throws Exception {
|
|
138 |
Thread.sleep(2000); // make sure ktab timestamp is different
|
|
139 |
c.startAsClient(OneKDC.SERVER, GSSUtil.GSS_KRB5_MECH_OID);
|
|
140 |
s.startAsServer(GSSUtil.GSS_KRB5_MECH_OID);
|
|
141 |
Context.handshake(c, s);
|
|
142 |
}
|
|
143 |
}
|