Skip to content

Commit 0d1a97f

Browse files
committed
8279064: New options for ktab to provide non-default salt
Reviewed-by: valeriep
1 parent c359c35 commit 0d1a97f

File tree

5 files changed

+157
-23
lines changed

5 files changed

+157
-23
lines changed

src/java.security.jgss/share/classes/sun/security/krb5/internal/ktab/KeyTab.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -376,12 +376,33 @@ public void addEntry(PrincipalName service, char[] psswd,
376376
addEntry(service, service.getSalt(), psswd, kvno, append);
377377
}
378378

379-
// Called by KDC test
379+
/**
380+
* Adds a new entry in the key table.
381+
* @param service the service which will have a new entry in the key table.
382+
* @param salt specified non default salt, cannot be null
383+
* @param psswd the password which generates the key.
384+
* @param kvno the kvno to use, -1 means automatic increasing
385+
* @param append false if entries with old kvno would be removed.
386+
* Note: if kvno is not -1, entries with the same kvno are always removed
387+
*/
380388
public void addEntry(PrincipalName service, String salt, char[] psswd,
381389
int kvno, boolean append) throws KrbException {
382390

383391
EncryptionKey[] encKeys = EncryptionKey.acquireSecretKeys(
384-
psswd, salt);
392+
psswd, salt);
393+
addEntry(service, encKeys, kvno, append);
394+
}
395+
396+
/**
397+
* Adds a new entry in the key table.
398+
* @param service the service which will have a new entry in the key table.
399+
* @param encKeys the keys to be added
400+
* @param kvno the kvno to use, -1 means automatic increasing
401+
* @param append false if entries with old kvno would be removed.
402+
* Note: if kvno is not -1, entries with the same kvno are always removed
403+
*/
404+
public void addEntry(PrincipalName service, EncryptionKey[] encKeys,
405+
int kvno, boolean append) throws KrbException {
385406

386407
// There should be only one maximum KVNO value for all etypes, so that
387408
// all added keys can have the same KVNO.

src/java.security.jgss/windows/classes/sun/security/krb5/internal/tools/Ktab.java

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -35,7 +35,6 @@
3535
import java.io.IOException;
3636
import java.io.BufferedReader;
3737
import java.io.InputStreamReader;
38-
import java.io.File;
3938
import java.text.DateFormat;
4039
import java.util.Arrays;
4140
import java.util.Date;
@@ -61,10 +60,12 @@ public class Ktab {
6160
int etype = -1;
6261
char[] password = null;
6362

64-
boolean forced = false; // true if delete without prompt. Default false
63+
boolean fopt = false; // true if delete without prompt or
64+
// add by contacting KDC. Default false
6565
boolean append = false; // true if new keys are appended. Default false
6666
int vDel = -1; // kvno to delete, -1 all, -2 old. Default -1
6767
int vAdd = -1; // kvno to add. Default -1, means auto incremented
68+
String salt = null; // salt to use. Default null, means default salt
6869

6970
/**
7071
* The main program that can be invoked at command line.
@@ -186,6 +187,12 @@ void processArgs(String[] args) {
186187
error(args[i] + " is not valid after -" + action);
187188
}
188189
break;
190+
case "-s": // salt for -a
191+
if (++i >= args.length || args[i].startsWith("-")) {
192+
error("A salt string must be specified after -s");
193+
}
194+
salt = args[i];
195+
break;
189196
case "-n": // kvno for -a
190197
if (++i >= args.length || args[i].startsWith("-")) {
191198
error("A KVNO must be specified after -n");
@@ -213,8 +220,8 @@ void processArgs(String[] args) {
213220
case "-t": // list timestamps
214221
showTime = true;
215222
break;
216-
case "-f": // force delete, no prompt
217-
forced = true;
223+
case "-f": // force delete or get salt from KDC
224+
fopt = true;
218225
break;
219226
case "-append": // -a, new keys append to file
220227
append = true;
@@ -258,6 +265,10 @@ void processArgs(String[] args) {
258265
* a new key table.
259266
*/
260267
void addEntry() {
268+
if (salt != null && fopt) {
269+
System.err.println("-s and -f cannot coexist when adding a keytab entry.");
270+
System.exit(-1);
271+
}
261272
PrincipalName pname = null;
262273
try {
263274
pname = new PrincipalName(principal);
@@ -283,7 +294,15 @@ void addEntry() {
283294
}
284295
try {
285296
// admin.addEntry(pname, password);
286-
table.addEntry(pname, password, vAdd, append);
297+
if (fopt) {
298+
KrbAsReqBuilder builder = new KrbAsReqBuilder(pname, password);
299+
builder.action();
300+
table.addEntry(pname, builder.getKeys(true), vAdd, append);
301+
} else if (salt != null) {
302+
table.addEntry(pname, salt, password, vAdd, append);
303+
} else {
304+
table.addEntry(pname, password, vAdd, append);
305+
}
287306
Arrays.fill(password, '0'); // clear password
288307
// admin.save();
289308
table.save();
@@ -367,7 +386,7 @@ void deleteEntry() {
367386
PrincipalName pname = null;
368387
try {
369388
pname = new PrincipalName(principal);
370-
if (!forced) {
389+
if (!fopt) {
371390
String answer;
372391
BufferedReader cis =
373392
new BufferedReader(new InputStreamReader(System.in));
@@ -424,6 +443,7 @@ void error(String... errors) {
424443
printHelp();
425444
System.exit(-1);
426445
}
446+
427447
/**
428448
* Prints out the help information.
429449
*/
@@ -434,11 +454,13 @@ void printHelp() {
434454
System.out.println();
435455
System.out.println("-l [-e] [-t]\n"
436456
+ " list the keytab name and entries. -e with etype, -t with timestamp.");
437-
System.out.println("-a <principal name> [<password>] [-n <kvno>] [-append]\n"
457+
System.out.println("-a <principal name> [<password>] [-n <kvno>] [-f | -s <salt>] [-append]\n"
438458
+ " add new key entries to the keytab for the given principal name with\n"
439459
+ " optional <password>. If a <kvno> is specified, new keys' Key Version\n"
440460
+ " Numbers equal to the value, otherwise, automatically incrementing\n"
441-
+ " the Key Version Numbers. If -append is specified, new keys are\n"
461+
+ " the Key Version Numbers. If <salt> is specified, it will be used\n"
462+
+ " instead of the default salt. If -f is specified, the KDC will be\n"
463+
+ " contacted to fetch the salt. If -append is specified, new keys are\n"
442464
+ " appended to the keytab, otherwise, old keys for the\n"
443465
+ " same principal are removed.");
444466
System.out.println("-d <principal name> [-f] [-e <etype>] [<kvno> | all | old]\n"

test/jdk/sun/security/krb5/auto/Context.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -207,13 +207,26 @@ public static Context fromUserKtab(
207207
*/
208208
public static Context fromUserKtab(Subject s,
209209
String user, String ktab, boolean storeKey) throws Exception {
210+
return fromUserKtab(s, user, ktab, false, storeKey);
211+
}
212+
213+
/**
214+
* Logins with username/keytab as a client.
215+
*/
216+
public static Context fromUserKtabAsClient(
217+
String user, String ktab, boolean storeKey) throws Exception {
218+
return fromUserKtab(new Subject(), user, ktab, true, storeKey);
219+
}
220+
221+
private static Context fromUserKtab(Subject s,
222+
String user, String ktab, boolean isInitiator, boolean storeKey) throws Exception {
210223
Context out = new Context();
211224
out.name = user;
212225
out.s = s;
213226
Krb5LoginModule krb5 = new Krb5LoginModule();
214227
Map<String, String> map = new HashMap<>();
215228

216-
map.put("isInitiator", "false");
229+
map.put("isInitiator", Boolean.toString(isInitiator));
217230
map.put("doNotPrompt", "true");
218231
map.put("useTicketCache", "false");
219232
map.put("useKeyTab", "true");
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright (c) 2022, 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 8279064
27+
* @summary New options for ktab to provide non-default salt
28+
* @requires os.family == "windows"
29+
* @library /test/lib
30+
* @library /sun/security/krb5/auto
31+
* @compile -XDignore.symbol.file KtabSalt.java
32+
* @run main jdk.test.lib.FileInstaller ../TestHosts TestHosts
33+
* @run main/othervm -Djdk.net.hosts.file=TestHosts KtabSalt
34+
*/
35+
36+
import jdk.test.lib.SecurityTools;
37+
import jdk.test.lib.Utils;
38+
import jdk.test.lib.process.OutputAnalyzer;
39+
40+
import javax.security.auth.login.LoginException;
41+
42+
public class KtabSalt {
43+
44+
public static void main(String[] args) throws Exception {
45+
46+
OneKDC kdc = new OneKDC(null).writeJAASConf();
47+
kdc.addPrincipal("u1", "password".toCharArray(),
48+
"this_is_my_salt", null);
49+
50+
// Using password works
51+
Context.fromUserPass("u1", "password".toCharArray(), true);
52+
53+
// Using KDC's keytab works
54+
kdc.writeKtab("ktab0");
55+
Context.fromUserKtabAsClient("u1", "ktab0", true);
56+
57+
// Self-created keytab with default salt does not work
58+
ktab("-a u1 password -k ktab1");
59+
Utils.runAndCheckException(
60+
() -> Context.fromUserKtabAsClient("u1", "ktab1", true),
61+
LoginException.class);
62+
63+
// Self-creating keytab with specified salt works
64+
ktab("-a u1 password -s this_is_my_salt -k ktab2");
65+
Context.fromUserKtabAsClient("u1", "ktab2", true);
66+
67+
// Self-creating keytab with salt from KDC works
68+
ktab("-a u1 password -f -k ktab3");
69+
Context.fromUserKtabAsClient("u1", "ktab3", true);
70+
}
71+
72+
static OutputAnalyzer ktab(String cmdLine) throws Exception {
73+
String fullCmdLine = String.format(
74+
"-J-Djava.security.krb5.conf=%s -J-Djdk.net.hosts.file=TestHosts %s",
75+
OneKDC.KRB5_CONF, cmdLine);
76+
return SecurityTools.ktab(fullCmdLine).shouldHaveExitValue(0);
77+
}
78+
}

test/jdk/sun/security/krb5/tools/KtabCheck.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -69,31 +69,31 @@ public static void main(String[] args) throws Exception {
6969
check(3,17,3,18,3,19,4,17,4,18,4,19,5,17,5,18,5,19);
7070
ktab("-a me mine -n 6 -append");
7171
check(3,17,3,18,3,19,4,17,4,18,4,19,5,17,5,18,5,19,6,17,6,18,6,19);
72-
ktab("-d me 3");
72+
ktab("-d me -f 3");
7373
check(4,17,4,18,4,19,5,17,5,18,5,19,6,17,6,18,6,19);
74-
ktab("-d me -e 17 6");
74+
ktab("-d me -f -e 17 6");
7575
check(4,17,4,18,4,19,5,17,5,18,5,19,6,18,6,19);
76-
ktab("-d me -e 19 6");
76+
ktab("-d me -f -e 19 6");
7777
check(4,17,4,18,4,19,5,17,5,18,5,19,6,18);
78-
ktab("-d me -e 17 5");
78+
ktab("-d me -f -e 17 5");
7979
check(4,17,4,18,4,19,5,18,5,19,6,18);
80-
ktab("-d me old");
80+
ktab("-d me -f old");
8181
check(4,17,5,19,6,18);
8282
try {
83-
ktab("-d me old");
83+
ktab("-d me -f old");
8484
throw new Exception("Should fail");
8585
} catch (Exception e) {
8686
// no-op
8787
}
8888
check(4,17,5,19,6,18);
89-
ktab("-d me");
89+
ktab("-d me -f");
9090
check();
9191
}
9292

9393
static void ktab(String s) throws Exception {
9494
File conf = new File(System.getProperty("test.src"), "onlythree.conf");
9595
SecurityTools.ktab("-J-Djava.security.krb5.conf=" + conf
96-
+ " -k " + KEYTAB + " -f " + s).shouldHaveExitValue(0);
96+
+ " -k " + KEYTAB + " " + s).shouldHaveExitValue(0);
9797
}
9898

9999
/**

0 commit comments

Comments
 (0)