30904
|
1 |
/*
|
56542
|
2 |
* Copyright (c) 1996, 2018, Oracle and/or its affiliates. All rights reserved.
|
30904
|
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. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package sun.security.ssl;
|
|
27 |
|
56694
|
28 |
import java.io.EOFException;
|
|
29 |
import java.io.IOException;
|
|
30 |
import java.io.InputStream;
|
|
31 |
import java.io.OutputStream;
|
|
32 |
import java.nio.ByteBuffer;
|
56542
|
33 |
import java.security.GeneralSecurityException;
|
|
34 |
import java.util.ArrayList;
|
30904
|
35 |
import javax.crypto.BadPaddingException;
|
56694
|
36 |
import javax.net.ssl.SSLException;
|
|
37 |
import javax.net.ssl.SSLHandshakeException;
|
|
38 |
import javax.net.ssl.SSLProtocolException;
|
|
39 |
|
56542
|
40 |
import sun.security.ssl.SSLCipher.SSLReadCipher;
|
30904
|
41 |
|
|
42 |
/**
|
|
43 |
* {@code InputRecord} implementation for {@code SSLSocket}.
|
|
44 |
*
|
|
45 |
* @author David Brownell
|
|
46 |
*/
|
|
47 |
final class SSLSocketInputRecord extends InputRecord implements SSLRecord {
|
56542
|
48 |
private InputStream is = null;
|
|
49 |
private OutputStream os = null;
|
|
50 |
private final byte[] temporary = new byte[1024];
|
30904
|
51 |
|
|
52 |
private boolean formatVerified = false; // SSLv2 ruled out?
|
|
53 |
|
56542
|
54 |
// Cache for incomplete handshake messages.
|
|
55 |
private ByteBuffer handshakeBuffer = null;
|
|
56 |
|
30904
|
57 |
private boolean hasHeader = false; // Had read the record header
|
|
58 |
|
56542
|
59 |
SSLSocketInputRecord(HandshakeHash handshakeHash) {
|
|
60 |
super(handshakeHash, SSLReadCipher.nullTlsReadCipher());
|
30904
|
61 |
}
|
|
62 |
|
|
63 |
@Override
|
56542
|
64 |
int bytesInCompletePacket() throws IOException {
|
30904
|
65 |
if (!hasHeader) {
|
|
66 |
// read exactly one record
|
56712
|
67 |
try {
|
|
68 |
int really = read(is, temporary, 0, headerSize);
|
|
69 |
if (really < 0) {
|
|
70 |
// EOF: peer shut down incorrectly
|
|
71 |
return -1;
|
|
72 |
}
|
|
73 |
} catch (EOFException eofe) {
|
|
74 |
// The caller will handle EOF.
|
56542
|
75 |
return -1;
|
30904
|
76 |
}
|
|
77 |
hasHeader = true;
|
|
78 |
}
|
|
79 |
|
|
80 |
byte byteZero = temporary[0];
|
|
81 |
int len = 0;
|
|
82 |
|
|
83 |
/*
|
|
84 |
* If we have already verified previous packets, we can
|
|
85 |
* ignore the verifications steps, and jump right to the
|
56694
|
86 |
* determination. Otherwise, try one last heuristic to
|
30904
|
87 |
* see if it's SSL/TLS.
|
|
88 |
*/
|
|
89 |
if (formatVerified ||
|
56542
|
90 |
(byteZero == ContentType.HANDSHAKE.id) ||
|
|
91 |
(byteZero == ContentType.ALERT.id)) {
|
30904
|
92 |
/*
|
|
93 |
* Last sanity check that it's not a wild record
|
|
94 |
*/
|
56542
|
95 |
if (!ProtocolVersion.isNegotiable(
|
|
96 |
temporary[1], temporary[2], false, false)) {
|
|
97 |
throw new SSLException("Unrecognized record version " +
|
|
98 |
ProtocolVersion.nameOf(temporary[1], temporary[2]) +
|
|
99 |
" , plaintext connection?");
|
|
100 |
}
|
30904
|
101 |
|
|
102 |
/*
|
|
103 |
* Reasonably sure this is a V3, disable further checks.
|
|
104 |
* We can't do the same in the v2 check below, because
|
|
105 |
* read still needs to parse/handle the v2 clientHello.
|
|
106 |
*/
|
|
107 |
formatVerified = true;
|
|
108 |
|
|
109 |
/*
|
|
110 |
* One of the SSLv3/TLS message types.
|
|
111 |
*/
|
|
112 |
len = ((temporary[3] & 0xFF) << 8) +
|
|
113 |
(temporary[4] & 0xFF) + headerSize;
|
|
114 |
} else {
|
|
115 |
/*
|
|
116 |
* Must be SSLv2 or something unknown.
|
|
117 |
* Check if it's short (2 bytes) or
|
|
118 |
* long (3) header.
|
|
119 |
*
|
|
120 |
* Internals can warn about unsupported SSLv2
|
|
121 |
*/
|
|
122 |
boolean isShort = ((byteZero & 0x80) != 0);
|
|
123 |
|
|
124 |
if (isShort && ((temporary[2] == 1) || (temporary[2] == 4))) {
|
56542
|
125 |
if (!ProtocolVersion.isNegotiable(
|
|
126 |
temporary[3], temporary[4], false, false)) {
|
|
127 |
throw new SSLException("Unrecognized record version " +
|
|
128 |
ProtocolVersion.nameOf(temporary[3], temporary[4]) +
|
|
129 |
" , plaintext connection?");
|
|
130 |
}
|
30904
|
131 |
|
|
132 |
/*
|
|
133 |
* Client or Server Hello
|
|
134 |
*/
|
|
135 |
//
|
|
136 |
// Short header is using here. We reverse the code here
|
37880
|
137 |
// in case it is used in the future.
|
30904
|
138 |
//
|
|
139 |
// int mask = (isShort ? 0x7F : 0x3F);
|
|
140 |
// len = ((byteZero & mask) << 8) +
|
|
141 |
// (temporary[1] & 0xFF) + (isShort ? 2 : 3);
|
|
142 |
//
|
|
143 |
len = ((byteZero & 0x7F) << 8) + (temporary[1] & 0xFF) + 2;
|
|
144 |
} else {
|
|
145 |
// Gobblygook!
|
|
146 |
throw new SSLException(
|
|
147 |
"Unrecognized SSL message, plaintext connection?");
|
|
148 |
}
|
|
149 |
}
|
|
150 |
|
|
151 |
return len;
|
|
152 |
}
|
|
153 |
|
56542
|
154 |
// Note that the input arguments are not used actually.
|
30904
|
155 |
@Override
|
56542
|
156 |
Plaintext[] decode(ByteBuffer[] srcs, int srcsOffset,
|
|
157 |
int srcsLength) throws IOException, BadPaddingException {
|
30904
|
158 |
|
|
159 |
if (isClosed) {
|
|
160 |
return null;
|
|
161 |
}
|
|
162 |
|
|
163 |
if (!hasHeader) {
|
|
164 |
// read exactly one record
|
|
165 |
int really = read(is, temporary, 0, headerSize);
|
|
166 |
if (really < 0) {
|
|
167 |
throw new EOFException("SSL peer shut down incorrectly");
|
|
168 |
}
|
|
169 |
hasHeader = true;
|
|
170 |
}
|
|
171 |
|
|
172 |
Plaintext plaintext = null;
|
|
173 |
if (!formatVerified) {
|
|
174 |
formatVerified = true;
|
|
175 |
|
|
176 |
/*
|
|
177 |
* The first record must either be a handshake record or an
|
|
178 |
* alert message. If it's not, it is either invalid or an
|
|
179 |
* SSLv2 message.
|
|
180 |
*/
|
56542
|
181 |
if ((temporary[0] != ContentType.HANDSHAKE.id) &&
|
|
182 |
(temporary[0] != ContentType.ALERT.id)) {
|
|
183 |
hasHeader = false;
|
|
184 |
return handleUnknownRecord(temporary);
|
30904
|
185 |
}
|
|
186 |
}
|
|
187 |
|
56694
|
188 |
// The record header should has consumed.
|
30904
|
189 |
hasHeader = false;
|
56542
|
190 |
return decodeInputRecord(temporary);
|
|
191 |
}
|
30904
|
192 |
|
56542
|
193 |
@Override
|
|
194 |
void setReceiverStream(InputStream inputStream) {
|
|
195 |
this.is = inputStream;
|
30904
|
196 |
}
|
|
197 |
|
|
198 |
@Override
|
|
199 |
void setDeliverStream(OutputStream outputStream) {
|
56542
|
200 |
this.os = outputStream;
|
30904
|
201 |
}
|
|
202 |
|
|
203 |
// Note that destination may be null
|
56542
|
204 |
private Plaintext[] decodeInputRecord(
|
|
205 |
byte[] header) throws IOException, BadPaddingException {
|
|
206 |
byte contentType = header[0]; // pos: 0
|
|
207 |
byte majorVersion = header[1]; // pos: 1
|
|
208 |
byte minorVersion = header[2]; // pos: 2
|
|
209 |
int contentLen = ((header[3] & 0xFF) << 8) +
|
|
210 |
(header[4] & 0xFF); // pos: 3, 4
|
30904
|
211 |
|
56542
|
212 |
if (SSLLogger.isOn && SSLLogger.isOn("record")) {
|
|
213 |
SSLLogger.fine(
|
|
214 |
"READ: " +
|
|
215 |
ProtocolVersion.nameOf(majorVersion, minorVersion) +
|
|
216 |
" " + ContentType.nameOf(contentType) + ", length = " +
|
|
217 |
contentLen);
|
|
218 |
}
|
30904
|
219 |
|
|
220 |
//
|
|
221 |
// Check for upper bound.
|
|
222 |
//
|
|
223 |
// Note: May check packetSize limit in the future.
|
|
224 |
if (contentLen < 0 || contentLen > maxLargeRecordSize - headerSize) {
|
|
225 |
throw new SSLProtocolException(
|
|
226 |
"Bad input record size, TLSCiphertext.length = " + contentLen);
|
|
227 |
}
|
|
228 |
|
|
229 |
//
|
|
230 |
// Read a complete record.
|
|
231 |
//
|
56542
|
232 |
ByteBuffer destination = ByteBuffer.allocate(headerSize + contentLen);
|
30904
|
233 |
int dstPos = destination.position();
|
|
234 |
destination.put(temporary, 0, headerSize);
|
|
235 |
while (contentLen > 0) {
|
|
236 |
int howmuch = Math.min(temporary.length, contentLen);
|
|
237 |
int really = read(is, temporary, 0, howmuch);
|
|
238 |
if (really < 0) {
|
|
239 |
throw new EOFException("SSL peer shut down incorrectly");
|
|
240 |
}
|
|
241 |
|
|
242 |
destination.put(temporary, 0, howmuch);
|
|
243 |
contentLen -= howmuch;
|
|
244 |
}
|
|
245 |
destination.flip();
|
|
246 |
destination.position(dstPos + headerSize);
|
|
247 |
|
56542
|
248 |
if (SSLLogger.isOn && SSLLogger.isOn("record")) {
|
|
249 |
SSLLogger.fine(
|
|
250 |
"READ: " +
|
|
251 |
ProtocolVersion.nameOf(majorVersion, minorVersion) +
|
|
252 |
" " + ContentType.nameOf(contentType) + ", length = " +
|
30904
|
253 |
destination.remaining());
|
|
254 |
}
|
|
255 |
|
|
256 |
//
|
|
257 |
// Decrypt the fragment
|
|
258 |
//
|
56542
|
259 |
ByteBuffer fragment;
|
|
260 |
try {
|
|
261 |
Plaintext plaintext =
|
|
262 |
readCipher.decrypt(contentType, destination, null);
|
|
263 |
fragment = plaintext.fragment;
|
|
264 |
contentType = plaintext.contentType;
|
|
265 |
} catch (BadPaddingException bpe) {
|
|
266 |
throw bpe;
|
|
267 |
} catch (GeneralSecurityException gse) {
|
|
268 |
throw (SSLProtocolException)(new SSLProtocolException(
|
|
269 |
"Unexpected exception")).initCause(gse);
|
|
270 |
}
|
56694
|
271 |
if (contentType != ContentType.HANDSHAKE.id &&
|
|
272 |
handshakeBuffer != null && handshakeBuffer.hasRemaining()) {
|
30904
|
273 |
throw new SSLProtocolException(
|
|
274 |
"Expected to get a handshake fragment");
|
|
275 |
}
|
|
276 |
|
|
277 |
//
|
56542
|
278 |
// parse handshake messages
|
30904
|
279 |
//
|
56542
|
280 |
if (contentType == ContentType.HANDSHAKE.id) {
|
|
281 |
ByteBuffer handshakeFrag = fragment;
|
|
282 |
if ((handshakeBuffer != null) &&
|
|
283 |
(handshakeBuffer.remaining() != 0)) {
|
|
284 |
ByteBuffer bb = ByteBuffer.wrap(new byte[
|
|
285 |
handshakeBuffer.remaining() + fragment.remaining()]);
|
|
286 |
bb.put(handshakeBuffer);
|
|
287 |
bb.put(fragment);
|
|
288 |
handshakeFrag = bb.rewind();
|
|
289 |
handshakeBuffer = null;
|
|
290 |
}
|
30904
|
291 |
|
56542
|
292 |
ArrayList<Plaintext> plaintexts = new ArrayList<>(5);
|
|
293 |
while (handshakeFrag.hasRemaining()) {
|
|
294 |
int remaining = handshakeFrag.remaining();
|
|
295 |
if (remaining < handshakeHeaderSize) {
|
|
296 |
handshakeBuffer = ByteBuffer.wrap(new byte[remaining]);
|
|
297 |
handshakeBuffer.put(handshakeFrag);
|
|
298 |
handshakeBuffer.rewind();
|
|
299 |
break;
|
30904
|
300 |
}
|
|
301 |
|
56542
|
302 |
handshakeFrag.mark();
|
|
303 |
// skip the first byte: handshake type
|
|
304 |
byte handshakeType = handshakeFrag.get();
|
|
305 |
int handshakeBodyLen = Record.getInt24(handshakeFrag);
|
|
306 |
handshakeFrag.reset();
|
|
307 |
int handshakeMessageLen =
|
|
308 |
handshakeHeaderSize + handshakeBodyLen;
|
|
309 |
if (remaining < handshakeMessageLen) {
|
|
310 |
handshakeBuffer = ByteBuffer.wrap(new byte[remaining]);
|
|
311 |
handshakeBuffer.put(handshakeFrag);
|
|
312 |
handshakeBuffer.rewind();
|
|
313 |
break;
|
|
314 |
} if (remaining == handshakeMessageLen) {
|
|
315 |
if (handshakeHash.isHashable(handshakeType)) {
|
|
316 |
handshakeHash.receive(handshakeFrag);
|
|
317 |
}
|
30904
|
318 |
|
56542
|
319 |
plaintexts.add(
|
|
320 |
new Plaintext(contentType,
|
|
321 |
majorVersion, minorVersion, -1, -1L, handshakeFrag)
|
|
322 |
);
|
|
323 |
break;
|
30904
|
324 |
} else {
|
56542
|
325 |
int fragPos = handshakeFrag.position();
|
|
326 |
int fragLim = handshakeFrag.limit();
|
|
327 |
int nextPos = fragPos + handshakeMessageLen;
|
|
328 |
handshakeFrag.limit(nextPos);
|
|
329 |
|
|
330 |
if (handshakeHash.isHashable(handshakeType)) {
|
|
331 |
handshakeHash.receive(handshakeFrag);
|
30904
|
332 |
}
|
|
333 |
|
56542
|
334 |
plaintexts.add(
|
|
335 |
new Plaintext(contentType, majorVersion, minorVersion,
|
|
336 |
-1, -1L, handshakeFrag.slice())
|
|
337 |
);
|
30904
|
338 |
|
56542
|
339 |
handshakeFrag.position(nextPos);
|
|
340 |
handshakeFrag.limit(fragLim);
|
|
341 |
}
|
30904
|
342 |
}
|
56542
|
343 |
|
|
344 |
return plaintexts.toArray(new Plaintext[0]);
|
30904
|
345 |
}
|
|
346 |
|
56542
|
347 |
return new Plaintext[] {
|
|
348 |
new Plaintext(contentType,
|
|
349 |
majorVersion, minorVersion, -1, -1L, fragment)
|
|
350 |
// recordEpoch, recordSeq, plaintext);
|
|
351 |
};
|
30904
|
352 |
}
|
|
353 |
|
56542
|
354 |
private Plaintext[] handleUnknownRecord(
|
|
355 |
byte[] header) throws IOException, BadPaddingException {
|
30904
|
356 |
byte firstByte = header[0];
|
|
357 |
byte thirdByte = header[2];
|
|
358 |
|
|
359 |
// Does it look like a Version 2 client hello (V2ClientHello)?
|
|
360 |
if (((firstByte & 0x80) != 0) && (thirdByte == 1)) {
|
|
361 |
/*
|
|
362 |
* If SSLv2Hello is not enabled, throw an exception.
|
|
363 |
*/
|
|
364 |
if (helloVersion != ProtocolVersion.SSL20Hello) {
|
|
365 |
throw new SSLHandshakeException("SSLv2Hello is not enabled");
|
|
366 |
}
|
|
367 |
|
|
368 |
byte majorVersion = header[3];
|
|
369 |
byte minorVersion = header[4];
|
|
370 |
|
|
371 |
if ((majorVersion == ProtocolVersion.SSL20Hello.major) &&
|
|
372 |
(minorVersion == ProtocolVersion.SSL20Hello.minor)) {
|
|
373 |
|
|
374 |
/*
|
|
375 |
* Looks like a V2 client hello, but not one saying
|
|
376 |
* "let's talk SSLv3". So we need to send an SSLv2
|
|
377 |
* error message, one that's treated as fatal by
|
|
378 |
* clients (Otherwise we'll hang.)
|
|
379 |
*/
|
56542
|
380 |
os.write(SSLRecord.v2NoCipher); // SSLv2Hello
|
30904
|
381 |
|
56542
|
382 |
if (SSLLogger.isOn) {
|
|
383 |
if (SSLLogger.isOn("record")) {
|
|
384 |
SSLLogger.fine(
|
30904
|
385 |
"Requested to negotiate unsupported SSLv2!");
|
|
386 |
}
|
|
387 |
|
56542
|
388 |
if (SSLLogger.isOn("packet")) {
|
|
389 |
SSLLogger.fine("Raw write", SSLRecord.v2NoCipher);
|
30904
|
390 |
}
|
|
391 |
}
|
|
392 |
|
|
393 |
throw new SSLException("Unsupported SSL v2.0 ClientHello");
|
|
394 |
}
|
|
395 |
|
|
396 |
int msgLen = ((header[0] & 0x7F) << 8) | (header[1] & 0xFF);
|
|
397 |
|
56542
|
398 |
ByteBuffer destination = ByteBuffer.allocate(headerSize + msgLen);
|
30904
|
399 |
destination.put(temporary, 0, headerSize);
|
|
400 |
msgLen -= 3; // had read 3 bytes of content as header
|
|
401 |
while (msgLen > 0) {
|
|
402 |
int howmuch = Math.min(temporary.length, msgLen);
|
|
403 |
int really = read(is, temporary, 0, howmuch);
|
|
404 |
if (really < 0) {
|
|
405 |
throw new EOFException("SSL peer shut down incorrectly");
|
|
406 |
}
|
|
407 |
|
|
408 |
destination.put(temporary, 0, howmuch);
|
|
409 |
msgLen -= howmuch;
|
|
410 |
}
|
|
411 |
destination.flip();
|
|
412 |
|
|
413 |
/*
|
|
414 |
* If we can map this into a V3 ClientHello, read and
|
|
415 |
* hash the rest of the V2 handshake, turn it into a
|
|
416 |
* V3 ClientHello message, and pass it up.
|
|
417 |
*/
|
|
418 |
destination.position(2); // exclude the header
|
56542
|
419 |
handshakeHash.receive(destination);
|
30904
|
420 |
destination.position(0);
|
|
421 |
|
|
422 |
ByteBuffer converted = convertToClientHello(destination);
|
|
423 |
|
56542
|
424 |
if (SSLLogger.isOn && SSLLogger.isOn("packet")) {
|
|
425 |
SSLLogger.fine(
|
30904
|
426 |
"[Converted] ClientHello", converted);
|
|
427 |
}
|
|
428 |
|
56542
|
429 |
return new Plaintext[] {
|
|
430 |
new Plaintext(ContentType.HANDSHAKE.id,
|
|
431 |
majorVersion, minorVersion, -1, -1L, converted)
|
|
432 |
};
|
30904
|
433 |
} else {
|
|
434 |
if (((firstByte & 0x80) != 0) && (thirdByte == 4)) {
|
|
435 |
throw new SSLException("SSL V2.0 servers are not supported.");
|
|
436 |
}
|
|
437 |
|
|
438 |
throw new SSLException("Unsupported or unrecognized SSL message");
|
|
439 |
}
|
|
440 |
}
|
|
441 |
|
|
442 |
// Read the exact bytes of data, otherwise, return -1.
|
|
443 |
private static int read(InputStream is,
|
|
444 |
byte[] buffer, int offset, int len) throws IOException {
|
|
445 |
int n = 0;
|
|
446 |
while (n < len) {
|
|
447 |
int readLen = is.read(buffer, offset + n, len - n);
|
|
448 |
if (readLen < 0) {
|
56542
|
449 |
if (SSLLogger.isOn && SSLLogger.isOn("packet")) {
|
|
450 |
SSLLogger.fine("Raw read: EOF");
|
|
451 |
}
|
30904
|
452 |
return -1;
|
|
453 |
}
|
|
454 |
|
56542
|
455 |
if (SSLLogger.isOn && SSLLogger.isOn("packet")) {
|
|
456 |
ByteBuffer bb = ByteBuffer.wrap(buffer, offset + n, readLen);
|
|
457 |
SSLLogger.fine("Raw read", bb);
|
30904
|
458 |
}
|
|
459 |
|
|
460 |
n += readLen;
|
|
461 |
}
|
|
462 |
|
|
463 |
return n;
|
|
464 |
}
|
|
465 |
}
|