1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
|
/*
* Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8270290
* @library /test/lib
* @run main/othervm NTLMHeadTest SERVER
* @run main/othervm NTLMHeadTest PROXY
* @run main/othervm NTLMHeadTest TUNNEL
* @summary test for the incorrect logic in reading (and discarding) HTTP
* response body when processing NTLMSSP_CHALLENGE response
* (to CONNECT request) from proxy server. When this response is received
* by client, reset() is called on the connection to read and discard the
* response body. This code path was broken when initial client request
* uses HEAD method and HTTPS resource, in this case CONNECT is sent to
* proxy server (to establish TLS tunnel) and response body is not read
* from a socket (because initial method on client connection is HEAD).
* This does not cause problems with the majority of proxy servers because
* InputStream opened over the response socket is buffered with 8kb buffer
* size. Problem is only reproducible if the response size (headers +
* body) is larger than 8kb. The code path with HTTPS tunneling is checked
* with TUNNEL argument. Additional checks for HEAD handling are included
* for direct server (SERVER) and HTTP proxying (PROXY) code paths, in
* these (non-tunnel) cases client must NOT attempt to read response data
* (to not block on socket read) because HEAD is sent to server and
* NTLMSSP_CHALLENGE response includes Content-Length, but does not
* include the body.
*/
import java.net.*;
import java.io.*;
import java.util.*;
import jdk.test.lib.net.HttpHeaderParser;
import jdk.test.lib.net.URIBuilder;
public class NTLMHeadTest {
enum Mode { SERVER, PROXY, TUNNEL }
static final int BODY_LEN = 8192;
static final String RESP_SERVER_AUTH =
"HTTP/1.1 401 Unauthorized\r\n" +
"WWW-Authenticate: NTLM\r\n" +
"Connection: close\r\n" +
"Content-Length: " + BODY_LEN + "\r\n" +
"\r\n";
static final String RESP_SERVER_NTLM =
"HTTP/1.1 401 Unauthorized\r\n" +
"WWW-Authenticate: NTLM TlRMTVNTUAACAAAAAAAAACgAAAABggAAU3J2Tm9uY2UAAAAAAAAAAA==\r\n" +
"Connection: Keep-Alive\r\n" +
"Content-Length: " + BODY_LEN + "\r\n" +
"\r\n";
static final String RESP_SERVER_OR_PROXY_DEST =
"HTTP/1.1 200 OK\r\n" +
"Connection: close\r\n" +
"Content-Length: 42\r\n" +
"\r\n";
static final String RESP_PROXY_AUTH =
"HTTP/1.1 407 Proxy Authentication Required\r\n" +
"Proxy-Authenticate: NTLM\r\n" +
"Proxy-Connection: close\r\n" +
"Connection: close\r\n" +
"Content-Length: " + BODY_LEN + "\r\n" +
"\r\n";
static final String RESP_PROXY_NTLM =
"HTTP/1.1 407 Proxy Authentication Required\r\n" +
"Proxy-Authenticate: NTLM TlRMTVNTUAACAAAAAAAAACgAAAABggAAU3J2Tm9uY2UAAAAAAAAAAA==\r\n" +
"Proxy-Connection: Keep-Alive\r\n" +
"Connection: Keep-Alive\r\n" +
"Content-Length: " + BODY_LEN + "\r\n" +
"\r\n";
static final String RESP_TUNNEL_AUTH =
"HTTP/1.1 407 Proxy Authentication Required\r\n" +
"Proxy-Authenticate: NTLM\r\n" +
"Proxy-Connection: close\r\n" +
"Connection: close\r\n" +
"Content-Length: " + BODY_LEN + "\r\n" +
"\r\n" +
generateBody(BODY_LEN);
static final String RESP_TUNNEL_NTLM =
"HTTP/1.1 407 Proxy Authentication Required\r\n" +
"Proxy-Authenticate: NTLM TlRMTVNTUAACAAAAAAAAACgAAAABggAAU3J2Tm9uY2UAAAAAAAAAAA==\r\n" +
"Proxy-Connection: Keep-Alive\r\n" +
"Connection: Keep-Alive\r\n" +
"Content-Length: " + BODY_LEN + "\r\n" +
"\r\n" +
generateBody(BODY_LEN);
static final String RESP_TUNNEL_ESTABLISHED =
"HTTP/1.1 200 Connection Established\r\n\r\n";
public static void main(String[] args) throws Exception {
Authenticator.setDefault(new TestAuthenticator());
if (1 != args.length) {
throw new IllegalArgumentException("Mode value must be specified, one of: [SERVER, PROXY, TUNNEL]");
}
Mode mode = Mode.valueOf(args[0]);
System.out.println("Running with mode: " + mode);
switch (mode) {
case SERVER: testSever(); return;
case PROXY: testProxy(); return;
case TUNNEL: testTunnel(); return;
default: throw new IllegalArgumentException("Invalid mode: " + mode);
}
}
static void testSever() throws Exception {
try (NTLMServer server = startServer(new ServerSocket(0, 0, InetAddress.getLoopbackAddress()), Mode.SERVER)) {
URL url = URIBuilder.newBuilder()
.scheme("http")
.loopback()
.port(server.getLocalPort())
.path("/")
.toURLUnchecked();
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
uc.setRequestMethod("HEAD");
uc.getInputStream().readAllBytes();
}
}
static void testProxy() throws Exception {
InetAddress loopback = InetAddress.getLoopbackAddress();
try (NTLMServer server = startServer(new ServerSocket(0, 0, loopback), Mode.PROXY)) {
SocketAddress proxyAddr = new InetSocketAddress(loopback, server.getLocalPort());
Proxy proxy = new Proxy(java.net.Proxy.Type.HTTP, proxyAddr);
URL url = URIBuilder.newBuilder()
.scheme("http")
.loopback()
.port(8080)
.path("/")
.toURLUnchecked();
HttpURLConnection uc = (HttpURLConnection) url.openConnection(proxy);
uc.setRequestMethod("HEAD");
uc.getInputStream().readAllBytes();
}
}
static void testTunnel() throws Exception {
InetAddress loopback = InetAddress.getLoopbackAddress();
try (NTLMServer server = startServer(new ServerSocket(0, 0, loopback), Mode.TUNNEL)) {
SocketAddress proxyAddr = new InetSocketAddress(loopback, server.getLocalPort());
Proxy proxy = new Proxy(java.net.Proxy.Type.HTTP, proxyAddr);
URL url = URIBuilder.newBuilder()
.scheme("https")
.loopback()
.port(8443)
.path("/")
.toURLUnchecked();
HttpURLConnection uc = (HttpURLConnection) url.openConnection(proxy);
uc.setRequestMethod("HEAD");
try {
uc.getInputStream().readAllBytes();
} catch (IOException e) {
// can be SocketException or SSLHandshakeException
// Tunnel established and closed by server
System.out.println("Tunnel established successfully");
} catch (NoSuchElementException e) {
System.err.println("Error: cannot read 200 response code");
throw e;
}
}
}
static class NTLMServer extends Thread implements AutoCloseable {
final ServerSocket ss;
final Mode mode;
volatile boolean closed;
NTLMServer(ServerSocket serverSS, Mode mode) {
super();
setDaemon(true);
this.ss = serverSS;
this.mode = mode;
}
int getLocalPort() { return ss.getLocalPort(); }
@Override
public void run() {
boolean doing2ndStageNTLM = false;
while (!closed) {
try {
Socket s = ss.accept();
InputStream is = s.getInputStream();
OutputStream os = s.getOutputStream();
switch(mode) {
case SERVER:
doServer(is, os, doing2ndStageNTLM);
break;
case PROXY:
doProxy(is, os, doing2ndStageNTLM);
break;
case TUNNEL:
doTunnel(is, os, doing2ndStageNTLM);
break;
default: throw new IllegalArgumentException();
}
if (!doing2ndStageNTLM) {
doing2ndStageNTLM = true;
} else {
os.close();
}
} catch (IOException ioe) {
if (!closed) {
ioe.printStackTrace();
}
}
}
}
@Override
public void close() {
if (closed) return;
synchronized(this) {
if (closed) return;
closed = true;
}
try { ss.close(); } catch (IOException x) { };
}
}
static NTLMServer startServer(ServerSocket serverSS, Mode mode) {
NTLMServer server = new NTLMServer(serverSS, mode);
server.start();
return server;
}
static String generateBody(int length) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < length; i++) {
sb.append(i % 10);
}
return sb.toString();
}
static void doServer(InputStream is, OutputStream os, boolean doing2ndStageNTLM) throws IOException {
if (!doing2ndStageNTLM) {
new HttpHeaderParser(is);
os.write(RESP_SERVER_AUTH.getBytes("ASCII"));
} else {
new HttpHeaderParser(is);
os.write(RESP_SERVER_NTLM.getBytes("ASCII"));
new HttpHeaderParser(is);
os.write(RESP_SERVER_OR_PROXY_DEST.getBytes("ASCII"));
}
}
static void doProxy(InputStream is, OutputStream os, boolean doing2ndStageNTLM) throws IOException {
if (!doing2ndStageNTLM) {
new HttpHeaderParser(is);
os.write(RESP_PROXY_AUTH.getBytes("ASCII"));
} else {
new HttpHeaderParser(is);
os.write(RESP_PROXY_NTLM.getBytes("ASCII"));
new HttpHeaderParser(is);
os.write(RESP_SERVER_OR_PROXY_DEST.getBytes("ASCII"));
}
}
static void doTunnel(InputStream is, OutputStream os, boolean doing2ndStageNTLM) throws IOException {
if (!doing2ndStageNTLM) {
new HttpHeaderParser(is);
os.write(RESP_TUNNEL_AUTH.getBytes("ASCII"));
} else {
new HttpHeaderParser(is);
os.write(RESP_TUNNEL_NTLM.getBytes("ASCII"));
new HttpHeaderParser(is);
os.write(RESP_TUNNEL_ESTABLISHED.getBytes("ASCII"));
}
}
static class TestAuthenticator extends java.net.Authenticator {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("test", "secret".toCharArray());
}
}
}
|