Repository navigation
Expand file tree
/
Copy pathNativeSslEngineTest.java
More file actions
665 lines (637 loc) Β· 28.5 KB
/
Copy pathNativeSslEngineTest.java
File metadata and controls
665 lines (637 loc) Β· 28.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
import dev.elide.bemo.transport.*;
import dev.elide.bemo.transport.tls.*;
import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.Unpooled;
import io.netty.buffer.UnpooledByteBufAllocator;
import io.netty.channel.*;
import io.netty.channel.nio.NioIoHandler;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.*;
import io.netty.handler.codec.http2.*;
import io.netty.handler.ssl.*;
import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil;
import java.io.ByteArrayOutputStream;
import java.net.InetSocketAddress;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import javax.net.ssl.SSLHandshakeException;
/** Netty SslHandler contracts over NativeSslEngine, on v2 sockets and on NIO with heap buffers. */
public final class NativeSslEngineTest {
record Stack(
String name,
EventLoopGroup group,
Class<? extends ServerChannel> server,
Class<? extends Channel> client,
ByteBufAllocator allocator) {}
public static void main(String[] args) throws Exception {
TransportNative api = new BackendTransport(new FfmTransportNative(Path.of(args[0])));
verify(api, Files.readAllBytes(Path.of(args[1])), Files.readAllBytes(Path.of(args[2])));
}
public static void verify(TransportNative api, byte[] cert, byte[] key) throws Exception {
workloadClosure(api, cert);
EventLoopGroup v2 =
new MultiThreadIoEventLoopGroup(
2, NativeIoHandler.newFactory(api, 0, 128, 32 * 1024 * 1024));
EventLoopGroup nio = new MultiThreadIoEventLoopGroup(2, NioIoHandler.newFactory());
NativeSslContext server =
NativeSslContextBuilder.forServer(api, cert, key)
.applicationProtocols(
ApplicationProtocolNames.HTTP_2, ApplicationProtocolNames.HTTP_1_1)
.build();
NativeSslContext client =
NativeSslContextBuilder.forClient(api)
.trustAnchors(cert)
.applicationProtocols(
ApplicationProtocolNames.HTTP_2, ApplicationProtocolNames.HTTP_1_1)
.build();
NativeSslContext http1Client =
NativeSslContextBuilder.forClient(api)
.trustAnchors(cert)
.applicationProtocols(ApplicationProtocolNames.HTTP_1_1)
.build();
NativeSslContext plainClient =
NativeSslContextBuilder.forClient(api).trustAnchors(cert).build();
NativeSslContext insecureClient =
NativeSslContextBuilder.forClient(api)
.insecureSkipVerify(true)
.applicationProtocols(
ApplicationProtocolNames.HTTP_2, ApplicationProtocolNames.HTTP_1_1)
.build();
try {
for (Stack stack :
new Stack[] {
new Stack("v2", v2, NativeServerSocketChannel.class, NativeSocketChannel.class, null),
new Stack(
"nio-heap",
nio,
NioServerSocketChannel.class,
NioSocketChannel.class,
new UnpooledByteBufAllocator(false)),
}) {
echoAndSession(stack, server, client);
echoAndSession(stack, server, insecureClient);
closeNotify(stack, server, client);
wrongName(stack, server, client);
http1(stack, server, http1Client, ApplicationProtocolNames.HTTP_1_1);
http1(stack, server, plainClient, null);
http2(stack, server, client);
System.out.println("SslHandler contracts over NativeSslEngine passed: " + stack.name());
}
derContext(api, cert, key, v2);
} finally {
for (NativeSslContext context :
new NativeSslContext[] {
server, client, http1Client, plainClient, insecureClient,
}) {
// Engines release their context when SslHandler leaves the pipeline, after close completes.
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5);
while (context.refCnt() > 1 && System.nanoTime() < deadline) Thread.sleep(10);
if (context.refCnt() != 1 || !context.release())
throw new AssertionError("Context references leaked");
}
v2.shutdownGracefully(0, 5, TimeUnit.SECONDS).syncUninterruptibly();
nio.shutdownGracefully(0, 5, TimeUnit.SECONDS).syncUninterruptibly();
}
}
static void workloadClosure(TransportNative api, byte[] cert) throws Exception {
long owner = api.ownerNew(16 * 1024 * 1024);
long sibling = api.ownerNew(16 * 1024 * 1024);
NativeSslContext first =
NativeSslContextBuilder.forClient(api).workload(owner).trustAnchors(cert).build();
NativeSslContext second =
NativeSslContextBuilder.forClient(api).workload(sibling).trustAnchors(cert).build();
var active = first.newEngine(null, "localhost", 443);
var other = second.newEngine(null, "localhost", 443);
try {
active.beginHandshake();
require(api.workloadClose(owner) == 0, "close TLS workload");
try {
active.wrap(java.nio.ByteBuffer.allocate(0), java.nio.ByteBuffer.allocate(32768));
throw new AssertionError("Closed TLS workload admitted wrap");
} catch (javax.net.ssl.SSLException expected) {
require(
expected.getMessage().contains("workload is closed"), "workload failure diagnostic");
}
other.beginHandshake();
require(
other
.wrap(java.nio.ByteBuffer.allocate(0), java.nio.ByteBuffer.allocate(32768))
.bytesProduced()
> 0,
"sibling TLS workload remains open");
} finally {
ReferenceCountUtil.release(active);
ReferenceCountUtil.release(other);
first.release();
second.release();
require(api.ownerRelease(owner) == 0, "release TLS workload");
require(api.ownerRelease(sibling) == 0, "release sibling TLS workload");
}
System.out.println("Native TLS engine workload isolation checks passed");
}
static Channel listen(Stack stack, Consumer<Channel> initializer) throws Exception {
ServerBootstrap bootstrap = new ServerBootstrap().group(stack.group()).channel(stack.server());
if (stack.allocator() != null)
bootstrap.childOption(ChannelOption.ALLOCATOR, stack.allocator());
return bootstrap
.childHandler(
new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel channel) {
initializer.accept(channel);
}
})
.bind(new InetSocketAddress("127.0.0.1", 0))
.sync()
.channel();
}
static Channel connect(Stack stack, Channel server, Consumer<Channel> initializer)
throws Exception {
Bootstrap bootstrap = new Bootstrap().group(stack.group()).channel(stack.client());
if (stack.allocator() != null) bootstrap.option(ChannelOption.ALLOCATOR, stack.allocator());
return bootstrap
.handler(
new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel channel) {
initializer.accept(channel);
}
})
.connect(server.localAddress())
.sync()
.channel();
}
static SslHandler clientHandler(SslContext context, Channel channel, String host) {
return context.newHandler(channel.alloc(), host, 443);
}
static final class Collector extends ChannelInboundHandlerAdapter {
final ByteArrayOutputStream received = new ByteArrayOutputStream();
final CompletableFuture<byte[]> done = new CompletableFuture<>();
final int expected;
Collector(int expected) {
this.expected = expected;
}
@Override
public void channelRead(ChannelHandlerContext context, Object message) {
ByteBuf bytes = (ByteBuf) message;
try {
byte[] chunk = new byte[bytes.readableBytes()];
bytes.readBytes(chunk);
received.writeBytes(chunk);
if (received.size() >= expected) done.complete(received.toByteArray());
} finally {
bytes.release();
}
}
@Override
public void exceptionCaught(ChannelHandlerContext context, Throwable error) {
done.completeExceptionally(error);
context.close();
}
}
/** Expected handshake failures end at this handler instead of the pipeline tail. */
static final class Quiet extends ChannelInboundHandlerAdapter {
@Override
public void exceptionCaught(ChannelHandlerContext context, Throwable error) {
context.close();
}
}
static final class Echo extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext context, Object message) {
context.write(message);
}
@Override
public void channelReadComplete(ChannelHandlerContext context) {
context.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext context, Throwable error) {
context.close();
}
}
static void echoAndSession(
Stack stack, NativeSslContext serverContext, NativeSslContext clientContext)
throws Exception {
CompletableFuture<String> serverProtocol = new CompletableFuture<>();
Channel server =
listen(
stack,
channel -> {
channel.pipeline().addLast(serverContext.newHandler(channel.alloc()));
channel
.pipeline()
.addLast(
new ApplicationProtocolNegotiationHandler(ApplicationProtocolNames.HTTP_1_1) {
@Override
protected void configurePipeline(
ChannelHandlerContext context, String protocol) {
serverProtocol.complete(protocol);
context.pipeline().addLast(new Echo());
}
});
});
byte[] payload = new byte[1024 * 1024 + 1000 * 10];
for (int i = 0; i < payload.length; i++) payload[i] = (byte) (i * 31 + 7);
Collector collector = new Collector(payload.length);
CompletableFuture<String> clientProtocol = new CompletableFuture<>();
Channel channel =
connect(
stack,
server,
ch -> {
ch.pipeline().addLast(clientHandler(clientContext, ch, "localhost"));
ch.pipeline()
.addLast(
new ApplicationProtocolNegotiationHandler(ApplicationProtocolNames.HTTP_1_1) {
@Override
protected void configurePipeline(
ChannelHandlerContext context, String protocol) {
clientProtocol.complete(protocol);
context.pipeline().addLast(collector);
}
});
});
try {
SslHandler ssl = channel.pipeline().get(SslHandler.class);
ssl.handshakeFuture().sync();
require("h2".equals(clientProtocol.get(10, TimeUnit.SECONDS)), "client ALPN");
require("h2".equals(serverProtocol.get(10, TimeUnit.SECONDS)), "server ALPN");
require("h2".equals(ssl.applicationProtocol()), "SslHandler.applicationProtocol");
var session = ssl.engine().getSession();
require("TLSv1.3".equals(session.getProtocol()), "protocol " + session.getProtocol());
require(
session.getCipherSuite().startsWith("TLS_AES_")
|| session.getCipherSuite().startsWith("TLS_CHACHA20"),
"suite " + session.getCipherSuite());
require(session.getId().length == 32, "session id");
var peer = (X509Certificate) session.getPeerCertificates()[0];
require(peer.getSubjectX500Principal().getName().contains("127.0.0.1"), "peer certificate");
require(session.getPacketBufferSize() >= 16384 + 5, "packet size");
// One large write crosses many records; a burst of small writes exercises coalescing.
channel.write(Unpooled.wrappedBuffer(payload, 0, 1024 * 1024));
for (int i = 0; i < 1000; i++)
channel.write(Unpooled.wrappedBuffer(payload, 1024 * 1024 + i * 10, 10));
channel.flush();
require(Arrays.equals(collector.done.get(20, TimeUnit.SECONDS), payload), "echo mismatch");
} finally {
channel.close().syncUninterruptibly();
server.close().syncUninterruptibly();
}
}
static void closeNotify(
Stack stack, NativeSslContext serverContext, NativeSslContext clientContext)
throws Exception {
CompletableFuture<SslHandler> serverClosed = new CompletableFuture<>();
Channel server =
listen(
stack,
channel -> {
channel.pipeline().addLast(serverContext.newHandler(channel.alloc()));
channel
.pipeline()
.addLast(
new ChannelInboundHandlerAdapter() {
@Override
public void userEventTriggered(
ChannelHandlerContext context, Object event) {
if (event instanceof SslCloseCompletionEvent closed
&& closed.isSuccess()) {
SslHandler ssl = context.pipeline().get(SslHandler.class);
// TLS 1.3 half-close: output stays open, as with JSSE.
context
.writeAndFlush(Unpooled.copiedBuffer("bye", CharsetUtil.US_ASCII))
.addListener(done -> serverClosed.complete(ssl));
}
context.fireUserEventTriggered(event);
}
});
});
Collector collector = new Collector(3);
Channel channel =
connect(
stack,
server,
ch -> {
ch.pipeline().addLast(clientHandler(clientContext, ch, "localhost"));
ch.pipeline().addLast(collector);
});
try {
SslHandler ssl = channel.pipeline().get(SslHandler.class);
ssl.handshakeFuture().sync();
ssl.closeOutbound().sync();
require(ssl.engine().isOutboundDone(), "client outbound done");
SslHandler peer = serverClosed.get(10, TimeUnit.SECONDS);
require(peer.engine().isInboundDone(), "server inbound done");
require(!peer.engine().isOutboundDone(), "server output stays open");
require(
"bye".equals(new String(collector.done.get(10, TimeUnit.SECONDS), CharsetUtil.US_ASCII)),
"half-close write");
} finally {
channel.close().syncUninterruptibly();
server.close().syncUninterruptibly();
}
}
static void wrongName(Stack stack, NativeSslContext serverContext, NativeSslContext clientContext)
throws Exception {
CompletableFuture<Throwable> serverFailure = new CompletableFuture<>();
Channel server =
listen(
stack,
channel -> {
SslHandler ssl = serverContext.newHandler(channel.alloc());
ssl.handshakeFuture().addListener(done -> serverFailure.complete(done.cause()));
channel.pipeline().addLast(ssl, new Quiet());
});
CompletableFuture<Throwable> clientFailure = new CompletableFuture<>();
Channel channel =
connect(
stack,
server,
ch -> {
SslHandler ssl = clientHandler(clientContext, ch, "example.com");
ssl.handshakeFuture().addListener(done -> clientFailure.complete(done.cause()));
ch.pipeline().addLast(ssl, new Quiet());
});
try {
Throwable failure = clientFailure.get(10, TimeUnit.SECONDS);
require(failure instanceof SSLHandshakeException, "handshake exception " + failure);
require(failure.getMessage().contains("certificate"), "failure text " + failure.getMessage());
require(serverFailure.get(10, TimeUnit.SECONDS) != null, "server saw the alert");
} finally {
channel.close().syncUninterruptibly();
server.close().syncUninterruptibly();
}
}
static void http1(
Stack stack, NativeSslContext serverContext, NativeSslContext clientContext, String offered)
throws Exception {
CompletableFuture<String> serverProtocol = new CompletableFuture<>();
Channel server =
listen(
stack,
channel -> {
channel.pipeline().addLast(serverContext.newHandler(channel.alloc()));
channel
.pipeline()
.addLast(
new ApplicationProtocolNegotiationHandler(ApplicationProtocolNames.HTTP_1_1) {
@Override
protected void configurePipeline(
ChannelHandlerContext context, String protocol) {
serverProtocol.complete(protocol);
context
.pipeline()
.addLast(
new HttpServerCodec(),
new HttpObjectAggregator(65536),
new SimpleChannelInboundHandler<FullHttpRequest>() {
@Override
protected void channelRead0(
ChannelHandlerContext ctx, FullHttpRequest request) {
var response =
new DefaultFullHttpResponse(
HttpVersion.HTTP_1_1,
HttpResponseStatus.OK,
Unpooled.copiedBuffer(
"h1 " + request.uri(), CharsetUtil.US_ASCII));
HttpUtil.setContentLength(
response, response.content().readableBytes());
ctx.writeAndFlush(response);
}
});
}
});
});
CompletableFuture<String> body = new CompletableFuture<>();
Channel channel =
connect(
stack,
server,
ch -> {
ch.pipeline().addLast(clientHandler(clientContext, ch, "localhost"));
ch.pipeline()
.addLast(
new HttpClientCodec(),
new HttpObjectAggregator(65536),
new SimpleChannelInboundHandler<FullHttpResponse>() {
@Override
protected void channelRead0(
ChannelHandlerContext ctx, FullHttpResponse response) {
body.complete(response.content().toString(CharsetUtil.US_ASCII));
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable error) {
body.completeExceptionally(error);
}
});
});
try {
SslHandler ssl = channel.pipeline().get(SslHandler.class);
ssl.handshakeFuture().sync();
require(
java.util.Objects.equals(ssl.applicationProtocol(), offered),
"client ALPN " + ssl.applicationProtocol());
channel.writeAndFlush(
new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/hello"));
require("h1 /hello".equals(body.get(10, TimeUnit.SECONDS)), "HTTP/1.1 body");
require(
ApplicationProtocolNames.HTTP_1_1.equals(serverProtocol.get(10, TimeUnit.SECONDS)),
"server fallback");
} finally {
channel.close().syncUninterruptibly();
server.close().syncUninterruptibly();
}
}
static void http2(Stack stack, NativeSslContext serverContext, NativeSslContext clientContext)
throws Exception {
Channel server =
listen(
stack,
channel -> {
channel.pipeline().addLast(serverContext.newHandler(channel.alloc()));
channel
.pipeline()
.addLast(
new ApplicationProtocolNegotiationHandler(ApplicationProtocolNames.HTTP_1_1) {
@Override
protected void configurePipeline(
ChannelHandlerContext context, String protocol) {
if (!ApplicationProtocolNames.HTTP_2.equals(protocol)) {
context.close();
return;
}
context
.pipeline()
.addLast(
Http2FrameCodecBuilder.forServer().build(),
new Http2MultiplexHandler(
new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel stream) {
stream
.pipeline()
.addLast(
new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(
ChannelHandlerContext ctx, Object message) {
if (message
instanceof Http2HeadersFrame headers
&& headers.isEndStream()) {
ctx.write(
new DefaultHttp2HeadersFrame(
new DefaultHttp2Headers()
.status("200")));
ctx.writeAndFlush(
new DefaultHttp2DataFrame(
Unpooled.copiedBuffer(
"h2 "
+ headers.headers().path(),
CharsetUtil.US_ASCII),
true));
}
ReferenceCountUtil.release(message);
}
});
}
}));
}
});
});
CompletableFuture<Channel> ready = new CompletableFuture<>();
Channel channel =
connect(
stack,
server,
ch -> {
ch.pipeline().addLast(clientHandler(clientContext, ch, "localhost"));
ch.pipeline()
.addLast(
new ApplicationProtocolNegotiationHandler(ApplicationProtocolNames.HTTP_1_1) {
@Override
protected void configurePipeline(
ChannelHandlerContext context, String protocol) {
if (!ApplicationProtocolNames.HTTP_2.equals(protocol)) {
ready.completeExceptionally(
new AssertionError("negotiated " + protocol));
return;
}
context
.pipeline()
.addLast(
Http2FrameCodecBuilder.forClient().build(),
new Http2MultiplexHandler(new ChannelInboundHandlerAdapter()));
ready.complete(context.channel());
}
});
});
try {
Channel parent = ready.get(10, TimeUnit.SECONDS);
CompletableFuture<String> body = new CompletableFuture<>();
ByteArrayOutputStream data = new ByteArrayOutputStream();
Http2StreamChannel stream =
new Http2StreamChannelBootstrap(parent)
.handler(
new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object message) {
if (message instanceof Http2DataFrame frame) {
byte[] chunk = new byte[frame.content().readableBytes()];
frame.content().readBytes(chunk);
data.writeBytes(chunk);
if (frame.isEndStream()) body.complete(data.toString(CharsetUtil.US_ASCII));
}
ReferenceCountUtil.release(message);
}
})
.open()
.sync()
.getNow();
stream.writeAndFlush(
new DefaultHttp2HeadersFrame(
new DefaultHttp2Headers()
.method("GET")
.path("/stream")
.scheme("https")
.authority("localhost"),
true));
require("h2 /stream".equals(body.get(10, TimeUnit.SECONDS)), "HTTP/2 body");
} finally {
channel.close().syncUninterruptibly();
server.close().syncUninterruptibly();
}
}
/** Certificates and keys arrive as JDK objects and cross the ABI as DER. */
static void derContext(TransportNative api, byte[] cert, byte[] key, EventLoopGroup group)
throws Exception {
var factory = java.security.cert.CertificateFactory.getInstance("X.509");
X509Certificate certificate =
(X509Certificate) factory.generateCertificate(new java.io.ByteArrayInputStream(cert));
String pem = new String(key, CharsetUtil.US_ASCII).replaceAll("-----[A-Z ]+-----|\\s", "");
var privateKey =
java.security.KeyFactory.getInstance("RSA")
.generatePrivate(
new java.security.spec.PKCS8EncodedKeySpec(
java.util.Base64.getDecoder().decode(pem)));
NativeSslContext server =
NativeSslContextBuilder.forServer(api, privateKey, certificate).build();
NativeSslContext client =
NativeSslContextBuilder.forClient(api).trustAnchors(java.util.List.of(certificate)).build();
try {
var engine = server.newEngine(null);
require(engine.getSession().getLocalCertificates() != null, "local certificates");
ReferenceCountUtil.release(engine);
echoAndSessionWithoutAlpn(
new Stack(
"v2-der", group, NativeServerSocketChannel.class, NativeSocketChannel.class, null),
server,
client);
System.out.println("DER-configured NativeSslContext checks passed");
} finally {
server.release();
client.release();
}
}
static void echoAndSessionWithoutAlpn(
Stack stack, NativeSslContext serverContext, NativeSslContext clientContext)
throws Exception {
Channel server =
listen(
stack,
channel ->
channel.pipeline().addLast(serverContext.newHandler(channel.alloc()), new Echo()));
Collector collector = new Collector(5);
Channel channel =
connect(
stack,
server,
ch -> ch.pipeline().addLast(clientHandler(clientContext, ch, "127.0.0.1"), collector));
try {
SslHandler ssl = channel.pipeline().get(SslHandler.class);
ssl.handshakeFuture().sync();
require(ssl.applicationProtocol() == null, "no ALPN");
channel.writeAndFlush(Unpooled.copiedBuffer("hello", CharsetUtil.US_ASCII));
require(
"hello"
.equals(new String(collector.done.get(10, TimeUnit.SECONDS), CharsetUtil.US_ASCII)),
"DER echo");
} finally {
channel.close().syncUninterruptibly();
server.close().syncUninterruptibly();
}
}
static void require(boolean condition, String message) {
if (!condition) throw new AssertionError(message);
}
}