Skip to content

Commit 87deb6c

Browse files
martinuyRealCLanger
authored andcommitted
8286526: Improve NTLM support
Reviewed-by: mbaesken Backport-of: 2d1bc2e55c7ed5f123f3ab5d505b7866a28ba4c7
1 parent 068c3b3 commit 87deb6c

File tree

5 files changed

+23
-13
lines changed

5 files changed

+23
-13
lines changed

src/java.base/share/classes/com/sun/security/ntlm/Client.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2013, 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
@@ -117,9 +117,10 @@ public byte[] type1() {
117117
* {@code nonce} is null for NTLM v1.
118118
*/
119119
public byte[] type3(byte[] type2, byte[] nonce) throws NTLMException {
120-
if (type2 == null || (v != Version.NTLM && nonce == null)) {
120+
if (type2 == null || (v != Version.NTLM && nonce == null) ||
121+
(nonce != null && nonce.length != 8)) {
121122
throw new NTLMException(NTLMException.PROTOCOL,
122-
"type2 and nonce cannot be null");
123+
"type2 cannot be null, and nonce must be 8-byte long");
123124
}
124125
debug("NTLM Client: Type 2 received\n");
125126
debug(type2);

src/java.base/share/classes/com/sun/security/ntlm/NTLM.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,23 +226,27 @@ void writeBytes(int offset, byte[] data) {
226226
System.arraycopy(data, 0, internal, offset, data.length);
227227
}
228228

229-
void writeSecurityBuffer(int offset, byte[] data) {
229+
void writeSecurityBuffer(int offset, byte[] data) throws NTLMException {
230230
if (data == null) {
231-
writeShort(offset+4, current);
231+
writeInt(offset+4, current);
232232
} else {
233233
int len = data.length;
234+
if (len > 65535) {
235+
throw new NTLMException(NTLMException.INVALID_INPUT,
236+
"Invalid data length " + len);
237+
}
234238
if (current + len > internal.length) {
235239
internal = Arrays.copyOf(internal, current + len + 256);
236240
}
237241
writeShort(offset, len);
238242
writeShort(offset+2, len);
239-
writeShort(offset+4, current);
243+
writeInt(offset+4, current);
240244
System.arraycopy(data, 0, internal, current, len);
241245
current += len;
242246
}
243247
}
244248

245-
void writeSecurityBuffer(int offset, String str, boolean unicode) {
249+
void writeSecurityBuffer(int offset, String str, boolean unicode) throws NTLMException {
246250
writeSecurityBuffer(offset, str == null ? null : str.getBytes(
247251
unicode ? StandardCharsets.UTF_16LE
248252
: StandardCharsets.ISO_8859_1));

src/java.base/share/classes/com/sun/security/ntlm/NTLMException.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ public final class NTLMException extends GeneralSecurityException {
6565
*/
6666
public static final int PROTOCOL = 6;
6767

68+
/**
69+
* If an invalid input is provided.
70+
*/
71+
public static final int INVALID_INPUT = 7;
72+
6873
private int errorCode;
6974

7075
/**

src/java.base/share/classes/com/sun/security/ntlm/Server.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2012, 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
@@ -85,9 +85,9 @@ public Server(String version, String domain) throws NTLMException {
8585
* {@code nonce} is null.
8686
*/
8787
public byte[] type2(byte[] type1, byte[] nonce) throws NTLMException {
88-
if (nonce == null) {
88+
if (nonce == null || nonce.length != 8) {
8989
throw new NTLMException(NTLMException.PROTOCOL,
90-
"nonce cannot be null");
90+
"nonce must be 8-byte long");
9191
}
9292
debug("NTLM Server: Type 1 received\n");
9393
if (type1 != null) debug(type1);

test/jdk/sun/net/www/protocol/http/NULLTargetInfoTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 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
@@ -23,7 +23,7 @@
2323

2424
/*
2525
* @test
26-
* @bug 8151788
26+
* @bug 8151788 8286526
2727
* @summary NullPointerException from ntlm.Client.type3
2828
* @modules java.base/com.sun.security.ntlm
2929
* @run main NULLTargetInfoTest
@@ -42,7 +42,7 @@ public static void main(String[] args) throws Exception {
4242
"4E 54 4C 4D 53 53 50 00 02 00 00 00 00 00 00 00"
4343
+ "00 00 00 00 05 82 89 00 0B 87 81 B6 2D 6E 8B C1"
4444
+ "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00");
45-
byte[] nonce = new byte[10];
45+
byte[] nonce = new byte[8];
4646
c.type3(type2, nonce);
4747
}
4848

0 commit comments

Comments
 (0)