|
15 | 15 | */ |
16 | 16 | package io.netty.handler.ssl.ocsp; |
17 | 17 |
|
| 18 | +import io.netty.buffer.ByteBuf; |
18 | 19 | import io.netty.channel.ChannelHandlerContext; |
19 | 20 | import io.netty.channel.ChannelInboundHandlerAdapter; |
| 21 | +import io.netty.channel.ChannelOutboundHandler; |
| 22 | +import io.netty.channel.ChannelPromise; |
| 23 | +import io.netty.handler.codec.ByteToMessageDecoder; |
20 | 24 | import io.netty.handler.ssl.SslHandler; |
21 | 25 | import io.netty.handler.ssl.SslHandshakeCompletionEvent; |
22 | 26 | import io.netty.resolver.dns.DnsNameResolver; |
|
30 | 34 | import org.bouncycastle.cert.ocsp.RevokedStatus; |
31 | 35 | import org.bouncycastle.cert.ocsp.SingleResp; |
32 | 36 |
|
| 37 | +import java.net.SocketAddress; |
33 | 38 | import java.security.cert.Certificate; |
34 | 39 | import java.security.cert.X509Certificate; |
35 | 40 | import java.util.Date; |
| 41 | +import java.util.List; |
36 | 42 |
|
37 | 43 | import static io.netty.util.internal.ObjectUtil.checkNotNull; |
38 | 44 |
|
|
41 | 47 | * using OCSP. Once TLS handshake is completed, {@link SslHandshakeCompletionEvent#SUCCESS} is fired, validator |
42 | 48 | * will perform certificate validation using OCSP over HTTP/1.1 with the server's certificate issuer OCSP responder. |
43 | 49 | */ |
44 | | -public class OcspServerCertificateValidator extends ChannelInboundHandlerAdapter { |
| 50 | +public class OcspServerCertificateValidator extends ByteToMessageDecoder implements ChannelOutboundHandler { |
45 | 51 | /** |
46 | 52 | * An attribute used to mark all channels created by the {@link OcspServerCertificateValidator}. |
47 | 53 | */ |
@@ -52,6 +58,8 @@ public class OcspServerCertificateValidator extends ChannelInboundHandlerAdapter |
52 | 58 | private final boolean validateNonce; |
53 | 59 | private final IoTransport ioTransport; |
54 | 60 | private final DnsNameResolver dnsNameResolver; |
| 61 | + private boolean ocspQueryInProgress; |
| 62 | + private boolean readPending; |
55 | 63 |
|
56 | 64 | /** |
57 | 65 | * Create a new {@link OcspServerCertificateValidator} instance without nonce validation |
@@ -128,9 +136,12 @@ protected static DnsNameResolver createDefaultResolver(final IoTransport ioTrans |
128 | 136 | } |
129 | 137 |
|
130 | 138 | @Override |
131 | | - public void userEventTriggered(final ChannelHandlerContext ctx, final Object evt) throws Exception { |
132 | | - ctx.fireUserEventTriggered(evt); |
| 139 | + protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { |
| 140 | + // Just buffer until the handler is removed which will happen once we did finish the OCSP processing. |
| 141 | + } |
133 | 142 |
|
| 143 | + @Override |
| 144 | + public void userEventTriggered(final ChannelHandlerContext ctx, final Object evt) throws Exception { |
134 | 145 | if (evt instanceof SslHandshakeCompletionEvent) { |
135 | 146 | SslHandshakeCompletionEvent sslHandshakeCompletionEvent = (SslHandshakeCompletionEvent) evt; |
136 | 147 |
|
@@ -146,52 +157,109 @@ public void userEventTriggered(final ChannelHandlerContext ctx, final Object evt |
146 | 157 |
|
147 | 158 | Promise<BasicOCSPResp> ocspRespPromise = OcspClient.query((X509Certificate) certificates[0], |
148 | 159 | (X509Certificate) certificates[1], validateNonce, ioTransport, dnsNameResolver); |
149 | | - |
| 160 | + ocspQueryInProgress = true; |
150 | 161 | ocspRespPromise.addListener((GenericFutureListener<Future<BasicOCSPResp>>) future -> { |
151 | | - // If Future is success then we have successfully received OCSP response |
152 | | - // from OCSP responder. We will validate it now and process. |
153 | | - if (future.isSuccess()) { |
154 | | - SingleResp response = future.getNow().getResponses()[0]; |
155 | | - |
156 | | - Date current = new Date(); |
157 | | - if (!(current.after(response.getThisUpdate()) && |
158 | | - current.before(response.getNextUpdate()))) { |
159 | | - ctx.fireExceptionCaught(new IllegalStateException("OCSP Response is out-of-date")); |
160 | | - } |
| 162 | + ocspQueryInProgress = false; |
| 163 | + try { |
| 164 | + // If Future is success then we have successfully received OCSP response |
| 165 | + // from OCSP responder. We will validate it now and process. |
| 166 | + if (future.isSuccess()) { |
| 167 | + SingleResp response = future.getNow().getResponses()[0]; |
161 | 168 |
|
162 | | - OcspResponse.Status status; |
163 | | - if (response.getCertStatus() == null) { |
164 | | - // 'null' means certificate is valid |
165 | | - status = OcspResponse.Status.VALID; |
166 | | - } else if (response.getCertStatus() instanceof RevokedStatus) { |
167 | | - status = OcspResponse.Status.REVOKED; |
168 | | - } else { |
169 | | - status = OcspResponse.Status.UNKNOWN; |
170 | | - } |
| 169 | + Date current = new Date(); |
| 170 | + if (!(current.after(response.getThisUpdate()) && |
| 171 | + current.before(response.getNextUpdate()))) { |
| 172 | + ctx.fireExceptionCaught(new IllegalStateException("OCSP Response is out-of-date")); |
| 173 | + return; |
| 174 | + } |
| 175 | + |
| 176 | + OcspResponse.Status status; |
| 177 | + if (response.getCertStatus() == null) { |
| 178 | + // 'null' means certificate is valid |
| 179 | + status = OcspResponse.Status.VALID; |
| 180 | + } else if (response.getCertStatus() instanceof RevokedStatus) { |
| 181 | + status = OcspResponse.Status.REVOKED; |
| 182 | + } else { |
| 183 | + status = OcspResponse.Status.UNKNOWN; |
| 184 | + } |
171 | 185 |
|
172 | | - ctx.fireUserEventTriggered(new OcspValidationEvent( |
173 | | - new OcspResponse(status, response.getThisUpdate(), response.getNextUpdate()))); |
| 186 | + ctx.fireUserEventTriggered(new OcspValidationEvent( |
| 187 | + new OcspResponse(status, response.getThisUpdate(), response.getNextUpdate()))); |
174 | 188 |
|
175 | | - // If Certificate is not VALID and 'closeAndThrowIfNotValid' is set |
176 | | - // to 'true' then close the channel and throw an exception. |
177 | | - if (status != OcspResponse.Status.VALID && closeAndThrowIfNotValid) { |
178 | | - ctx.channel().close(); |
179 | | - // Certificate is not valid. Throw |
180 | | - ctx.fireExceptionCaught(new OCSPException( |
181 | | - "Certificate not valid. Status: " + status)); |
| 189 | + // If Certificate is not VALID and 'closeAndThrowIfNotValid' is set |
| 190 | + // to 'true' then close the channel and throw an exception. |
| 191 | + if (status != OcspResponse.Status.VALID && closeAndThrowIfNotValid) { |
| 192 | + // Certificate is not valid. Throw |
| 193 | + ctx.fireExceptionCaught(new OCSPException( |
| 194 | + "Certificate not valid. Status: " + status)); |
| 195 | + ctx.close(); |
| 196 | + } |
| 197 | + } else { |
| 198 | + ctx.fireExceptionCaught(future.cause()); |
| 199 | + } |
| 200 | + } finally { |
| 201 | + ctx.fireUserEventTriggered(evt); |
| 202 | + // Lets remove ourselves from the pipeline because we are done processing validation. |
| 203 | + ctx.pipeline().remove(this); |
| 204 | + if (readPending) { |
| 205 | + readPending = false; |
| 206 | + ctx.read(); |
182 | 207 | } |
183 | | - } else { |
184 | | - ctx.fireExceptionCaught(future.cause()); |
185 | 208 | } |
186 | 209 | }); |
187 | 210 | } |
188 | | - // Lets remove ourselves from the pipeline because we are done processing validation. |
189 | | - ctx.pipeline().remove(this); |
190 | 211 | } |
191 | 212 | } |
192 | 213 |
|
193 | 214 | @Override |
194 | 215 | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { |
195 | | - ctx.channel().close(); |
| 216 | + ctx.close(); |
| 217 | + } |
| 218 | + |
| 219 | + @Override |
| 220 | + public void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) throws Exception { |
| 221 | + ctx.bind(localAddress, promise); |
| 222 | + } |
| 223 | + |
| 224 | + @Override |
| 225 | + public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, |
| 226 | + SocketAddress localAddress, ChannelPromise promise) throws Exception { |
| 227 | + ctx.connect(remoteAddress, localAddress, promise); |
| 228 | + } |
| 229 | + |
| 230 | + @Override |
| 231 | + public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { |
| 232 | + ctx.disconnect(promise); |
| 233 | + } |
| 234 | + |
| 235 | + @Override |
| 236 | + public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { |
| 237 | + ctx.close(promise); |
| 238 | + } |
| 239 | + |
| 240 | + @Override |
| 241 | + public void deregister(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { |
| 242 | + ctx.deregister(promise); |
| 243 | + } |
| 244 | + |
| 245 | + @Override |
| 246 | + public void read(ChannelHandlerContext ctx) throws Exception { |
| 247 | + // Let's stop reading until we are done with the processing of the OCSP query. |
| 248 | + if (ocspQueryInProgress) { |
| 249 | + readPending = true; |
| 250 | + } else { |
| 251 | + readPending = false; |
| 252 | + ctx.read(); |
| 253 | + } |
| 254 | + } |
| 255 | + |
| 256 | + @Override |
| 257 | + public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { |
| 258 | + ctx.write(msg, promise); |
| 259 | + } |
| 260 | + |
| 261 | + @Override |
| 262 | + public void flush(ChannelHandlerContext ctx) throws Exception { |
| 263 | + ctx.flush(); |
196 | 264 | } |
197 | 265 | } |