Skip to content

Commit dce5300

Browse files
author
Alexey Bakhtin
committed
8168518: rcache interop with krb5-1.15
Reviewed-by: andrew Backport-of: e10da9956fba577bcc097b63eb3b4d09896fa77d
1 parent 3dc011b commit dce5300

File tree

8 files changed

+402
-167
lines changed

8 files changed

+402
-167
lines changed

jdk/src/share/classes/sun/security/krb5/KrbApReq.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,12 +301,13 @@ private void authenticate(Krb5AcceptCredential cred, InetAddress initiator)
301301
if (!authenticator.ctime.inClockSkew())
302302
throw new KrbApErrException(Krb5.KRB_AP_ERR_SKEW);
303303

304+
String alg = AuthTimeWithHash.DEFAULT_HASH_ALG;
304305
byte[] hash;
305306
try {
306-
hash = MessageDigest.getInstance("MD5")
307+
hash = MessageDigest.getInstance(AuthTimeWithHash.realAlg(alg))
307308
.digest(apReqMessg.authenticator.cipher);
308309
} catch (NoSuchAlgorithmException ex) {
309-
throw new AssertionError("Impossible");
310+
throw new AssertionError("Impossible " + alg);
310311
}
311312

312313
char[] h = new char[hash.length * 2];
@@ -319,6 +320,7 @@ private void authenticate(Krb5AcceptCredential cred, InetAddress initiator)
319320
apReqMessg.ticket.sname.toString(),
320321
authenticator.ctime.getSeconds(),
321322
authenticator.cusec,
323+
alg,
322324
new String(h));
323325
rcache.checkAndStore(KerberosTime.now(), time);
324326

jdk/src/share/classes/sun/security/krb5/internal/rcache/AuthTime.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,14 @@ public static AuthTime readFrom(SeekableByteChannel chan)
116116
if (st.countTokens() != 6) {
117117
throw new IOException("Incorrect rcache style");
118118
}
119-
st.nextToken();
119+
String hashAlg = st.nextToken();
120120
String hash = st.nextToken();
121121
st.nextToken();
122122
client = st.nextToken();
123123
st.nextToken();
124124
server = st.nextToken();
125125
return new AuthTimeWithHash(
126-
client, server, ctime, cusec, hash);
126+
client, server, ctime, cusec, hashAlg, hash);
127127
} else {
128128
return new AuthTime(
129129
client, server, ctime, cusec);

jdk/src/share/classes/sun/security/krb5/internal/rcache/AuthTimeWithHash.java

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
package sun.security.krb5.internal.rcache;
2727

28+
import sun.security.action.GetPropertyAction;
29+
2830
import java.util.Objects;
2931

3032
/**
@@ -34,14 +36,39 @@
3436
public class AuthTimeWithHash extends AuthTime
3537
implements Comparable<AuthTimeWithHash> {
3638

39+
// The hash algorithm can be "HASH" or "SHA256".
40+
public static final String DEFAULT_HASH_ALG;
41+
42+
static {
43+
if (GetPropertyAction.privilegedGetProperty(
44+
"jdk.krb5.rcache.useMD5", "false").equals("true")) {
45+
DEFAULT_HASH_ALG = "HASH";
46+
} else {
47+
DEFAULT_HASH_ALG = "SHA256";
48+
}
49+
}
50+
51+
public static String realAlg(String alg) {
52+
switch (alg) {
53+
case "HASH":
54+
return "MD5";
55+
case "SHA256":
56+
return "SHA-256";
57+
default:
58+
throw new AssertionError(alg + " is not HASH or SHA256");
59+
}
60+
}
61+
62+
final String hashAlg;
3763
final String hash;
3864

3965
/**
4066
* Constructs a new <code>AuthTimeWithHash</code>.
4167
*/
4268
public AuthTimeWithHash(String client, String server,
43-
int ctime, int cusec, String hash) {
69+
int ctime, int cusec, String hashAlg, String hash) {
4470
super(client, server, ctime, cusec);
71+
this.hashAlg = hashAlg;
4572
this.hash = hash;
4673
}
4774

@@ -56,6 +83,7 @@ public boolean equals(Object o) {
5683
if (!(o instanceof AuthTimeWithHash)) return false;
5784
AuthTimeWithHash that = (AuthTimeWithHash)o;
5885
return Objects.equals(hash, that.hash)
86+
&& Objects.equals(hashAlg, that.hashAlg)
5987
&& Objects.equals(client, that.client)
6088
&& Objects.equals(server, that.server)
6189
&& ctime == that.ctime
@@ -88,6 +116,19 @@ public int compareTo(AuthTimeWithHash other) {
88116
return cmp;
89117
}
90118

119+
/**
120+
* Compares with a possibly old style object. Used
121+
* in DflCache$Storage#loadAndCheck.
122+
* @return true if all AuthTime fields are the same but different hash
123+
*/
124+
public boolean sameTimeDiffHash(AuthTimeWithHash old) {
125+
if (!this.isSameIgnoresHash(old)) {
126+
return false;
127+
}
128+
return this.hashAlg.equals(old.hashAlg) &&
129+
!this.hash.equals(old.hash);
130+
}
131+
91132
/**
92133
* Compares with a possibly old style object. Used
93134
* in DflCache$Storage#loadAndCheck.
@@ -112,7 +153,7 @@ public byte[] encode(boolean withHash) {
112153
String sstring;
113154
if (withHash) {
114155
cstring = "";
115-
sstring = String.format("HASH:%s %d:%s %d:%s", hash,
156+
sstring = String.format("%s:%s %d:%s %d:%s", hashAlg, hash,
116157
client.length(), client,
117158
server.length(), server);
118159
} else {

jdk/src/share/classes/sun/security/krb5/internal/rcache/DflCache.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@
9797
* Java also does this way.
9898
*
9999
* See src/lib/krb5/rcache/rc_io.c and src/lib/krb5/rcache/rc_dfl.c.
100+
*
101+
* Update: New version can use other hash algorithms.
100102
*/
101103
public class DflCache extends ReplayCache {
102104

@@ -307,7 +309,7 @@ private int loadAndCheck(Path p, AuthTimeWithHash time,
307309
if (time.equals(a)) {
308310
// Exact match, must be a replay
309311
throw new KrbApErrException(Krb5.KRB_AP_ERR_REPEAT);
310-
} else if (time.isSameIgnoresHash(a)) {
312+
} else if (time.sameTimeDiffHash((AuthTimeWithHash)a)) {
311313
// Two different authenticators in the same second.
312314
// Remember it
313315
seeNewButNotSame = true;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ public static void main(String[] args) throws Exception {
4747
int count = Integer.parseInt(args[0]);
4848
ReplayCache cache = ReplayCache.getInstance("dfl:./");
4949
AuthTimeWithHash a1 =
50-
new AuthTimeWithHash(client, server, time(-400), 0, hash("1"));
50+
new AuthTimeWithHash(client, server, time(-400), 0, "HASH", hash("1"));
5151
AuthTimeWithHash a2 =
52-
new AuthTimeWithHash(client, server, time(0), 0, hash("4"));
52+
new AuthTimeWithHash(client, server, time(0), 0, "HASH", hash("4"));
5353
KerberosTime now = new KerberosTime(time(0)*1000L);
5454
KerberosTime then = new KerberosTime(time(-300)*1000L);
5555

5656
// Once upon a time, we added a lot of events
5757
for (int i=0; i<count; i++) {
58-
a1 = new AuthTimeWithHash(client, server, time(-400), 0, hash(""));
58+
a1 = new AuthTimeWithHash(client, server, time(-400), 0, "HASH", hash(""));
5959
cache.checkAndStore(then, a1);
6060
}
6161

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ public class ReplayCachePrecise {
4848
public static void main(String[] args) throws Exception {
4949

5050
AuthTimeWithHash a1 = new AuthTimeWithHash(client, server, time(0), 0,
51-
"1111111111111111");
51+
"HASH", "1111111111111111");
5252
AuthTimeWithHash a2 = new AuthTimeWithHash(client, server, time(0), 0,
53-
"2222222222222222");
53+
"HASH", "2222222222222222");
5454
KerberosTime now = new KerberosTime(time(0)*1000L);
5555

5656
// When all new styles, must exact match

0 commit comments

Comments
 (0)