GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Port 4.1: Don't use the network in testsuite-jpms OCSPTest (#18043) (… · netty/netty@2018abb

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • testsuite-jpms/src/test/java
    • io/netty/testsuite_jpms/test
      • OCSPTest.java
    • module-info.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎testsuite-jpms/src/test/java/io/netty/testsuite_jpms/test/OCSPTest.java‎

Copy file name to clipboardExpand all lines: testsuite-jpms/src/test/java/io/netty/testsuite_jpms/test/OCSPTest.java
+176-47Lines changed: 176 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -16,86 +16,215 @@
1616
package io.netty.testsuite_jpms.test;
1717

1818
import io.netty.bootstrap.Bootstrap;
19-
import io.netty.channel.ChannelInitializer;
19+
import io.netty.bootstrap.ServerBootstrap;
20+
import io.netty.buffer.Unpooled;
21+
import io.netty.channel.Channel;
2022
import io.netty.channel.ChannelHandlerContext;
21-
import io.netty.channel.ChannelFuture;
22-
import io.netty.channel.SimpleChannelInboundHandler;
23+
import io.netty.channel.ChannelInitializer;
2324
import io.netty.channel.ChannelOption;
2425
import io.netty.channel.EventLoopGroup;
25-
import io.netty.channel.ChannelPipeline;
2626
import io.netty.channel.MultiThreadIoEventLoopGroup;
27+
import io.netty.channel.SimpleChannelInboundHandler;
2728
import io.netty.channel.nio.NioIoHandler;
2829
import io.netty.channel.socket.SocketChannel;
30+
import io.netty.channel.socket.nio.NioServerSocketChannel;
2931
import io.netty.channel.socket.nio.NioSocketChannel;
32+
import io.netty.handler.codec.http.DefaultFullHttpResponse;
33+
import io.netty.handler.codec.http.FullHttpRequest;
34+
import io.netty.handler.codec.http.FullHttpResponse;
35+
import io.netty.handler.codec.http.HttpHeaderNames;
36+
import io.netty.handler.codec.http.HttpObjectAggregator;
37+
import io.netty.handler.codec.http.HttpResponseStatus;
38+
import io.netty.handler.codec.http.HttpServerCodec;
39+
import io.netty.handler.codec.http.HttpVersion;
3040
import io.netty.handler.ssl.SslContext;
3141
import io.netty.handler.ssl.SslContextBuilder;
3242
import io.netty.handler.ssl.ocsp.OcspResponse;
3343
import io.netty.handler.ssl.ocsp.OcspServerCertificateValidator;
3444
import io.netty.handler.ssl.ocsp.OcspValidationEvent;
3545
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
46+
import io.netty.pkitesting.CertificateBuilder;
47+
import io.netty.pkitesting.X509Bundle;
48+
import io.netty.util.NetUtil;
49+
import org.bouncycastle.asn1.x509.AccessDescription;
50+
import org.bouncycastle.asn1.x509.AuthorityInformationAccess;
51+
import org.bouncycastle.asn1.x509.GeneralName;
52+
import org.bouncycastle.cert.X509CertificateHolder;
53+
import org.bouncycastle.cert.jcajce.JcaX509CertificateHolder;
54+
import org.bouncycastle.cert.ocsp.BasicOCSPResp;
55+
import org.bouncycastle.cert.ocsp.BasicOCSPRespBuilder;
56+
import org.bouncycastle.cert.ocsp.CertificateStatus;
57+
import org.bouncycastle.cert.ocsp.OCSPReq;
58+
import org.bouncycastle.cert.ocsp.OCSPRespBuilder;
59+
import org.bouncycastle.cert.ocsp.Req;
60+
import org.bouncycastle.cert.ocsp.RespID;
61+
import org.bouncycastle.operator.ContentSigner;
62+
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
3663
import org.junit.jupiter.api.Test;
3764

65+
import java.net.InetSocketAddress;
66+
import java.util.Date;
3867
import java.util.concurrent.CountDownLatch;
3968
import java.util.concurrent.TimeUnit;
40-
import java.util.concurrent.atomic.AtomicBoolean;
69+
import java.util.concurrent.atomic.AtomicReference;
4170

42-
import static org.junit.jupiter.api.Assertions.*;
71+
import static org.junit.jupiter.api.Assertions.assertEquals;
72+
import static org.junit.jupiter.api.Assertions.assertTrue;
4373

4474
public class OCSPTest {
4575

4676
@Test
4777
public void testSimpleQueryTest() throws Exception {
48-
final AtomicBoolean ocspStatus = new AtomicBoolean();
49-
EventLoopGroup eventLoopGroup = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
78+
// Everything stays on the loopback interface so the test does not depend on external hosts.
79+
X509Bundle issuer = new CertificateBuilder()
80+
.rsa2048()
81+
.subject("CN=OcspIssuerCA")
82+
.setIsCertificateAuthority(true)
83+
.buildSelfSigned();
5084

85+
EventLoopGroup group = new MultiThreadIoEventLoopGroup(2, NioIoHandler.newFactory());
5186
try {
52-
final CountDownLatch latch = new CountDownLatch(1);
53-
final SslContext sslContext = SslContextBuilder.forClient()
54-
.trustManager(InsecureTrustManagerFactory.INSTANCE)
55-
.build();
56-
57-
Bootstrap bootstrap = new Bootstrap()
58-
.group(eventLoopGroup)
59-
.channel(NioSocketChannel.class)
60-
.option(ChannelOption.TCP_NODELAY, true)
61-
.handler(new ChannelInitializer<SocketChannel>() {
87+
Channel ocspResponder = new ServerBootstrap()
88+
.group(group)
89+
.channel(NioServerSocketChannel.class)
90+
.childHandler(new ChannelInitializer<SocketChannel>() {
6291
@Override
6392
protected void initChannel(SocketChannel ch) {
64-
ChannelPipeline pipeline = ch.pipeline();
65-
pipeline.addLast(sslContext.newHandler(ch.alloc(), "apple.com", 443));
66-
pipeline.addLast(new OcspServerCertificateValidator(false));
67-
pipeline.addLast(new SimpleChannelInboundHandler<>() {
68-
@Override
69-
protected void channelRead0(ChannelHandlerContext ctx, Object msg) {
70-
// NOOP
71-
}
72-
73-
@Override
74-
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
75-
if (evt instanceof OcspValidationEvent) {
76-
OcspValidationEvent event = (OcspValidationEvent) evt;
77-
78-
ocspStatus.set(event.response().status() == OcspResponse.Status.VALID);
93+
ch.pipeline().addLast(new HttpServerCodec(), new HttpObjectAggregator(64 * 1024),
94+
new LocalOcspResponder(issuer));
95+
}
96+
})
97+
.bind(new InetSocketAddress(NetUtil.LOCALHOST4, 0))
98+
.sync().channel();
99+
try {
100+
int ocspPort = ((InetSocketAddress) ocspResponder.localAddress()).getPort();
101+
// Point the server certificate at the local OCSP responder using the AIA extension.
102+
GeneralName ocspName = new GeneralName(GeneralName.uniformResourceIdentifier,
103+
"http://" + NetUtil.LOCALHOST4.getHostAddress() + ':' + ocspPort + '/');
104+
AuthorityInformationAccess aia = new AuthorityInformationAccess(
105+
new AccessDescription(AccessDescription.id_ad_ocsp, ocspName));
106+
X509Bundle serverBundle = new CertificateBuilder()
107+
.rsa2048()
108+
.subject("CN=localhost")
109+
.addSanDnsName("localhost")
110+
.addExtendedKeyUsageServerAuth()
111+
.addExtensionOctetString("1.3.6.1.5.5.7.1.1", false, aia.getEncoded())
112+
.buildIssuedBy(issuer);
113+
114+
final SslContext serverContext = SslContextBuilder.forServer(
115+
serverBundle.getKeyPair().getPrivate(), serverBundle.getCertificatePathWithRoot()).build();
116+
Channel tlsServer = new ServerBootstrap()
117+
.group(group)
118+
.channel(NioServerSocketChannel.class)
119+
.childHandler(new ChannelInitializer<SocketChannel>() {
120+
@Override
121+
protected void initChannel(SocketChannel ch) {
122+
ch.pipeline().addLast(serverContext.newHandler(ch.alloc()));
123+
}
124+
})
125+
.bind(new InetSocketAddress(NetUtil.LOCALHOST4, 0))
126+
.sync().channel();
127+
try {
128+
checkOcspValidation(group, (InetSocketAddress) tlsServer.localAddress());
129+
} finally {
130+
tlsServer.close().sync();
131+
}
132+
} finally {
133+
ocspResponder.close().sync();
134+
}
135+
} finally {
136+
group.shutdownGracefully(0, 1, TimeUnit.SECONDS).syncUninterruptibly();
137+
}
138+
}
139+
140+
private static void checkOcspValidation(EventLoopGroup group, InetSocketAddress serverAddress)
141+
throws Exception {
142+
final AtomicReference<OcspResponse.Status> ocspStatus = new AtomicReference<>();
143+
final AtomicReference<Throwable> failure = new AtomicReference<>();
144+
final CountDownLatch latch = new CountDownLatch(1);
145+
final SslContext sslContext = SslContextBuilder.forClient()
146+
.trustManager(InsecureTrustManagerFactory.INSTANCE)
147+
.build();
148+
149+
Bootstrap bootstrap = new Bootstrap()
150+
.group(group)
151+
.channel(NioSocketChannel.class)
152+
.option(ChannelOption.TCP_NODELAY, true)
153+
.handler(new ChannelInitializer<SocketChannel>() {
154+
@Override
155+
protected void initChannel(SocketChannel ch) {
156+
ch.pipeline().addLast(
157+
sslContext.newHandler(ch.alloc(), "localhost", serverAddress.getPort()),
158+
new OcspServerCertificateValidator(false),
159+
new SimpleChannelInboundHandler<>() {
160+
@Override
161+
protected void channelRead0(ChannelHandlerContext ctx, Object msg) {
162+
// NOOP
163+
}
164+
165+
@Override
166+
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
167+
if (evt instanceof OcspValidationEvent) {
168+
OcspValidationEvent event = (OcspValidationEvent) evt;
169+
ocspStatus.set(event.response().status());
170+
ctx.channel().close();
171+
latch.countDown();
172+
}
173+
}
174+
175+
@Override
176+
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
177+
failure.set(cause);
79178
ctx.channel().close();
80179
latch.countDown();
81180
}
82-
}
83-
});
84-
}
85-
});
181+
});
182+
}
183+
});
86184

87-
ChannelFuture channelFuture = bootstrap.connect("apple.com", 443);
88-
channelFuture.sync();
185+
Channel channel = bootstrap.connect(serverAddress).sync().channel();
89186

90-
// Wait for maximum of 1 minute for Ocsp validation to happen
91-
latch.await(1, TimeUnit.MINUTES);
92-
assertTrue(ocspStatus.get());
187+
// Wait for maximum of 1 minute for Ocsp validation to happen
188+
assertTrue(latch.await(1, TimeUnit.MINUTES), "No OCSP validation event received");
189+
assertEquals(OcspResponse.Status.VALID, ocspStatus.get(), "OCSP validation failed: " + failure.get());
93190

94-
// Wait for Channel to be closed
95-
channelFuture.channel().closeFuture().sync();
96-
} finally {
97-
eventLoopGroup.shutdownGracefully();
98-
}
191+
// Wait for Channel to be closed
192+
channel.closeFuture().sync();
99193
}
100194

195+
/**
196+
* Minimal OCSP responder. It answers every request with a signed {@code good} status for the requested
197+
* certificates.
198+
*/
199+
private static final class LocalOcspResponder extends SimpleChannelInboundHandler<FullHttpRequest> {
200+
private final X509Bundle issuer;
201+
202+
LocalOcspResponder(X509Bundle issuer) {
203+
this.issuer = issuer;
204+
}
205+
206+
@Override
207+
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
208+
byte[] requestBytes = new byte[request.content().readableBytes()];
209+
request.content().readBytes(requestBytes);
210+
OCSPReq ocspReq = new OCSPReq(requestBytes);
211+
212+
X509CertificateHolder issuerHolder = new JcaX509CertificateHolder(issuer.getCertificate());
213+
BasicOCSPRespBuilder respBuilder = new BasicOCSPRespBuilder(new RespID(issuerHolder.getSubject()));
214+
for (Req req : ocspReq.getRequestList()) {
215+
respBuilder.addResponse(req.getCertID(), CertificateStatus.GOOD);
216+
}
217+
ContentSigner signer = new JcaContentSignerBuilder("SHA256withRSA")
218+
.build(issuer.getKeyPair().getPrivate());
219+
BasicOCSPResp basicResp = respBuilder.build(
220+
signer, new X509CertificateHolder[] { issuerHolder }, new Date());
221+
byte[] encoded = new OCSPRespBuilder().build(OCSPRespBuilder.SUCCESSFUL, basicResp).getEncoded();
222+
223+
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
224+
Unpooled.wrappedBuffer(encoded));
225+
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/ocsp-response");
226+
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, encoded.length);
227+
ctx.writeAndFlush(response).addListener(f -> ctx.close());
228+
}
229+
}
101230
}
Collapse file

‎testsuite-jpms/src/test/java/module-info.java‎

Copy file name to clipboardExpand all lines: testsuite-jpms/src/test/java/module-info.java
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
requires io.netty.codec.http2;
4444
requires io.netty.codec.http3;
4545
requires io.netty.codec.classes.quic;
46+
requires org.bouncycastle.pkix;
47+
requires org.bouncycastle.provider;
4648
requires org.jboss.marshalling;
4749

4850
requires static org.slf4j;

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.