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.SSLEngine;
53  import javax.net.ssl.SSLException;
54  import javax.net.ssl.SSLHandshakeException;
55  import javax.net.ssl.TrustManager;
56  import javax.net.ssl.X509ExtendedKeyManager;
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[nextProtoList.size()]);
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             }
490         } finally {
491             writerLock.unlock();
492         }
493     }
494 
495     protected static X509Certificate[] certificates(byte[][] chain) {
496         X509Certificate[] peerCerts = new X509Certificate[chain.length];
497         for (int i = 0; i < peerCerts.length; i++) {
498             peerCerts[i] = new OpenSslX509Certificate(chain[i]);
499         }
500         return peerCerts;
501     }
502 
503     protected static X509TrustManager chooseTrustManager(TrustManager[] managers) {
504         for (TrustManager m : managers) {
505             if (m instanceof X509TrustManager) {
506                 return (X509TrustManager) m;
507             }
508         }
509         throw new IllegalStateException("no X509TrustManager found");
510     }
511 
512     protected static X509KeyManager chooseX509KeyManager(KeyManager[] kms) {
513         for (KeyManager km : kms) {
514             if (km instanceof X509KeyManager) {
515                 return (X509KeyManager) km;
516             }
517         }
518         throw new IllegalStateException("no X509KeyManager found");
519     }
520 
521     /**
522      * Translate a {@link ApplicationProtocolConfig} object to a
523      * {@link OpenSslApplicationProtocolNegotiator} object.
524      *
525      * @param config The configuration which defines the translation
526      * @return The results of the translation
527      */
528     @SuppressWarnings("deprecation")
529     static OpenSslApplicationProtocolNegotiator toNegotiator(ApplicationProtocolConfig config) {
530         if (config == null) {
531             return NONE_PROTOCOL_NEGOTIATOR;
532         }
533 
534         switch (config.protocol()) {
535             case NONE:
536                 return NONE_PROTOCOL_NEGOTIATOR;
537             case ALPN:
538             case NPN:
539             case NPN_AND_ALPN:
540                 switch (config.selectedListenerFailureBehavior()) {
541                     case CHOOSE_MY_LAST_PROTOCOL:
542                     case ACCEPT:
543                         switch (config.selectorFailureBehavior()) {
544                             case CHOOSE_MY_LAST_PROTOCOL:
545                             case NO_ADVERTISE:
546                                 return new OpenSslDefaultApplicationProtocolNegotiator(
547                                         config);
548                             default:
549                                 throw new UnsupportedOperationException(
550                                         new StringBuilder("OpenSSL provider does not support ")
551                                                 .append(config.selectorFailureBehavior())
552                                                 .append(" behavior").toString());
553                         }
554                     default:
555                         throw new UnsupportedOperationException(
556                                 new StringBuilder("OpenSSL provider does not support ")
557                                         .append(config.selectedListenerFailureBehavior())
558                                         .append(" behavior").toString());
559                 }
560             default:
561                 throw new Error();
562         }
563     }
564 
565     static boolean useExtendedTrustManager(X509TrustManager trustManager) {
566         return PlatformDependent.javaVersion() >= 7 && trustManager instanceof X509ExtendedTrustManager;
567     }
568 
569     static boolean useExtendedKeyManager(X509KeyManager keyManager) {
570         return PlatformDependent.javaVersion() >= 7 && keyManager instanceof X509ExtendedKeyManager;
571     }
572 
573     @Override
574     public final int refCnt() {
575         return refCnt.refCnt();
576     }
577 
578     @Override
579     public final ReferenceCounted retain() {
580         refCnt.retain();
581         return this;
582     }
583 
584     @Override
585     public final ReferenceCounted retain(int increment) {
586         refCnt.retain(increment);
587         return this;
588     }
589 
590     @Override
591     public final ReferenceCounted touch() {
592         refCnt.touch();
593         return this;
594     }
595 
596     @Override
597     public final ReferenceCounted touch(Object hint) {
598         refCnt.touch(hint);
599         return this;
600     }
601 
602     @Override
603     public final boolean release() {
604         return refCnt.release();
605     }
606 
607     @Override
608     public final boolean release(int decrement) {
609         return refCnt.release(decrement);
610     }
611 
612     abstract static class AbstractCertificateVerifier extends CertificateVerifier {
613         private final OpenSslEngineMap engineMap;
614 
615         AbstractCertificateVerifier(OpenSslEngineMap engineMap) {
616             this.engineMap = engineMap;
617         }
618 
619         @Override
620         public final int verify(long ssl, byte[][] chain, String auth) {
621             X509Certificate[] peerCerts = certificates(chain);
622             final ReferenceCountedOpenSslEngine engine = engineMap.get(ssl);
623             try {
624                 verify(engine, peerCerts, auth);
625                 return CertificateVerifier.X509_V_OK;
626             } catch (Throwable cause) {
627                 logger.debug("verification of certificate failed", cause);
628                 SSLHandshakeException e = new SSLHandshakeException("General OpenSslEngine problem");
629                 e.initCause(cause);
630                 engine.handshakeException = e;
631 
632                 // Try to extract the correct error code that should be used.
633                 if (cause instanceof OpenSslCertificateException) {
634                     // This will never return a negative error code as its validated when constructing the
635                     // OpenSslCertificateException.
636                     return ((OpenSslCertificateException) cause).errorCode();
637                 }
638                 if (cause instanceof CertificateExpiredException) {
639                     return CertificateVerifier.X509_V_ERR_CERT_HAS_EXPIRED;
640                 }
641                 if (cause instanceof CertificateNotYetValidException) {
642                     return CertificateVerifier.X509_V_ERR_CERT_NOT_YET_VALID;
643                 }
644                 if (PlatformDependent.javaVersion() >= 7) {
645                     if (cause instanceof CertificateRevokedException) {
646                         return CertificateVerifier.X509_V_ERR_CERT_REVOKED;
647                     }
648 
649                     // The X509TrustManagerImpl uses a Validator which wraps a CertPathValidatorException into
650                     // an CertificateException. So we need to handle the wrapped CertPathValidatorException to be
651                     // able to send the correct alert.
652                     Throwable wrapped = cause.getCause();
653                     while (wrapped != null) {
654                         if (wrapped instanceof CertPathValidatorException) {
655                             CertPathValidatorException ex = (CertPathValidatorException) wrapped;
656                             CertPathValidatorException.Reason reason = ex.getReason();
657                             if (reason == CertPathValidatorException.BasicReason.EXPIRED) {
658                                 return CertificateVerifier.X509_V_ERR_CERT_HAS_EXPIRED;
659                             }
660                             if (reason == CertPathValidatorException.BasicReason.NOT_YET_VALID) {
661                                 return CertificateVerifier.X509_V_ERR_CERT_NOT_YET_VALID;
662                             }
663                             if (reason == CertPathValidatorException.BasicReason.REVOKED) {
664                                 return CertificateVerifier.X509_V_ERR_CERT_REVOKED;
665                             }
666                         }
667                         wrapped = wrapped.getCause();
668                     }
669                 }
670 
671                 // Could not detect a specific error code to use, so fallback to a default code.
672                 return CertificateVerifier.X509_V_ERR_UNSPECIFIED;
673             }
674         }
675 
676         abstract void verify(ReferenceCountedOpenSslEngine engine, X509Certificate[] peerCerts,
677                              String auth) throws Exception;
678     }
679 
680     private static final class DefaultOpenSslEngineMap implements OpenSslEngineMap {
681         private final Map<Long, ReferenceCountedOpenSslEngine> engines = PlatformDependent.newConcurrentHashMap();
682 
683         @Override
684         public ReferenceCountedOpenSslEngine remove(long ssl) {
685             return engines.remove(ssl);
686         }
687 
688         @Override
689         public void add(ReferenceCountedOpenSslEngine engine) {
690             engines.put(engine.sslPointer(), engine);
691         }
692 
693         @Override
694         public ReferenceCountedOpenSslEngine get(long ssl) {
695             return engines.get(ssl);
696         }
697     }
698 
699     static void setKeyMaterial(long ctx, X509Certificate[] keyCertChain, PrivateKey key, String keyPassword)
700             throws SSLException {
701          /* Load the certificate file and private key. */
702         long keyBio = 0;
703         long keyCertChainBio = 0;
704         long keyCertChainBio2 = 0;
705         PemEncoded encoded = null;
706         try {
707             // Only encode one time
708             encoded = PemX509Certificate.toPEM(ByteBufAllocator.DEFAULT, true, keyCertChain);
709             keyCertChainBio = toBIO(ByteBufAllocator.DEFAULT, encoded.retain());
710             keyCertChainBio2 = toBIO(ByteBufAllocator.DEFAULT, encoded.retain());
711 
712             if (key != null) {
713                 keyBio = toBIO(key);
714             }
715 
716             SSLContext.setCertificateBio(
717                     ctx, keyCertChainBio, keyBio,
718                     keyPassword == null ? StringUtil.EMPTY_STRING : keyPassword);
719             // We may have more then one cert in the chain so add all of them now.
720             SSLContext.setCertificateChainBio(ctx, keyCertChainBio2, true);
721         } catch (SSLException e) {
722             throw e;
723         } catch (Exception e) {
724             throw new SSLException("failed to set certificate and key", e);
725         } finally {
726             freeBio(keyBio);
727             freeBio(keyCertChainBio);
728             freeBio(keyCertChainBio2);
729             if (encoded != null) {
730                 encoded.release();
731             }
732         }
733     }
734 
735     static void freeBio(long bio) {
736         if (bio != 0) {
737             SSL.freeBIO(bio);
738         }
739     }
740 
741     /**
742      * Return the pointer to a <a href="https://www.openssl.org/docs/crypto/BIO_get_mem_ptr.html">in-memory BIO</a>
743      * or {@code 0} if the {@code key} is {@code null}. The BIO contains the content of the {@code key}.
744      */
745     static long toBIO(PrivateKey key) throws Exception {
746         if (key == null) {
747             return 0;
748         }
749 
750         ByteBufAllocator allocator = ByteBufAllocator.DEFAULT;
751         PemEncoded pem = PemPrivateKey.toPEM(allocator, true, key);
752         try {
753             return toBIO(allocator, pem.retain());
754         } finally {
755             pem.release();
756         }
757     }
758 
759     /**
760      * Return the pointer to a <a href="https://www.openssl.org/docs/crypto/BIO_get_mem_ptr.html">in-memory BIO</a>
761      * or {@code 0} if the {@code certChain} is {@code null}. The BIO contains the content of the {@code certChain}.
762      */
763     static long toBIO(X509Certificate... certChain) throws Exception {
764         if (certChain == null) {
765             return 0;
766         }
767 
768         if (certChain.length == 0) {
769             throw new IllegalArgumentException("certChain can't be empty");
770         }
771 
772         ByteBufAllocator allocator = ByteBufAllocator.DEFAULT;
773         PemEncoded pem = PemX509Certificate.toPEM(allocator, true, certChain);
774         try {
775             return toBIO(allocator, pem.retain());
776         } finally {
777             pem.release();
778         }
779     }
780 
781     static long toBIO(ByteBufAllocator allocator, PemEncoded pem) throws Exception {
782         try {
783             // We can turn direct buffers straight into BIOs. No need to
784             // make a yet another copy.
785             ByteBuf content = pem.content();
786 
787             if (content.isDirect()) {
788                 return newBIO(content.retainedSlice());
789             }
790 
791             ByteBuf buffer = allocator.directBuffer(content.readableBytes());
792             try {
793                 buffer.writeBytes(content, content.readerIndex(), content.readableBytes());
794                 return newBIO(buffer.retainedSlice());
795             } finally {
796                 try {
797                     // If the contents of the ByteBuf is sensitive (e.g. a PrivateKey) we
798                     // need to zero out the bytes of the copy before we're releasing it.
799                     if (pem.isSensitive()) {
800                         SslUtils.zeroout(buffer);
801                     }
802                 } finally {
803                     buffer.release();
804                 }
805             }
806         } finally {
807             pem.release();
808         }
809     }
810 
811     private static long newBIO(ByteBuf buffer) throws Exception {
812         try {
813             long bio = SSL.newMemBIO();
814             int readable = buffer.readableBytes();
815             if (SSL.bioWrite(bio, OpenSsl.memoryAddress(buffer) + buffer.readerIndex(), readable) != readable) {
816                 SSL.freeBIO(bio);
817                 throw new IllegalStateException("Could not write data to memory BIO");
818             }
819             return bio;
820         } finally {
821             buffer.release();
822         }
823     }
824 }