View Javadoc
1   /*
2    * Copyright 2016 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    *   http://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;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.buffer.ByteBufAllocator;
20  import io.netty.internal.tcnative.CertificateVerifier;
21  import io.netty.internal.tcnative.SSL;
22  import io.netty.internal.tcnative.SSLContext;
23  import io.netty.util.AbstractReferenceCounted;
24  import io.netty.util.ReferenceCounted;
25  import io.netty.util.ResourceLeakDetector;
26  import io.netty.util.ResourceLeakDetectorFactory;
27  import io.netty.util.ResourceLeakTracker;
28  import io.netty.util.internal.PlatformDependent;
29  import io.netty.util.internal.StringUtil;
30  import io.netty.util.internal.SystemPropertyUtil;
31  import io.netty.util.internal.logging.InternalLogger;
32  import io.netty.util.internal.logging.InternalLoggerFactory;
33  
34  import java.security.AccessController;
35  import java.security.PrivateKey;
36  import java.security.PrivilegedAction;
37  import java.security.cert.CertPathValidatorException;
38  import java.security.cert.Certificate;
39  import java.security.cert.CertificateExpiredException;
40  import java.security.cert.CertificateNotYetValidException;
41  import java.security.cert.CertificateRevokedException;
42  import java.security.cert.X509Certificate;
43  import java.util.Arrays;
44  import java.util.Collections;
45  import java.util.List;
46  import java.util.Map;
47  import java.util.concurrent.locks.Lock;
48  import java.util.concurrent.locks.ReadWriteLock;
49  import java.util.concurrent.locks.ReentrantReadWriteLock;
50  
51  import javax.net.ssl.KeyManager;
52  import javax.net.ssl.KeyManagerFactory;
53  import javax.net.ssl.SSLEngine;
54  import javax.net.ssl.SSLException;
55  import javax.net.ssl.SSLHandshakeException;
56  import javax.net.ssl.TrustManager;
57  import javax.net.ssl.X509ExtendedTrustManager;
58  import javax.net.ssl.X509KeyManager;
59  import javax.net.ssl.X509TrustManager;
60  
61  import static io.netty.handler.ssl.OpenSsl.DEFAULT_CIPHERS;
62  import static io.netty.handler.ssl.OpenSsl.availableJavaCipherSuites;
63  import static io.netty.util.internal.ObjectUtil.checkNotNull;
64  import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
65  
66  /**
67   * An implementation of {@link SslContext} which works with libraries that support the
68   * <a href="https://www.openssl.org/">OpenSsl</a> C library API.
69   * <p>Instances of this class must be {@link #release() released} or else native memory will leak!
70   *
71   * <p>Instances of this class <strong>must not</strong> be released before any {@link ReferenceCountedOpenSslEngine}
72   * which depends upon the instance of this class is released. Otherwise if any method of
73   * {@link ReferenceCountedOpenSslEngine} is called which uses this class's JNI resources the JVM may crash.
74   */
75  public abstract class ReferenceCountedOpenSslContext extends SslContext implements ReferenceCounted {
76      private static final InternalLogger logger =
77              InternalLoggerFactory.getInstance(ReferenceCountedOpenSslContext.class);
78  
79      private static final int DEFAULT_BIO_NON_APPLICATION_BUFFER_SIZE =
80              AccessController.doPrivileged(new PrivilegedAction<Integer>() {
81                  @Override
82                  public Integer run() {
83                      return Math.max(1,
84                              SystemPropertyUtil.getInt("io.netty.handler.ssl.openssl.bioNonApplicationBufferSize",
85                                                        2048));
86                  }
87              });
88  
89      private static final Integer DH_KEY_LENGTH;
90      private static final ResourceLeakDetector<ReferenceCountedOpenSslContext> leakDetector =
91              ResourceLeakDetectorFactory.instance().newResourceLeakDetector(ReferenceCountedOpenSslContext.class);
92  
93      // TODO: Maybe make configurable ?
94      protected static final int VERIFY_DEPTH = 10;
95  
96      /**
97       * The OpenSSL SSL_CTX object.
98       *
99       * <strong>{@link #ctxLock} must be hold while using ctx!</strong>
100      */
101     protected long ctx;
102     private final List<String> unmodifiableCiphers;
103     private final long sessionCacheSize;
104     private final long sessionTimeout;
105     private final OpenSslApplicationProtocolNegotiator apn;
106     private final int mode;
107 
108     // Reference Counting
109     private final ResourceLeakTracker<ReferenceCountedOpenSslContext> leak;
110     private final AbstractReferenceCounted refCnt = new AbstractReferenceCounted() {
111         @Override
112         public ReferenceCounted touch(Object hint) {
113             if (leak != null) {
114                 leak.record(hint);
115             }
116 
117             return ReferenceCountedOpenSslContext.this;
118         }
119 
120         @Override
121         protected void deallocate() {
122             destroy();
123             if (leak != null) {
124                 boolean closed = leak.close(ReferenceCountedOpenSslContext.this);
125                 assert closed;
126             }
127         }
128     };
129 
130     final Certificate[] keyCertChain;
131     final ClientAuth clientAuth;
132     final String[] protocols;
133     final boolean enableOcsp;
134     final OpenSslEngineMap engineMap = new DefaultOpenSslEngineMap();
135     final ReadWriteLock ctxLock = new ReentrantReadWriteLock();
136 
137     private volatile int bioNonApplicationBufferSize = DEFAULT_BIO_NON_APPLICATION_BUFFER_SIZE;
138 
139     @SuppressWarnings("deprecation")
140     static final OpenSslApplicationProtocolNegotiator NONE_PROTOCOL_NEGOTIATOR =
141             new OpenSslApplicationProtocolNegotiator() {
142                 @Override
143                 public ApplicationProtocolConfig.Protocol protocol() {
144                     return ApplicationProtocolConfig.Protocol.NONE;
145                 }
146 
147                 @Override
148                 public List<String> protocols() {
149                     return Collections.emptyList();
150                 }
151 
152                 @Override
153                 public ApplicationProtocolConfig.SelectorFailureBehavior selectorFailureBehavior() {
154                     return ApplicationProtocolConfig.SelectorFailureBehavior.CHOOSE_MY_LAST_PROTOCOL;
155                 }
156 
157                 @Override
158                 public ApplicationProtocolConfig.SelectedListenerFailureBehavior selectedListenerFailureBehavior() {
159                     return ApplicationProtocolConfig.SelectedListenerFailureBehavior.ACCEPT;
160                 }
161             };
162 
163     static {
164         Integer dhLen = null;
165 
166         try {
167             String dhKeySize = AccessController.doPrivileged(new PrivilegedAction<String>() {
168                 @Override
169                 public String run() {
170                     return SystemPropertyUtil.get("jdk.tls.ephemeralDHKeySize");
171                 }
172             });
173             if (dhKeySize != null) {
174                 try {
175                     dhLen = Integer.valueOf(dhKeySize);
176                 } catch (NumberFormatException e) {
177                     logger.debug("ReferenceCountedOpenSslContext supports -Djdk.tls.ephemeralDHKeySize={int}, but got: "
178                             + dhKeySize);
179                 }
180             }
181         } catch (Throwable ignore) {
182             // ignore
183         }
184         DH_KEY_LENGTH = dhLen;
185     }
186 
187     ReferenceCountedOpenSslContext(Iterable<String> ciphers, CipherSuiteFilter cipherFilter,
188                                    ApplicationProtocolConfig apnCfg, long sessionCacheSize, long sessionTimeout,
189                                    int mode, Certificate[] keyCertChain, ClientAuth clientAuth, String[] protocols,
190                                    boolean startTls, boolean enableOcsp, boolean leakDetection) throws SSLException {
191         this(ciphers, cipherFilter, toNegotiator(apnCfg), sessionCacheSize, sessionTimeout, mode, keyCertChain,
192                 clientAuth, protocols, startTls, enableOcsp, leakDetection);
193     }
194 
195     ReferenceCountedOpenSslContext(Iterable<String> ciphers, CipherSuiteFilter cipherFilter,
196                                    OpenSslApplicationProtocolNegotiator apn, long sessionCacheSize,
197                                    long sessionTimeout, int mode, Certificate[] keyCertChain,
198                                    ClientAuth clientAuth, String[] protocols, boolean startTls, boolean enableOcsp,
199                                    boolean leakDetection) throws SSLException {
200         super(startTls);
201 
202         OpenSsl.ensureAvailability();
203 
204         if (enableOcsp && !OpenSsl.isOcspSupported()) {
205             throw new IllegalStateException("OCSP is not supported.");
206         }
207 
208         if (mode != SSL.SSL_MODE_SERVER && mode != SSL.SSL_MODE_CLIENT) {
209             throw new IllegalArgumentException("mode most be either SSL.SSL_MODE_SERVER or SSL.SSL_MODE_CLIENT");
210         }
211         leak = leakDetection ? leakDetector.track(this) : null;
212         this.mode = mode;
213         this.clientAuth = isServer() ? checkNotNull(clientAuth, "clientAuth") : ClientAuth.NONE;
214         this.protocols = protocols;
215         this.enableOcsp = enableOcsp;
216 
217         this.keyCertChain = keyCertChain == null ? null : keyCertChain.clone();
218 
219         unmodifiableCiphers = Arrays.asList(checkNotNull(cipherFilter, "cipherFilter").filterCipherSuites(
220                 ciphers, DEFAULT_CIPHERS, availableJavaCipherSuites()));
221 
222         this.apn = checkNotNull(apn, "apn");
223 
224         // Create a new SSL_CTX and configure it.
225         boolean success = false;
226         try {
227             try {
228                 ctx = SSLContext.make(SSL.SSL_PROTOCOL_ALL, mode);
229             } catch (Exception e) {
230                 throw new SSLException("failed to create an SSL_CTX", e);
231             }
232 
233             SSLContext.setOptions(ctx, SSLContext.getOptions(ctx) |
234                     SSL.SSL_OP_NO_SSLv2 |
235                     SSL.SSL_OP_NO_SSLv3 |
236                     SSL.SSL_OP_CIPHER_SERVER_PREFERENCE |
237 
238                     // We do not support compression at the moment so we should explicitly disable it.
239                     SSL.SSL_OP_NO_COMPRESSION |
240 
241                     // Disable ticket support by default to be more inline with SSLEngineImpl of the JDK.
242                     // This also let SSLSession.getId() work the same way for the JDK implementation and the
243                     // OpenSSLEngine. If tickets are supported SSLSession.getId() will only return an ID on the
244                     // server-side if it could make use of tickets.
245                     SSL.SSL_OP_NO_TICKET);
246 
247             // We need to enable SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER as the memory address may change between
248             // calling OpenSSLEngine.wrap(...).
249             // See https://github.com/netty/netty-tcnative/issues/100
250             SSLContext.setMode(ctx, SSLContext.getMode(ctx) | SSL.SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
251 
252             if (DH_KEY_LENGTH != null) {
253                 SSLContext.setTmpDHLength(ctx, DH_KEY_LENGTH);
254             }
255 
256                 /* List the ciphers that are permitted to negotiate. */
257             try {
258                 SSLContext.setCipherSuite(ctx, CipherSuiteConverter.toOpenSsl(unmodifiableCiphers));
259             } catch (SSLException e) {
260                 throw e;
261             } catch (Exception e) {
262                 throw new SSLException("failed to set cipher suite: " + unmodifiableCiphers, e);
263             }
264 
265             List<String> nextProtoList = apn.protocols();
266                 /* Set next protocols for next protocol negotiation extension, if specified */
267             if (!nextProtoList.isEmpty()) {
268                 String[] appProtocols = nextProtoList.toArray(new String[0]);
269                 int selectorBehavior = opensslSelectorFailureBehavior(apn.selectorFailureBehavior());
270 
271                 switch (apn.protocol()) {
272                     case NPN:
273                         SSLContext.setNpnProtos(ctx, appProtocols, selectorBehavior);
274                         break;
275                     case ALPN:
276                         SSLContext.setAlpnProtos(ctx, appProtocols, selectorBehavior);
277                         break;
278                     case NPN_AND_ALPN:
279                         SSLContext.setNpnProtos(ctx, appProtocols, selectorBehavior);
280                         SSLContext.setAlpnProtos(ctx, appProtocols, selectorBehavior);
281                         break;
282                     default:
283                         throw new Error();
284                 }
285             }
286 
287             /* Set session cache size, if specified */
288             if (sessionCacheSize <= 0) {
289                 // Get the default session cache size using SSLContext.setSessionCacheSize()
290                 sessionCacheSize = SSLContext.setSessionCacheSize(ctx, 20480);
291             }
292             this.sessionCacheSize = sessionCacheSize;
293             SSLContext.setSessionCacheSize(ctx, sessionCacheSize);
294 
295             /* Set session timeout, if specified */
296             if (sessionTimeout <= 0) {
297                 // Get the default session timeout using SSLContext.setSessionCacheTimeout()
298                 sessionTimeout = SSLContext.setSessionCacheTimeout(ctx, 300);
299             }
300             this.sessionTimeout = sessionTimeout;
301             SSLContext.setSessionCacheTimeout(ctx, sessionTimeout);
302 
303             if (enableOcsp) {
304                 SSLContext.enableOcsp(ctx, isClient());
305             }
306             success = true;
307         } finally {
308             if (!success) {
309                 release();
310             }
311         }
312     }
313 
314     private static int opensslSelectorFailureBehavior(ApplicationProtocolConfig.SelectorFailureBehavior behavior) {
315         switch (behavior) {
316             case NO_ADVERTISE:
317                 return SSL.SSL_SELECTOR_FAILURE_NO_ADVERTISE;
318             case CHOOSE_MY_LAST_PROTOCOL:
319                 return SSL.SSL_SELECTOR_FAILURE_CHOOSE_MY_LAST_PROTOCOL;
320             default:
321                 throw new Error();
322         }
323     }
324 
325     @Override
326     public final List<String> cipherSuites() {
327         return unmodifiableCiphers;
328     }
329 
330     @Override
331     public final long sessionCacheSize() {
332         return sessionCacheSize;
333     }
334 
335     @Override
336     public final long sessionTimeout() {
337         return sessionTimeout;
338     }
339 
340     @Override
341     public ApplicationProtocolNegotiator applicationProtocolNegotiator() {
342         return apn;
343     }
344 
345     @Override
346     public final boolean isClient() {
347         return mode == SSL.SSL_MODE_CLIENT;
348     }
349 
350     @Override
351     public final SSLEngine newEngine(ByteBufAllocator alloc, String peerHost, int peerPort) {
352         return newEngine0(alloc, peerHost, peerPort, true);
353     }
354 
355     @Override
356     protected final SslHandler newHandler(ByteBufAllocator alloc, boolean startTls) {
357         return new SslHandler(newEngine0(alloc, null, -1, false), startTls);
358     }
359 
360     @Override
361     protected final SslHandler newHandler(ByteBufAllocator alloc, String peerHost, int peerPort, boolean startTls) {
362         return new SslHandler(newEngine0(alloc, peerHost, peerPort, false), startTls);
363     }
364 
365     SSLEngine newEngine0(ByteBufAllocator alloc, String peerHost, int peerPort, boolean jdkCompatibilityMode) {
366         return new ReferenceCountedOpenSslEngine(this, alloc, peerHost, peerPort, jdkCompatibilityMode, true);
367     }
368 
369     abstract OpenSslKeyMaterialManager keyMaterialManager();
370 
371     /**
372      * Returns a new server-side {@link SSLEngine} with the current configuration.
373      */
374     @Override
375     public final SSLEngine newEngine(ByteBufAllocator alloc) {
376         return newEngine(alloc, null, -1);
377     }
378 
379     /**
380      * Returns the pointer to the {@code SSL_CTX} object for this {@link ReferenceCountedOpenSslContext}.
381      * Be aware that it is freed as soon as the {@link #finalize()}  method is called.
382      * At this point {@code 0} will be returned.
383      *
384      * @deprecated this method is considered unsafe as the returned pointer may be released later. Dont use it!
385      */
386     @Deprecated
387     public final long context() {
388         Lock readerLock = ctxLock.readLock();
389         readerLock.lock();
390         try {
391             return ctx;
392         } finally {
393             readerLock.unlock();
394         }
395     }
396 
397     /**
398      * Returns the stats of this context.
399      *
400      * @deprecated use {@link #sessionContext#stats()}
401      */
402     @Deprecated
403     public final OpenSslSessionStats stats() {
404         return sessionContext().stats();
405     }
406 
407     /**
408      * {@deprecated Renegotiation is not supported}
409      * Specify if remote initiated renegotiation is supported or not. If not supported and the remote side tries
410      * to initiate a renegotiation a {@link SSLHandshakeException} will be thrown during decoding.
411      */
412     @Deprecated
413     public void setRejectRemoteInitiatedRenegotiation(boolean rejectRemoteInitiatedRenegotiation) {
414         if (!rejectRemoteInitiatedRenegotiation) {
415             throw new UnsupportedOperationException("Renegotiation is not supported");
416         }
417     }
418 
419     /**
420      * {@deprecated Renegotiation is not supported}
421      * @return {@code true} because renegotiation is not supported.
422      */
423     @Deprecated
424     public boolean getRejectRemoteInitiatedRenegotiation() {
425         return true;
426     }
427 
428     /**
429      * Set the size of the buffer used by the BIO for non-application based writes
430      * (e.g. handshake, renegotiation, etc...).
431      */
432     public void setBioNonApplicationBufferSize(int bioNonApplicationBufferSize) {
433         this.bioNonApplicationBufferSize =
434                 checkPositiveOrZero(bioNonApplicationBufferSize, "bioNonApplicationBufferSize");
435     }
436 
437     /**
438      * Returns the size of the buffer used by the BIO for non-application based writes
439      */
440     public int getBioNonApplicationBufferSize() {
441         return bioNonApplicationBufferSize;
442     }
443 
444     /**
445      * Sets the SSL session ticket keys of this context.
446      *
447      * @deprecated use {@link OpenSslSessionContext#setTicketKeys(byte[])}
448      */
449     @Deprecated
450     public final void setTicketKeys(byte[] keys) {
451         sessionContext().setTicketKeys(keys);
452     }
453 
454     @Override
455     public abstract OpenSslSessionContext sessionContext();
456 
457     /**
458      * Returns the pointer to the {@code SSL_CTX} object for this {@link ReferenceCountedOpenSslContext}.
459      * Be aware that it is freed as soon as the {@link #release()} method is called.
460      * At this point {@code 0} will be returned.
461      *
462      * @deprecated this method is considered unsafe as the returned pointer may be released later. Dont use it!
463      */
464     @Deprecated
465     public final long sslCtxPointer() {
466         Lock readerLock = ctxLock.readLock();
467         readerLock.lock();
468         try {
469             return ctx;
470         } finally {
471             readerLock.unlock();
472         }
473     }
474 
475     // IMPORTANT: This method must only be called from either the constructor or the finalizer as a user MUST never
476     //            get access to an OpenSslSessionContext after this method was called to prevent the user from
477     //            producing a segfault.
478     private void destroy() {
479         Lock writerLock = ctxLock.writeLock();
480         writerLock.lock();
481         try {
482             if (ctx != 0) {
483                 if (enableOcsp) {
484                     SSLContext.disableOcsp(ctx);
485                 }
486 
487                 SSLContext.free(ctx);
488                 ctx = 0;
489                 sessionContext().destroy();
490             }
491         } finally {
492             writerLock.unlock();
493         }
494     }
495 
496     protected static X509Certificate[] certificates(byte[][] chain) {
497         X509Certificate[] peerCerts = new X509Certificate[chain.length];
498         for (int i = 0; i < peerCerts.length; i++) {
499             peerCerts[i] = new OpenSslX509Certificate(chain[i]);
500         }
501         return peerCerts;
502     }
503 
504     protected static X509TrustManager chooseTrustManager(TrustManager[] managers) {
505         for (TrustManager m : managers) {
506             if (m instanceof X509TrustManager) {
507                 return (X509TrustManager) m;
508             }
509         }
510         throw new IllegalStateException("no X509TrustManager found");
511     }
512 
513     protected static X509KeyManager chooseX509KeyManager(KeyManager[] kms) {
514         for (KeyManager km : kms) {
515             if (km instanceof X509KeyManager) {
516                 return (X509KeyManager) km;
517             }
518         }
519         throw new IllegalStateException("no X509KeyManager found");
520     }
521 
522     /**
523      * Translate a {@link ApplicationProtocolConfig} object to a
524      * {@link OpenSslApplicationProtocolNegotiator} object.
525      *
526      * @param config The configuration which defines the translation
527      * @return The results of the translation
528      */
529     @SuppressWarnings("deprecation")
530     static OpenSslApplicationProtocolNegotiator toNegotiator(ApplicationProtocolConfig config) {
531         if (config == null) {
532             return NONE_PROTOCOL_NEGOTIATOR;
533         }
534 
535         switch (config.protocol()) {
536             case NONE:
537                 return NONE_PROTOCOL_NEGOTIATOR;
538             case ALPN:
539             case NPN:
540             case NPN_AND_ALPN:
541                 switch (config.selectedListenerFailureBehavior()) {
542                     case CHOOSE_MY_LAST_PROTOCOL:
543                     case ACCEPT:
544                         switch (config.selectorFailureBehavior()) {
545                             case CHOOSE_MY_LAST_PROTOCOL:
546                             case NO_ADVERTISE:
547                                 return new OpenSslDefaultApplicationProtocolNegotiator(
548                                         config);
549                             default:
550                                 throw new UnsupportedOperationException(
551                                         new StringBuilder("OpenSSL provider does not support ")
552                                                 .append(config.selectorFailureBehavior())
553                                                 .append(" behavior").toString());
554                         }
555                     default:
556                         throw new UnsupportedOperationException(
557                                 new StringBuilder("OpenSSL provider does not support ")
558                                         .append(config.selectedListenerFailureBehavior())
559                                         .append(" behavior").toString());
560                 }
561             default:
562                 throw new Error();
563         }
564     }
565 
566     static boolean useExtendedTrustManager(X509TrustManager trustManager) {
567         return PlatformDependent.javaVersion() >= 7 && trustManager instanceof X509ExtendedTrustManager;
568     }
569 
570     @Override
571     public final int refCnt() {
572         return refCnt.refCnt();
573     }
574 
575     @Override
576     public final ReferenceCounted retain() {
577         refCnt.retain();
578         return this;
579     }
580 
581     @Override
582     public final ReferenceCounted retain(int increment) {
583         refCnt.retain(increment);
584         return this;
585     }
586 
587     @Override
588     public final ReferenceCounted touch() {
589         refCnt.touch();
590         return this;
591     }
592 
593     @Override
594     public final ReferenceCounted touch(Object hint) {
595         refCnt.touch(hint);
596         return this;
597     }
598 
599     @Override
600     public final boolean release() {
601         return refCnt.release();
602     }
603 
604     @Override
605     public final boolean release(int decrement) {
606         return refCnt.release(decrement);
607     }
608 
609     abstract static class AbstractCertificateVerifier extends CertificateVerifier {
610         private final OpenSslEngineMap engineMap;
611 
612         AbstractCertificateVerifier(OpenSslEngineMap engineMap) {
613             this.engineMap = engineMap;
614         }
615 
616         @Override
617         public final int verify(long ssl, byte[][] chain, String auth) {
618             X509Certificate[] peerCerts = certificates(chain);
619             final ReferenceCountedOpenSslEngine engine = engineMap.get(ssl);
620             try {
621                 verify(engine, peerCerts, auth);
622                 return CertificateVerifier.X509_V_OK;
623             } catch (Throwable cause) {
624                 logger.debug("verification of certificate failed", cause);
625                 SSLHandshakeException e = new SSLHandshakeException("General OpenSslEngine problem");
626                 e.initCause(cause);
627                 engine.handshakeException = e;
628 
629                 // Try to extract the correct error code that should be used.
630                 if (cause instanceof OpenSslCertificateException) {
631                     // This will never return a negative error code as its validated when constructing the
632                     // OpenSslCertificateException.
633                     return ((OpenSslCertificateException) cause).errorCode();
634                 }
635                 if (cause instanceof CertificateExpiredException) {
636                     return CertificateVerifier.X509_V_ERR_CERT_HAS_EXPIRED;
637                 }
638                 if (cause instanceof CertificateNotYetValidException) {
639                     return CertificateVerifier.X509_V_ERR_CERT_NOT_YET_VALID;
640                 }
641                 if (PlatformDependent.javaVersion() >= 7) {
642                     if (cause instanceof CertificateRevokedException) {
643                         return CertificateVerifier.X509_V_ERR_CERT_REVOKED;
644                     }
645 
646                     // The X509TrustManagerImpl uses a Validator which wraps a CertPathValidatorException into
647                     // an CertificateException. So we need to handle the wrapped CertPathValidatorException to be
648                     // able to send the correct alert.
649                     Throwable wrapped = cause.getCause();
650                     while (wrapped != null) {
651                         if (wrapped instanceof CertPathValidatorException) {
652                             CertPathValidatorException ex = (CertPathValidatorException) wrapped;
653                             CertPathValidatorException.Reason reason = ex.getReason();
654                             if (reason == CertPathValidatorException.BasicReason.EXPIRED) {
655                                 return CertificateVerifier.X509_V_ERR_CERT_HAS_EXPIRED;
656                             }
657                             if (reason == CertPathValidatorException.BasicReason.NOT_YET_VALID) {
658                                 return CertificateVerifier.X509_V_ERR_CERT_NOT_YET_VALID;
659                             }
660                             if (reason == CertPathValidatorException.BasicReason.REVOKED) {
661                                 return CertificateVerifier.X509_V_ERR_CERT_REVOKED;
662                             }
663                         }
664                         wrapped = wrapped.getCause();
665                     }
666                 }
667 
668                 // Could not detect a specific error code to use, so fallback to a default code.
669                 return CertificateVerifier.X509_V_ERR_UNSPECIFIED;
670             }
671         }
672 
673         abstract void verify(ReferenceCountedOpenSslEngine engine, X509Certificate[] peerCerts,
674                              String auth) throws Exception;
675     }
676 
677     private static final class DefaultOpenSslEngineMap implements OpenSslEngineMap {
678         private final Map<Long, ReferenceCountedOpenSslEngine> engines = PlatformDependent.newConcurrentHashMap();
679 
680         @Override
681         public ReferenceCountedOpenSslEngine remove(long ssl) {
682             return engines.remove(ssl);
683         }
684 
685         @Override
686         public void add(ReferenceCountedOpenSslEngine engine) {
687             engines.put(engine.sslPointer(), engine);
688         }
689 
690         @Override
691         public ReferenceCountedOpenSslEngine get(long ssl) {
692             return engines.get(ssl);
693         }
694     }
695 
696     static void setKeyMaterial(long ctx, X509Certificate[] keyCertChain, PrivateKey key, String keyPassword)
697             throws SSLException {
698          /* Load the certificate file and private key. */
699         long keyBio = 0;
700         long keyCertChainBio = 0;
701         long keyCertChainBio2 = 0;
702         PemEncoded encoded = null;
703         try {
704             // Only encode one time
705             encoded = PemX509Certificate.toPEM(ByteBufAllocator.DEFAULT, true, keyCertChain);
706             keyCertChainBio = toBIO(ByteBufAllocator.DEFAULT, encoded.retain());
707             keyCertChainBio2 = toBIO(ByteBufAllocator.DEFAULT, encoded.retain());
708 
709             if (key != null) {
710                 keyBio = toBIO(ByteBufAllocator.DEFAULT, key);
711             }
712 
713             SSLContext.setCertificateBio(
714                     ctx, keyCertChainBio, keyBio,
715                     keyPassword == null ? StringUtil.EMPTY_STRING : keyPassword);
716             // We may have more then one cert in the chain so add all of them now.
717             SSLContext.setCertificateChainBio(ctx, keyCertChainBio2, true);
718         } catch (SSLException e) {
719             throw e;
720         } catch (Exception e) {
721             throw new SSLException("failed to set certificate and key", e);
722         } finally {
723             freeBio(keyBio);
724             freeBio(keyCertChainBio);
725             freeBio(keyCertChainBio2);
726             if (encoded != null) {
727                 encoded.release();
728             }
729         }
730     }
731 
732     static void freeBio(long bio) {
733         if (bio != 0) {
734             SSL.freeBIO(bio);
735         }
736     }
737 
738     /**
739      * Return the pointer to a <a href="https://www.openssl.org/docs/crypto/BIO_get_mem_ptr.html">in-memory BIO</a>
740      * or {@code 0} if the {@code key} is {@code null}. The BIO contains the content of the {@code key}.
741      */
742     static long toBIO(ByteBufAllocator allocator, PrivateKey key) throws Exception {
743         if (key == null) {
744             return 0;
745         }
746 
747         PemEncoded pem = PemPrivateKey.toPEM(allocator, true, key);
748         try {
749             return toBIO(allocator, pem.retain());
750         } finally {
751             pem.release();
752         }
753     }
754 
755     /**
756      * Return the pointer to a <a href="https://www.openssl.org/docs/crypto/BIO_get_mem_ptr.html">in-memory BIO</a>
757      * or {@code 0} if the {@code certChain} is {@code null}. The BIO contains the content of the {@code certChain}.
758      */
759     static long toBIO(ByteBufAllocator allocator, X509Certificate... certChain) throws Exception {
760         if (certChain == null) {
761             return 0;
762         }
763 
764         if (certChain.length == 0) {
765             throw new IllegalArgumentException("certChain can't be empty");
766         }
767 
768         PemEncoded pem = PemX509Certificate.toPEM(allocator, true, certChain);
769         try {
770             return toBIO(allocator, pem.retain());
771         } finally {
772             pem.release();
773         }
774     }
775 
776     static long toBIO(ByteBufAllocator allocator, PemEncoded pem) throws Exception {
777         try {
778             // We can turn direct buffers straight into BIOs. No need to
779             // make a yet another copy.
780             ByteBuf content = pem.content();
781 
782             if (content.isDirect()) {
783                 return newBIO(content.retainedSlice());
784             }
785 
786             ByteBuf buffer = allocator.directBuffer(content.readableBytes());
787             try {
788                 buffer.writeBytes(content, content.readerIndex(), content.readableBytes());
789                 return newBIO(buffer.retainedSlice());
790             } finally {
791                 try {
792                     // If the contents of the ByteBuf is sensitive (e.g. a PrivateKey) we
793                     // need to zero out the bytes of the copy before we're releasing it.
794                     if (pem.isSensitive()) {
795                         SslUtils.zeroout(buffer);
796                     }
797                 } finally {
798                     buffer.release();
799                 }
800             }
801         } finally {
802             pem.release();
803         }
804     }
805 
806     private static long newBIO(ByteBuf buffer) throws Exception {
807         try {
808             long bio = SSL.newMemBIO();
809             int readable = buffer.readableBytes();
810             if (SSL.bioWrite(bio, OpenSsl.memoryAddress(buffer) + buffer.readerIndex(), readable) != readable) {
811                 SSL.freeBIO(bio);
812                 throw new IllegalStateException("Could not write data to memory BIO");
813             }
814             return bio;
815         } finally {
816             buffer.release();
817         }
818     }
819 
820     /**
821      * Returns the {@link OpenSslKeyMaterialProvider} that should be used for OpenSSL. Depending on the given
822      * {@link KeyManagerFactory} this may cache the {@link OpenSslKeyMaterial} for better performance if it can
823      * ensure that the same material is always returned for the same alias.
824      */
825     static OpenSslKeyMaterialProvider providerFor(KeyManagerFactory factory, String password) {
826         if (factory instanceof OpenSslX509KeyManagerFactory) {
827             return ((OpenSslX509KeyManagerFactory) factory).newProvider();
828         }
829 
830         X509KeyManager keyManager = chooseX509KeyManager(factory.getKeyManagers());
831         if (factory instanceof OpenSslCachingX509KeyManagerFactory) {
832             // The user explicit used OpenSslCachingX509KeyManagerFactory which signals us that its fine to cache.
833             return new OpenSslCachingKeyMaterialProvider(keyManager, password);
834         }
835         // We can not be sure if the material may change at runtime so we will not cache it.
836         return new OpenSslKeyMaterialProvider(keyManager, password);
837     }
838 }