author | wetmore |
Tue, 12 Mar 2013 15:31:49 -0700 | |
changeset 16067 | 36055e4b5305 |
parent 16045 | 9d08c3b9a6a0 |
child 16126 | aad71cf676d7 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
16067 | 2 |
* Copyright (c) 2003, 2007, 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 |
/* |
|
16067 | 181 |
* Verifies and removes the MAC value. Returns true if |
182 |
* the MAC checks out OK. |
|
183 |
* |
|
184 |
* On entry: |
|
185 |
* position = beginning of app/MAC data |
|
186 |
* limit = end of MAC data. |
|
187 |
* |
|
188 |
* On return: |
|
189 |
* position = beginning of app data |
|
190 |
* limit = end of app data |
|
191 |
*/ |
|
192 |
boolean checkMAC(MAC signer, ByteBuffer bb) { |
|
193 |
if (internalData) { |
|
194 |
return checkMAC(signer); |
|
195 |
} |
|
196 |
||
197 |
int len = signer.MAClen(); |
|
198 |
if (len == 0) { // no mac |
|
199 |
return true; |
|
200 |
} |
|
201 |
||
202 |
/* |
|
203 |
* Grab the original limit |
|
204 |
*/ |
|
205 |
int lim = bb.limit(); |
|
206 |
||
207 |
/* |
|
208 |
* Delineate the area to apply a MAC on. |
|
209 |
*/ |
|
210 |
int macData = lim - len; |
|
211 |
bb.limit(macData); |
|
212 |
||
213 |
byte[] mac = signer.compute(contentType(), bb); |
|
214 |
||
215 |
if (len != mac.length) { |
|
216 |
throw new RuntimeException("Internal MAC error"); |
|
217 |
} |
|
218 |
||
219 |
/* |
|
220 |
* Delineate the MAC values, position was already set |
|
221 |
* by doing the compute above. |
|
222 |
* |
|
223 |
* We could zero the MAC area, but not much useful information |
|
224 |
* there anyway. |
|
225 |
*/ |
|
226 |
bb.position(macData); |
|
227 |
bb.limit(lim); |
|
228 |
||
229 |
try { |
|
230 |
for (int i = 0; i < len; i++) { |
|
231 |
if (bb.get() != mac[i]) { // No BB.equals(byte []); ! |
|
232 |
return false; |
|
233 |
} |
|
234 |
} |
|
235 |
return true; |
|
236 |
} finally { |
|
237 |
/* |
|
238 |
* Position to the data. |
|
239 |
*/ |
|
240 |
bb.rewind(); |
|
241 |
bb.limit(macData); |
|
242 |
} |
|
243 |
} |
|
244 |
||
245 |
/* |
|
2 | 246 |
* Pass the data down if it's internally cached, otherwise |
247 |
* do it here. |
|
248 |
* |
|
249 |
* If internal data, data is decrypted internally. |
|
250 |
* |
|
251 |
* If external data(app), return a new ByteBuffer with data to |
|
252 |
* process. |
|
253 |
*/ |
|
16067 | 254 |
ByteBuffer decrypt(CipherBox box, ByteBuffer bb) |
255 |
throws BadPaddingException { |
|
2 | 256 |
|
257 |
if (internalData) { |
|
16067 | 258 |
decrypt(box); |
2 | 259 |
return tmpBB; |
260 |
} |
|
261 |
||
16067 | 262 |
box.decrypt(bb); |
263 |
bb.rewind(); |
|
2 | 264 |
|
265 |
return bb.slice(); |
|
266 |
} |
|
267 |
||
268 |
/* |
|
269 |
* Override the actual write below. We do things this way to be |
|
270 |
* consistent with InputRecord. InputRecord may try to write out |
|
271 |
* data to the peer, and *then* throw an Exception. This forces |
|
272 |
* data to be generated/output before the exception is ever |
|
273 |
* generated. |
|
274 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
5506
diff
changeset
|
275 |
@Override |
2 | 276 |
void writeBuffer(OutputStream s, byte [] buf, int off, int len) |
277 |
throws IOException { |
|
278 |
/* |
|
279 |
* Copy data out of buffer, it's ready to go. |
|
280 |
*/ |
|
281 |
ByteBuffer netBB = (ByteBuffer) |
|
282 |
(ByteBuffer.allocate(len).put(buf, 0, len).flip()); |
|
283 |
engine.writer.putOutboundDataSync(netBB); |
|
284 |
} |
|
285 |
||
286 |
/* |
|
287 |
* Delineate or read a complete packet from src. |
|
288 |
* |
|
289 |
* If internal data (hs, alert, ccs), the data is read and |
|
290 |
* stored internally. |
|
291 |
* |
|
292 |
* If external data (app), return a new ByteBuffer which points |
|
293 |
* to the data to process. |
|
294 |
*/ |
|
295 |
ByteBuffer read(ByteBuffer srcBB) throws IOException { |
|
296 |
/* |
|
297 |
* Could have a src == null/dst == null check here, |
|
298 |
* but that was already checked by SSLEngine.unwrap before |
|
299 |
* ever attempting to read. |
|
300 |
*/ |
|
301 |
||
302 |
/* |
|
303 |
* If we have anything besides application data, |
|
304 |
* or if we haven't even done the initial v2 verification, |
|
305 |
* we send this down to be processed by the underlying |
|
306 |
* internal cache. |
|
307 |
*/ |
|
308 |
if (!formatVerified || |
|
309 |
(srcBB.get(srcBB.position()) != ct_application_data)) { |
|
310 |
internalData = true; |
|
311 |
read(new ByteBufferInputStream(srcBB), (OutputStream) null); |
|
312 |
return tmpBB; |
|
313 |
} |
|
314 |
||
315 |
internalData = false; |
|
316 |
||
317 |
int srcPos = srcBB.position(); |
|
318 |
int srcLim = srcBB.limit(); |
|
319 |
||
320 |
ProtocolVersion recordVersion = ProtocolVersion.valueOf( |
|
321 |
srcBB.get(srcPos + 1), srcBB.get(srcPos + 2)); |
|
322 |
// Check if too old (currently not possible) |
|
323 |
// or if the major version does not match. |
|
324 |
// The actual version negotiation is in the handshaker classes |
|
325 |
if ((recordVersion.v < ProtocolVersion.MIN.v) |
|
326 |
|| (recordVersion.major > ProtocolVersion.MAX.major)) { |
|
327 |
throw new SSLException( |
|
328 |
"Unsupported record version " + recordVersion); |
|
329 |
} |
|
330 |
||
331 |
/* |
|
332 |
* It's really application data. How much to consume? |
|
333 |
* Jump over the header. |
|
334 |
*/ |
|
335 |
int len = bytesInCompletePacket(srcBB); |
|
336 |
assert(len > 0); |
|
337 |
||
338 |
if (debug != null && Debug.isOn("packet")) { |
|
339 |
try { |
|
340 |
HexDumpEncoder hd = new HexDumpEncoder(); |
|
16067 | 341 |
srcBB.limit(srcPos + len); |
2 | 342 |
ByteBuffer bb = srcBB.duplicate(); // Use copy of BB |
343 |
||
344 |
System.out.println("[Raw read (bb)]: length = " + len); |
|
345 |
hd.encodeBuffer(bb, System.out); |
|
346 |
} catch (IOException e) { } |
|
347 |
} |
|
348 |
||
349 |
// Demarcate past header to end of packet. |
|
350 |
srcBB.position(srcPos + headerSize); |
|
351 |
srcBB.limit(srcPos + len); |
|
352 |
||
353 |
// Protect remainder of buffer, create slice to actually |
|
354 |
// operate on. |
|
355 |
ByteBuffer bb = srcBB.slice(); |
|
356 |
||
357 |
srcBB.position(srcBB.limit()); |
|
358 |
srcBB.limit(srcLim); |
|
359 |
||
360 |
return bb; |
|
361 |
} |
|
362 |
} |