author | lana |
Tue, 12 Mar 2013 19:04:49 -0700 | |
changeset 16126 | aad71cf676d7 |
parent 16067 | 36055e4b5305 |
parent 16113 | 946ec9b22004 |
child 16913 | a6f4d1626ad9 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
16113 | 2 |
* Copyright (c) 2003, 2013, 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 |
import javax.net.ssl.*; |
|
32 |
import javax.crypto.BadPaddingException; |
|
33 |
import sun.misc.HexDumpEncoder; |
|
34 |
||
35 |
||
36 |
/** |
|
37 |
* Wrapper class around InputRecord. |
|
38 |
* |
|
39 |
* Application data is kept external to the InputRecord, |
|
40 |
* but handshake data (alert/change_cipher_spec/handshake) will |
|
41 |
* be kept internally in the ByteArrayInputStream. |
|
42 |
* |
|
43 |
* @author Brad Wetmore |
|
44 |
*/ |
|
45 |
final class EngineInputRecord extends InputRecord { |
|
46 |
||
47 |
private SSLEngineImpl engine; |
|
48 |
||
49 |
/* |
|
50 |
* A dummy ByteBuffer we'll pass back even when the data |
|
51 |
* is stored internally. It'll never actually be used. |
|
52 |
*/ |
|
53 |
static private ByteBuffer tmpBB = ByteBuffer.allocate(0); |
|
54 |
||
55 |
/* |
|
56 |
* Flag to tell whether the last read/parsed data resides |
|
57 |
* internal in the ByteArrayInputStream, or in the external |
|
58 |
* buffers. |
|
59 |
*/ |
|
60 |
private boolean internalData; |
|
61 |
||
62 |
EngineInputRecord(SSLEngineImpl engine) { |
|
63 |
super(); |
|
64 |
this.engine = engine; |
|
65 |
} |
|
66 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
5506
diff
changeset
|
67 |
@Override |
2 | 68 |
byte contentType() { |
69 |
if (internalData) { |
|
70 |
return super.contentType(); |
|
71 |
} else { |
|
72 |
return ct_application_data; |
|
73 |
} |
|
74 |
} |
|
75 |
||
76 |
/* |
|
77 |
* Check if there is enough inbound data in the ByteBuffer |
|
78 |
* to make a inbound packet. Look for both SSLv2 and SSLv3. |
|
79 |
* |
|
80 |
* @return -1 if there are not enough bytes to tell (small header), |
|
81 |
*/ |
|
82 |
int bytesInCompletePacket(ByteBuffer buf) throws SSLException { |
|
83 |
||
84 |
/* |
|
85 |
* SSLv2 length field is in bytes 0/1 |
|
86 |
* SSLv3/TLS length field is in bytes 3/4 |
|
87 |
*/ |
|
88 |
if (buf.remaining() < 5) { |
|
89 |
return -1; |
|
90 |
} |
|
91 |
||
92 |
int pos = buf.position(); |
|
93 |
byte byteZero = buf.get(pos); |
|
94 |
||
95 |
int len = 0; |
|
96 |
||
97 |
/* |
|
98 |
* If we have already verified previous packets, we can |
|
99 |
* ignore the verifications steps, and jump right to the |
|
100 |
* determination. Otherwise, try one last hueristic to |
|
101 |
* see if it's SSL/TLS. |
|
102 |
*/ |
|
103 |
if (formatVerified || |
|
104 |
(byteZero == ct_handshake) || |
|
105 |
(byteZero == ct_alert)) { |
|
106 |
/* |
|
107 |
* Last sanity check that it's not a wild record |
|
108 |
*/ |
|
109 |
ProtocolVersion recordVersion = |
|
110 |
ProtocolVersion.valueOf(buf.get(pos + 1), buf.get(pos + 2)); |
|
111 |
||
112 |
// Check if too old (currently not possible) |
|
113 |
// or if the major version does not match. |
|
114 |
// The actual version negotiation is in the handshaker classes |
|
115 |
if ((recordVersion.v < ProtocolVersion.MIN.v) |
|
116 |
|| (recordVersion.major > ProtocolVersion.MAX.major)) { |
|
117 |
throw new SSLException( |
|
118 |
"Unsupported record version " + recordVersion); |
|
119 |
} |
|
120 |
||
121 |
/* |
|
122 |
* Reasonably sure this is a V3, disable further checks. |
|
123 |
* We can't do the same in the v2 check below, because |
|
124 |
* read still needs to parse/handle the v2 clientHello. |
|
125 |
*/ |
|
126 |
formatVerified = true; |
|
127 |
||
128 |
/* |
|
129 |
* One of the SSLv3/TLS message types. |
|
130 |
*/ |
|
131 |
len = ((buf.get(pos + 3) & 0xff) << 8) + |
|
132 |
(buf.get(pos + 4) & 0xff) + headerSize; |
|
133 |
||
134 |
} else { |
|
135 |
/* |
|
136 |
* Must be SSLv2 or something unknown. |
|
137 |
* Check if it's short (2 bytes) or |
|
138 |
* long (3) header. |
|
139 |
* |
|
140 |
* Internals can warn about unsupported SSLv2 |
|
141 |
*/ |
|
142 |
boolean isShort = ((byteZero & 0x80) != 0); |
|
143 |
||
144 |
if (isShort && |
|
145 |
((buf.get(pos + 2) == 1) || buf.get(pos + 2) == 4)) { |
|
146 |
||
147 |
ProtocolVersion recordVersion = |
|
148 |
ProtocolVersion.valueOf(buf.get(pos + 3), buf.get(pos + 4)); |
|
149 |
||
150 |
// Check if too old (currently not possible) |
|
151 |
// or if the major version does not match. |
|
152 |
// The actual version negotiation is in the handshaker classes |
|
153 |
if ((recordVersion.v < ProtocolVersion.MIN.v) |
|
154 |
|| (recordVersion.major > ProtocolVersion.MAX.major)) { |
|
155 |
||
156 |
// if it's not SSLv2, we're out of here. |
|
157 |
if (recordVersion.v != ProtocolVersion.SSL20Hello.v) { |
|
158 |
throw new SSLException( |
|
159 |
"Unsupported record version " + recordVersion); |
|
160 |
} |
|
161 |
} |
|
162 |
||
163 |
/* |
|
164 |
* Client or Server Hello |
|
165 |
*/ |
|
166 |
int mask = (isShort ? 0x7f : 0x3f); |
|
167 |
len = ((byteZero & mask) << 8) + (buf.get(pos + 1) & 0xff) + |
|
168 |
(isShort ? 2 : 3); |
|
169 |
||
170 |
} else { |
|
171 |
// Gobblygook! |
|
172 |
throw new SSLException( |
|
173 |
"Unrecognized SSL message, plaintext connection?"); |
|
174 |
} |
|
175 |
} |
|
176 |
||
177 |
return len; |
|
178 |
} |
|
179 |
||
180 |
/* |
|
181 |
* Pass the data down if it's internally cached, otherwise |
|
182 |
* do it here. |
|
183 |
* |
|
184 |
* If internal data, data is decrypted internally. |
|
185 |
* |
|
186 |
* If external data(app), return a new ByteBuffer with data to |
|
187 |
* process. |
|
188 |
*/ |
|
16113 | 189 |
ByteBuffer decrypt(MAC signer, |
190 |
CipherBox box, ByteBuffer bb) throws BadPaddingException { |
|
2 | 191 |
|
192 |
if (internalData) { |
|
16113 | 193 |
decrypt(signer, box); // MAC is checked during decryption |
2 | 194 |
return tmpBB; |
195 |
} |
|
196 |
||
16113 | 197 |
BadPaddingException reservedBPE = null; |
198 |
int tagLen = signer.MAClen(); |
|
199 |
int cipheredLength = bb.remaining(); |
|
200 |
||
201 |
if (!box.isNullCipher()) { |
|
202 |
// sanity check length of the ciphertext |
|
203 |
if (!box.sanityCheck(tagLen, cipheredLength)) { |
|
204 |
throw new BadPaddingException( |
|
205 |
"ciphertext sanity check failed"); |
|
206 |
} |
|
207 |
||
208 |
try { |
|
209 |
// Note that the CipherBox.decrypt() does not change |
|
210 |
// the capacity of the buffer. |
|
211 |
box.decrypt(bb, tagLen); |
|
212 |
} catch (BadPaddingException bpe) { |
|
213 |
// RFC 2246 states that decryption_failed should be used |
|
214 |
// for this purpose. However, that allows certain attacks, |
|
215 |
// so we just send bad record MAC. We also need to make |
|
216 |
// sure to always check the MAC to avoid a timing attack |
|
217 |
// for the same issue. See paper by Vaudenay et al and the |
|
218 |
// update in RFC 4346/5246. |
|
219 |
// |
|
220 |
// Failover to message authentication code checking. |
|
221 |
reservedBPE = bpe; |
|
222 |
} finally { |
|
223 |
bb.rewind(); |
|
224 |
} |
|
225 |
} |
|
226 |
||
227 |
if (tagLen != 0) { |
|
228 |
int macOffset = bb.limit() - tagLen; |
|
229 |
||
230 |
// Note that although it is not necessary, we run the same MAC |
|
231 |
// computation and comparison on the payload for both stream |
|
232 |
// cipher and CBC block cipher. |
|
233 |
if (bb.remaining() < tagLen) { |
|
234 |
// negative data length, something is wrong |
|
235 |
if (reservedBPE == null) { |
|
236 |
reservedBPE = new BadPaddingException("bad record"); |
|
237 |
} |
|
238 |
||
239 |
// set offset of the dummy MAC |
|
240 |
macOffset = cipheredLength - tagLen; |
|
241 |
bb.limit(cipheredLength); |
|
242 |
} |
|
243 |
||
244 |
// Run MAC computation and comparison on the payload. |
|
245 |
if (checkMacTags(contentType(), bb, signer, false)) { |
|
246 |
if (reservedBPE == null) { |
|
247 |
reservedBPE = new BadPaddingException("bad record MAC"); |
|
248 |
} |
|
249 |
} |
|
250 |
||
251 |
// Run MAC computation and comparison on the remainder. |
|
252 |
// |
|
253 |
// It is only necessary for CBC block cipher. It is used to get a |
|
254 |
// constant time of MAC computation and comparison on each record. |
|
255 |
if (box.isCBCMode()) { |
|
256 |
int remainingLen = calculateRemainingLen( |
|
257 |
signer, cipheredLength, macOffset); |
|
258 |
||
259 |
// NOTE: here we use the InputRecord.buf because I did not find |
|
260 |
// an effective way to work on ByteBuffer when its capacity is |
|
261 |
// less than remainingLen. |
|
262 |
||
263 |
// NOTE: remainingLen may be bigger (less than 1 block of the |
|
264 |
// hash algorithm of the MAC) than the cipheredLength. However, |
|
265 |
// We won't need to worry about it because we always use a |
|
266 |
// maximum buffer for every record. We need a change here if |
|
267 |
// we use small buffer size in the future. |
|
268 |
if (remainingLen > buf.length) { |
|
269 |
// unlikely to happen, just a placehold |
|
270 |
throw new RuntimeException( |
|
271 |
"Internal buffer capacity error"); |
|
272 |
} |
|
273 |
||
274 |
// Won't need to worry about the result on the remainder. And |
|
275 |
// then we won't need to worry about what's actual data to |
|
276 |
// check MAC tag on. We start the check from the header of the |
|
277 |
// buffer so that we don't need to construct a new byte buffer. |
|
278 |
checkMacTags(contentType(), buf, 0, remainingLen, signer, true); |
|
279 |
} |
|
280 |
||
281 |
bb.limit(macOffset); |
|
282 |
} |
|
283 |
||
284 |
// Is it a failover? |
|
285 |
if (reservedBPE != null) { |
|
286 |
throw reservedBPE; |
|
287 |
} |
|
2 | 288 |
|
289 |
return bb.slice(); |
|
290 |
} |
|
291 |
||
292 |
/* |
|
16113 | 293 |
* Run MAC computation and comparison |
294 |
* |
|
295 |
* Please DON'T change the content of the ByteBuffer parameter! |
|
296 |
*/ |
|
297 |
private static boolean checkMacTags(byte contentType, ByteBuffer bb, |
|
298 |
MAC signer, boolean isSimulated) { |
|
299 |
||
300 |
int tagLen = signer.MAClen(); |
|
301 |
int lim = bb.limit(); |
|
302 |
int macData = lim - tagLen; |
|
303 |
||
304 |
bb.limit(macData); |
|
305 |
byte[] hash = signer.compute(contentType, bb, isSimulated); |
|
306 |
if (hash == null || tagLen != hash.length) { |
|
307 |
// Something is wrong with MAC implementation. |
|
308 |
throw new RuntimeException("Internal MAC error"); |
|
309 |
} |
|
310 |
||
311 |
bb.position(macData); |
|
312 |
bb.limit(lim); |
|
313 |
try { |
|
314 |
int[] results = compareMacTags(bb, hash); |
|
315 |
return (results[0] != 0); |
|
316 |
} finally { |
|
317 |
bb.rewind(); |
|
318 |
bb.limit(macData); |
|
319 |
} |
|
320 |
} |
|
321 |
||
322 |
/* |
|
323 |
* A constant-time comparison of the MAC tags. |
|
324 |
* |
|
325 |
* Please DON'T change the content of the ByteBuffer parameter! |
|
326 |
*/ |
|
327 |
private static int[] compareMacTags(ByteBuffer bb, byte[] tag) { |
|
328 |
||
329 |
// An array of hits is used to prevent Hotspot optimization for |
|
330 |
// the purpose of a constant-time check. |
|
331 |
int[] results = {0, 0}; // {missed #, matched #} |
|
332 |
||
333 |
// The caller ensures there are enough bytes available in the buffer. |
|
334 |
// So we won't need to check the remaining of the buffer. |
|
335 |
for (int i = 0; i < tag.length; i++) { |
|
336 |
if (bb.get() != tag[i]) { |
|
337 |
results[0]++; // mismatched bytes |
|
338 |
} else { |
|
339 |
results[1]++; // matched bytes |
|
340 |
} |
|
341 |
} |
|
342 |
||
343 |
return results; |
|
344 |
} |
|
345 |
||
346 |
/* |
|
2 | 347 |
* Override the actual write below. We do things this way to be |
348 |
* consistent with InputRecord. InputRecord may try to write out |
|
349 |
* data to the peer, and *then* throw an Exception. This forces |
|
350 |
* data to be generated/output before the exception is ever |
|
351 |
* generated. |
|
352 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
5506
diff
changeset
|
353 |
@Override |
2 | 354 |
void writeBuffer(OutputStream s, byte [] buf, int off, int len) |
355 |
throws IOException { |
|
356 |
/* |
|
357 |
* Copy data out of buffer, it's ready to go. |
|
358 |
*/ |
|
359 |
ByteBuffer netBB = (ByteBuffer) |
|
360 |
(ByteBuffer.allocate(len).put(buf, 0, len).flip()); |
|
361 |
engine.writer.putOutboundDataSync(netBB); |
|
362 |
} |
|
363 |
||
364 |
/* |
|
365 |
* Delineate or read a complete packet from src. |
|
366 |
* |
|
367 |
* If internal data (hs, alert, ccs), the data is read and |
|
368 |
* stored internally. |
|
369 |
* |
|
370 |
* If external data (app), return a new ByteBuffer which points |
|
371 |
* to the data to process. |
|
372 |
*/ |
|
373 |
ByteBuffer read(ByteBuffer srcBB) throws IOException { |
|
374 |
/* |
|
375 |
* Could have a src == null/dst == null check here, |
|
376 |
* but that was already checked by SSLEngine.unwrap before |
|
377 |
* ever attempting to read. |
|
378 |
*/ |
|
379 |
||
380 |
/* |
|
381 |
* If we have anything besides application data, |
|
382 |
* or if we haven't even done the initial v2 verification, |
|
383 |
* we send this down to be processed by the underlying |
|
384 |
* internal cache. |
|
385 |
*/ |
|
386 |
if (!formatVerified || |
|
387 |
(srcBB.get(srcBB.position()) != ct_application_data)) { |
|
388 |
internalData = true; |
|
389 |
read(new ByteBufferInputStream(srcBB), (OutputStream) null); |
|
390 |
return tmpBB; |
|
391 |
} |
|
392 |
||
393 |
internalData = false; |
|
394 |
||
395 |
int srcPos = srcBB.position(); |
|
396 |
int srcLim = srcBB.limit(); |
|
397 |
||
398 |
ProtocolVersion recordVersion = ProtocolVersion.valueOf( |
|
399 |
srcBB.get(srcPos + 1), srcBB.get(srcPos + 2)); |
|
400 |
// Check if too old (currently not possible) |
|
401 |
// or if the major version does not match. |
|
402 |
// The actual version negotiation is in the handshaker classes |
|
403 |
if ((recordVersion.v < ProtocolVersion.MIN.v) |
|
404 |
|| (recordVersion.major > ProtocolVersion.MAX.major)) { |
|
405 |
throw new SSLException( |
|
406 |
"Unsupported record version " + recordVersion); |
|
407 |
} |
|
408 |
||
409 |
/* |
|
410 |
* It's really application data. How much to consume? |
|
411 |
* Jump over the header. |
|
412 |
*/ |
|
413 |
int len = bytesInCompletePacket(srcBB); |
|
414 |
assert(len > 0); |
|
415 |
||
416 |
if (debug != null && Debug.isOn("packet")) { |
|
417 |
try { |
|
418 |
HexDumpEncoder hd = new HexDumpEncoder(); |
|
419 |
srcBB.limit(srcPos + len); |
|
420 |
ByteBuffer bb = srcBB.duplicate(); // Use copy of BB |
|
421 |
||
422 |
System.out.println("[Raw read (bb)]: length = " + len); |
|
423 |
hd.encodeBuffer(bb, System.out); |
|
424 |
} catch (IOException e) { } |
|
425 |
} |
|
426 |
||
427 |
// Demarcate past header to end of packet. |
|
428 |
srcBB.position(srcPos + headerSize); |
|
429 |
srcBB.limit(srcPos + len); |
|
430 |
||
431 |
// Protect remainder of buffer, create slice to actually |
|
432 |
// operate on. |
|
433 |
ByteBuffer bb = srcBB.slice(); |
|
434 |
||
435 |
srcBB.position(srcBB.limit()); |
|
436 |
srcBB.limit(srcLim); |
|
437 |
||
438 |
return bb; |
|
439 |
} |
|
440 |
} |