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.concurrent.Future;
29  import io.netty.util.concurrent.GenericFutureListener;
30  import io.netty.util.concurrent.Promise;
31  import io.netty.util.internal.SystemPropertyUtil;
32  import org.bouncycastle.cert.ocsp.BasicOCSPResp;
33  import org.bouncycastle.cert.ocsp.OCSPException;
34  import org.bouncycastle.cert.ocsp.RevokedStatus;
35  import org.bouncycastle.cert.ocsp.SingleResp;
36  
37  import java.net.SocketAddress;
38  import java.security.cert.Certificate;
39  import java.security.cert.X509Certificate;
40  import java.util.Date;
41  import java.util.List;
42  import java.util.concurrent.TimeUnit;
43  
44  import static io.netty.util.internal.ObjectUtil.checkNotNull;
45  
46  /**
47   * {@link OcspServerCertificateValidator} validates incoming server's certificate
48   * using OCSP. Once TLS handshake is completed, {@link SslHandshakeCompletionEvent#SUCCESS} is fired, validator
49   * will perform certificate validation using OCSP over HTTP/1.1 with the server's certificate issuer OCSP responder.
50   */
51  public class OcspServerCertificateValidator extends ByteToMessageDecoder implements ChannelOutboundHandler {
52      /**
53       * An attribute used to mark all channels created by the {@link OcspServerCertificateValidator}.
54       */
55      public static final AttributeKey<Boolean> OCSP_PIPELINE_ATTRIBUTE =
56              AttributeKey.newInstance("io.netty.handler.ssl.ocsp.pipeline");
57  
58      /**
59       * Tolerate some clock skew in the OCSP validity time. Default to 15 minutes, which is the same as the JDK.
60       */
61      private static final long CLOCK_SKEW_TOLERANCE_MILLIS = getClockSkewTolerance();
62  
63      private static long getClockSkewTolerance() {
64          long defaultToleranceSeconds = TimeUnit.MINUTES.toSeconds(15);
65          long maxToleranceSeconds = TimeUnit.DAYS.toSeconds(2);
66          long configuredToleranceSeconds = SystemPropertyUtil.getLong("io.netty.handler.ssl.ocsp.clockSkew",
67              SystemPropertyUtil.getLong("com.sun.security.ocsp.clockSkew", defaultToleranceSeconds));
68          if (configuredToleranceSeconds < 0 || configuredToleranceSeconds > maxToleranceSeconds) {
69              // Ignore negative and extremely large values.
70              configuredToleranceSeconds = defaultToleranceSeconds;
71          }
72          return TimeUnit.SECONDS.toMillis(configuredToleranceSeconds);
73      }
74  
75      private final boolean closeAndThrowIfNotValid;
76      private final boolean validateNonce;
77      private final IoTransport ioTransport;
78      private final DnsNameResolver dnsNameResolver;
79      private boolean ocspQueryInProgress;
80      private boolean readPending;
81  
82      /**
83       * Create a new {@link OcspServerCertificateValidator} instance without nonce validation
84       * on OCSP response, using default {@link IoTransport#DEFAULT} instance,
85       * default {@link DnsNameResolver} implementation and with {@link #closeAndThrowIfNotValid}
86       * set to {@code true}
87       */
88      public OcspServerCertificateValidator() {
89          this(false);
90      }
91  
92      /**
93       * Create a new {@link OcspServerCertificateValidator} instance with
94       * default {@link IoTransport#DEFAULT} instance and default {@link DnsNameResolver} implementation
95       * and {@link #closeAndThrowIfNotValid} set to {@code true}.
96       *
97       * @param validateNonce Set to {@code true} if we should force nonce validation on
98       *                      OCSP response else set to {@code false}
99       */
100     public OcspServerCertificateValidator(boolean validateNonce) {
101         this(validateNonce, IoTransport.DEFAULT);
102     }
103 
104     /**
105      * Create a new {@link OcspServerCertificateValidator} instance
106      *
107      * @param validateNonce Set to {@code true} if we should force nonce validation on
108      *                      OCSP response else set to {@code false}
109      * @param ioTransport   {@link IoTransport} to use
110      */
111     public OcspServerCertificateValidator(boolean validateNonce, IoTransport ioTransport) {
112         this(validateNonce, ioTransport, createDefaultResolver(ioTransport));
113     }
114 
115     /**
116      * Create a new {@link IoTransport} instance with {@link #closeAndThrowIfNotValid} set to {@code true}
117      *
118      * @param validateNonce   Set to {@code true} if we should force nonce validation on
119      *                        OCSP response else set to {@code false}
120      * @param ioTransport     {@link IoTransport} to use
121      * @param dnsNameResolver {@link DnsNameResolver} implementation to use
122      */
123     public OcspServerCertificateValidator(boolean validateNonce, IoTransport ioTransport,
124                                           DnsNameResolver dnsNameResolver) {
125         this(true, validateNonce, ioTransport, dnsNameResolver);
126     }
127 
128     /**
129      * Create a new {@link IoTransport} instance
130      *
131      * @param closeAndThrowIfNotValid If set to {@code true} then we will close the channel and throw an exception
132      *                                when certificate is not {@link OcspResponse.Status#VALID}.
133      *                                If set to {@code false} then we will simply pass the {@link OcspValidationEvent}
134      *                                to the next handler in pipeline and let it decide what to do.
135      * @param validateNonce           Set to {@code true} if we should force nonce validation on
136      *                                OCSP response else set to {@code false}
137      * @param ioTransport             {@link IoTransport} to use
138      * @param dnsNameResolver         {@link DnsNameResolver} implementation to use
139      */
140     public OcspServerCertificateValidator(boolean closeAndThrowIfNotValid, boolean validateNonce,
141                                           IoTransport ioTransport, DnsNameResolver dnsNameResolver) {
142         this.closeAndThrowIfNotValid = closeAndThrowIfNotValid;
143         this.validateNonce = validateNonce;
144         this.ioTransport = checkNotNull(ioTransport, "IoTransport");
145         this.dnsNameResolver = checkNotNull(dnsNameResolver, "DnsNameResolver");
146     }
147 
148     protected static DnsNameResolver createDefaultResolver(final IoTransport ioTransport) {
149         return new DnsNameResolverBuilder()
150                 .eventLoop(ioTransport.eventLoop())
151                 .datagramChannelFactory(ioTransport.datagramChannel())
152                 .socketChannelFactory(ioTransport.socketChannel())
153                 .build();
154     }
155 
156     @Override
157     protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
158         // Just buffer until the handler is removed which will happen once we did finish the OCSP processing.
159     }
160 
161     @Override
162     public void userEventTriggered(final ChannelHandlerContext ctx, final Object evt) throws Exception {
163         if (evt instanceof SslHandshakeCompletionEvent) {
164             SslHandshakeCompletionEvent sslHandshakeCompletionEvent = (SslHandshakeCompletionEvent) evt;
165 
166             // If TLS handshake was successful then only we will perform OCSP certificate validation.
167             // If not, then just forward the event to next handler in pipeline and remove ourselves from pipeline.
168             if (sslHandshakeCompletionEvent.isSuccess()) {
169                 Certificate[] certificates = ctx.pipeline().get(SslHandler.class)
170                         .engine()
171                         .getSession()
172                         .getPeerCertificates();
173 
174                 assert certificates.length >= 2 : "There must an end-entity certificate and issuer certificate";
175 
176                 Promise<BasicOCSPResp> ocspRespPromise = ctx.executor().newPromise();
177                 OcspClient.query((X509Certificate) certificates[0], (X509Certificate) certificates[1],
178                         validateNonce, ioTransport, dnsNameResolver, ocspRespPromise);
179                 ocspQueryInProgress = true;
180                 ocspRespPromise.addListener(new GenericFutureListener<Future<BasicOCSPResp>>() {
181                     @Override
182                     public void operationComplete(Future<BasicOCSPResp> future) throws Exception {
183                         ocspQueryInProgress = false;
184                         try {
185                             // If Future is success then we have successfully received OCSP response
186                             // from OCSP responder. We will validate it now and process.
187                             if (future.isSuccess()) {
188                                 SingleResp response = future.getNow().getResponses()[0];
189 
190                                 Date thisUpdate = response.getThisUpdate();
191                                 Date nextUpdate = response.getNextUpdate();
192                                 long now = System.currentTimeMillis();
193                                 Date nowLower = new Date(now - CLOCK_SKEW_TOLERANCE_MILLIS);
194                                 Date nowUpper = new Date(now + CLOCK_SKEW_TOLERANCE_MILLIS);
195                                 if (thisUpdate == null || nowUpper.before(thisUpdate) ||
196                                     nowLower.after(nextUpdate == null ? thisUpdate : nextUpdate)) {
197                                     ctx.fireExceptionCaught(new IllegalStateException("OCSP Response is out-of-date"));
198                                     if (closeAndThrowIfNotValid) {
199                                         ctx.close();
200                                     }
201                                     return;
202                                 }
203 
204                                 OcspResponse.Status status;
205                                 if (response.getCertStatus() == null) {
206                                     // 'null' means certificate is valid
207                                     status = OcspResponse.Status.VALID;
208                                 } else if (response.getCertStatus() instanceof RevokedStatus) {
209                                     status = OcspResponse.Status.REVOKED;
210                                 } else {
211                                     status = OcspResponse.Status.UNKNOWN;
212                                 }
213 
214                                 ctx.fireUserEventTriggered(new OcspValidationEvent(
215                                     new OcspResponse(status, thisUpdate, nextUpdate)));
216 
217                                 // If Certificate is not VALID and 'closeAndThrowIfNotValid' is set
218                                 // to 'true' then close the channel and throw an exception.
219                                 if (status != OcspResponse.Status.VALID && closeAndThrowIfNotValid) {
220                                     // Certificate is not valid. Throw
221                                     ctx.fireExceptionCaught(new OCSPException(
222                                         "Certificate not valid. Status: " + status));
223                                     ctx.close();
224                                 }
225                             } else {
226                                 ctx.fireExceptionCaught(future.cause());
227                                 if (closeAndThrowIfNotValid) {
228                                     ctx.close();
229                                 }
230                             }
231                         } catch (Throwable th) {
232                             ctx.fireExceptionCaught(th);
233                             if (closeAndThrowIfNotValid) {
234                                 ctx.close();
235                             }
236                         } finally {
237                             ctx.fireUserEventTriggered(evt);
238                             // Lets remove ourselves from the pipeline because we are done processing validation.
239                             ctx.pipeline().remove(OcspServerCertificateValidator.this);
240                             if (readPending) {
241                                 readPending = false;
242                                 ctx.read();
243                             }
244                         }
245                     }
246                 });
247             } else {
248                 ctx.fireUserEventTriggered(evt);
249             }
250         } else {
251             ctx.fireUserEventTriggered(evt);
252         }
253     }
254 
255     @Override
256     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
257         ctx.close();
258     }
259 
260     @Override
261     public void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) throws Exception {
262         ctx.bind(localAddress, promise);
263     }
264 
265     @Override
266     public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress,
267                         SocketAddress localAddress, ChannelPromise promise) throws Exception {
268         ctx.connect(remoteAddress, localAddress, promise);
269     }
270 
271     @Override
272     public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
273         ctx.disconnect(promise);
274     }
275 
276     @Override
277     public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
278         ctx.close(promise);
279     }
280 
281     @Override
282     public void deregister(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
283         ctx.deregister(promise);
284     }
285 
286     @Override
287     public void read(ChannelHandlerContext ctx) throws Exception {
288         // Let's stop reading until we are done with the processing of the OCSP query.
289         if (ocspQueryInProgress) {
290             readPending = true;
291         } else {
292             readPending = false;
293             ctx.read();
294         }
295     }
296 
297     @Override
298     public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
299         ctx.write(msg, promise);
300     }
301 
302     @Override
303     public void flush(ChannelHandlerContext ctx) throws Exception {
304         ctx.flush();
305     }
306 }