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 |
|
56542
|
28 |
import java.io.ByteArrayInputStream;
|
|
29 |
import java.io.IOException;
|
|
30 |
import java.io.OutputStream;
|
|
31 |
import java.nio.ByteBuffer;
|
|
32 |
import javax.net.ssl.SSLHandshakeException;
|
30904
|
33 |
|
56542
|
34 |
import sun.security.ssl.KeyUpdate.KeyUpdateMessage;
|
30904
|
35 |
|
|
36 |
/**
|
|
37 |
* {@code OutputRecord} implementation for {@code SSLSocket}.
|
|
38 |
*/
|
|
39 |
final class SSLSocketOutputRecord extends OutputRecord implements SSLRecord {
|
|
40 |
private OutputStream deliverStream = null;
|
|
41 |
|
56542
|
42 |
SSLSocketOutputRecord(HandshakeHash handshakeHash) {
|
|
43 |
this(handshakeHash, null);
|
|
44 |
}
|
30904
|
45 |
|
56542
|
46 |
SSLSocketOutputRecord(HandshakeHash handshakeHash,
|
|
47 |
TransportContext tc) {
|
|
48 |
super(handshakeHash, SSLCipher.SSLWriteCipher.nullTlsWriteCipher());
|
|
49 |
this.tc = tc;
|
30904
|
50 |
this.packetSize = SSLRecord.maxRecordSize;
|
56542
|
51 |
this.protocolVersion = ProtocolVersion.NONE;
|
30904
|
52 |
}
|
|
53 |
|
|
54 |
@Override
|
|
55 |
void encodeAlert(byte level, byte description) throws IOException {
|
|
56 |
// use the buf of ByteArrayOutputStream
|
|
57 |
int position = headerSize + writeCipher.getExplicitNonceSize();
|
|
58 |
count = position;
|
|
59 |
|
|
60 |
write(level);
|
|
61 |
write(description);
|
56542
|
62 |
if (SSLLogger.isOn && SSLLogger.isOn("record")) {
|
|
63 |
SSLLogger.fine("WRITE: " + protocolVersion +
|
|
64 |
" " + ContentType.ALERT.name +
|
30904
|
65 |
", length = " + (count - headerSize));
|
|
66 |
}
|
|
67 |
|
|
68 |
// Encrypt the fragment and wrap up a record.
|
56542
|
69 |
encrypt(writeCipher, ContentType.ALERT.id, headerSize);
|
30904
|
70 |
|
|
71 |
// deliver this message
|
|
72 |
deliverStream.write(buf, 0, count); // may throw IOException
|
|
73 |
deliverStream.flush(); // may throw IOException
|
|
74 |
|
56542
|
75 |
if (SSLLogger.isOn && SSLLogger.isOn("packet")) {
|
|
76 |
SSLLogger.fine("Raw write",
|
|
77 |
(new ByteArrayInputStream(buf, 0, count)));
|
30904
|
78 |
}
|
|
79 |
|
|
80 |
// reset the internal buffer
|
|
81 |
count = 0;
|
|
82 |
}
|
|
83 |
|
|
84 |
@Override
|
|
85 |
void encodeHandshake(byte[] source,
|
|
86 |
int offset, int length) throws IOException {
|
|
87 |
if (firstMessage) {
|
|
88 |
firstMessage = false;
|
|
89 |
|
|
90 |
if ((helloVersion == ProtocolVersion.SSL20Hello) &&
|
56542
|
91 |
(source[offset] == SSLHandshake.CLIENT_HELLO.id) &&
|
30904
|
92 |
// 5: recode header size
|
|
93 |
(source[offset + 4 + 2 + 32] == 0)) {
|
|
94 |
// V3 session ID is empty
|
|
95 |
// 4: handshake header size
|
|
96 |
// 2: client_version in ClientHello
|
|
97 |
// 32: random in ClientHello
|
|
98 |
|
|
99 |
ByteBuffer v2ClientHello = encodeV2ClientHello(
|
|
100 |
source, (offset + 4), (length - 4));
|
|
101 |
|
|
102 |
byte[] record = v2ClientHello.array(); // array offset is zero
|
|
103 |
int limit = v2ClientHello.limit();
|
56542
|
104 |
handshakeHash.deliver(record, 2, (limit - 2));
|
30904
|
105 |
|
56542
|
106 |
if (SSLLogger.isOn && SSLLogger.isOn("record")) {
|
|
107 |
SSLLogger.fine(
|
|
108 |
"WRITE: SSLv2 ClientHello message" +
|
|
109 |
", length = " + limit);
|
30904
|
110 |
}
|
|
111 |
|
|
112 |
// deliver this message
|
|
113 |
//
|
|
114 |
// Version 2 ClientHello message should be plaintext.
|
|
115 |
//
|
|
116 |
// No max fragment length negotiation.
|
|
117 |
deliverStream.write(record, 0, limit);
|
|
118 |
deliverStream.flush();
|
|
119 |
|
56542
|
120 |
if (SSLLogger.isOn && SSLLogger.isOn("packet")) {
|
|
121 |
SSLLogger.fine("Raw write",
|
|
122 |
(new ByteArrayInputStream(record, 0, limit)));
|
30904
|
123 |
}
|
|
124 |
|
|
125 |
return;
|
|
126 |
}
|
|
127 |
}
|
|
128 |
|
|
129 |
byte handshakeType = source[0];
|
56542
|
130 |
if (handshakeHash.isHashable(handshakeType)) {
|
|
131 |
handshakeHash.deliver(source, offset, length);
|
30904
|
132 |
}
|
|
133 |
|
|
134 |
int fragLimit = getFragLimit();
|
|
135 |
int position = headerSize + writeCipher.getExplicitNonceSize();
|
|
136 |
if (count == 0) {
|
|
137 |
count = position;
|
|
138 |
}
|
|
139 |
|
|
140 |
if ((count - position) < (fragLimit - length)) {
|
|
141 |
write(source, offset, length);
|
|
142 |
return;
|
|
143 |
}
|
|
144 |
|
|
145 |
for (int limit = (offset + length); offset < limit;) {
|
|
146 |
|
|
147 |
int remains = (limit - offset) + (count - position);
|
|
148 |
int fragLen = Math.min(fragLimit, remains);
|
|
149 |
|
|
150 |
// use the buf of ByteArrayOutputStream
|
|
151 |
write(source, offset, fragLen);
|
|
152 |
if (remains < fragLimit) {
|
|
153 |
return;
|
|
154 |
}
|
|
155 |
|
56542
|
156 |
if (SSLLogger.isOn && SSLLogger.isOn("record")) {
|
|
157 |
SSLLogger.fine(
|
|
158 |
"WRITE: " + protocolVersion +
|
|
159 |
" " + ContentType.HANDSHAKE.name +
|
30904
|
160 |
", length = " + (count - headerSize));
|
|
161 |
}
|
|
162 |
|
|
163 |
// Encrypt the fragment and wrap up a record.
|
56542
|
164 |
encrypt(writeCipher, ContentType.HANDSHAKE.id, headerSize);
|
30904
|
165 |
|
|
166 |
// deliver this message
|
|
167 |
deliverStream.write(buf, 0, count); // may throw IOException
|
|
168 |
deliverStream.flush(); // may throw IOException
|
|
169 |
|
56542
|
170 |
if (SSLLogger.isOn && SSLLogger.isOn("packet")) {
|
|
171 |
SSLLogger.fine("Raw write",
|
|
172 |
(new ByteArrayInputStream(buf, 0, count)));
|
30904
|
173 |
}
|
|
174 |
|
|
175 |
// reset the offset
|
|
176 |
offset += fragLen;
|
|
177 |
|
|
178 |
// reset the internal buffer
|
|
179 |
count = position;
|
|
180 |
}
|
|
181 |
}
|
|
182 |
|
|
183 |
@Override
|
|
184 |
void encodeChangeCipherSpec() throws IOException {
|
|
185 |
|
|
186 |
// use the buf of ByteArrayOutputStream
|
|
187 |
int position = headerSize + writeCipher.getExplicitNonceSize();
|
|
188 |
count = position;
|
|
189 |
|
|
190 |
write((byte)1); // byte 1: change_cipher_spec(
|
|
191 |
|
|
192 |
// Encrypt the fragment and wrap up a record.
|
56542
|
193 |
encrypt(writeCipher, ContentType.CHANGE_CIPHER_SPEC.id, headerSize);
|
30904
|
194 |
|
|
195 |
// deliver this message
|
|
196 |
deliverStream.write(buf, 0, count); // may throw IOException
|
|
197 |
// deliverStream.flush(); // flush in Finished
|
|
198 |
|
56542
|
199 |
if (SSLLogger.isOn && SSLLogger.isOn("packet")) {
|
|
200 |
SSLLogger.fine("Raw write",
|
|
201 |
(new ByteArrayInputStream(buf, 0, count)));
|
30904
|
202 |
}
|
|
203 |
|
|
204 |
// reset the internal buffer
|
|
205 |
count = 0;
|
|
206 |
}
|
|
207 |
|
|
208 |
@Override
|
|
209 |
public void flush() throws IOException {
|
|
210 |
int position = headerSize + writeCipher.getExplicitNonceSize();
|
|
211 |
if (count <= position) {
|
|
212 |
return;
|
|
213 |
}
|
|
214 |
|
56542
|
215 |
if (SSLLogger.isOn && SSLLogger.isOn("record")) {
|
|
216 |
SSLLogger.fine(
|
|
217 |
"WRITE: " + protocolVersion +
|
|
218 |
" " + ContentType.HANDSHAKE.name +
|
30904
|
219 |
", length = " + (count - headerSize));
|
|
220 |
}
|
|
221 |
|
|
222 |
// Encrypt the fragment and wrap up a record.
|
56542
|
223 |
encrypt(writeCipher, ContentType.HANDSHAKE.id, headerSize);
|
30904
|
224 |
|
|
225 |
// deliver this message
|
|
226 |
deliverStream.write(buf, 0, count); // may throw IOException
|
|
227 |
deliverStream.flush(); // may throw IOException
|
|
228 |
|
56542
|
229 |
if (SSLLogger.isOn && SSLLogger.isOn("packet")) {
|
|
230 |
SSLLogger.fine("Raw write",
|
|
231 |
(new ByteArrayInputStream(buf, 0, count)));
|
30904
|
232 |
}
|
|
233 |
|
|
234 |
// reset the internal buffer
|
|
235 |
count = 0; // DON'T use position
|
|
236 |
}
|
|
237 |
|
|
238 |
@Override
|
|
239 |
void deliver(byte[] source, int offset, int length) throws IOException {
|
56542
|
240 |
if (writeCipher.authenticator.seqNumOverflow()) {
|
|
241 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
|
|
242 |
SSLLogger.fine(
|
|
243 |
"sequence number extremely close to overflow " +
|
30904
|
244 |
"(2^64-1 packets). Closing connection.");
|
|
245 |
}
|
|
246 |
|
|
247 |
throw new SSLHandshakeException("sequence number overflow");
|
|
248 |
}
|
|
249 |
|
|
250 |
boolean isFirstRecordOfThePayload = true;
|
|
251 |
for (int limit = (offset + length); offset < limit;) {
|
|
252 |
int fragLen;
|
|
253 |
if (packetSize > 0) {
|
|
254 |
fragLen = Math.min(maxRecordSize, packetSize);
|
56542
|
255 |
fragLen =
|
|
256 |
writeCipher.calculateFragmentSize(fragLen, headerSize);
|
30904
|
257 |
|
|
258 |
fragLen = Math.min(fragLen, Record.maxDataSize);
|
|
259 |
} else {
|
|
260 |
fragLen = Record.maxDataSize;
|
|
261 |
}
|
|
262 |
|
|
263 |
if (fragmentSize > 0) {
|
|
264 |
fragLen = Math.min(fragLen, fragmentSize);
|
|
265 |
}
|
|
266 |
|
|
267 |
if (isFirstRecordOfThePayload && needToSplitPayload()) {
|
|
268 |
fragLen = 1;
|
|
269 |
isFirstRecordOfThePayload = false;
|
|
270 |
} else {
|
|
271 |
fragLen = Math.min(fragLen, (limit - offset));
|
|
272 |
}
|
|
273 |
|
|
274 |
// use the buf of ByteArrayOutputStream
|
|
275 |
int position = headerSize + writeCipher.getExplicitNonceSize();
|
|
276 |
count = position;
|
|
277 |
write(source, offset, fragLen);
|
|
278 |
|
56542
|
279 |
if (SSLLogger.isOn && SSLLogger.isOn("record")) {
|
|
280 |
SSLLogger.fine(
|
|
281 |
"WRITE: " + protocolVersion +
|
|
282 |
" " + ContentType.APPLICATION_DATA.name +
|
|
283 |
", length = " + (count - position));
|
30904
|
284 |
}
|
|
285 |
|
|
286 |
// Encrypt the fragment and wrap up a record.
|
56542
|
287 |
encrypt(writeCipher, ContentType.APPLICATION_DATA.id, headerSize);
|
30904
|
288 |
|
|
289 |
// deliver this message
|
|
290 |
deliverStream.write(buf, 0, count); // may throw IOException
|
|
291 |
deliverStream.flush(); // may throw IOException
|
|
292 |
|
56542
|
293 |
if (SSLLogger.isOn && SSLLogger.isOn("packet")) {
|
|
294 |
SSLLogger.fine("Raw write",
|
|
295 |
(new ByteArrayInputStream(buf, 0, count)));
|
30904
|
296 |
}
|
|
297 |
|
|
298 |
// reset the internal buffer
|
|
299 |
count = 0;
|
|
300 |
|
|
301 |
if (isFirstAppOutputRecord) {
|
|
302 |
isFirstAppOutputRecord = false;
|
|
303 |
}
|
|
304 |
|
|
305 |
offset += fragLen;
|
56542
|
306 |
|
|
307 |
if (writeCipher.atKeyLimit()) {
|
|
308 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) {
|
|
309 |
SSLLogger.fine("KeyUpdate: triggered");
|
|
310 |
}
|
|
311 |
|
|
312 |
PostHandshakeContext p = new PostHandshakeContext(tc);
|
|
313 |
KeyUpdate.handshakeProducer.produce(p,
|
|
314 |
new KeyUpdateMessage(p, KeyUpdateMessage.REQUSTED));
|
|
315 |
}
|
30904
|
316 |
}
|
|
317 |
}
|
|
318 |
|
|
319 |
@Override
|
|
320 |
void setDeliverStream(OutputStream outputStream) {
|
|
321 |
this.deliverStream = outputStream;
|
|
322 |
}
|
|
323 |
|
|
324 |
/*
|
|
325 |
* Need to split the payload except the following cases:
|
|
326 |
*
|
|
327 |
* 1. protocol version is TLS 1.1 or later;
|
|
328 |
* 2. bulk cipher does not use CBC mode, including null bulk cipher suites.
|
|
329 |
* 3. the payload is the first application record of a freshly
|
|
330 |
* negotiated TLS session.
|
|
331 |
* 4. the CBC protection is disabled;
|
|
332 |
*
|
|
333 |
* By default, we counter chosen plaintext issues on CBC mode
|
|
334 |
* ciphersuites in SSLv3/TLS1.0 by sending one byte of application
|
|
335 |
* data in the first record of every payload, and the rest in
|
|
336 |
* subsequent record(s). Note that the issues have been solved in
|
|
337 |
* TLS 1.1 or later.
|
|
338 |
*
|
|
339 |
* It is not necessary to split the very first application record of
|
|
340 |
* a freshly negotiated TLS session, as there is no previous
|
|
341 |
* application data to guess. To improve compatibility, we will not
|
|
342 |
* split such records.
|
|
343 |
*
|
|
344 |
* This avoids issues in the outbound direction. For a full fix,
|
|
345 |
* the peer must have similar protections.
|
|
346 |
*/
|
|
347 |
boolean needToSplitPayload() {
|
|
348 |
return (!protocolVersion.useTLS11PlusSpec()) &&
|
|
349 |
writeCipher.isCBCMode() && !isFirstAppOutputRecord &&
|
|
350 |
Record.enableCBCProtection;
|
|
351 |
}
|
|
352 |
|
|
353 |
private int getFragLimit() {
|
|
354 |
int fragLimit;
|
|
355 |
if (packetSize > 0) {
|
|
356 |
fragLimit = Math.min(maxRecordSize, packetSize);
|
56542
|
357 |
fragLimit =
|
|
358 |
writeCipher.calculateFragmentSize(fragLimit, headerSize);
|
30904
|
359 |
|
|
360 |
fragLimit = Math.min(fragLimit, Record.maxDataSize);
|
|
361 |
} else {
|
|
362 |
fragLimit = Record.maxDataSize;
|
|
363 |
}
|
|
364 |
|
|
365 |
if (fragmentSize > 0) {
|
|
366 |
fragLimit = Math.min(fragLimit, fragmentSize);
|
|
367 |
}
|
|
368 |
|
|
369 |
return fragLimit;
|
|
370 |
}
|
|
371 |
}
|