1 /*
2  * Copyright (c) 2009, 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 6893158 6907425 7197159 8194486
27  * @summary AP_REQ check should use key version number
28  * @library /test/lib
29  * @run main jdk.test.lib.FileInstaller TestHosts TestHosts
30  * @run main/othervm -Djdk.net.hosts.file=TestHosts MoreKvno
31  */
32 
33 import org.ietf.jgss.GSSException;
34 import sun.security.jgss.GSSUtil;
35 import sun.security.krb5.KrbException;
36 import sun.security.krb5.PrincipalName;
37 import sun.security.krb5.internal.ktab.KeyTab;
38 import sun.security.krb5.internal.Krb5;
39 
40 public class MoreKvno {
41 
42     static PrincipalName p;
main(String[] args)43     public static void main(String[] args)
44             throws Exception {
45 
46         OneKDC kdc = new OneKDC(null);
47         kdc.writeJAASConf();
48 
49         // Rewrite keytab, 3 set of keys with different kvno
50         KeyTab ktab = KeyTab.create(OneKDC.KTAB);
51         p = new PrincipalName(
52             OneKDC.SERVER+"@"+OneKDC.REALM, PrincipalName.KRB_NT_SRV_HST);
53         ktab.addEntry(p, "pass1".toCharArray(), 1, true);
54         ktab.addEntry(p, "pass3".toCharArray(), 3, true);
55         ktab.addEntry(p, "pass2".toCharArray(), 2, true);
56         ktab.save();
57 
58         char[] pass = "pass2".toCharArray();
59         kdc.addPrincipal(OneKDC.SERVER, pass);
60         go(OneKDC.SERVER, "com.sun.security.jgss.krb5.accept", pass);
61 
62         pass = "pass3".toCharArray();
63         kdc.addPrincipal(OneKDC.SERVER, pass);
64         // "server" initiate also, check pass2 is used at authentication
65         go(OneKDC.SERVER, "server", pass);
66 
67         try {
68             pass = "pass4".toCharArray();
69             kdc.addPrincipal(OneKDC.SERVER, pass);
70             go(OneKDC.SERVER, "com.sun.security.jgss.krb5.accept", pass);
71             throw new Exception("This test should fail");
72         } catch (GSSException gsse) {
73             // Since 7197159, different kvno is accepted, this return code
74             // will never be thrown out again.
75             //KrbException ke = (KrbException)gsse.getCause();
76             //if (ke.returnCode() != Krb5.KRB_AP_ERR_BADKEYVER) {
77             //    throw new Exception("Not expected failure code: " +
78             //            ke.returnCode());
79             //}
80         }
81     }
82 
go(String server, String entry, char[] pass)83     static void go(String server, String entry, char[] pass) throws Exception {
84         Context c, s;
85 
86         // Part 1: Test keytab
87         c = Context.fromUserPass("dummy", "bogus".toCharArray(), false);
88         s = Context.fromJAAS(entry);
89 
90         c.startAsClient(server, GSSUtil.GSS_KRB5_MECH_OID);
91         s.startAsServer(GSSUtil.GSS_KRB5_MECH_OID);
92 
93         Context.handshake(c, s);
94 
95         s.dispose();
96         c.dispose();
97 
98         // Part 2: Test username/password pair
99         c = Context.fromUserPass("dummy", "bogus".toCharArray(), false);
100         s = Context.fromUserPass(p.getNameString(), pass, true);
101 
102         c.startAsClient(server, GSSUtil.GSS_KRB5_MECH_OID);
103         s.startAsServer(GSSUtil.GSS_KRB5_MECH_OID);
104 
105         Context.handshake(c, s);
106 
107         s.dispose();
108         c.dispose();
109     }
110 }
111