author | xuelei |
Sun, 10 Jun 2018 13:36:40 -0700 | |
branch | JDK-8145252-TLS13-branch |
changeset 56717 | e4fe7c97b1de |
parent 56712 | bad9f4c7eeec |
child 56784 | 6210466cf1ac |
permissions | -rw-r--r-- |
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 |
} |
|
56717
e4fe7c97b1de
Use StackWalker and checking content type after decryption
xuelei
parents:
56712
diff
changeset
|
271 |
|
56694 | 272 |
if (contentType != ContentType.HANDSHAKE.id && |
273 |
handshakeBuffer != null && handshakeBuffer.hasRemaining()) { |
|
30904 | 274 |
throw new SSLProtocolException( |
56717
e4fe7c97b1de
Use StackWalker and checking content type after decryption
xuelei
parents:
56712
diff
changeset
|
275 |
"Expecting a handshake fragment, but received " + |
e4fe7c97b1de
Use StackWalker and checking content type after decryption
xuelei
parents:
56712
diff
changeset
|
276 |
ContentType.nameOf(contentType)); |
30904 | 277 |
} |
278 |
||
279 |
// |
|
56542 | 280 |
// parse handshake messages |
30904 | 281 |
// |
56542 | 282 |
if (contentType == ContentType.HANDSHAKE.id) { |
283 |
ByteBuffer handshakeFrag = fragment; |
|
284 |
if ((handshakeBuffer != null) && |
|
285 |
(handshakeBuffer.remaining() != 0)) { |
|
286 |
ByteBuffer bb = ByteBuffer.wrap(new byte[ |
|
287 |
handshakeBuffer.remaining() + fragment.remaining()]); |
|
288 |
bb.put(handshakeBuffer); |
|
289 |
bb.put(fragment); |
|
290 |
handshakeFrag = bb.rewind(); |
|
291 |
handshakeBuffer = null; |
|
292 |
} |
|
30904 | 293 |
|
56542 | 294 |
ArrayList<Plaintext> plaintexts = new ArrayList<>(5); |
295 |
while (handshakeFrag.hasRemaining()) { |
|
296 |
int remaining = handshakeFrag.remaining(); |
|
297 |
if (remaining < handshakeHeaderSize) { |
|
298 |
handshakeBuffer = ByteBuffer.wrap(new byte[remaining]); |
|
299 |
handshakeBuffer.put(handshakeFrag); |
|
300 |
handshakeBuffer.rewind(); |
|
301 |
break; |
|
30904 | 302 |
} |
303 |
||
56542 | 304 |
handshakeFrag.mark(); |
305 |
// skip the first byte: handshake type |
|
306 |
byte handshakeType = handshakeFrag.get(); |
|
307 |
int handshakeBodyLen = Record.getInt24(handshakeFrag); |
|
308 |
handshakeFrag.reset(); |
|
309 |
int handshakeMessageLen = |
|
310 |
handshakeHeaderSize + handshakeBodyLen; |
|
311 |
if (remaining < handshakeMessageLen) { |
|
312 |
handshakeBuffer = ByteBuffer.wrap(new byte[remaining]); |
|
313 |
handshakeBuffer.put(handshakeFrag); |
|
314 |
handshakeBuffer.rewind(); |
|
315 |
break; |
|
316 |
} if (remaining == handshakeMessageLen) { |
|
317 |
if (handshakeHash.isHashable(handshakeType)) { |
|
318 |
handshakeHash.receive(handshakeFrag); |
|
319 |
} |
|
30904 | 320 |
|
56542 | 321 |
plaintexts.add( |
322 |
new Plaintext(contentType, |
|
323 |
majorVersion, minorVersion, -1, -1L, handshakeFrag) |
|
324 |
); |
|
325 |
break; |
|
30904 | 326 |
} else { |
56542 | 327 |
int fragPos = handshakeFrag.position(); |
328 |
int fragLim = handshakeFrag.limit(); |
|
329 |
int nextPos = fragPos + handshakeMessageLen; |
|
330 |
handshakeFrag.limit(nextPos); |
|
331 |
||
332 |
if (handshakeHash.isHashable(handshakeType)) { |
|
333 |
handshakeHash.receive(handshakeFrag); |
|
30904 | 334 |
} |
335 |
||
56542 | 336 |
plaintexts.add( |
337 |
new Plaintext(contentType, majorVersion, minorVersion, |
|
338 |
-1, -1L, handshakeFrag.slice()) |
|
339 |
); |
|
30904 | 340 |
|
56542 | 341 |
handshakeFrag.position(nextPos); |
342 |
handshakeFrag.limit(fragLim); |
|
343 |
} |
|
30904 | 344 |
} |
56542 | 345 |
|
346 |
return plaintexts.toArray(new Plaintext[0]); |
|
30904 | 347 |
} |
348 |
||
56542 | 349 |
return new Plaintext[] { |
350 |
new Plaintext(contentType, |
|
351 |
majorVersion, minorVersion, -1, -1L, fragment) |
|
352 |
// recordEpoch, recordSeq, plaintext); |
|
353 |
}; |
|
30904 | 354 |
} |
355 |
||
56542 | 356 |
private Plaintext[] handleUnknownRecord( |
357 |
byte[] header) throws IOException, BadPaddingException { |
|
30904 | 358 |
byte firstByte = header[0]; |
359 |
byte thirdByte = header[2]; |
|
360 |
||
361 |
// Does it look like a Version 2 client hello (V2ClientHello)? |
|
362 |
if (((firstByte & 0x80) != 0) && (thirdByte == 1)) { |
|
363 |
/* |
|
364 |
* If SSLv2Hello is not enabled, throw an exception. |
|
365 |
*/ |
|
366 |
if (helloVersion != ProtocolVersion.SSL20Hello) { |
|
367 |
throw new SSLHandshakeException("SSLv2Hello is not enabled"); |
|
368 |
} |
|
369 |
||
370 |
byte majorVersion = header[3]; |
|
371 |
byte minorVersion = header[4]; |
|
372 |
||
373 |
if ((majorVersion == ProtocolVersion.SSL20Hello.major) && |
|
374 |
(minorVersion == ProtocolVersion.SSL20Hello.minor)) { |
|
375 |
||
376 |
/* |
|
377 |
* Looks like a V2 client hello, but not one saying |
|
378 |
* "let's talk SSLv3". So we need to send an SSLv2 |
|
379 |
* error message, one that's treated as fatal by |
|
380 |
* clients (Otherwise we'll hang.) |
|
381 |
*/ |
|
56542 | 382 |
os.write(SSLRecord.v2NoCipher); // SSLv2Hello |
30904 | 383 |
|
56542 | 384 |
if (SSLLogger.isOn) { |
385 |
if (SSLLogger.isOn("record")) { |
|
386 |
SSLLogger.fine( |
|
30904 | 387 |
"Requested to negotiate unsupported SSLv2!"); |
388 |
} |
|
389 |
||
56542 | 390 |
if (SSLLogger.isOn("packet")) { |
391 |
SSLLogger.fine("Raw write", SSLRecord.v2NoCipher); |
|
30904 | 392 |
} |
393 |
} |
|
394 |
||
395 |
throw new SSLException("Unsupported SSL v2.0 ClientHello"); |
|
396 |
} |
|
397 |
||
398 |
int msgLen = ((header[0] & 0x7F) << 8) | (header[1] & 0xFF); |
|
399 |
||
56542 | 400 |
ByteBuffer destination = ByteBuffer.allocate(headerSize + msgLen); |
30904 | 401 |
destination.put(temporary, 0, headerSize); |
402 |
msgLen -= 3; // had read 3 bytes of content as header |
|
403 |
while (msgLen > 0) { |
|
404 |
int howmuch = Math.min(temporary.length, msgLen); |
|
405 |
int really = read(is, temporary, 0, howmuch); |
|
406 |
if (really < 0) { |
|
407 |
throw new EOFException("SSL peer shut down incorrectly"); |
|
408 |
} |
|
409 |
||
410 |
destination.put(temporary, 0, howmuch); |
|
411 |
msgLen -= howmuch; |
|
412 |
} |
|
413 |
destination.flip(); |
|
414 |
||
415 |
/* |
|
416 |
* If we can map this into a V3 ClientHello, read and |
|
417 |
* hash the rest of the V2 handshake, turn it into a |
|
418 |
* V3 ClientHello message, and pass it up. |
|
419 |
*/ |
|
420 |
destination.position(2); // exclude the header |
|
56542 | 421 |
handshakeHash.receive(destination); |
30904 | 422 |
destination.position(0); |
423 |
||
424 |
ByteBuffer converted = convertToClientHello(destination); |
|
425 |
||
56542 | 426 |
if (SSLLogger.isOn && SSLLogger.isOn("packet")) { |
427 |
SSLLogger.fine( |
|
30904 | 428 |
"[Converted] ClientHello", converted); |
429 |
} |
|
430 |
||
56542 | 431 |
return new Plaintext[] { |
432 |
new Plaintext(ContentType.HANDSHAKE.id, |
|
433 |
majorVersion, minorVersion, -1, -1L, converted) |
|
434 |
}; |
|
30904 | 435 |
} else { |
436 |
if (((firstByte & 0x80) != 0) && (thirdByte == 4)) { |
|
437 |
throw new SSLException("SSL V2.0 servers are not supported."); |
|
438 |
} |
|
439 |
||
440 |
throw new SSLException("Unsupported or unrecognized SSL message"); |
|
441 |
} |
|
442 |
} |
|
443 |
||
444 |
// Read the exact bytes of data, otherwise, return -1. |
|
445 |
private static int read(InputStream is, |
|
446 |
byte[] buffer, int offset, int len) throws IOException { |
|
447 |
int n = 0; |
|
448 |
while (n < len) { |
|
449 |
int readLen = is.read(buffer, offset + n, len - n); |
|
450 |
if (readLen < 0) { |
|
56542 | 451 |
if (SSLLogger.isOn && SSLLogger.isOn("packet")) { |
452 |
SSLLogger.fine("Raw read: EOF"); |
|
453 |
} |
|
30904 | 454 |
return -1; |
455 |
} |
|
456 |
||
56542 | 457 |
if (SSLLogger.isOn && SSLLogger.isOn("packet")) { |
458 |
ByteBuffer bb = ByteBuffer.wrap(buffer, offset + n, readLen); |
|
459 |
SSLLogger.fine("Raw read", bb); |
|
30904 | 460 |
} |
461 |
||
462 |
n += readLen; |
|
463 |
} |
|
464 |
||
465 |
return n; |
|
466 |
} |
|
467 |
} |