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