1 /*
2  * Copyright (c) 2009, 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  * @test
25  * @bug 6867665
26  * @bug 6875033
27  * @run main/othervm HighestKvno
28  * @summary Problem with keytabs with multiple kvno's (key versions)
29  */
30 
31 import sun.security.krb5.internal.ktab.*;
32 import sun.security.krb5.*;
33 import java.io.File;
34 import java.io.FileOutputStream;
35 
36 public class HighestKvno {
37 
main(String[] args)38     public static void main(String[] args) throws Exception {
39         // kt is a keytab including these entries:
40         //
41         // me@MAD.LOCAL: Type: 3, KVNO: 4
42         // me@MAD.LOCAL: Type: 23, KVNO: 4
43         // me@MAD.LOCAL: Type: 16, KVNO: 4
44         // me@MAD.LOCAL: Type: 1, KVNO: 5
45         // me@MAD.LOCAL: Type: 17, KVNO: 5
46         // me@MAD.LOCAL: Type: 18, KVNO: 5
47         // me@MAD.LOCAL: Type: 1, KVNO: 3
48         // me@MAD.LOCAL: Type: 17, KVNO: 3
49         // me@MAD.LOCAL: Type: 18, KVNO: 3
50         // he@MAD.LOCAL: Type: 1, KVNO: 1
51         // he@MAD.LOCAL: Type: 17, KVNO: 1
52         // he@MAD.LOCAL: Type: 18, KVNO: 1
53         //
54         // This file is created with these steps:
55         // 1. Modify JRE's Ktab.java so that adding new entries
56         //    does not remove the old one.
57         // 2. Run the modified Ktab to create 4 sets of keys
58         // 3. Manually hex edit the KVNO as above
59 
60         byte[] kt = {
61             (byte)0x05, (byte)0x02, (byte)0x00, (byte)0x00,
62             (byte)0x00, (byte)0x26, (byte)0x00, (byte)0x01,
63             (byte)0x00, (byte)0x09, (byte)0x4D, (byte)0x41,
64             (byte)0x44, (byte)0x2E, (byte)0x4C, (byte)0x4F,
65             (byte)0x43, (byte)0x41, (byte)0x4C, (byte)0x00,
66             (byte)0x02, (byte)0x6D, (byte)0x65, (byte)0x00,
67             (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x4A,
68             (byte)0x79, (byte)0x45, (byte)0xCD, (byte)0x04,
69             (byte)0x00, (byte)0x03, (byte)0x00, (byte)0x08,
70             (byte)0xE6, (byte)0xB0, (byte)0x07, (byte)0xA8,
71             (byte)0x5B, (byte)0xF8, (byte)0x73, (byte)0xAD,
72             (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x2E,
73             (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x09,
74             (byte)0x4D, (byte)0x41, (byte)0x44, (byte)0x2E,
75             (byte)0x4C, (byte)0x4F, (byte)0x43, (byte)0x41,
76             (byte)0x4C, (byte)0x00, (byte)0x02, (byte)0x6D,
77             (byte)0x65, (byte)0x00, (byte)0x00, (byte)0x00,
78             (byte)0x00, (byte)0x4A, (byte)0x79, (byte)0x45,
79             (byte)0xCD, (byte)0x04, (byte)0x00, (byte)0x17,
80             (byte)0x00, (byte)0x10, (byte)0x50, (byte)0x92,
81             (byte)0x01, (byte)0x6B, (byte)0xCF, (byte)0x5A,
82             (byte)0x2A, (byte)0x7A, (byte)0x4F, (byte)0xE8,
83             (byte)0x39, (byte)0xD9, (byte)0x90, (byte)0xB5,
84             (byte)0x9C, (byte)0xEB, (byte)0x00, (byte)0x00,
85             (byte)0x00, (byte)0x36, (byte)0x00, (byte)0x01,
86             (byte)0x00, (byte)0x09, (byte)0x4D, (byte)0x41,
87             (byte)0x44, (byte)0x2E, (byte)0x4C, (byte)0x4F,
88             (byte)0x43, (byte)0x41, (byte)0x4C, (byte)0x00,
89             (byte)0x02, (byte)0x6D, (byte)0x65, (byte)0x00,
90             (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x4A,
91             (byte)0x79, (byte)0x45, (byte)0xCD, (byte)0x04,
92             (byte)0x00, (byte)0x10, (byte)0x00, (byte)0x18,
93             (byte)0xDF, (byte)0xDF, (byte)0x62, (byte)0x86,
94             (byte)0x37, (byte)0xCE, (byte)0x29, (byte)0xBA,
95             (byte)0xBC, (byte)0x23, (byte)0x15, (byte)0xDC,
96             (byte)0x86, (byte)0x7C, (byte)0xB6, (byte)0x89,
97             (byte)0x25, (byte)0x25, (byte)0xCD, (byte)0x4A,
98             (byte)0x9B, (byte)0xCE, (byte)0xF4, (byte)0xAE,
99             (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x26,
100             (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x09,
101             (byte)0x4D, (byte)0x41, (byte)0x44, (byte)0x2E,
102             (byte)0x4C, (byte)0x4F, (byte)0x43, (byte)0x41,
103             (byte)0x4C, (byte)0x00, (byte)0x02, (byte)0x6D,
104             (byte)0x65, (byte)0x00, (byte)0x00, (byte)0x00,
105             (byte)0x00, (byte)0x4A, (byte)0x79, (byte)0x4B,
106             (byte)0x5E, (byte)0x05, (byte)0x00, (byte)0x01,
107             (byte)0x00, (byte)0x08, (byte)0xE6, (byte)0xB0,
108             (byte)0x07, (byte)0xA8, (byte)0x5B, (byte)0xF8,
109             (byte)0x73, (byte)0xAD, (byte)0x00, (byte)0x00,
110             (byte)0x00, (byte)0x2E, (byte)0x00, (byte)0x01,
111             (byte)0x00, (byte)0x09, (byte)0x4D, (byte)0x41,
112             (byte)0x44, (byte)0x2E, (byte)0x4C, (byte)0x4F,
113             (byte)0x43, (byte)0x41, (byte)0x4C, (byte)0x00,
114             (byte)0x02, (byte)0x6D, (byte)0x65, (byte)0x00,
115             (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x4A,
116             (byte)0x79, (byte)0x4B, (byte)0x5E, (byte)0x05,
117             (byte)0x00, (byte)0x11, (byte)0x00, (byte)0x10,
118             (byte)0xEA, (byte)0xF5, (byte)0xA8, (byte)0x36,
119             (byte)0xA5, (byte)0x3E, (byte)0x5F, (byte)0x5C,
120             (byte)0x26, (byte)0xE9, (byte)0xDD, (byte)0x8B,
121             (byte)0x8C, (byte)0xE8, (byte)0x92, (byte)0x9C,
122             (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x3E,
123             (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x09,
124             (byte)0x4D, (byte)0x41, (byte)0x44, (byte)0x2E,
125             (byte)0x4C, (byte)0x4F, (byte)0x43, (byte)0x41,
126             (byte)0x4C, (byte)0x00, (byte)0x02, (byte)0x6D,
127             (byte)0x65, (byte)0x00, (byte)0x00, (byte)0x00,
128             (byte)0x00, (byte)0x4A, (byte)0x79, (byte)0x4B,
129             (byte)0x5E, (byte)0x05, (byte)0x00, (byte)0x12,
130             (byte)0x00, (byte)0x20, (byte)0x68, (byte)0xBE,
131             (byte)0xD4, (byte)0x17, (byte)0x3A, (byte)0x06,
132             (byte)0xE0, (byte)0x0C, (byte)0x62, (byte)0x11,
133             (byte)0xB7, (byte)0x53, (byte)0x1B, (byte)0x3E,
134             (byte)0xB2, (byte)0x6B, (byte)0x0D, (byte)0x48,
135             (byte)0xD8, (byte)0x52, (byte)0x5A, (byte)0x4C,
136             (byte)0xBE, (byte)0x24, (byte)0xBB, (byte)0x3D,
137             (byte)0xC1, (byte)0x74, (byte)0x69, (byte)0xDA,
138             (byte)0x34, (byte)0x98, (byte)0x00, (byte)0x00,
139             (byte)0x00, (byte)0x26, (byte)0x00, (byte)0x01,
140             (byte)0x00, (byte)0x09, (byte)0x4D, (byte)0x41,
141             (byte)0x44, (byte)0x2E, (byte)0x4C, (byte)0x4F,
142             (byte)0x43, (byte)0x41, (byte)0x4C, (byte)0x00,
143             (byte)0x02, (byte)0x6D, (byte)0x65, (byte)0x00,
144             (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x4A,
145             (byte)0x79, (byte)0x51, (byte)0x27, (byte)0x03,
146             (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x08,
147             (byte)0xE6, (byte)0xB0, (byte)0x07, (byte)0xA8,
148             (byte)0x5B, (byte)0xF8, (byte)0x73, (byte)0xAD,
149             (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x2E,
150             (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x09,
151             (byte)0x4D, (byte)0x41, (byte)0x44, (byte)0x2E,
152             (byte)0x4C, (byte)0x4F, (byte)0x43, (byte)0x41,
153             (byte)0x4C, (byte)0x00, (byte)0x02, (byte)0x6D,
154             (byte)0x65, (byte)0x00, (byte)0x00, (byte)0x00,
155             (byte)0x00, (byte)0x4A, (byte)0x79, (byte)0x51,
156             (byte)0x27, (byte)0x03, (byte)0x00, (byte)0x11,
157             (byte)0x00, (byte)0x10, (byte)0xEA, (byte)0xF5,
158             (byte)0xA8, (byte)0x36, (byte)0xA5, (byte)0x3E,
159             (byte)0x5F, (byte)0x5C, (byte)0x26, (byte)0xE9,
160             (byte)0xDD, (byte)0x8B, (byte)0x8C, (byte)0xE8,
161             (byte)0x92, (byte)0x9C, (byte)0x00, (byte)0x00,
162             (byte)0x00, (byte)0x3E, (byte)0x00, (byte)0x01,
163             (byte)0x00, (byte)0x09, (byte)0x4D, (byte)0x41,
164             (byte)0x44, (byte)0x2E, (byte)0x4C, (byte)0x4F,
165             (byte)0x43, (byte)0x41, (byte)0x4C, (byte)0x00,
166             (byte)0x02, (byte)0x6D, (byte)0x65, (byte)0x00,
167             (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x4A,
168             (byte)0x79, (byte)0x51, (byte)0x27, (byte)0x03,
169             (byte)0x00, (byte)0x12, (byte)0x00, (byte)0x20,
170             (byte)0x68, (byte)0xBE, (byte)0xD4, (byte)0x17,
171             (byte)0x3A, (byte)0x06, (byte)0xE0, (byte)0x0C,
172             (byte)0x62, (byte)0x11, (byte)0xB7, (byte)0x53,
173             (byte)0x1B, (byte)0x3E, (byte)0xB2, (byte)0x6B,
174             (byte)0x0D, (byte)0x48, (byte)0xD8, (byte)0x52,
175             (byte)0x5A, (byte)0x4C, (byte)0xBE, (byte)0x24,
176             (byte)0xBB, (byte)0x3D, (byte)0xC1, (byte)0x74,
177             (byte)0x69, (byte)0xDA, (byte)0x34, (byte)0x98,
178             (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x26,
179             (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x09,
180             (byte)0x4D, (byte)0x41, (byte)0x44, (byte)0x2E,
181             (byte)0x4C, (byte)0x4F, (byte)0x43, (byte)0x41,
182             (byte)0x4C, (byte)0x00, (byte)0x02, (byte)0x68,
183             (byte)0x65, (byte)0x00, (byte)0x00, (byte)0x00,
184             (byte)0x00, (byte)0x4A, (byte)0x79, (byte)0x54,
185             (byte)0xC7, (byte)0x01, (byte)0x00, (byte)0x01,
186             (byte)0x00, (byte)0x08, (byte)0x85, (byte)0x5B,
187             (byte)0xE3, (byte)0x13, (byte)0x3E, (byte)0xF8,
188             (byte)0x76, (byte)0xEC, (byte)0x00, (byte)0x00,
189             (byte)0x00, (byte)0x2E, (byte)0x00, (byte)0x01,
190             (byte)0x00, (byte)0x09, (byte)0x4D, (byte)0x41,
191             (byte)0x44, (byte)0x2E, (byte)0x4C, (byte)0x4F,
192             (byte)0x43, (byte)0x41, (byte)0x4C, (byte)0x00,
193             (byte)0x02, (byte)0x68, (byte)0x65, (byte)0x00,
194             (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x4A,
195             (byte)0x79, (byte)0x54, (byte)0xC7, (byte)0x01,
196             (byte)0x00, (byte)0x11, (byte)0x00, (byte)0x10,
197             (byte)0xEC, (byte)0xCC, (byte)0x16, (byte)0xCD,
198             (byte)0xE8, (byte)0x51, (byte)0x46, (byte)0x4C,
199             (byte)0x1B, (byte)0x57, (byte)0xAE, (byte)0x19,
200             (byte)0xC3, (byte)0xD2, (byte)0x55, (byte)0x1B,
201             (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x3E,
202             (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x09,
203             (byte)0x4D, (byte)0x41, (byte)0x44, (byte)0x2E,
204             (byte)0x4C, (byte)0x4F, (byte)0x43, (byte)0x41,
205             (byte)0x4C, (byte)0x00, (byte)0x02, (byte)0x68,
206             (byte)0x65, (byte)0x00, (byte)0x00, (byte)0x00,
207             (byte)0x00, (byte)0x4A, (byte)0x79, (byte)0x54,
208             (byte)0xC7, (byte)0x01, (byte)0x00, (byte)0x12,
209             (byte)0x00, (byte)0x20, (byte)0xAE, (byte)0xBA,
210             (byte)0xCB, (byte)0xF5, (byte)0xA8, (byte)0x09,
211             (byte)0xC1, (byte)0xB0, (byte)0x2C, (byte)0x2A,
212             (byte)0x3D, (byte)0x96, (byte)0x2C, (byte)0x2D,
213             (byte)0xF5, (byte)0xFE, (byte)0x65, (byte)0xEC,
214             (byte)0x75, (byte)0x72, (byte)0x5B, (byte)0x46,
215             (byte)0x84, (byte)0xD7, (byte)0x49, (byte)0x3E,
216             (byte)0xF2, (byte)0x27, (byte)0x32, (byte)0x69,
217             (byte)0x75, (byte)0x9B,
218         };
219         System.setProperty("java.security.krb5.conf",
220                 new File(System.getProperty("test.src"),
221                     "../krb5.conf").getAbsolutePath());
222         FileOutputStream fout = new FileOutputStream("kt");
223         fout.write(kt);
224         fout.close();
225         KeyTab ktab = KeyTab.getInstance("kt");
226         PrincipalName pn = new PrincipalName("me@MAD.LOCAL");
227         EncryptionKey[] keys = ktab.readServiceKeys(pn);
228         if (keys[0].getKeyVersionNumber() != 5) {
229             throw new Exception("Highest not first");
230         }
231         new File("kt").delete();
232     }
233 }
234