2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2004, 2006, Oracle and/or its affiliates. All rights reserved.
|
2
|
3 |
*
|
|
4 |
* Redistribution and use in source and binary forms, with or without
|
|
5 |
* modification, are permitted provided that the following conditions
|
|
6 |
* are met:
|
|
7 |
*
|
|
8 |
* - Redistributions of source code must retain the above copyright
|
|
9 |
* notice, this list of conditions and the following disclaimer.
|
|
10 |
*
|
|
11 |
* - Redistributions in binary form must reproduce the above copyright
|
|
12 |
* notice, this list of conditions and the following disclaimer in the
|
|
13 |
* documentation and/or other materials provided with the distribution.
|
|
14 |
*
|
5506
|
15 |
* - Neither the name of Oracle nor the names of its
|
2
|
16 |
* contributors may be used to endorse or promote products derived
|
|
17 |
* from this software without specific prior written permission.
|
|
18 |
*
|
|
19 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
|
20 |
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
21 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
22 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
23 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
24 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
25 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
26 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
27 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
28 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
29 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
30 |
*/
|
|
31 |
|
|
32 |
import java.io.*;
|
|
33 |
import java.nio.*;
|
|
34 |
import java.nio.channels.*;
|
|
35 |
import javax.net.ssl.*;
|
|
36 |
import javax.net.ssl.SSLEngineResult.*;
|
|
37 |
|
|
38 |
/**
|
|
39 |
* A helper class which performs I/O using the SSLEngine API.
|
|
40 |
* <P>
|
|
41 |
* Each connection has a SocketChannel and a SSLEngine that is
|
|
42 |
* used through the lifetime of the Channel. We allocate byte buffers
|
|
43 |
* for use as the outbound and inbound network buffers.
|
|
44 |
*
|
|
45 |
* <PRE>
|
|
46 |
* Application Data
|
|
47 |
* src requestBB
|
|
48 |
* | ^
|
|
49 |
* | | |
|
|
50 |
* v | |
|
|
51 |
* +----+-----|-----+----+
|
|
52 |
* | | |
|
|
53 |
* | SSL|Engine |
|
|
54 |
* wrap() | | | unwrap()
|
|
55 |
* | OUTBOUND | INBOUND |
|
|
56 |
* | | |
|
|
57 |
* +----+-----|-----+----+
|
|
58 |
* | | ^
|
|
59 |
* | | |
|
|
60 |
* v |
|
|
61 |
* outNetBB inNetBB
|
|
62 |
* Net data
|
|
63 |
* </PRE>
|
|
64 |
*
|
|
65 |
* These buffers handle all of the intermediary data for the SSL
|
|
66 |
* connection. To make things easy, we'll require outNetBB be
|
|
67 |
* completely flushed before trying to wrap any more data, but we
|
|
68 |
* could certainly remove that restriction by using larger buffers.
|
|
69 |
* <P>
|
|
70 |
* There are many, many ways to handle compute and I/O strategies.
|
|
71 |
* What follows is a relatively simple one. The reader is encouraged
|
|
72 |
* to develop the strategy that best fits the application.
|
|
73 |
* <P>
|
|
74 |
* In most of the non-blocking operations in this class, we let the
|
|
75 |
* Selector tell us when we're ready to attempt an I/O operation (by the
|
|
76 |
* application repeatedly calling our methods). Another option would be
|
|
77 |
* to attempt the operation and return from the method when no forward
|
|
78 |
* progress can be made.
|
|
79 |
* <P>
|
|
80 |
* There's lots of room for enhancements and improvement in this example.
|
|
81 |
* <P>
|
|
82 |
* We're checking for SSL/TLS end-of-stream truncation attacks via
|
|
83 |
* sslEngine.closeInbound(). When you reach the end of a input stream
|
|
84 |
* via a read() returning -1 or an IOException, we call
|
|
85 |
* sslEngine.closeInbound() to signal to the sslEngine that no more
|
|
86 |
* input will be available. If the peer's close_notify message has not
|
|
87 |
* yet been received, this could indicate a trucation attack, in which
|
|
88 |
* an attacker is trying to prematurely close the connection. The
|
|
89 |
* closeInbound() will throw an exception if this condition were
|
|
90 |
* present.
|
|
91 |
*
|
|
92 |
* @author Brad R. Wetmore
|
|
93 |
* @author Mark Reinhold
|
|
94 |
*/
|
|
95 |
class ChannelIOSecure extends ChannelIO {
|
|
96 |
|
|
97 |
private SSLEngine sslEngine = null;
|
|
98 |
|
|
99 |
private int appBBSize;
|
|
100 |
private int netBBSize;
|
|
101 |
|
|
102 |
/*
|
|
103 |
* All I/O goes through these buffers.
|
|
104 |
* <P>
|
|
105 |
* It might be nice to use a cache of ByteBuffers so we're
|
|
106 |
* not alloc/dealloc'ing ByteBuffer's for each new SSLEngine.
|
|
107 |
* <P>
|
|
108 |
* We use our superclass' requestBB for our application input buffer.
|
|
109 |
* Outbound application data is supplied to us by our callers.
|
|
110 |
*/
|
|
111 |
private ByteBuffer inNetBB;
|
|
112 |
private ByteBuffer outNetBB;
|
|
113 |
|
|
114 |
/*
|
|
115 |
* An empty ByteBuffer for use when one isn't available, say
|
|
116 |
* as a source buffer during initial handshake wraps or for close
|
|
117 |
* operations.
|
|
118 |
*/
|
|
119 |
private static ByteBuffer hsBB = ByteBuffer.allocate(0);
|
|
120 |
|
|
121 |
/*
|
|
122 |
* The FileChannel we're currently transferTo'ing (reading).
|
|
123 |
*/
|
|
124 |
private ByteBuffer fileChannelBB = null;
|
|
125 |
|
|
126 |
/*
|
|
127 |
* During our initial handshake, keep track of the next
|
|
128 |
* SSLEngine operation that needs to occur:
|
|
129 |
*
|
|
130 |
* NEED_WRAP/NEED_UNWRAP
|
|
131 |
*
|
|
132 |
* Once the initial handshake has completed, we can short circuit
|
|
133 |
* handshake checks with initialHSComplete.
|
|
134 |
*/
|
|
135 |
private HandshakeStatus initialHSStatus;
|
|
136 |
private boolean initialHSComplete;
|
|
137 |
|
|
138 |
/*
|
|
139 |
* We have received the shutdown request by our caller, and have
|
|
140 |
* closed our outbound side.
|
|
141 |
*/
|
|
142 |
private boolean shutdown = false;
|
|
143 |
|
|
144 |
/*
|
|
145 |
* Constructor for a secure ChannelIO variant.
|
|
146 |
*/
|
|
147 |
protected ChannelIOSecure(SocketChannel sc, boolean blocking,
|
|
148 |
SSLContext sslc) throws IOException {
|
|
149 |
super(sc, blocking);
|
|
150 |
|
|
151 |
/*
|
|
152 |
* We're a server, so no need to use host/port variant.
|
|
153 |
*
|
|
154 |
* The first call for a server is a NEED_UNWRAP.
|
|
155 |
*/
|
|
156 |
sslEngine = sslc.createSSLEngine();
|
|
157 |
sslEngine.setUseClientMode(false);
|
|
158 |
initialHSStatus = HandshakeStatus.NEED_UNWRAP;
|
|
159 |
initialHSComplete = false;
|
|
160 |
|
|
161 |
// Create a buffer using the normal expected packet size we'll
|
|
162 |
// be getting. This may change, depending on the peer's
|
|
163 |
// SSL implementation.
|
|
164 |
netBBSize = sslEngine.getSession().getPacketBufferSize();
|
|
165 |
inNetBB = ByteBuffer.allocate(netBBSize);
|
|
166 |
outNetBB = ByteBuffer.allocate(netBBSize);
|
|
167 |
outNetBB.position(0);
|
|
168 |
outNetBB.limit(0);
|
|
169 |
}
|
|
170 |
|
|
171 |
/*
|
|
172 |
* Static factory method for creating a secure ChannelIO object.
|
|
173 |
* <P>
|
|
174 |
* We need to allocate different sized application data buffers
|
|
175 |
* based on whether we're secure or not. We can't determine
|
|
176 |
* this until our sslEngine is created.
|
|
177 |
*/
|
|
178 |
static ChannelIOSecure getInstance(SocketChannel sc, boolean blocking,
|
|
179 |
SSLContext sslc) throws IOException {
|
|
180 |
|
|
181 |
ChannelIOSecure cio = new ChannelIOSecure(sc, blocking, sslc);
|
|
182 |
|
|
183 |
// Create a buffer using the normal expected application size we'll
|
|
184 |
// be getting. This may change, depending on the peer's
|
|
185 |
// SSL implementation.
|
|
186 |
cio.appBBSize = cio.sslEngine.getSession().getApplicationBufferSize();
|
|
187 |
cio.requestBB = ByteBuffer.allocate(cio.appBBSize);
|
|
188 |
|
|
189 |
return cio;
|
|
190 |
}
|
|
191 |
|
|
192 |
/*
|
|
193 |
* Calls up to the superclass to adjust the buffer size
|
|
194 |
* by an appropriate increment.
|
|
195 |
*/
|
|
196 |
protected void resizeRequestBB() {
|
|
197 |
resizeRequestBB(appBBSize);
|
|
198 |
}
|
|
199 |
|
|
200 |
/*
|
|
201 |
* Adjust the inbount network buffer to an appropriate size.
|
|
202 |
*/
|
|
203 |
private void resizeResponseBB() {
|
|
204 |
ByteBuffer bb = ByteBuffer.allocate(netBBSize);
|
|
205 |
inNetBB.flip();
|
|
206 |
bb.put(inNetBB);
|
|
207 |
inNetBB = bb;
|
|
208 |
}
|
|
209 |
|
|
210 |
/*
|
|
211 |
* Writes bb to the SocketChannel.
|
|
212 |
* <P>
|
|
213 |
* Returns true when the ByteBuffer has no remaining data.
|
|
214 |
*/
|
|
215 |
private boolean tryFlush(ByteBuffer bb) throws IOException {
|
|
216 |
super.write(bb);
|
|
217 |
return !bb.hasRemaining();
|
|
218 |
}
|
|
219 |
|
|
220 |
/*
|
|
221 |
* Perform any handshaking processing.
|
|
222 |
* <P>
|
|
223 |
* This variant is for Servers without SelectionKeys (e.g.
|
|
224 |
* blocking).
|
|
225 |
*/
|
|
226 |
boolean doHandshake() throws IOException {
|
|
227 |
return doHandshake(null);
|
|
228 |
}
|
|
229 |
|
|
230 |
/*
|
|
231 |
* Perform any handshaking processing.
|
|
232 |
* <P>
|
|
233 |
* If a SelectionKey is passed, register for selectable
|
|
234 |
* operations.
|
|
235 |
* <P>
|
|
236 |
* In the blocking case, our caller will keep calling us until
|
|
237 |
* we finish the handshake. Our reads/writes will block as expected.
|
|
238 |
* <P>
|
|
239 |
* In the non-blocking case, we just received the selection notification
|
|
240 |
* that this channel is ready for whatever the operation is, so give
|
|
241 |
* it a try.
|
|
242 |
* <P>
|
|
243 |
* return:
|
|
244 |
* true when handshake is done.
|
|
245 |
* false while handshake is in progress
|
|
246 |
*/
|
|
247 |
boolean doHandshake(SelectionKey sk) throws IOException {
|
|
248 |
|
|
249 |
SSLEngineResult result;
|
|
250 |
|
|
251 |
if (initialHSComplete) {
|
|
252 |
return initialHSComplete;
|
|
253 |
}
|
|
254 |
|
|
255 |
/*
|
|
256 |
* Flush out the outgoing buffer, if there's anything left in
|
|
257 |
* it.
|
|
258 |
*/
|
|
259 |
if (outNetBB.hasRemaining()) {
|
|
260 |
|
|
261 |
if (!tryFlush(outNetBB)) {
|
|
262 |
return false;
|
|
263 |
}
|
|
264 |
|
|
265 |
// See if we need to switch from write to read mode.
|
|
266 |
|
|
267 |
switch (initialHSStatus) {
|
|
268 |
|
|
269 |
/*
|
|
270 |
* Is this the last buffer?
|
|
271 |
*/
|
|
272 |
case FINISHED:
|
|
273 |
initialHSComplete = true;
|
|
274 |
// Fall-through to reregister need for a Read.
|
|
275 |
|
|
276 |
case NEED_UNWRAP:
|
|
277 |
if (sk != null) {
|
|
278 |
sk.interestOps(SelectionKey.OP_READ);
|
|
279 |
}
|
|
280 |
break;
|
|
281 |
}
|
|
282 |
|
|
283 |
return initialHSComplete;
|
|
284 |
}
|
|
285 |
|
|
286 |
|
|
287 |
switch (initialHSStatus) {
|
|
288 |
|
|
289 |
case NEED_UNWRAP:
|
|
290 |
if (sc.read(inNetBB) == -1) {
|
|
291 |
sslEngine.closeInbound();
|
|
292 |
return initialHSComplete;
|
|
293 |
}
|
|
294 |
|
|
295 |
needIO:
|
|
296 |
while (initialHSStatus == HandshakeStatus.NEED_UNWRAP) {
|
|
297 |
resizeRequestBB(); // expected room for unwrap
|
|
298 |
inNetBB.flip();
|
|
299 |
result = sslEngine.unwrap(inNetBB, requestBB);
|
|
300 |
inNetBB.compact();
|
|
301 |
|
|
302 |
initialHSStatus = result.getHandshakeStatus();
|
|
303 |
|
|
304 |
switch (result.getStatus()) {
|
|
305 |
|
|
306 |
case OK:
|
|
307 |
switch (initialHSStatus) {
|
|
308 |
case NOT_HANDSHAKING:
|
|
309 |
throw new IOException(
|
|
310 |
"Not handshaking during initial handshake");
|
|
311 |
|
|
312 |
case NEED_TASK:
|
|
313 |
initialHSStatus = doTasks();
|
|
314 |
break;
|
|
315 |
|
|
316 |
case FINISHED:
|
|
317 |
initialHSComplete = true;
|
|
318 |
break needIO;
|
|
319 |
}
|
|
320 |
|
|
321 |
break;
|
|
322 |
|
|
323 |
case BUFFER_UNDERFLOW:
|
|
324 |
// Resize buffer if needed.
|
|
325 |
netBBSize = sslEngine.getSession().getPacketBufferSize();
|
|
326 |
if (netBBSize > inNetBB.capacity()) {
|
|
327 |
resizeResponseBB();
|
|
328 |
}
|
|
329 |
|
|
330 |
/*
|
|
331 |
* Need to go reread the Channel for more data.
|
|
332 |
*/
|
|
333 |
if (sk != null) {
|
|
334 |
sk.interestOps(SelectionKey.OP_READ);
|
|
335 |
}
|
|
336 |
break needIO;
|
|
337 |
|
|
338 |
case BUFFER_OVERFLOW:
|
|
339 |
// Reset the application buffer size.
|
|
340 |
appBBSize =
|
|
341 |
sslEngine.getSession().getApplicationBufferSize();
|
|
342 |
break;
|
|
343 |
|
|
344 |
default: //CLOSED:
|
|
345 |
throw new IOException("Received" + result.getStatus() +
|
|
346 |
"during initial handshaking");
|
|
347 |
}
|
|
348 |
} // "needIO" block.
|
|
349 |
|
|
350 |
/*
|
|
351 |
* Just transitioned from read to write.
|
|
352 |
*/
|
|
353 |
if (initialHSStatus != HandshakeStatus.NEED_WRAP) {
|
|
354 |
break;
|
|
355 |
}
|
|
356 |
|
|
357 |
// Fall through and fill the write buffers.
|
|
358 |
|
|
359 |
case NEED_WRAP:
|
|
360 |
/*
|
|
361 |
* The flush above guarantees the out buffer to be empty
|
|
362 |
*/
|
|
363 |
outNetBB.clear();
|
|
364 |
result = sslEngine.wrap(hsBB, outNetBB);
|
|
365 |
outNetBB.flip();
|
|
366 |
|
|
367 |
initialHSStatus = result.getHandshakeStatus();
|
|
368 |
|
|
369 |
switch (result.getStatus()) {
|
|
370 |
case OK:
|
|
371 |
|
|
372 |
if (initialHSStatus == HandshakeStatus.NEED_TASK) {
|
|
373 |
initialHSStatus = doTasks();
|
|
374 |
}
|
|
375 |
|
|
376 |
if (sk != null) {
|
|
377 |
sk.interestOps(SelectionKey.OP_WRITE);
|
|
378 |
}
|
|
379 |
|
|
380 |
break;
|
|
381 |
|
|
382 |
default: // BUFFER_OVERFLOW/BUFFER_UNDERFLOW/CLOSED:
|
|
383 |
throw new IOException("Received" + result.getStatus() +
|
|
384 |
"during initial handshaking");
|
|
385 |
}
|
|
386 |
break;
|
|
387 |
|
|
388 |
default: // NOT_HANDSHAKING/NEED_TASK/FINISHED
|
|
389 |
throw new RuntimeException("Invalid Handshaking State" +
|
|
390 |
initialHSStatus);
|
|
391 |
} // switch
|
|
392 |
|
|
393 |
return initialHSComplete;
|
|
394 |
}
|
|
395 |
|
|
396 |
/*
|
|
397 |
* Do all the outstanding handshake tasks in the current Thread.
|
|
398 |
*/
|
|
399 |
private SSLEngineResult.HandshakeStatus doTasks() {
|
|
400 |
|
|
401 |
Runnable runnable;
|
|
402 |
|
|
403 |
/*
|
|
404 |
* We could run this in a separate thread, but
|
|
405 |
* do in the current for now.
|
|
406 |
*/
|
|
407 |
while ((runnable = sslEngine.getDelegatedTask()) != null) {
|
|
408 |
runnable.run();
|
|
409 |
}
|
|
410 |
return sslEngine.getHandshakeStatus();
|
|
411 |
}
|
|
412 |
|
|
413 |
/*
|
|
414 |
* Read the channel for more information, then unwrap the
|
|
415 |
* (hopefully application) data we get.
|
|
416 |
* <P>
|
|
417 |
* If we run out of data, we'll return to our caller (possibly using
|
|
418 |
* a Selector) to get notification that more is available.
|
|
419 |
* <P>
|
|
420 |
* Each call to this method will perform at most one underlying read().
|
|
421 |
*/
|
|
422 |
int read() throws IOException {
|
|
423 |
SSLEngineResult result;
|
|
424 |
|
|
425 |
if (!initialHSComplete) {
|
|
426 |
throw new IllegalStateException();
|
|
427 |
}
|
|
428 |
|
|
429 |
int pos = requestBB.position();
|
|
430 |
|
|
431 |
if (sc.read(inNetBB) == -1) {
|
|
432 |
sslEngine.closeInbound(); // probably throws exception
|
|
433 |
return -1;
|
|
434 |
}
|
|
435 |
|
|
436 |
do {
|
|
437 |
resizeRequestBB(); // expected room for unwrap
|
|
438 |
inNetBB.flip();
|
|
439 |
result = sslEngine.unwrap(inNetBB, requestBB);
|
|
440 |
inNetBB.compact();
|
|
441 |
|
|
442 |
/*
|
|
443 |
* Could check here for a renegotation, but we're only
|
|
444 |
* doing a simple read/write, and won't have enough state
|
|
445 |
* transitions to do a complete handshake, so ignore that
|
|
446 |
* possibility.
|
|
447 |
*/
|
|
448 |
switch (result.getStatus()) {
|
|
449 |
|
|
450 |
case BUFFER_OVERFLOW:
|
|
451 |
// Reset the application buffer size.
|
|
452 |
appBBSize = sslEngine.getSession().getApplicationBufferSize();
|
|
453 |
break;
|
|
454 |
|
|
455 |
case BUFFER_UNDERFLOW:
|
|
456 |
// Resize buffer if needed.
|
|
457 |
netBBSize = sslEngine.getSession().getPacketBufferSize();
|
|
458 |
if (netBBSize > inNetBB.capacity()) {
|
|
459 |
resizeResponseBB();
|
|
460 |
|
|
461 |
break; // break, next read will support larger buffer.
|
|
462 |
}
|
|
463 |
case OK:
|
|
464 |
if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
|
|
465 |
doTasks();
|
|
466 |
}
|
|
467 |
break;
|
|
468 |
|
|
469 |
default:
|
|
470 |
throw new IOException("sslEngine error during data read: " +
|
|
471 |
result.getStatus());
|
|
472 |
}
|
|
473 |
} while ((inNetBB.position() != 0) &&
|
|
474 |
result.getStatus() != Status.BUFFER_UNDERFLOW);
|
|
475 |
|
|
476 |
return (requestBB.position() - pos);
|
|
477 |
}
|
|
478 |
|
|
479 |
/*
|
|
480 |
* Try to write out as much as possible from the src buffer.
|
|
481 |
*/
|
|
482 |
int write(ByteBuffer src) throws IOException {
|
|
483 |
|
|
484 |
if (!initialHSComplete) {
|
|
485 |
throw new IllegalStateException();
|
|
486 |
}
|
|
487 |
|
|
488 |
return doWrite(src);
|
|
489 |
}
|
|
490 |
|
|
491 |
/*
|
|
492 |
* Try to flush out any existing outbound data, then try to wrap
|
|
493 |
* anything new contained in the src buffer.
|
|
494 |
* <P>
|
|
495 |
* Return the number of bytes actually consumed from the buffer,
|
|
496 |
* but the data may actually be still sitting in the output buffer,
|
|
497 |
* waiting to be flushed.
|
|
498 |
*/
|
|
499 |
private int doWrite(ByteBuffer src) throws IOException {
|
|
500 |
int retValue = 0;
|
|
501 |
|
|
502 |
if (outNetBB.hasRemaining() && !tryFlush(outNetBB)) {
|
|
503 |
return retValue;
|
|
504 |
}
|
|
505 |
|
|
506 |
/*
|
|
507 |
* The data buffer is empty, we can reuse the entire buffer.
|
|
508 |
*/
|
|
509 |
outNetBB.clear();
|
|
510 |
|
|
511 |
SSLEngineResult result = sslEngine.wrap(src, outNetBB);
|
|
512 |
retValue = result.bytesConsumed();
|
|
513 |
|
|
514 |
outNetBB.flip();
|
|
515 |
|
|
516 |
switch (result.getStatus()) {
|
|
517 |
|
|
518 |
case OK:
|
|
519 |
if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
|
|
520 |
doTasks();
|
|
521 |
}
|
|
522 |
break;
|
|
523 |
|
|
524 |
default:
|
|
525 |
throw new IOException("sslEngine error during data write: " +
|
|
526 |
result.getStatus());
|
|
527 |
}
|
|
528 |
|
|
529 |
/*
|
|
530 |
* Try to flush the data, regardless of whether or not
|
|
531 |
* it's been selected. Odds of a write buffer being full
|
|
532 |
* is less than a read buffer being empty.
|
|
533 |
*/
|
|
534 |
if (outNetBB.hasRemaining()) {
|
|
535 |
tryFlush(outNetBB);
|
|
536 |
}
|
|
537 |
|
|
538 |
return retValue;
|
|
539 |
}
|
|
540 |
|
|
541 |
/*
|
|
542 |
* Perform a FileChannel.TransferTo on the socket channel.
|
|
543 |
* <P>
|
|
544 |
* We have to copy the data into an intermediary app ByteBuffer
|
|
545 |
* first, then send it through the SSLEngine.
|
|
546 |
* <P>
|
|
547 |
* We return the number of bytes actually read out of the
|
|
548 |
* filechannel. However, the data may actually be stuck
|
|
549 |
* in the fileChannelBB or the outNetBB. The caller
|
|
550 |
* is responsible for making sure to call dataFlush()
|
|
551 |
* before shutting down.
|
|
552 |
*/
|
|
553 |
long transferTo(FileChannel fc, long pos, long len) throws IOException {
|
|
554 |
|
|
555 |
if (!initialHSComplete) {
|
|
556 |
throw new IllegalStateException();
|
|
557 |
}
|
|
558 |
|
|
559 |
if (fileChannelBB == null) {
|
|
560 |
fileChannelBB = ByteBuffer.allocate(appBBSize);
|
|
561 |
fileChannelBB.limit(0);
|
|
562 |
}
|
|
563 |
|
|
564 |
fileChannelBB.compact();
|
|
565 |
int fileRead = fc.read(fileChannelBB);
|
|
566 |
fileChannelBB.flip();
|
|
567 |
|
|
568 |
/*
|
|
569 |
* We ignore the return value here, we return the
|
|
570 |
* number of bytes actually consumed from the the file.
|
|
571 |
* We'll flush the output buffer before we start shutting down.
|
|
572 |
*/
|
|
573 |
doWrite(fileChannelBB);
|
|
574 |
|
|
575 |
return fileRead;
|
|
576 |
}
|
|
577 |
|
|
578 |
/*
|
|
579 |
* Flush any remaining data.
|
|
580 |
* <P>
|
|
581 |
* Return true when the fileChannelBB and outNetBB are empty.
|
|
582 |
*/
|
|
583 |
boolean dataFlush() throws IOException {
|
|
584 |
boolean fileFlushed = true;
|
|
585 |
|
|
586 |
if ((fileChannelBB != null) && fileChannelBB.hasRemaining()) {
|
|
587 |
doWrite(fileChannelBB);
|
|
588 |
fileFlushed = !fileChannelBB.hasRemaining();
|
|
589 |
} else if (outNetBB.hasRemaining()) {
|
|
590 |
tryFlush(outNetBB);
|
|
591 |
}
|
|
592 |
|
|
593 |
return (fileFlushed && !outNetBB.hasRemaining());
|
|
594 |
}
|
|
595 |
|
|
596 |
/*
|
|
597 |
* Begin the shutdown process.
|
|
598 |
* <P>
|
|
599 |
* Close out the SSLEngine if not already done so, then
|
|
600 |
* wrap our outgoing close_notify message and try to send it on.
|
|
601 |
* <P>
|
|
602 |
* Return true when we're done passing the shutdown messsages.
|
|
603 |
*/
|
|
604 |
boolean shutdown() throws IOException {
|
|
605 |
|
|
606 |
if (!shutdown) {
|
|
607 |
sslEngine.closeOutbound();
|
|
608 |
shutdown = true;
|
|
609 |
}
|
|
610 |
|
|
611 |
if (outNetBB.hasRemaining() && tryFlush(outNetBB)) {
|
|
612 |
return false;
|
|
613 |
}
|
|
614 |
|
|
615 |
/*
|
|
616 |
* By RFC 2616, we can "fire and forget" our close_notify
|
|
617 |
* message, so that's what we'll do here.
|
|
618 |
*/
|
|
619 |
outNetBB.clear();
|
|
620 |
SSLEngineResult result = sslEngine.wrap(hsBB, outNetBB);
|
|
621 |
if (result.getStatus() != Status.CLOSED) {
|
|
622 |
throw new SSLException("Improper close state");
|
|
623 |
}
|
|
624 |
outNetBB.flip();
|
|
625 |
|
|
626 |
/*
|
|
627 |
* We won't wait for a select here, but if this doesn't work,
|
|
628 |
* we'll cycle back through on the next select.
|
|
629 |
*/
|
|
630 |
if (outNetBB.hasRemaining()) {
|
|
631 |
tryFlush(outNetBB);
|
|
632 |
}
|
|
633 |
|
|
634 |
return (!outNetBB.hasRemaining() &&
|
|
635 |
(result.getHandshakeStatus() != HandshakeStatus.NEED_WRAP));
|
|
636 |
}
|
|
637 |
|
|
638 |
/*
|
|
639 |
* close() is not overridden
|
|
640 |
*/
|
|
641 |
}
|