|
16 | 16 | package io.netty.testsuite_jpms.test; |
17 | 17 |
|
18 | 18 | 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; |
20 | 22 | import io.netty.channel.ChannelHandlerContext; |
21 | | -import io.netty.channel.ChannelFuture; |
22 | | -import io.netty.channel.SimpleChannelInboundHandler; |
| 23 | +import io.netty.channel.ChannelInitializer; |
23 | 24 | import io.netty.channel.ChannelOption; |
24 | 25 | import io.netty.channel.EventLoopGroup; |
25 | | -import io.netty.channel.ChannelPipeline; |
26 | 26 | import io.netty.channel.MultiThreadIoEventLoopGroup; |
| 27 | +import io.netty.channel.SimpleChannelInboundHandler; |
27 | 28 | import io.netty.channel.nio.NioIoHandler; |
28 | 29 | import io.netty.channel.socket.SocketChannel; |
| 30 | +import io.netty.channel.socket.nio.NioServerSocketChannel; |
29 | 31 | 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; |
30 | 40 | import io.netty.handler.ssl.SslContext; |
31 | 41 | import io.netty.handler.ssl.SslContextBuilder; |
32 | 42 | import io.netty.handler.ssl.ocsp.OcspResponse; |
33 | 43 | import io.netty.handler.ssl.ocsp.OcspServerCertificateValidator; |
34 | 44 | import io.netty.handler.ssl.ocsp.OcspValidationEvent; |
35 | 45 | 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; |
36 | 63 | import org.junit.jupiter.api.Test; |
37 | 64 |
|
| 65 | +import java.net.InetSocketAddress; |
| 66 | +import java.util.Date; |
38 | 67 | import java.util.concurrent.CountDownLatch; |
39 | 68 | import java.util.concurrent.TimeUnit; |
40 | | -import java.util.concurrent.atomic.AtomicBoolean; |
| 69 | +import java.util.concurrent.atomic.AtomicReference; |
41 | 70 |
|
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; |
43 | 73 |
|
44 | 74 | public class OCSPTest { |
45 | 75 |
|
46 | 76 | @Test |
47 | 77 | 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(); |
50 | 84 |
|
| 85 | + EventLoopGroup group = new MultiThreadIoEventLoopGroup(2, NioIoHandler.newFactory()); |
51 | 86 | 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>() { |
62 | 91 | @Override |
63 | 92 | 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); |
79 | 178 | ctx.channel().close(); |
80 | 179 | latch.countDown(); |
81 | 180 | } |
82 | | - } |
83 | | - }); |
84 | | - } |
85 | | - }); |
| 181 | + }); |
| 182 | + } |
| 183 | + }); |
86 | 184 |
|
87 | | - ChannelFuture channelFuture = bootstrap.connect("apple.com", 443); |
88 | | - channelFuture.sync(); |
| 185 | + Channel channel = bootstrap.connect(serverAddress).sync().channel(); |
89 | 186 |
|
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()); |
93 | 190 |
|
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(); |
99 | 193 | } |
100 | 194 |
|
| 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 | + } |
101 | 230 | } |