author | dfuchs |
Mon, 23 Apr 2018 15:45:40 +0100 | |
branch | http-client-branch |
changeset 56474 | fe2bf7b369b8 |
parent 56463 | b583caf69b39 |
child 56481 | 247ed0848e48 |
permissions | -rw-r--r-- |
48083 | 1 |
/* |
49765 | 2 |
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. |
48083 | 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 |
||
49765 | 26 |
package jdk.internal.net.http.common; |
48083 | 27 |
|
49765 | 28 |
import jdk.internal.net.http.common.SubscriberWrapper.SchedulingAction; |
29 |
||
48083 | 30 |
import javax.net.ssl.SSLEngine; |
31 |
import javax.net.ssl.SSLEngineResult; |
|
32 |
import javax.net.ssl.SSLEngineResult.HandshakeStatus; |
|
33 |
import javax.net.ssl.SSLEngineResult.Status; |
|
34 |
import javax.net.ssl.SSLException; |
|
49765 | 35 |
import java.io.IOException; |
36 |
import java.nio.ByteBuffer; |
|
37 |
import java.util.ArrayList; |
|
38 |
import java.util.Collections; |
|
39 |
import java.util.Iterator; |
|
40 |
import java.util.LinkedList; |
|
41 |
import java.util.List; |
|
42 |
import java.util.concurrent.CompletableFuture; |
|
43 |
import java.util.concurrent.ConcurrentLinkedQueue; |
|
44 |
import java.util.concurrent.Executor; |
|
45 |
import java.util.concurrent.Flow; |
|
46 |
import java.util.concurrent.Flow.Subscriber; |
|
47 |
import java.util.concurrent.atomic.AtomicInteger; |
|
56474
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
48 |
import java.util.function.Consumer; |
48083 | 49 |
|
50 |
/** |
|
51 |
* Implements SSL using two SubscriberWrappers. |
|
52 |
* |
|
53 |
* <p> Constructor takes two Flow.Subscribers: one that receives the network |
|
54 |
* data (after it has been encrypted by SSLFlowDelegate) data, and one that |
|
55 |
* receives the application data (before it has been encrypted by SSLFlowDelegate). |
|
56 |
* |
|
57 |
* <p> Methods upstreamReader() and upstreamWriter() return the corresponding |
|
58 |
* Flow.Subscribers containing Flows for the encrypted/decrypted upstream data. |
|
59 |
* See diagram below. |
|
60 |
* |
|
61 |
* <p> How Flow.Subscribers are used in this class, and where they come from: |
|
62 |
* <pre> |
|
63 |
* {@code |
|
64 |
* |
|
65 |
* |
|
66 |
* |
|
67 |
* ---------> data flow direction |
|
68 |
* |
|
69 |
* |
|
70 |
* +------------------+ |
|
71 |
* upstreamWriter | | downWriter |
|
72 |
* ---------------> | | ------------> |
|
73 |
* obtained from this | | supplied to constructor |
|
74 |
* | SSLFlowDelegate | |
|
75 |
* downReader | | upstreamReader |
|
76 |
* <--------------- | | <-------------- |
|
77 |
* supplied to constructor | | obtained from this |
|
78 |
* +------------------+ |
|
49765 | 79 |
* |
80 |
* Errors are reported to the downReader Flow.Subscriber |
|
81 |
* |
|
48083 | 82 |
* } |
83 |
* </pre> |
|
84 |
*/ |
|
85 |
public class SSLFlowDelegate { |
|
86 |
||
49765 | 87 |
final Logger debug = |
88 |
Utils.getDebugLogger(this::dbgString, Utils.DEBUG); |
|
48083 | 89 |
|
90 |
final Executor exec; |
|
91 |
final Reader reader; |
|
92 |
final Writer writer; |
|
93 |
final SSLEngine engine; |
|
94 |
final String tubeName; // hack |
|
95 |
final CompletableFuture<String> alpnCF; // completes on initial handshake |
|
96 |
final static ByteBuffer SENTINEL = Utils.EMPTY_BYTEBUFFER; |
|
97 |
volatile boolean close_notify_received; |
|
49765 | 98 |
final CompletableFuture<Void> readerCF; |
99 |
final CompletableFuture<Void> writerCF; |
|
56474
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
100 |
final Consumer<ByteBuffer> recycler; |
49765 | 101 |
static AtomicInteger scount = new AtomicInteger(1); |
102 |
final int id; |
|
48083 | 103 |
|
104 |
/** |
|
105 |
* Creates an SSLFlowDelegate fed from two Flow.Subscribers. Each |
|
106 |
* Flow.Subscriber requires an associated {@link CompletableFuture} |
|
107 |
* for errors that need to be signaled from downstream to upstream. |
|
108 |
*/ |
|
109 |
public SSLFlowDelegate(SSLEngine engine, |
|
110 |
Executor exec, |
|
111 |
Subscriber<? super List<ByteBuffer>> downReader, |
|
112 |
Subscriber<? super List<ByteBuffer>> downWriter) |
|
113 |
{ |
|
56474
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
114 |
this(engine, exec, null, downReader, downWriter); |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
115 |
} |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
116 |
|
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
117 |
/** |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
118 |
* Creates an SSLFlowDelegate fed from two Flow.Subscribers. Each |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
119 |
* Flow.Subscriber requires an associated {@link CompletableFuture} |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
120 |
* for errors that need to be signaled from downstream to upstream. |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
121 |
*/ |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
122 |
public SSLFlowDelegate(SSLEngine engine, |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
123 |
Executor exec, |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
124 |
Consumer<ByteBuffer> recycler, |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
125 |
Subscriber<? super List<ByteBuffer>> downReader, |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
126 |
Subscriber<? super List<ByteBuffer>> downWriter) |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
127 |
{ |
49765 | 128 |
this.id = scount.getAndIncrement(); |
48083 | 129 |
this.tubeName = String.valueOf(downWriter); |
56474
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
130 |
this.recycler = recycler; |
48083 | 131 |
this.reader = new Reader(); |
132 |
this.writer = new Writer(); |
|
133 |
this.engine = engine; |
|
134 |
this.exec = exec; |
|
135 |
this.handshakeState = new AtomicInteger(NOT_HANDSHAKING); |
|
49765 | 136 |
this.readerCF = reader.completion(); |
137 |
this.writerCF = reader.completion(); |
|
138 |
readerCF.exceptionally(this::stopOnError); |
|
139 |
writerCF.exceptionally(this::stopOnError); |
|
140 |
||
141 |
CompletableFuture.allOf(reader.completion(), writer.completion()) |
|
142 |
.thenRun(this::normalStop); |
|
48083 | 143 |
this.alpnCF = new MinimalFuture<>(); |
144 |
||
145 |
// connect the Reader to the downReader and the |
|
146 |
// Writer to the downWriter. |
|
147 |
connect(downReader, downWriter); |
|
148 |
||
149 |
//Monitor.add(this::monitor); |
|
150 |
} |
|
151 |
||
152 |
/** |
|
153 |
* Returns true if the SSLFlowDelegate has detected a TLS |
|
154 |
* close_notify from the server. |
|
155 |
* @return true, if a close_notify was detected. |
|
156 |
*/ |
|
157 |
public boolean closeNotifyReceived() { |
|
158 |
return close_notify_received; |
|
159 |
} |
|
160 |
||
161 |
/** |
|
162 |
* Connects the read sink (downReader) to the SSLFlowDelegate Reader, |
|
163 |
* and the write sink (downWriter) to the SSLFlowDelegate Writer. |
|
164 |
* Called from within the constructor. Overwritten by SSLTube. |
|
165 |
* |
|
166 |
* @param downReader The left hand side read sink (typically, the |
|
167 |
* HttpConnection read subscriber). |
|
168 |
* @param downWriter The right hand side write sink (typically |
|
169 |
* the SocketTube write subscriber). |
|
170 |
*/ |
|
171 |
void connect(Subscriber<? super List<ByteBuffer>> downReader, |
|
172 |
Subscriber<? super List<ByteBuffer>> downWriter) { |
|
173 |
this.reader.subscribe(downReader); |
|
174 |
this.writer.subscribe(downWriter); |
|
175 |
} |
|
176 |
||
177 |
/** |
|
178 |
* Returns a CompletableFuture<String> which completes after |
|
179 |
* the initial handshake completes, and which contains the negotiated |
|
180 |
* alpn. |
|
181 |
*/ |
|
182 |
public CompletableFuture<String> alpn() { |
|
183 |
return alpnCF; |
|
184 |
} |
|
185 |
||
186 |
private void setALPN() { |
|
187 |
// Handshake is finished. So, can retrieve the ALPN now |
|
188 |
if (alpnCF.isDone()) |
|
189 |
return; |
|
190 |
String alpn = engine.getApplicationProtocol(); |
|
49765 | 191 |
if (debug.on()) debug.log("setALPN = %s", alpn); |
48083 | 192 |
alpnCF.complete(alpn); |
193 |
} |
|
194 |
||
195 |
public String monitor() { |
|
196 |
StringBuilder sb = new StringBuilder(); |
|
49765 | 197 |
sb.append("SSL: id ").append(id); |
198 |
sb.append(" HS state: " + states(handshakeState)); |
|
48083 | 199 |
sb.append(" Engine state: " + engine.getHandshakeStatus().toString()); |
200 |
sb.append(" LL : "); |
|
49765 | 201 |
for (String s: stateList) { |
202 |
sb.append(s).append(" "); |
|
48083 | 203 |
} |
204 |
sb.append("\r\n"); |
|
205 |
sb.append("Reader:: ").append(reader.toString()); |
|
206 |
sb.append("\r\n"); |
|
207 |
sb.append("Writer:: ").append(writer.toString()); |
|
208 |
sb.append("\r\n==================================="); |
|
209 |
return sb.toString(); |
|
210 |
} |
|
211 |
||
212 |
protected SchedulingAction enterReadScheduling() { |
|
213 |
return SchedulingAction.CONTINUE; |
|
214 |
} |
|
215 |
||
216 |
||
217 |
/** |
|
218 |
* Processing function for incoming data. Pass it thru SSLEngine.unwrap(). |
|
219 |
* Any decrypted buffers returned to be passed downstream. |
|
220 |
* Status codes: |
|
221 |
* NEED_UNWRAP: do nothing. Following incoming data will contain |
|
222 |
* any required handshake data |
|
223 |
* NEED_WRAP: call writer.addData() with empty buffer |
|
224 |
* NEED_TASK: delegate task to executor |
|
225 |
* BUFFER_OVERFLOW: allocate larger output buffer. Repeat unwrap |
|
226 |
* BUFFER_UNDERFLOW: keep buffer and wait for more data |
|
227 |
* OK: return generated buffers. |
|
228 |
* |
|
229 |
* Upstream subscription strategy is to try and keep no more than |
|
230 |
* TARGET_BUFSIZE bytes in readBuf |
|
231 |
*/ |
|
56474
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
232 |
class Reader extends SubscriberWrapper implements FlowTube.TubeSubscriber { |
56463
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
233 |
// Maximum record size is 16k. |
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
234 |
// Because SocketTube can feeds us up to 3 16K buffers, |
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
235 |
// then setting this size to 16K means that the readBuf |
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
236 |
// can store up to 64K-1 (16K-1 + 3*16K) |
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
237 |
static final int TARGET_BUFSIZE = 16 * 1024; |
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
238 |
|
48083 | 239 |
final SequentialScheduler scheduler; |
240 |
volatile ByteBuffer readBuf; |
|
49765 | 241 |
volatile boolean completing; |
48083 | 242 |
final Object readBufferLock = new Object(); |
49765 | 243 |
final Logger debugr = Utils.getDebugLogger(this::dbgString, Utils.DEBUG); |
48083 | 244 |
|
245 |
class ReaderDownstreamPusher implements Runnable { |
|
246 |
@Override public void run() { processData(); } |
|
247 |
} |
|
248 |
||
249 |
Reader() { |
|
250 |
super(); |
|
251 |
scheduler = SequentialScheduler.synchronizedScheduler( |
|
252 |
new ReaderDownstreamPusher()); |
|
253 |
this.readBuf = ByteBuffer.allocate(1024); |
|
254 |
readBuf.limit(0); // keep in read mode |
|
255 |
} |
|
256 |
||
56474
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
257 |
@Override |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
258 |
public boolean supportsRecycling() { |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
259 |
return recycler != null; |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
260 |
} |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
261 |
|
48083 | 262 |
protected SchedulingAction enterScheduling() { |
263 |
return enterReadScheduling(); |
|
264 |
} |
|
265 |
||
266 |
public final String dbgString() { |
|
267 |
return "SSL Reader(" + tubeName + ")"; |
|
268 |
} |
|
269 |
||
270 |
/** |
|
271 |
* entry point for buffers delivered from upstream Subscriber |
|
272 |
*/ |
|
273 |
@Override |
|
274 |
public void incoming(List<ByteBuffer> buffers, boolean complete) { |
|
49765 | 275 |
if (debugr.on()) |
276 |
debugr.log("Adding %d bytes to read buffer", |
|
277 |
Utils.remaining(buffers)); |
|
48083 | 278 |
addToReadBuf(buffers, complete); |
56463
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
279 |
scheduler.runOrSchedule(exec); |
48083 | 280 |
} |
281 |
||
282 |
@Override |
|
283 |
public String toString() { |
|
284 |
return "READER: " + super.toString() + " readBuf: " + readBuf.toString() |
|
285 |
+ " count: " + count.toString(); |
|
286 |
} |
|
287 |
||
288 |
private void reallocReadBuf() { |
|
289 |
int sz = readBuf.capacity(); |
|
290 |
ByteBuffer newb = ByteBuffer.allocate(sz*2); |
|
291 |
readBuf.flip(); |
|
292 |
Utils.copy(readBuf, newb); |
|
293 |
readBuf = newb; |
|
294 |
} |
|
295 |
||
296 |
@Override |
|
297 |
protected long upstreamWindowUpdate(long currentWindow, long downstreamQsize) { |
|
298 |
if (readBuf.remaining() > TARGET_BUFSIZE) { |
|
56463
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
299 |
if (debugr.on()) |
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
300 |
debugr.log("readBuf has more than TARGET_BUFSIZE: %d", |
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
301 |
readBuf.remaining()); |
48083 | 302 |
return 0; |
303 |
} else { |
|
304 |
return super.upstreamWindowUpdate(currentWindow, downstreamQsize); |
|
305 |
} |
|
306 |
} |
|
307 |
||
308 |
// readBuf is kept ready for reading outside of this method |
|
309 |
private void addToReadBuf(List<ByteBuffer> buffers, boolean complete) { |
|
310 |
synchronized (readBufferLock) { |
|
311 |
for (ByteBuffer buf : buffers) { |
|
312 |
readBuf.compact(); |
|
313 |
while (readBuf.remaining() < buf.remaining()) |
|
314 |
reallocReadBuf(); |
|
315 |
readBuf.put(buf); |
|
316 |
readBuf.flip(); |
|
56474
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
317 |
// should be safe to call inside lock |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
318 |
// since the only implementation |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
319 |
// offers the buffer to an unbounded queue. |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
320 |
// WARNING: do not touch buf after this point! |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56463
diff
changeset
|
321 |
if (recycler != null) recycler.accept(buf); |
48083 | 322 |
} |
323 |
if (complete) { |
|
324 |
this.completing = complete; |
|
325 |
} |
|
326 |
} |
|
327 |
} |
|
328 |
||
329 |
void schedule() { |
|
56463
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
330 |
scheduler.runOrSchedule(exec); |
48083 | 331 |
} |
332 |
||
333 |
void stop() { |
|
49765 | 334 |
if (debugr.on()) debugr.log("stop"); |
48083 | 335 |
scheduler.stop(); |
336 |
} |
|
337 |
||
338 |
AtomicInteger count = new AtomicInteger(0); |
|
339 |
||
56463
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
340 |
// minimum number of bytes required to call unwrap. |
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
341 |
// Usually this is 0, unless there was a buffer underflow. |
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
342 |
// In this case we need to wait for more bytes than what |
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
343 |
// we had before calling unwrap() again. |
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
344 |
volatile int minBytesRequired; |
48083 | 345 |
// work function where it all happens |
346 |
void processData() { |
|
347 |
try { |
|
49765 | 348 |
if (debugr.on()) |
349 |
debugr.log("processData:" |
|
350 |
+ " readBuf remaining:" + readBuf.remaining() |
|
351 |
+ ", state:" + states(handshakeState) |
|
352 |
+ ", engine handshake status:" + engine.getHandshakeStatus()); |
|
48083 | 353 |
int len; |
354 |
boolean complete = false; |
|
56463
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
355 |
while (readBuf.remaining() > (len = minBytesRequired)) { |
48083 | 356 |
boolean handshaking = false; |
357 |
try { |
|
358 |
EngineResult result; |
|
359 |
synchronized (readBufferLock) { |
|
360 |
complete = this.completing; |
|
56463
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
361 |
if (debugr.on()) debugr.log("Unwrapping: %s", readBuf.remaining()); |
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
362 |
// Unless there is a BUFFER_UNDERFLOW, we should try to |
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
363 |
// unwrap any number of bytes. Set minBytesRequired to 0: |
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
364 |
// we only need to do that if minBytesRequired is not already 0. |
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
365 |
len = len > 0 ? minBytesRequired = 0 : len; |
48083 | 366 |
result = unwrapBuffer(readBuf); |
56463
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
367 |
len = readBuf.remaining(); |
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
368 |
if (debugr.on()) { |
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
369 |
debugr.log("Unwrapped: result: %s", result.result); |
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
370 |
debugr.log("Unwrapped: consumed: %s", result.bytesConsumed()); |
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
371 |
} |
48083 | 372 |
} |
373 |
if (result.bytesProduced() > 0) { |
|
49765 | 374 |
if (debugr.on()) |
375 |
debugr.log("sending %d", result.bytesProduced()); |
|
48083 | 376 |
count.addAndGet(result.bytesProduced()); |
377 |
outgoing(result.destBuffer, false); |
|
378 |
} |
|
379 |
if (result.status() == Status.BUFFER_UNDERFLOW) { |
|
49765 | 380 |
if (debugr.on()) debugr.log("BUFFER_UNDERFLOW"); |
48083 | 381 |
// not enough data in the read buffer... |
56463
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
382 |
// no need to try to unwrap again unless we get more bytes |
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
383 |
// than minBytesRequired = len in the read buffer. |
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
384 |
minBytesRequired = len; |
48083 | 385 |
synchronized (readBufferLock) { |
56463
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
386 |
// more bytes could already have been added... |
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
387 |
assert readBuf.remaining() >= len; |
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
388 |
// check if we have received some data, and if so |
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
389 |
// we can just re-spin the loop |
48083 | 390 |
if (readBuf.remaining() > len) continue; |
391 |
} |
|
56463
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
392 |
// request more data and return. |
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
393 |
requestMore(); |
b583caf69b39
http-client-branch: do not set receive buffer size unless explicitly requested
dfuchs
parents:
56451
diff
changeset
|
394 |
return; |
48083 | 395 |
} |
396 |
if (complete && result.status() == Status.CLOSED) { |
|
49765 | 397 |
if (debugr.on()) debugr.log("Closed: completing"); |
48083 | 398 |
outgoing(Utils.EMPTY_BB_LIST, true); |
399 |
return; |
|
400 |
} |
|
401 |
if (result.handshaking() && !complete) { |
|
49765 | 402 |
if (debugr.on()) debugr.log("handshaking"); |
403 |
if (doHandshake(result, READER)) { |
|
404 |
resumeActivity(); |
|
405 |
} |
|
48083 | 406 |
handshaking = true; |
407 |
} else { |
|
49765 | 408 |
if ((handshakeState.getAndSet(NOT_HANDSHAKING)& ~DOING_TASKS) == HANDSHAKING) { |
48083 | 409 |
setALPN(); |
410 |
handshaking = false; |
|
411 |
resumeActivity(); |
|
412 |
} |
|
413 |
} |
|
414 |
} catch (IOException ex) { |
|
415 |
errorCommon(ex); |
|
416 |
handleError(ex); |
|
417 |
} |
|
418 |
if (handshaking && !complete) |
|
419 |
return; |
|
420 |
} |
|
421 |
if (!complete) { |
|
422 |
synchronized (readBufferLock) { |
|
423 |
complete = this.completing && !readBuf.hasRemaining(); |
|
424 |
} |
|
425 |
} |
|
426 |
if (complete) { |
|
49765 | 427 |
if (debugr.on()) debugr.log("completing"); |
48083 | 428 |
// Complete the alpnCF, if not already complete, regardless of |
429 |
// whether or not the ALPN is available, there will be no more |
|
430 |
// activity. |
|
431 |
setALPN(); |
|
432 |
outgoing(Utils.EMPTY_BB_LIST, true); |
|
433 |
} |
|
434 |
} catch (Throwable ex) { |
|
435 |
errorCommon(ex); |
|
436 |
handleError(ex); |
|
437 |
} |
|
438 |
} |
|
439 |
||
49765 | 440 |
EngineResult unwrapBuffer(ByteBuffer src) throws IOException { |
441 |
ByteBuffer dst = getAppBuffer(); |
|
442 |
while (true) { |
|
443 |
SSLEngineResult sslResult = engine.unwrap(src, dst); |
|
444 |
switch (sslResult.getStatus()) { |
|
445 |
case BUFFER_OVERFLOW: |
|
446 |
// may happen only if app size buffer was changed. |
|
447 |
// get it again if app buffer size changed |
|
448 |
int appSize = engine.getSession().getApplicationBufferSize(); |
|
449 |
ByteBuffer b = ByteBuffer.allocate(appSize + dst.position()); |
|
450 |
dst.flip(); |
|
451 |
b.put(dst); |
|
452 |
dst = b; |
|
453 |
break; |
|
454 |
case CLOSED: |
|
455 |
return doClosure(new EngineResult(sslResult)); |
|
456 |
case BUFFER_UNDERFLOW: |
|
457 |
// handled implicitly by compaction/reallocation of readBuf |
|
458 |
return new EngineResult(sslResult); |
|
459 |
case OK: |
|
460 |
dst.flip(); |
|
461 |
return new EngineResult(sslResult, dst); |
|
462 |
} |
|
463 |
} |
|
464 |
} |
|
48083 | 465 |
} |
466 |
||
467 |
public interface Monitorable { |
|
468 |
public String getInfo(); |
|
469 |
} |
|
470 |
||
471 |
public static class Monitor extends Thread { |
|
472 |
final List<Monitorable> list; |
|
473 |
static Monitor themon; |
|
474 |
||
475 |
static { |
|
476 |
themon = new Monitor(); |
|
477 |
themon.start(); // uncomment to enable Monitor |
|
478 |
} |
|
479 |
||
480 |
Monitor() { |
|
481 |
super("Monitor"); |
|
482 |
setDaemon(true); |
|
483 |
list = Collections.synchronizedList(new LinkedList<>()); |
|
484 |
} |
|
485 |
||
486 |
void addTarget(Monitorable o) { |
|
487 |
list.add(o); |
|
488 |
} |
|
489 |
||
490 |
public static void add(Monitorable o) { |
|
491 |
themon.addTarget(o); |
|
492 |
} |
|
493 |
||
494 |
@Override |
|
495 |
public void run() { |
|
496 |
System.out.println("Monitor starting"); |
|
49765 | 497 |
try { |
498 |
while (true) { |
|
499 |
Thread.sleep(20 * 1000); |
|
500 |
synchronized (list) { |
|
501 |
for (Monitorable o : list) { |
|
502 |
System.out.println(o.getInfo()); |
|
503 |
System.out.println("-------------------------"); |
|
504 |
} |
|
48083 | 505 |
} |
49765 | 506 |
System.out.println("--o-o-o-o-o-o-o-o-o-o-o-o-o-o-"); |
48083 | 507 |
} |
49765 | 508 |
} catch (InterruptedException e) { |
509 |
System.out.println("Monitor exiting with " + e); |
|
48083 | 510 |
} |
511 |
} |
|
512 |
} |
|
513 |
||
514 |
/** |
|
515 |
* Processing function for outgoing data. Pass it thru SSLEngine.wrap() |
|
516 |
* Any encrypted buffers generated are passed downstream to be written. |
|
517 |
* Status codes: |
|
518 |
* NEED_UNWRAP: call reader.addData() with empty buffer |
|
519 |
* NEED_WRAP: call addData() with empty buffer |
|
520 |
* NEED_TASK: delegate task to executor |
|
521 |
* BUFFER_OVERFLOW: allocate larger output buffer. Repeat wrap |
|
522 |
* BUFFER_UNDERFLOW: shouldn't happen on writing side |
|
523 |
* OK: return generated buffers |
|
524 |
*/ |
|
525 |
class Writer extends SubscriberWrapper { |
|
526 |
final SequentialScheduler scheduler; |
|
527 |
// queues of buffers received from upstream waiting |
|
528 |
// to be processed by the SSLEngine |
|
529 |
final List<ByteBuffer> writeList; |
|
49765 | 530 |
final Logger debugw = Utils.getDebugLogger(this::dbgString, Utils.DEBUG); |
48083 | 531 |
volatile boolean completing; |
532 |
boolean completed; // only accessed in processData |
|
533 |
||
534 |
class WriterDownstreamPusher extends SequentialScheduler.CompleteRestartableTask { |
|
535 |
@Override public void run() { processData(); } |
|
536 |
} |
|
537 |
||
538 |
Writer() { |
|
539 |
super(); |
|
540 |
writeList = Collections.synchronizedList(new LinkedList<>()); |
|
541 |
scheduler = new SequentialScheduler(new WriterDownstreamPusher()); |
|
542 |
} |
|
543 |
||
544 |
@Override |
|
545 |
protected void incoming(List<ByteBuffer> buffers, boolean complete) { |
|
546 |
assert complete ? buffers == Utils.EMPTY_BB_LIST : true; |
|
547 |
assert buffers != Utils.EMPTY_BB_LIST ? complete == false : true; |
|
548 |
if (complete) { |
|
49765 | 549 |
if (debugw.on()) debugw.log("adding SENTINEL"); |
48083 | 550 |
completing = true; |
551 |
writeList.add(SENTINEL); |
|
552 |
} else { |
|
553 |
writeList.addAll(buffers); |
|
554 |
} |
|
49765 | 555 |
if (debugw.on()) |
556 |
debugw.log("added " + buffers.size() |
|
557 |
+ " (" + Utils.remaining(buffers) |
|
558 |
+ " bytes) to the writeList"); |
|
48083 | 559 |
scheduler.runOrSchedule(); |
560 |
} |
|
561 |
||
562 |
public final String dbgString() { |
|
563 |
return "SSL Writer(" + tubeName + ")"; |
|
564 |
} |
|
565 |
||
566 |
protected void onSubscribe() { |
|
49765 | 567 |
if (debugw.on()) debugw.log("onSubscribe initiating handshaking"); |
568 |
addData(HS_TRIGGER); // initiates handshaking |
|
48083 | 569 |
} |
570 |
||
571 |
void schedule() { |
|
572 |
scheduler.runOrSchedule(); |
|
573 |
} |
|
574 |
||
575 |
void stop() { |
|
49765 | 576 |
if (debugw.on()) debugw.log("stop"); |
48083 | 577 |
scheduler.stop(); |
578 |
} |
|
579 |
||
580 |
@Override |
|
581 |
public boolean closing() { |
|
582 |
return closeNotifyReceived(); |
|
583 |
} |
|
584 |
||
585 |
private boolean isCompleting() { |
|
586 |
return completing; |
|
587 |
} |
|
588 |
||
589 |
@Override |
|
590 |
protected long upstreamWindowUpdate(long currentWindow, long downstreamQsize) { |
|
591 |
if (writeList.size() > 10) |
|
592 |
return 0; |
|
593 |
else |
|
594 |
return super.upstreamWindowUpdate(currentWindow, downstreamQsize); |
|
595 |
} |
|
596 |
||
597 |
private boolean hsTriggered() { |
|
598 |
synchronized(writeList) { |
|
599 |
for (ByteBuffer b : writeList) |
|
600 |
if (b == HS_TRIGGER) |
|
601 |
return true; |
|
602 |
return false; |
|
603 |
} |
|
604 |
} |
|
605 |
||
606 |
private void processData() { |
|
607 |
boolean completing = isCompleting(); |
|
608 |
||
609 |
try { |
|
49765 | 610 |
if (debugw.on()) |
611 |
debugw.log("processData, writeList remaining:" |
|
612 |
+ Utils.remaining(writeList) + ", hsTriggered:" |
|
613 |
+ hsTriggered() + ", needWrap:" + needWrap()); |
|
614 |
||
615 |
while (Utils.remaining(writeList) > 0 || hsTriggered() || needWrap()) { |
|
48083 | 616 |
ByteBuffer[] outbufs = writeList.toArray(Utils.EMPTY_BB_ARRAY); |
617 |
EngineResult result = wrapBuffers(outbufs); |
|
49765 | 618 |
if (debugw.on()) |
619 |
debugw.log("wrapBuffer returned %s", result.result); |
|
48083 | 620 |
|
621 |
if (result.status() == Status.CLOSED) { |
|
49765 | 622 |
if (!upstreamCompleted) { |
623 |
upstreamCompleted = true; |
|
624 |
upstreamSubscription.cancel(); |
|
625 |
} |
|
48083 | 626 |
if (result.bytesProduced() <= 0) |
627 |
return; |
|
628 |
||
629 |
if (!completing && !completed) { |
|
630 |
completing = this.completing = true; |
|
631 |
// There could still be some outgoing data in outbufs. |
|
632 |
writeList.add(SENTINEL); |
|
633 |
} |
|
634 |
} |
|
635 |
||
636 |
boolean handshaking = false; |
|
637 |
if (result.handshaking()) { |
|
49765 | 638 |
if (debugw.on()) debugw.log("handshaking"); |
639 |
doHandshake(result, WRITER); // ok to ignore return |
|
48083 | 640 |
handshaking = true; |
641 |
} else { |
|
642 |
if ((handshakeState.getAndSet(NOT_HANDSHAKING) & ~DOING_TASKS) == HANDSHAKING) { |
|
643 |
setALPN(); |
|
644 |
resumeActivity(); |
|
645 |
} |
|
646 |
} |
|
647 |
cleanList(writeList); // tidy up the source list |
|
648 |
sendResultBytes(result); |
|
649 |
if (handshaking && !completing) { |
|
49765 | 650 |
if (needWrap()) { |
651 |
continue; |
|
652 |
} else { |
|
653 |
return; |
|
48083 | 654 |
} |
655 |
} |
|
656 |
} |
|
657 |
if (completing && Utils.remaining(writeList) == 0) { |
|
658 |
if (!completed) { |
|
659 |
completed = true; |
|
660 |
writeList.clear(); |
|
661 |
outgoing(Utils.EMPTY_BB_LIST, true); |
|
662 |
} |
|
663 |
return; |
|
664 |
} |
|
665 |
if (writeList.isEmpty() && needWrap()) { |
|
666 |
writer.addData(HS_TRIGGER); |
|
667 |
} |
|
668 |
} catch (Throwable ex) { |
|
49765 | 669 |
errorCommon(ex); |
48083 | 670 |
handleError(ex); |
671 |
} |
|
672 |
} |
|
673 |
||
49765 | 674 |
@SuppressWarnings("fallthrough") |
675 |
EngineResult wrapBuffers(ByteBuffer[] src) throws SSLException { |
|
676 |
if (debugw.on()) |
|
677 |
debugw.log("wrapping " + Utils.remaining(src) + " bytes"); |
|
678 |
ByteBuffer dst = getNetBuffer(); |
|
679 |
while (true) { |
|
680 |
SSLEngineResult sslResult = engine.wrap(src, dst); |
|
681 |
if (debugw.on()) debugw.log("SSLResult: " + sslResult); |
|
682 |
switch (sslResult.getStatus()) { |
|
683 |
case BUFFER_OVERFLOW: |
|
684 |
// Shouldn't happen. We allocated buffer with packet size |
|
685 |
// get it again if net buffer size was changed |
|
686 |
if (debugw.on()) debugw.log("BUFFER_OVERFLOW"); |
|
687 |
int appSize = engine.getSession().getApplicationBufferSize(); |
|
688 |
ByteBuffer b = ByteBuffer.allocate(appSize + dst.position()); |
|
689 |
dst.flip(); |
|
690 |
b.put(dst); |
|
691 |
dst = b; |
|
692 |
break; // try again |
|
693 |
case CLOSED: |
|
694 |
if (debugw.on()) debugw.log("CLOSED"); |
|
695 |
// fallthrough. There could be some remaining data in dst. |
|
696 |
// CLOSED will be handled by the caller. |
|
697 |
case OK: |
|
698 |
dst.flip(); |
|
699 |
final ByteBuffer dest = dst; |
|
700 |
if (debugw.on()) |
|
701 |
debugw.log("OK => produced: %d, not wrapped: %d", |
|
702 |
dest.remaining(), Utils.remaining(src)); |
|
703 |
return new EngineResult(sslResult, dest); |
|
704 |
case BUFFER_UNDERFLOW: |
|
705 |
// Shouldn't happen. Doesn't returns when wrap() |
|
706 |
// underflow handled externally |
|
707 |
// assert false : "Buffer Underflow"; |
|
708 |
if (debug.on()) debug.log("BUFFER_UNDERFLOW"); |
|
709 |
return new EngineResult(sslResult); |
|
710 |
default: |
|
711 |
if (debugw.on()) |
|
712 |
debugw.log("result: %s", sslResult.getStatus()); |
|
713 |
assert false : "result:" + sslResult.getStatus(); |
|
714 |
} |
|
715 |
} |
|
716 |
} |
|
717 |
||
48083 | 718 |
private boolean needWrap() { |
719 |
return engine.getHandshakeStatus() == HandshakeStatus.NEED_WRAP; |
|
720 |
} |
|
721 |
||
722 |
private void sendResultBytes(EngineResult result) { |
|
723 |
if (result.bytesProduced() > 0) { |
|
49765 | 724 |
if (debugw.on()) |
725 |
debugw.log("Sending %d bytes downstream", |
|
726 |
result.bytesProduced()); |
|
48083 | 727 |
outgoing(result.destBuffer, false); |
728 |
} |
|
729 |
} |
|
730 |
||
731 |
@Override |
|
732 |
public String toString() { |
|
733 |
return "WRITER: " + super.toString() + |
|
734 |
" writeList size " + Integer.toString(writeList.size()); |
|
735 |
//" writeList: " + writeList.toString(); |
|
736 |
} |
|
737 |
} |
|
738 |
||
739 |
private void handleError(Throwable t) { |
|
49765 | 740 |
if (debug.on()) debug.log("handleError", t); |
741 |
readerCF.completeExceptionally(t); |
|
742 |
writerCF.completeExceptionally(t); |
|
48083 | 743 |
// no-op if already completed |
744 |
alpnCF.completeExceptionally(t); |
|
745 |
reader.stop(); |
|
746 |
writer.stop(); |
|
747 |
} |
|
748 |
||
49765 | 749 |
boolean stopped; |
750 |
||
751 |
private synchronized void normalStop() { |
|
752 |
if (stopped) |
|
753 |
return; |
|
754 |
stopped = true; |
|
48083 | 755 |
reader.stop(); |
756 |
writer.stop(); |
|
757 |
} |
|
758 |
||
49765 | 759 |
private Void stopOnError(Throwable currentlyUnused) { |
760 |
// maybe log, etc |
|
761 |
normalStop(); |
|
762 |
return null; |
|
763 |
} |
|
764 |
||
48083 | 765 |
private void cleanList(List<ByteBuffer> l) { |
766 |
synchronized (l) { |
|
767 |
Iterator<ByteBuffer> iter = l.iterator(); |
|
768 |
while (iter.hasNext()) { |
|
769 |
ByteBuffer b = iter.next(); |
|
770 |
if (!b.hasRemaining() && b != SENTINEL) { |
|
771 |
iter.remove(); |
|
772 |
} |
|
773 |
} |
|
774 |
} |
|
775 |
} |
|
776 |
||
777 |
/** |
|
778 |
* States for handshake. We avoid races when accessing/updating the AtomicInt |
|
779 |
* because updates always schedule an additional call to both the read() |
|
780 |
* and write() functions. |
|
781 |
*/ |
|
782 |
private static final int NOT_HANDSHAKING = 0; |
|
783 |
private static final int HANDSHAKING = 1; |
|
49765 | 784 |
|
48083 | 785 |
private static final int DOING_TASKS = 4; // bit added to above state |
786 |
private static final ByteBuffer HS_TRIGGER = ByteBuffer.allocate(0); |
|
787 |
||
788 |
private static final int READER = 1; |
|
789 |
private static final int WRITER = 2; |
|
790 |
||
791 |
private static String states(AtomicInteger state) { |
|
792 |
int s = state.get(); |
|
793 |
StringBuilder sb = new StringBuilder(); |
|
794 |
int x = s & ~DOING_TASKS; |
|
795 |
switch (x) { |
|
796 |
case NOT_HANDSHAKING: |
|
797 |
sb.append(" NOT_HANDSHAKING "); |
|
798 |
break; |
|
799 |
case HANDSHAKING: |
|
800 |
sb.append(" HANDSHAKING "); |
|
801 |
break; |
|
802 |
default: |
|
803 |
throw new InternalError(); |
|
804 |
} |
|
805 |
if ((s & DOING_TASKS) > 0) |
|
806 |
sb.append("|DOING_TASKS"); |
|
807 |
return sb.toString(); |
|
808 |
} |
|
809 |
||
810 |
private void resumeActivity() { |
|
811 |
reader.schedule(); |
|
812 |
writer.schedule(); |
|
813 |
} |
|
814 |
||
815 |
final AtomicInteger handshakeState; |
|
816 |
final ConcurrentLinkedQueue<String> stateList = new ConcurrentLinkedQueue<>(); |
|
817 |
||
49765 | 818 |
private boolean doHandshake(EngineResult r, int caller) { |
819 |
// unconditionally sets the HANDSHAKING bit, while preserving DOING_TASKS |
|
820 |
handshakeState.getAndAccumulate(HANDSHAKING, (current, update) -> update | (current & DOING_TASKS)); |
|
48083 | 821 |
stateList.add(r.handshakeStatus().toString()); |
822 |
stateList.add(Integer.toString(caller)); |
|
823 |
switch (r.handshakeStatus()) { |
|
824 |
case NEED_TASK: |
|
49765 | 825 |
int s = handshakeState.getAndUpdate((current) -> current | DOING_TASKS); |
48083 | 826 |
if ((s & DOING_TASKS) > 0) // someone else was doing tasks |
49765 | 827 |
return false; |
828 |
||
829 |
if (debug.on()) debug.log("obtaining and initiating task execution"); |
|
48083 | 830 |
List<Runnable> tasks = obtainTasks(); |
831 |
executeTasks(tasks); |
|
49765 | 832 |
return false; // executeTasks will resume activity |
48083 | 833 |
case NEED_WRAP: |
49765 | 834 |
if (caller == READER) { |
835 |
writer.addData(HS_TRIGGER); |
|
836 |
return false; |
|
837 |
} |
|
48083 | 838 |
break; |
839 |
case NEED_UNWRAP: |
|
840 |
case NEED_UNWRAP_AGAIN: |
|
841 |
// do nothing else |
|
49765 | 842 |
// receiving-side data will trigger unwrap |
48083 | 843 |
break; |
844 |
default: |
|
845 |
throw new InternalError("Unexpected handshake status:" |
|
846 |
+ r.handshakeStatus()); |
|
847 |
} |
|
49765 | 848 |
return true; |
48083 | 849 |
} |
850 |
||
851 |
private List<Runnable> obtainTasks() { |
|
852 |
List<Runnable> l = new ArrayList<>(); |
|
853 |
Runnable r; |
|
854 |
while ((r = engine.getDelegatedTask()) != null) { |
|
855 |
l.add(r); |
|
856 |
} |
|
857 |
return l; |
|
858 |
} |
|
859 |
||
860 |
private void executeTasks(List<Runnable> tasks) { |
|
49765 | 861 |
if (tasks.isEmpty()) |
862 |
return; |
|
48083 | 863 |
exec.execute(() -> { |
49765 | 864 |
try { |
865 |
List<Runnable> nextTasks = tasks; |
|
866 |
do { |
|
867 |
nextTasks.forEach(Runnable::run); |
|
48083 | 868 |
if (engine.getHandshakeStatus() == HandshakeStatus.NEED_TASK) { |
869 |
nextTasks = obtainTasks(); |
|
49765 | 870 |
} else { |
871 |
break; |
|
872 |
} |
|
873 |
} while (true); |
|
874 |
handshakeState.getAndUpdate((current) -> current & ~DOING_TASKS); |
|
875 |
//writer.addData(HS_TRIGGER); |
|
876 |
resumeActivity(); |
|
877 |
} catch (Throwable t) { |
|
878 |
handleError(t); |
|
879 |
} |
|
48083 | 880 |
}); |
881 |
} |
|
882 |
||
883 |
// FIXME: acknowledge a received CLOSE request from peer |
|
884 |
EngineResult doClosure(EngineResult r) throws IOException { |
|
49765 | 885 |
if (debug.on()) |
886 |
debug.log("doClosure(%s): %s [isOutboundDone: %s, isInboundDone: %s]", |
|
887 |
r.result, engine.getHandshakeStatus(), |
|
888 |
engine.isOutboundDone(), engine.isInboundDone()); |
|
48083 | 889 |
if (engine.getHandshakeStatus() == HandshakeStatus.NEED_WRAP) { |
890 |
// we have received TLS close_notify and need to send |
|
891 |
// an acknowledgement back. We're calling doHandshake |
|
892 |
// to finish the close handshake. |
|
893 |
if (engine.isInboundDone() && !engine.isOutboundDone()) { |
|
49765 | 894 |
if (debug.on()) debug.log("doClosure: close_notify received"); |
48083 | 895 |
close_notify_received = true; |
896 |
doHandshake(r, READER); |
|
897 |
} |
|
898 |
} |
|
899 |
return r; |
|
900 |
} |
|
901 |
||
902 |
/** |
|
903 |
* Returns the upstream Flow.Subscriber of the reading (incoming) side. |
|
904 |
* This flow must be given the encrypted data read from upstream (eg socket) |
|
905 |
* before it is decrypted. |
|
906 |
*/ |
|
907 |
public Flow.Subscriber<List<ByteBuffer>> upstreamReader() { |
|
908 |
return reader; |
|
909 |
} |
|
910 |
||
911 |
/** |
|
912 |
* Returns the upstream Flow.Subscriber of the writing (outgoing) side. |
|
913 |
* This flow contains the plaintext data before it is encrypted. |
|
914 |
*/ |
|
915 |
public Flow.Subscriber<List<ByteBuffer>> upstreamWriter() { |
|
916 |
return writer; |
|
917 |
} |
|
918 |
||
919 |
public boolean resumeReader() { |
|
920 |
return reader.signalScheduling(); |
|
921 |
} |
|
922 |
||
923 |
public void resetReaderDemand() { |
|
924 |
reader.resetDownstreamDemand(); |
|
925 |
} |
|
926 |
||
927 |
static class EngineResult { |
|
928 |
final SSLEngineResult result; |
|
929 |
final ByteBuffer destBuffer; |
|
930 |
||
931 |
// normal result |
|
932 |
EngineResult(SSLEngineResult result) { |
|
933 |
this(result, null); |
|
934 |
} |
|
935 |
||
936 |
EngineResult(SSLEngineResult result, ByteBuffer destBuffer) { |
|
937 |
this.result = result; |
|
938 |
this.destBuffer = destBuffer; |
|
939 |
} |
|
940 |
||
941 |
boolean handshaking() { |
|
942 |
HandshakeStatus s = result.getHandshakeStatus(); |
|
943 |
return s != HandshakeStatus.FINISHED |
|
944 |
&& s != HandshakeStatus.NOT_HANDSHAKING |
|
945 |
&& result.getStatus() != Status.CLOSED; |
|
946 |
} |
|
947 |
||
948 |
boolean needUnwrap() { |
|
949 |
HandshakeStatus s = result.getHandshakeStatus(); |
|
950 |
return s == HandshakeStatus.NEED_UNWRAP; |
|
951 |
} |
|
952 |
||
953 |
||
954 |
int bytesConsumed() { |
|
955 |
return result.bytesConsumed(); |
|
956 |
} |
|
957 |
||
958 |
int bytesProduced() { |
|
959 |
return result.bytesProduced(); |
|
960 |
} |
|
961 |
||
962 |
SSLEngineResult.HandshakeStatus handshakeStatus() { |
|
963 |
return result.getHandshakeStatus(); |
|
964 |
} |
|
965 |
||
966 |
SSLEngineResult.Status status() { |
|
967 |
return result.getStatus(); |
|
968 |
} |
|
969 |
} |
|
970 |
||
971 |
public ByteBuffer getNetBuffer() { |
|
972 |
return ByteBuffer.allocate(engine.getSession().getPacketBufferSize()); |
|
973 |
} |
|
974 |
||
975 |
private ByteBuffer getAppBuffer() { |
|
976 |
return ByteBuffer.allocate(engine.getSession().getApplicationBufferSize()); |
|
977 |
} |
|
978 |
||
979 |
final String dbgString() { |
|
980 |
return "SSLFlowDelegate(" + tubeName + ")"; |
|
981 |
} |
|
982 |
} |