View Javadoc
1   /*
2    * Copyright 2022 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.ssl.ocsp;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.channel.ChannelHandlerContext;
20  import io.netty.channel.ChannelOutboundHandler;
21  import io.netty.channel.ChannelPromise;
22  import io.netty.handler.codec.ByteToMessageDecoder;
23  import io.netty.handler.ssl.SslHandler;
24  import io.netty.handler.ssl.SslHandshakeCompletionEvent;
25  import io.netty.resolver.dns.DnsNameResolver;
26  import io.netty.resolver.dns.DnsNameResolverBuilder;
27  import io.netty.util.AttributeKey;
28  import io.netty.util.ReferenceCountUtil;
29  import io.netty.util.concurrent.Future;
30  import io.netty.util.concurrent.GenericFutureListener;
31  import io.netty.util.concurrent.Promise;
32  import io.netty.util.internal.SystemPropertyUtil;
33  import org.bouncycastle.cert.ocsp.BasicOCSPResp;
34  import org.bouncycastle.cert.ocsp.OCSPException;
35  import org.bouncycastle.cert.ocsp.RevokedStatus;
36  import org.bouncycastle.cert.ocsp.SingleResp;
37  
38  import java.net.SocketAddress;
39  import java.security.cert.Certificate;
40  import java.security.cert.X509Certificate;
41  import java.util.Date;
42  import java.util.List;
43  import java.util.concurrent.TimeUnit;
44  
45  import static io.netty.util.internal.ObjectUtil.checkNotNull;
46  
47  /**
48   * {@link OcspServerCertificateValidator} validates incoming server's certificate
49   * using OCSP. Once TLS handshake is completed, {@link SslHandshakeCompletionEvent#SUCCESS} is fired, validator
50   * will perform certificate validation using OCSP over HTTP/1.1 with the server's certificate issuer OCSP responder.
51   */
52  public class OcspServerCertificateValidator extends ByteToMessageDecoder implements ChannelOutboundHandler {
53      /**
54       * An attribute used to mark all channels created by the {@link OcspServerCertificateValidator}.
55       */
56      public static final AttributeKey<Boolean> OCSP_PIPELINE_ATTRIBUTE =
57              AttributeKey.newInstance("io.netty.handler.ssl.ocsp.pipeline");
58  
59      /**
60       * Tolerate some clock skew in the OCSP validity time. Default to 15 minutes, which is the same as the JDK.
61       */
62      private static final long CLOCK_SKEW_TOLERANCE_MILLIS = getClockSkewTolerance();
63  
64      private static long getClockSkewTolerance() {
65          long defaultToleranceSeconds = TimeUnit.MINUTES.toSeconds(15);
66          long maxToleranceSeconds = TimeUnit.DAYS.toSeconds(2);
67          long configuredToleranceSeconds = SystemPropertyUtil.getLong("io.netty.handler.ssl.ocsp.clockSkew",
68              SystemPropertyUtil.getLong("com.sun.security.ocsp.clockSkew", defaultToleranceSeconds));
69          if (configuredToleranceSeconds < 0 || configuredToleranceSeconds > maxToleranceSeconds) {
70              // Ignore negative and extremely large values.
71              configuredToleranceSeconds = defaultToleranceSeconds;
72          }
73          return TimeUnit.SECONDS.toMillis(configuredToleranceSeconds);
74      }
75  
76      private final boolean closeAndThrowIfNotValid;
77      private final boolean validateNonce;
78      private final IoTransport ioTransport;
79      private final DnsNameResolver dnsNameResolver;
80      private boolean ocspQueryInProgress;
81      private boolean readPending;
82      private boolean discardInbound;
83  
84      /**
85       * Create a new {@link OcspServerCertificateValidator} instance without nonce validation
86       * on OCSP response, using default {@link IoTransport#DEFAULT} instance,
87       * default {@link DnsNameResolver} implementation and with {@link #closeAndThrowIfNotValid}
88       * set to {@code true}
89       */
90      public OcspServerCertificateValidator() {
91          this(false);
92      }
93  
94      /**
95       * Create a new {@link OcspServerCertificateValidator} instance with
96       * default {@link IoTransport#DEFAULT} instance and default {@link DnsNameResolver} implementation
97       * and {@link #closeAndThrowIfNotValid} set to {@code true}.
98       *
99       * @param validateNonce Set to {@code true} if we should force nonce validation on
100      *                      OCSP response else set to {@code false}
101      */
102     public OcspServerCertificateValidator(boolean validateNonce) {
103         this(validateNonce, IoTransport.DEFAULT);
104     }
105 
106     /**
107      * Create a new {@link OcspServerCertificateValidator} instance
108      *
109      * @param validateNonce Set to {@code true} if we should force nonce validation on
110      *                      OCSP response else set to {@code false}
111      * @param ioTransport   {@link IoTransport} to use
112      */
113     public OcspServerCertificateValidator(boolean validateNonce, IoTransport ioTransport) {
114         this(validateNonce, ioTransport, createDefaultResolver(ioTransport));
115     }
116 
117     /**
118      * Create a new {@link IoTransport} instance with {@link #closeAndThrowIfNotValid} set to {@code true}
119      *
120      * @param validateNonce   Set to {@code true} if we should force nonce validation on
121      *                        OCSP response else set to {@code false}
122      * @param ioTransport     {@link IoTransport} to use
123      * @param dnsNameResolver {@link DnsNameResolver} implementation to use
124      */
125     public OcspServerCertificateValidator(boolean validateNonce, IoTransport ioTransport,
126                                           DnsNameResolver dnsNameResolver) {
127         this(true, validateNonce, ioTransport, dnsNameResolver);
128     }
129 
130     /**
131      * Create a new {@link IoTransport} instance
132      *
133      * @param closeAndThrowIfNotValid If set to {@code true} then we will close the channel and throw an exception
134      *                                when certificate is not {@link OcspResponse.Status#VALID}.
135      *                                If set to {@code false} then we will simply pass the {@link OcspValidationEvent}
136      *                                to the next handler in pipeline and let it decide what to do.
137      * @param validateNonce           Set to {@code true} if we should force nonce validation on
138      *                                OCSP response else set to {@code false}
139      * @param ioTransport             {@link IoTransport} to use
140      * @param dnsNameResolver         {@link DnsNameResolver} implementation to use
141      */
142     public OcspServerCertificateValidator(boolean closeAndThrowIfNotValid, boolean validateNonce,
143                                           IoTransport ioTransport, DnsNameResolver dnsNameResolver) {
144         this.closeAndThrowIfNotValid = closeAndThrowIfNotValid;
145         this.validateNonce = validateNonce;
146         this.ioTransport = checkNotNull(ioTransport, "IoTransport");
147         this.dnsNameResolver = checkNotNull(dnsNameResolver, "DnsNameResolver");
148     }
149 
150     protected static DnsNameResolver createDefaultResolver(final IoTransport ioTransport) {
151         return new DnsNameResolverBuilder()
152                 .eventLoop(ioTransport.eventLoop())
153                 .datagramChannelFactory(ioTransport.datagramChannel())
154                 .socketChannelFactory(ioTransport.socketChannel())
155                 // The result of the resolution is security sensitive, so make it harder to spoof the responses.
156                 .caseRandomizationEnabled(true)
157                 .dnsCookiesEnabled(true)
158                 .build();
159     }
160 
161     @Override
162     protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
163         // Just buffer until the handler is removed which will happen once we did finish the OCSP processing.
164     }
165 
166     @Override
167     public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
168         if (discardInbound) {
169             ReferenceCountUtil.release(msg);
170             return;
171         }
172         super.channelRead(ctx, msg);
173     }
174 
175     @Override
176     public void userEventTriggered(final ChannelHandlerContext ctx, final Object evt) throws Exception {
177         if (evt instanceof SslHandshakeCompletionEvent) {
178             SslHandshakeCompletionEvent sslHandshakeCompletionEvent = (SslHandshakeCompletionEvent) evt;
179 
180             // If TLS handshake was successful then only we will perform OCSP certificate validation.
181             // If not, then just forward the event to next handler in pipeline and remove ourselves from pipeline.
182             if (sslHandshakeCompletionEvent.isSuccess()) {
183                 Certificate[] certificates = ctx.pipeline().get(SslHandler.class)
184                         .engine()
185                         .getSession()
186                         .getPeerCertificates();
187 
188                 assert certificates.length >= 2 : "There must an end-entity certificate and issuer certificate";
189 
190                 Promise<BasicOCSPResp> ocspRespPromise = ctx.executor().newPromise();
191                 OcspClient.query((X509Certificate) certificates[0], (X509Certificate) certificates[1],
192                         validateNonce, ioTransport, dnsNameResolver, ocspRespPromise);
193                 ocspQueryInProgress = true;
194                 ocspRespPromise.addListener(new GenericFutureListener<Future<BasicOCSPResp>>() {
195                     @Override
196                     public void operationComplete(Future<BasicOCSPResp> future) throws Exception {
197                         ocspQueryInProgress = false;
198                         // Assume revoked as default.
199                         OcspResponse.Status status = OcspResponse.Status.REVOKED;
200                         try {
201                             // If Future is success then we have successfully received OCSP response
202                             // from OCSP responder. We will validate it now and process.
203                             if (future.isSuccess()) {
204                                 SingleResp response = future.getNow().getResponses()[0];
205 
206                                 Date thisUpdate = response.getThisUpdate();
207                                 Date nextUpdate = response.getNextUpdate();
208                                 long now = System.currentTimeMillis();
209                                 Date nowLower = new Date(now - CLOCK_SKEW_TOLERANCE_MILLIS);
210                                 Date nowUpper = new Date(now + CLOCK_SKEW_TOLERANCE_MILLIS);
211                                 if (thisUpdate == null || nowUpper.before(thisUpdate) ||
212                                     nowLower.after(nextUpdate == null ? thisUpdate : nextUpdate)) {
213                                     ctx.fireExceptionCaught(new IllegalStateException("OCSP Response is out-of-date"));
214                                     if (closeAndThrowIfNotValid) {
215                                         ctx.close();
216                                     }
217                                     return;
218                                 }
219 
220                                 if (response.getCertStatus() == null) {
221                                     // 'null' means certificate is valid
222                                     status = OcspResponse.Status.VALID;
223                                 } else if (response.getCertStatus() instanceof RevokedStatus) {
224                                     status = OcspResponse.Status.REVOKED;
225                                 } else {
226                                     status = OcspResponse.Status.UNKNOWN;
227                                 }
228 
229                                 ctx.fireUserEventTriggered(new OcspValidationEvent(
230                                     new OcspResponse(status, thisUpdate, nextUpdate)));
231 
232                                 // If Certificate is not VALID and 'closeAndThrowIfNotValid' is set
233                                 // to 'true' then close the channel and throw an exception.
234                                 if (status != OcspResponse.Status.VALID && closeAndThrowIfNotValid) {
235                                     // Certificate is not valid. Throw
236                                     ctx.fireExceptionCaught(new OCSPException(
237                                         "Certificate not valid. Status: " + status));
238                                     ctx.close();
239                                 }
240                             } else {
241                                 ctx.fireExceptionCaught(future.cause());
242                                 if (closeAndThrowIfNotValid) {
243                                     ctx.close();
244                                 }
245                             }
246                         } catch (Throwable th) {
247                             ctx.fireExceptionCaught(th);
248                             if (closeAndThrowIfNotValid) {
249                                 ctx.close();
250                             }
251                         } finally {
252                             if (status != OcspResponse.Status.VALID && closeAndThrowIfNotValid) {
253                                 // The peer can't be trusted, so drop all bytes on the floor and stay in the pipeline
254                                 // until the channel is closed. This way neither the bytes we buffered nor anything that
255                                 // arrives later (for example data still buffered in the SslHandler) is handed to the
256                                 // following handlers.
257                                 discardInbound = true;
258                                 internalBuffer().clear();
259                                 ctx.fireUserEventTriggered(evt);
260                             } else {
261                                 ctx.fireUserEventTriggered(evt);
262                                 // Lets remove ourselves from the pipeline because we are done processing validation.
263                                 // This will also forward all the buffered bytes to the next handler.
264                                 ctx.pipeline().remove(OcspServerCertificateValidator.this);
265                             }
266                             if (readPending) {
267                                 readPending = false;
268                                 ctx.read();
269                             }
270                         }
271                     }
272                 });
273             } else {
274                 ctx.fireUserEventTriggered(evt);
275             }
276         } else {
277             ctx.fireUserEventTriggered(evt);
278         }
279     }
280 
281     @Override
282     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
283         ctx.close();
284     }
285 
286     @Override
287     public void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) throws Exception {
288         ctx.bind(localAddress, promise);
289     }
290 
291     @Override
292     public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress,
293                         SocketAddress localAddress, ChannelPromise promise) throws Exception {
294         ctx.connect(remoteAddress, localAddress, promise);
295     }
296 
297     @Override
298     public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
299         ctx.disconnect(promise);
300     }
301 
302     @Override
303     public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
304         ctx.close(promise);
305     }
306 
307     @Override
308     public void deregister(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
309         ctx.deregister(promise);
310     }
311 
312     @Override
313     public void read(ChannelHandlerContext ctx) throws Exception {
314         // Let's stop reading until we are done with the processing of the OCSP query.
315         if (ocspQueryInProgress) {
316             readPending = true;
317         } else {
318             readPending = false;
319             ctx.read();
320         }
321     }
322 
323     @Override
324     public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
325         ctx.write(msg, promise);
326     }
327 
328     @Override
329     public void flush(ChannelHandlerContext ctx) throws Exception {
330         ctx.flush();
331     }
332 }