author | wetmore |
Fri, 11 May 2018 15:53:12 -0700 | |
branch | JDK-8145252-TLS13-branch |
changeset 56542 | 56aaa6cb3693 |
parent 47216 | 71c04702a3d5 |
child 56694 | aa54a1f8e426 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
56542 | 2 |
* Copyright (c) 1996, 2018, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.security.ssl; |
|
27 |
||
28 |
import java.io.*; |
|
29 |
import java.nio.*; |
|
30 |
import javax.crypto.BadPaddingException; |
|
56542 | 31 |
import sun.security.ssl.SSLCipher.SSLReadCipher; |
2 | 32 |
|
33 |
/** |
|
30904 | 34 |
* {@code InputRecord} takes care of the management of SSL/TLS/DTLS input |
35 |
* records, including buffering, decryption, handshake messages marshal, etc. |
|
2 | 36 |
* |
37 |
* @author David Brownell |
|
38 |
*/ |
|
56542 | 39 |
abstract class InputRecord implements Record, Closeable { |
40 |
SSLReadCipher readCipher; |
|
2 | 41 |
|
56542 | 42 |
final HandshakeHash handshakeHash; |
30904 | 43 |
boolean isClosed; |
2 | 44 |
|
45 |
// The ClientHello version to accept. If set to ProtocolVersion.SSL20Hello |
|
46 |
// and the first message we read is a ClientHello in V2 format, we convert |
|
47 |
// it to V3. Otherwise we throw an exception when encountering a V2 hello. |
|
30904 | 48 |
ProtocolVersion helloVersion; |
2 | 49 |
|
30904 | 50 |
// fragment size |
51 |
int fragmentSize; |
|
2 | 52 |
|
56542 | 53 |
InputRecord(HandshakeHash handshakeHash, SSLReadCipher readCipher) { |
54 |
this.readCipher = readCipher; |
|
55 |
this.helloVersion = ProtocolVersion.TLS10; |
|
56 |
this.handshakeHash = handshakeHash; |
|
57 |
this.isClosed = false; |
|
30904 | 58 |
this.fragmentSize = Record.maxDataSize; |
2 | 59 |
} |
60 |
||
61 |
void setHelloVersion(ProtocolVersion helloVersion) { |
|
62 |
this.helloVersion = helloVersion; |
|
63 |
} |
|
64 |
||
65 |
ProtocolVersion getHelloVersion() { |
|
66 |
return helloVersion; |
|
67 |
} |
|
68 |
||
30904 | 69 |
boolean seqNumIsHuge() { |
56542 | 70 |
return (readCipher.authenticator != null) && |
71 |
readCipher.authenticator.seqNumIsHuge(); |
|
30904 | 72 |
} |
73 |
||
74 |
boolean isEmpty() { |
|
75 |
return false; |
|
76 |
} |
|
77 |
||
78 |
// apply to DTLS SSLEngine |
|
79 |
void expectingFinishFlight() { |
|
80 |
// blank |
|
81 |
} |
|
82 |
||
56542 | 83 |
// apply to DTLS SSLEngine |
84 |
void finishHandshake() { |
|
85 |
// blank |
|
86 |
} |
|
87 |
||
30904 | 88 |
/** |
89 |
* Prevent any more data from being read into this record, |
|
90 |
* and flag the record as holding no data. |
|
91 |
*/ |
|
92 |
@Override |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
30904
diff
changeset
|
93 |
public synchronized void close() throws IOException { |
30904 | 94 |
if (!isClosed) { |
95 |
isClosed = true; |
|
96 |
readCipher.dispose(); |
|
97 |
} |
|
98 |
} |
|
99 |
||
56542 | 100 |
synchronized boolean isClosed() { |
101 |
return isClosed; |
|
102 |
} |
|
103 |
||
30904 | 104 |
// apply to SSLSocket and SSLEngine |
56542 | 105 |
void changeReadCiphers(SSLReadCipher readCipher) { |
30904 | 106 |
|
107 |
/* |
|
108 |
* Dispose of any intermediate state in the underlying cipher. |
|
109 |
* For PKCS11 ciphers, this will release any attached sessions, |
|
110 |
* and thus make finalization faster. |
|
111 |
* |
|
112 |
* Since MAC's doFinal() is called for every SSL/TLS packet, it's |
|
113 |
* not necessary to do the same with MAC's. |
|
114 |
*/ |
|
115 |
readCipher.dispose(); |
|
116 |
||
117 |
this.readCipher = readCipher; |
|
118 |
} |
|
119 |
||
120 |
// change fragment size |
|
121 |
void changeFragmentSize(int fragmentSize) { |
|
122 |
this.fragmentSize = fragmentSize; |
|
123 |
} |
|
124 |
||
125 |
/* |
|
126 |
* Check if there is enough inbound data in the ByteBuffer to make |
|
127 |
* a inbound packet. |
|
128 |
* |
|
129 |
* @return -1 if there are not enough bytes to tell (small header), |
|
130 |
*/ |
|
131 |
// apply to SSLEngine only |
|
56542 | 132 |
int bytesInCompletePacket( |
133 |
ByteBuffer[] srcs, int srcsOffset, int srcsLength) throws IOException { |
|
134 |
||
135 |
throw new UnsupportedOperationException("Not supported yet."); |
|
136 |
} |
|
137 |
||
138 |
// apply to SSLSocket only |
|
139 |
int bytesInCompletePacket() throws IOException { |
|
30904 | 140 |
throw new UnsupportedOperationException(); |
141 |
} |
|
142 |
||
143 |
// apply to SSLSocket only |
|
56542 | 144 |
void setReceiverStream(InputStream inputStream) { |
30904 | 145 |
throw new UnsupportedOperationException(); |
146 |
} |
|
147 |
||
148 |
// apply to DTLS SSLEngine only |
|
149 |
Plaintext acquirePlaintext() |
|
150 |
throws IOException, BadPaddingException { |
|
151 |
throw new UnsupportedOperationException(); |
|
152 |
} |
|
153 |
||
154 |
// read, decrypt and decompress the network record. |
|
155 |
// |
|
56542 | 156 |
abstract Plaintext[] decode(ByteBuffer[] srcs, int srcsOffset, |
157 |
int srcsLength) throws IOException, BadPaddingException; |
|
30904 | 158 |
|
159 |
// apply to SSLSocket only |
|
160 |
void setDeliverStream(OutputStream outputStream) { |
|
161 |
throw new UnsupportedOperationException(); |
|
162 |
} |
|
163 |
||
164 |
// calculate plaintext fragment size |
|
165 |
// |
|
166 |
// apply to SSLEngine only |
|
167 |
int estimateFragmentSize(int packetSize) { |
|
168 |
throw new UnsupportedOperationException(); |
|
2 | 169 |
} |
170 |
||
30904 | 171 |
// |
172 |
// shared helpers |
|
173 |
// |
|
174 |
||
175 |
// Not apply to DTLS |
|
176 |
static ByteBuffer convertToClientHello(ByteBuffer packet) { |
|
177 |
int srcPos = packet.position(); |
|
178 |
||
179 |
byte firstByte = packet.get(); |
|
180 |
byte secondByte = packet.get(); |
|
181 |
int recordLen = (((firstByte & 0x7F) << 8) | (secondByte & 0xFF)) + 2; |
|
182 |
||
183 |
packet.position(srcPos + 3); // the V2ClientHello record header |
|
184 |
||
185 |
byte majorVersion = packet.get(); |
|
186 |
byte minorVersion = packet.get(); |
|
187 |
||
188 |
int cipherSpecLen = ((packet.get() & 0xFF) << 8) + |
|
189 |
(packet.get() & 0xFF); |
|
190 |
int sessionIdLen = ((packet.get() & 0xFF) << 8) + |
|
191 |
(packet.get() & 0xFF); |
|
192 |
int nonceLen = ((packet.get() & 0xFF) << 8) + |
|
193 |
(packet.get() & 0xFF); |
|
194 |
||
195 |
// Required space for the target SSLv3 ClientHello message. |
|
196 |
// 5: record header size |
|
197 |
// 4: handshake header size |
|
198 |
// 2: ClientHello.client_version |
|
199 |
// 32: ClientHello.random |
|
200 |
// 1: length byte of ClientHello.session_id |
|
43008
c6c74a38f1ad
8172273: SSLEngine.unwrap fails with ArrayIndexOutOfBoundsException
xuelei
parents:
34687
diff
changeset
|
201 |
// 2: length bytes of ClientHello.cipher_suites |
30904 | 202 |
// 2: empty ClientHello.compression_methods |
56542 | 203 |
int requiredSize = 48 + sessionIdLen + ((cipherSpecLen * 2 ) / 3); |
30904 | 204 |
byte[] converted = new byte[requiredSize]; |
205 |
||
206 |
/* |
|
207 |
* Build the first part of the V3 record header from the V2 one |
|
208 |
* that's now buffered up. (Lengths are fixed up later). |
|
209 |
*/ |
|
210 |
// Note: need not to set the header actually. |
|
56542 | 211 |
converted[0] = ContentType.HANDSHAKE.id; |
30904 | 212 |
converted[1] = majorVersion; |
213 |
converted[2] = minorVersion; |
|
214 |
// header [3..4] for handshake message length |
|
215 |
// required size is 5; |
|
216 |
||
217 |
/* |
|
218 |
* Store the generic V3 handshake header: 4 bytes |
|
219 |
*/ |
|
220 |
converted[5] = 1; // HandshakeMessage.ht_client_hello |
|
221 |
// buf [6..8] for length of ClientHello (int24) |
|
222 |
// required size += 4; |
|
223 |
||
224 |
/* |
|
225 |
* ClientHello header starts with SSL version |
|
226 |
*/ |
|
227 |
converted[9] = majorVersion; |
|
228 |
converted[10] = minorVersion; |
|
229 |
// required size += 2; |
|
230 |
int pointer = 11; |
|
231 |
||
232 |
/* |
|
233 |
* Copy Random value/nonce ... if less than the 32 bytes of |
|
234 |
* a V3 "Random", right justify and zero pad to the left. Else |
|
235 |
* just take the last 32 bytes. |
|
236 |
*/ |
|
237 |
int offset = srcPos + 11 + cipherSpecLen + sessionIdLen; |
|
238 |
||
239 |
if (nonceLen < 32) { |
|
240 |
for (int i = 0; i < (32 - nonceLen); i++) { |
|
241 |
converted[pointer++] = 0; |
|
242 |
} |
|
243 |
packet.position(offset); |
|
244 |
packet.get(converted, pointer, nonceLen); |
|
245 |
||
246 |
pointer += nonceLen; |
|
247 |
} else { |
|
248 |
packet.position(offset + nonceLen - 32); |
|
249 |
packet.get(converted, pointer, 32); |
|
250 |
||
251 |
pointer += 32; |
|
252 |
} |
|
253 |
||
254 |
/* |
|
255 |
* Copy session ID (only one byte length!) |
|
256 |
*/ |
|
257 |
offset -= sessionIdLen; |
|
258 |
converted[pointer++] = (byte)(sessionIdLen & 0xFF); |
|
259 |
packet.position(offset); |
|
260 |
packet.get(converted, pointer, sessionIdLen); |
|
261 |
||
262 |
/* |
|
263 |
* Copy and translate cipher suites ... V2 specs with first byte zero |
|
264 |
* are really V3 specs (in the last 2 bytes), just copy those and drop |
|
265 |
* the other ones. Preference order remains unchanged. |
|
266 |
* |
|
267 |
* Example: Netscape Navigator 3.0 (exportable) says: |
|
268 |
* |
|
269 |
* 0/3, SSL_RSA_EXPORT_WITH_RC4_40_MD5 |
|
270 |
* 0/6, SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5 |
|
271 |
* |
|
272 |
* Microsoft Internet Explorer 3.0 (exportable) supports only |
|
273 |
* |
|
274 |
* 0/3, SSL_RSA_EXPORT_WITH_RC4_40_MD5 |
|
275 |
*/ |
|
276 |
int j; |
|
277 |
||
278 |
offset -= cipherSpecLen; |
|
279 |
packet.position(offset); |
|
280 |
||
281 |
j = pointer + 2; |
|
282 |
for (int i = 0; i < cipherSpecLen; i += 3) { |
|
283 |
if (packet.get() != 0) { |
|
284 |
// Ignore version 2.0 specifix cipher suite. Clients |
|
285 |
// should also include the version 3.0 equivalent in |
|
286 |
// the V2ClientHello message. |
|
287 |
packet.get(); // ignore the 2nd byte |
|
288 |
packet.get(); // ignore the 3rd byte |
|
289 |
continue; |
|
290 |
} |
|
291 |
||
292 |
converted[j++] = packet.get(); |
|
293 |
converted[j++] = packet.get(); |
|
294 |
} |
|
295 |
||
296 |
j -= pointer + 2; |
|
297 |
converted[pointer++] = (byte)((j >>> 8) & 0xFF); |
|
298 |
converted[pointer++] = (byte)(j & 0xFF); |
|
299 |
pointer += j; |
|
300 |
||
301 |
/* |
|
302 |
* Append compression methods (default/null only) |
|
303 |
*/ |
|
304 |
converted[pointer++] = 1; |
|
305 |
converted[pointer++] = 0; // Session.compression_null |
|
306 |
||
307 |
/* |
|
308 |
* Fill in lengths of the messages we synthesized (nested: |
|
309 |
* V3 handshake message within V3 record). |
|
310 |
*/ |
|
311 |
// Note: need not to set the header actually. |
|
312 |
int fragLen = pointer - 5; // TLSPlaintext.length |
|
313 |
converted[3] = (byte)((fragLen >>> 8) & 0xFF); |
|
314 |
converted[4] = (byte)(fragLen & 0xFF); |
|
315 |
||
316 |
/* |
|
317 |
* Handshake.length, length of ClientHello message |
|
318 |
*/ |
|
319 |
fragLen = pointer - 9; // Handshake.length |
|
320 |
converted[6] = (byte)((fragLen >>> 16) & 0xFF); |
|
321 |
converted[7] = (byte)((fragLen >>> 8) & 0xFF); |
|
322 |
converted[8] = (byte)(fragLen & 0xFF); |
|
323 |
||
324 |
// consume the full record |
|
325 |
packet.position(srcPos + recordLen); |
|
326 |
||
327 |
// Need no header bytes. |
|
328 |
return ByteBuffer.wrap(converted, 5, pointer - 5); // 5: header size |
|
329 |
} |
|
330 |
||
56542 | 331 |
// Extract an SSL/(D)TLS record from the specified source buffers. |
332 |
static ByteBuffer extract( |
|
333 |
ByteBuffer[] buffers, int offset, int length, int headerSize) { |
|
2 | 334 |
|
56542 | 335 |
boolean hasFullHeader = false; |
336 |
int contentLen = -1; |
|
337 |
for (int i = offset, j = 0; |
|
338 |
i < (offset + length) && j < headerSize; i++) { |
|
339 |
int remains = buffers[i].remaining(); |
|
340 |
int pos = buffers[i].position(); |
|
341 |
for (int k = 0; k < remains && j < headerSize; j++, k++) { |
|
342 |
byte b = buffers[i].get(pos + k); |
|
343 |
if (j == (headerSize - 2)) { |
|
344 |
contentLen = ((b & 0xFF) << 8); |
|
345 |
} else if (j == (headerSize -1)) { |
|
346 |
contentLen |= (b & 0xFF); |
|
347 |
hasFullHeader = true; |
|
348 |
break; |
|
16113 | 349 |
} |
350 |
} |
|
2 | 351 |
} |
352 |
||
56542 | 353 |
if (!hasFullHeader) { |
354 |
throw new BufferUnderflowException(); |
|
30904 | 355 |
} |
356 |
||
56542 | 357 |
int packetLen = headerSize + contentLen; |
358 |
int remains = 0; |
|
359 |
for (int i = offset; i < offset + length; i++) { |
|
360 |
remains += buffers[i].remaining(); |
|
361 |
if (remains >= packetLen) { |
|
362 |
break; |
|
30904 | 363 |
} |
364 |
} |
|
365 |
||
56542 | 366 |
if (remains < packetLen) { |
367 |
throw new BufferUnderflowException(); |
|
2 | 368 |
} |
369 |
||
56542 | 370 |
byte[] packet = new byte[packetLen]; |
371 |
int packetOffset = 0; |
|
372 |
int packetSpaces = packetLen; |
|
373 |
for (int i = offset; i < offset + length; i++) { |
|
374 |
if (buffers[i].hasRemaining()) { |
|
375 |
int len = Math.min(packetSpaces, buffers[i].remaining()); |
|
376 |
buffers[i].get(packet, packetOffset, len); |
|
377 |
packetOffset += len; |
|
378 |
packetSpaces -= len; |
|
379 |
} |
|
16113 | 380 |
|
56542 | 381 |
if (packetSpaces <= 0) { |
382 |
break; |
|
2 | 383 |
} |
384 |
} |
|
16113 | 385 |
|
56542 | 386 |
return ByteBuffer.wrap(packet); |
2 | 387 |
} |
30904 | 388 |
} |