author | xuelei |
Sat, 24 Nov 2012 04:09:19 -0800 | |
changeset 14664 | e71aa0962e70 |
parent 12428 | e9feb65d37fa |
child 16113 | 946ec9b22004 |
child 16045 | 9d08c3b9a6a0 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
12428 | 2 |
* Copyright (c) 2003, 2012, 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 |
||
27 |
package sun.security.ssl; |
|
28 |
||
29 |
import java.io.*; |
|
30 |
import java.nio.*; |
|
31 |
||
32 |
||
33 |
/** |
|
34 |
* A OutputRecord class extension which uses external ByteBuffers |
|
35 |
* or the internal ByteArrayOutputStream for data manipulations. |
|
36 |
* <P> |
|
37 |
* Instead of rewriting this entire class |
|
38 |
* to use ByteBuffers, we leave things intact, so handshake, CCS, |
|
39 |
* and alerts will continue to use the internal buffers, but application |
|
40 |
* data will use external buffers. |
|
41 |
* |
|
42 |
* @author Brad Wetmore |
|
43 |
*/ |
|
44 |
final class EngineOutputRecord extends OutputRecord { |
|
45 |
||
10915 | 46 |
private SSLEngineImpl engine; |
2 | 47 |
private EngineWriter writer; |
48 |
||
49 |
private boolean finishedMsg = false; |
|
50 |
||
51 |
/* |
|
52 |
* All handshake hashing is done by the superclass |
|
53 |
*/ |
|
54 |
||
55 |
/* |
|
56 |
* Default constructor makes a record supporting the maximum |
|
57 |
* SSL record size. It allocates the header bytes directly. |
|
58 |
* |
|
59 |
* @param type the content type for the record |
|
60 |
*/ |
|
61 |
EngineOutputRecord(byte type, SSLEngineImpl engine) { |
|
62 |
super(type, recordSize(type)); |
|
10915 | 63 |
this.engine = engine; |
2 | 64 |
writer = engine.writer; |
65 |
} |
|
66 |
||
67 |
/** |
|
68 |
* Get the size of the buffer we need for records of the specified |
|
69 |
* type. |
|
70 |
* <P> |
|
71 |
* Application data buffers will provide their own byte buffers, |
|
72 |
* and will not use the internal byte caching. |
|
73 |
*/ |
|
74 |
private static int recordSize(byte type) { |
|
75 |
switch (type) { |
|
76 |
||
77 |
case ct_change_cipher_spec: |
|
78 |
case ct_alert: |
|
79 |
return maxAlertRecordSize; |
|
80 |
||
81 |
case ct_handshake: |
|
82 |
return maxRecordSize; |
|
83 |
||
84 |
case ct_application_data: |
|
85 |
return 0; |
|
86 |
} |
|
87 |
||
88 |
throw new RuntimeException("Unknown record type: " + type); |
|
89 |
} |
|
90 |
||
91 |
void setFinishedMsg() { |
|
92 |
finishedMsg = true; |
|
93 |
} |
|
94 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
12428
diff
changeset
|
95 |
@Override |
2 | 96 |
public void flush() throws IOException { |
97 |
finishedMsg = false; |
|
98 |
} |
|
99 |
||
100 |
boolean isFinishedMsg() { |
|
101 |
return finishedMsg; |
|
102 |
} |
|
103 |
||
104 |
||
105 |
/** |
|
106 |
* Calculate the MAC value, storing the result either in |
|
107 |
* the internal buffer, or at the end of the destination |
|
108 |
* ByteBuffer. |
|
109 |
* <P> |
|
110 |
* We assume that the higher levels have assured us enough |
|
111 |
* room, otherwise we'll indirectly throw a |
|
112 |
* BufferOverFlowException runtime exception. |
|
113 |
* |
|
114 |
* position should equal limit, and points to the next |
|
115 |
* free spot. |
|
116 |
*/ |
|
117 |
private void addMAC(MAC signer, ByteBuffer bb) |
|
118 |
throws IOException { |
|
119 |
||
120 |
if (signer.MAClen() != 0) { |
|
121 |
byte[] hash = signer.compute(contentType(), bb); |
|
122 |
||
123 |
/* |
|
124 |
* position was advanced to limit in compute above. |
|
125 |
* |
|
126 |
* Mark next area as writable (above layers should have |
|
127 |
* established that we have plenty of room), then write |
|
128 |
* out the hash. |
|
129 |
*/ |
|
130 |
bb.limit(bb.limit() + hash.length); |
|
131 |
bb.put(hash); |
|
132 |
} |
|
133 |
} |
|
134 |
||
135 |
/* |
|
136 |
* Encrypt a ByteBuffer. |
|
137 |
* |
|
138 |
* We assume that the higher levels have assured us enough |
|
139 |
* room for the encryption (plus padding), otherwise we'll |
|
140 |
* indirectly throw a BufferOverFlowException runtime exception. |
|
141 |
* |
|
142 |
* position and limit will be the same, and points to the |
|
143 |
* next free spot. |
|
144 |
*/ |
|
145 |
void encrypt(CipherBox box, ByteBuffer bb) { |
|
146 |
box.encrypt(bb); |
|
147 |
} |
|
148 |
||
149 |
/* |
|
150 |
* Override the actual write below. We do things this way to be |
|
151 |
* consistent with InputRecord. InputRecord may try to write out |
|
152 |
* data to the peer, and *then* throw an Exception. This forces |
|
153 |
* data to be generated/output before the exception is ever |
|
154 |
* generated. |
|
155 |
*/ |
|
12428 | 156 |
@Override |
157 |
void writeBuffer(OutputStream s, byte [] buf, int off, int len, |
|
158 |
int debugOffset) throws IOException { |
|
2 | 159 |
/* |
160 |
* Copy data out of buffer, it's ready to go. |
|
161 |
*/ |
|
162 |
ByteBuffer netBB = (ByteBuffer) |
|
163 |
ByteBuffer.allocate(len).put(buf, 0, len).flip(); |
|
164 |
writer.putOutboundData(netBB); |
|
165 |
} |
|
166 |
||
167 |
/* |
|
168 |
* Main method for writing non-application data. |
|
169 |
* We MAC/encrypt, then send down for processing. |
|
170 |
*/ |
|
171 |
void write(MAC writeMAC, CipherBox writeCipher) throws IOException { |
|
172 |
/* |
|
173 |
* Sanity check. |
|
174 |
*/ |
|
175 |
switch (contentType()) { |
|
176 |
case ct_change_cipher_spec: |
|
177 |
case ct_alert: |
|
178 |
case ct_handshake: |
|
179 |
break; |
|
180 |
default: |
|
181 |
throw new RuntimeException("unexpected byte buffers"); |
|
182 |
} |
|
183 |
||
184 |
/* |
|
185 |
* Don't bother to really write empty records. We went this |
|
186 |
* far to drive the handshake machinery, for correctness; not |
|
187 |
* writing empty records improves performance by cutting CPU |
|
188 |
* time and network resource usage. Also, some protocol |
|
189 |
* implementations are fragile and don't like to see empty |
|
190 |
* records, so this increases robustness. |
|
191 |
* |
|
192 |
* (Even change cipher spec messages have a byte of data!) |
|
193 |
*/ |
|
194 |
if (!isEmpty()) { |
|
195 |
// compress(); // eventually |
|
196 |
addMAC(writeMAC); |
|
197 |
encrypt(writeCipher); |
|
12428 | 198 |
write((OutputStream)null, false, // send down for processing |
199 |
(ByteArrayOutputStream)null); |
|
2 | 200 |
} |
201 |
return; |
|
202 |
} |
|
203 |
||
204 |
/** |
|
205 |
* Main wrap/write driver. |
|
206 |
*/ |
|
207 |
void write(EngineArgs ea, MAC writeMAC, CipherBox writeCipher) |
|
208 |
throws IOException { |
|
209 |
/* |
|
210 |
* sanity check to make sure someone didn't inadvertantly |
|
211 |
* send us an impossible combination we don't know how |
|
212 |
* to process. |
|
213 |
*/ |
|
214 |
assert(contentType() == ct_application_data); |
|
215 |
||
216 |
/* |
|
217 |
* Have we set the MAC's yet? If not, we're not ready |
|
218 |
* to process application data yet. |
|
219 |
*/ |
|
220 |
if (writeMAC == MAC.NULL) { |
|
221 |
return; |
|
222 |
} |
|
223 |
||
224 |
/* |
|
225 |
* Don't bother to really write empty records. We went this |
|
226 |
* far to drive the handshake machinery, for correctness; not |
|
227 |
* writing empty records improves performance by cutting CPU |
|
228 |
* time and network resource usage. Also, some protocol |
|
229 |
* implementations are fragile and don't like to see empty |
|
230 |
* records, so this increases robustness. |
|
231 |
*/ |
|
10915 | 232 |
if (ea.getAppRemaining() == 0) { |
2 | 233 |
return; |
234 |
} |
|
235 |
||
236 |
/* |
|
10915 | 237 |
* By default, we counter chosen plaintext issues on CBC mode |
238 |
* ciphersuites in SSLv3/TLS1.0 by sending one byte of application |
|
239 |
* data in the first record of every payload, and the rest in |
|
240 |
* subsequent record(s). Note that the issues have been solved in |
|
241 |
* TLS 1.1 or later. |
|
242 |
* |
|
243 |
* It is not necessary to split the very first application record of |
|
244 |
* a freshly negotiated TLS session, as there is no previous |
|
245 |
* application data to guess. To improve compatibility, we will not |
|
246 |
* split such records. |
|
247 |
* |
|
248 |
* Because of the compatibility, we'd better produce no more than |
|
249 |
* SSLSession.getPacketBufferSize() net data for each wrap. As we |
|
250 |
* need a one-byte record at first, the 2nd record size should be |
|
251 |
* equal to or less than Record.maxDataSizeMinusOneByteRecord. |
|
252 |
* |
|
253 |
* This avoids issues in the outbound direction. For a full fix, |
|
254 |
* the peer must have similar protections. |
|
255 |
*/ |
|
256 |
int length; |
|
257 |
if (engine.needToSplitPayload(writeCipher, protocolVersion)) { |
|
258 |
write(ea, writeMAC, writeCipher, 0x01); |
|
259 |
ea.resetLim(); // reset application data buffer limit |
|
260 |
length = Math.min(ea.getAppRemaining(), |
|
261 |
maxDataSizeMinusOneByteRecord); |
|
262 |
} else { |
|
263 |
length = Math.min(ea.getAppRemaining(), maxDataSize); |
|
264 |
} |
|
265 |
||
266 |
// Don't bother to really write empty records. |
|
267 |
if (length > 0) { |
|
268 |
write(ea, writeMAC, writeCipher, length); |
|
269 |
} |
|
270 |
||
271 |
return; |
|
272 |
} |
|
273 |
||
274 |
void write(EngineArgs ea, MAC writeMAC, CipherBox writeCipher, |
|
275 |
int length) throws IOException { |
|
276 |
/* |
|
2 | 277 |
* Copy out existing buffer values. |
278 |
*/ |
|
279 |
ByteBuffer dstBB = ea.netData; |
|
280 |
int dstPos = dstBB.position(); |
|
281 |
int dstLim = dstBB.limit(); |
|
282 |
||
283 |
/* |
|
284 |
* Where to put the data. Jump over the header. |
|
285 |
* |
|
286 |
* Don't need to worry about SSLv2 rewrites, if we're here, |
|
287 |
* that's long since done. |
|
288 |
*/ |
|
289 |
int dstData = dstPos + headerSize; |
|
290 |
dstBB.position(dstData); |
|
291 |
||
292 |
ea.gather(length); |
|
293 |
||
294 |
/* |
|
295 |
* "flip" but skip over header again, add MAC & encrypt |
|
296 |
* addMAC will expand the limit to reflect the new |
|
297 |
* data. |
|
298 |
*/ |
|
299 |
dstBB.limit(dstBB.position()); |
|
300 |
dstBB.position(dstData); |
|
301 |
addMAC(writeMAC, dstBB); |
|
302 |
||
303 |
/* |
|
304 |
* Encrypt may pad, so again the limit may have changed. |
|
305 |
*/ |
|
306 |
dstBB.limit(dstBB.position()); |
|
307 |
dstBB.position(dstData); |
|
308 |
encrypt(writeCipher, dstBB); |
|
309 |
||
310 |
if (debug != null |
|
311 |
&& (Debug.isOn("record") || Debug.isOn("handshake"))) { |
|
312 |
if ((debug != null && Debug.isOn("record")) |
|
313 |
|| contentType() == ct_change_cipher_spec) |
|
314 |
System.out.println(Thread.currentThread().getName() |
|
315 |
// v3.0/v3.1 ... |
|
316 |
+ ", WRITE: " + protocolVersion |
|
317 |
+ " " + InputRecord.contentName(contentType()) |
|
318 |
+ ", length = " + length); |
|
319 |
} |
|
320 |
||
321 |
int packetLength = dstBB.limit() - dstData; |
|
322 |
||
323 |
/* |
|
324 |
* Finish out the record header. |
|
325 |
*/ |
|
326 |
dstBB.put(dstPos, contentType()); |
|
327 |
dstBB.put(dstPos + 1, protocolVersion.major); |
|
328 |
dstBB.put(dstPos + 2, protocolVersion.minor); |
|
329 |
dstBB.put(dstPos + 3, (byte)(packetLength >> 8)); |
|
330 |
dstBB.put(dstPos + 4, (byte)packetLength); |
|
331 |
||
332 |
/* |
|
333 |
* Position was already set by encrypt() above. |
|
334 |
*/ |
|
335 |
dstBB.limit(dstLim); |
|
336 |
||
337 |
return; |
|
338 |
} |
|
339 |
} |