|
| 1 | +/* |
| 2 | + * To the extent possible under law, Kevin Locke has waived all copyright and |
| 3 | + * related or neighboring rights to this work. |
| 4 | + * <p/> |
| 5 | + * A legal description of this waiver is available in <a href="https://gist.github.com/kevinoid/3829665">LICENSE.txt</a> |
| 6 | + */ |
| 7 | +package org.asynchttpclient.util; |
| 8 | + |
| 9 | +import javax.net.ssl.HostnameVerifier; |
| 10 | +import javax.net.ssl.SSLPeerUnverifiedException; |
| 11 | +import javax.net.ssl.SSLSession; |
| 12 | +import javax.security.auth.kerberos.KerberosPrincipal; |
| 13 | +import java.security.Principal; |
| 14 | +import java.security.cert.Certificate; |
| 15 | +import java.security.cert.CertificateException; |
| 16 | +import java.security.cert.X509Certificate; |
| 17 | +import java.util.logging.Level; |
| 18 | +import java.util.logging.Logger; |
| 19 | + |
| 20 | +/** |
| 21 | + * Uses the internal HostnameChecker to verify the server's hostname matches with the |
| 22 | + * certificate. This is a requirement for HTTPS, but the raw SSLEngine does not have |
| 23 | + * this functionality. As such, it has to be added in manually. For a more complete |
| 24 | + * description of hostname verification and why it's important, |
| 25 | + * please read |
| 26 | + * <a href="http://tersesystems.com/2014/03/23/fixing-hostname-verification/">Fixing |
| 27 | + * Hostname Verification</a>. |
| 28 | + * <p/> |
| 29 | + * This code is based on Kevin Locke's <a href="http://kevinlocke.name/bits/2012/10/03/ssl-certificate-verification-in-dispatch-and-asynchttpclient/">guide</a> . |
| 30 | + * <p/> |
| 31 | + */ |
| 32 | +public class DefaultHostnameVerifier implements HostnameVerifier { |
| 33 | + |
| 34 | + private HostnameChecker checker; |
| 35 | + |
| 36 | + private HostnameVerifier extraHostnameVerifier; |
| 37 | + |
| 38 | + // Logger to log exceptions. |
| 39 | + private static final Logger log = Logger.getLogger(DefaultHostnameVerifier.class.getName()); |
| 40 | + |
| 41 | + /** |
| 42 | + * A hostname verifier that uses the {{sun.security.util.HostnameChecker}} under the hood. |
| 43 | + */ |
| 44 | + public DefaultHostnameVerifier() { |
| 45 | + this.checker = new ProxyHostnameChecker(); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * A hostname verifier that takes an external hostname checker. Useful for testing. |
| 50 | + * |
| 51 | + * @param checker a hostnamechecker. |
| 52 | + */ |
| 53 | + public DefaultHostnameVerifier(HostnameChecker checker) { |
| 54 | + this.checker = checker; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * A hostname verifier that falls back to another hostname verifier if not found. |
| 59 | + * |
| 60 | + * @param extraHostnameVerifier another hostname verifier. |
| 61 | + */ |
| 62 | + public DefaultHostnameVerifier(HostnameVerifier extraHostnameVerifier) { |
| 63 | + this.checker = new ProxyHostnameChecker(); |
| 64 | + this.extraHostnameVerifier = extraHostnameVerifier; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * A hostname verifier with a hostname checker, that falls back to another hostname verifier if not found. |
| 69 | + * |
| 70 | + * @param checker a custom HostnameChecker. |
| 71 | + * @param extraHostnameVerifier another hostname verifier. |
| 72 | + */ |
| 73 | + public DefaultHostnameVerifier(HostnameChecker checker, HostnameVerifier extraHostnameVerifier) { |
| 74 | + this.checker = checker; |
| 75 | + this.extraHostnameVerifier = extraHostnameVerifier; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Matches the hostname against the peer certificate in the session. |
| 80 | + * |
| 81 | + * @param hostname the IP address or hostname of the expected server. |
| 82 | + * @param session the SSL session containing the certificates with the ACTUAL hostname/ipaddress. |
| 83 | + * @return true if the hostname matches, false otherwise. |
| 84 | + */ |
| 85 | + private boolean hostnameMatches(String hostname, SSLSession session) { |
| 86 | + log.log(Level.FINE, "hostname = {0}, session = {1}", new Object[] { hostname, Base64.encode(session.getId()) }); |
| 87 | + |
| 88 | + try { |
| 89 | + final Certificate[] peerCertificates = session.getPeerCertificates(); |
| 90 | + if (peerCertificates.length == 0) { |
| 91 | + log.log(Level.FINE, "No peer certificates"); |
| 92 | + return false; |
| 93 | + } |
| 94 | + |
| 95 | + if (peerCertificates[0] instanceof X509Certificate) { |
| 96 | + X509Certificate peerCertificate = (X509Certificate) peerCertificates[0]; |
| 97 | + log.log(Level.FINE, "peerCertificate = {0}", peerCertificate); |
| 98 | + try { |
| 99 | + checker.match(hostname, peerCertificate); |
| 100 | + // Certificate matches hostname if no exception is thrown. |
| 101 | + return true; |
| 102 | + } catch (CertificateException ex) { |
| 103 | + log.log(Level.FINE, "Certificate does not match hostname", ex); |
| 104 | + } |
| 105 | + } else { |
| 106 | + log.log(Level.FINE, "Peer does not have any certificates or they aren't X.509"); |
| 107 | + } |
| 108 | + return false; |
| 109 | + } catch (SSLPeerUnverifiedException ex) { |
| 110 | + log.log(Level.FINE, "Not using certificates for peers, try verifying the principal"); |
| 111 | + try { |
| 112 | + Principal peerPrincipal = session.getPeerPrincipal(); |
| 113 | + log.log(Level.FINE, "peerPrincipal = {0}", peerPrincipal); |
| 114 | + if (peerPrincipal instanceof KerberosPrincipal) { |
| 115 | + return checker.match(hostname, (KerberosPrincipal) peerPrincipal); |
| 116 | + } else { |
| 117 | + log.log(Level.FINE, "Can't verify principal, not Kerberos"); |
| 118 | + } |
| 119 | + } catch (SSLPeerUnverifiedException ex2) { |
| 120 | + // Can't verify principal, no principal |
| 121 | + log.log(Level.FINE, "Can't verify principal, no principal", ex2); |
| 122 | + } |
| 123 | + return false; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Verifies the hostname against the peer certificates in a session. Falls back to extraHostnameVerifier if |
| 129 | + * there is no match. |
| 130 | + * |
| 131 | + * @param hostname the IP address or hostname of the expected server. |
| 132 | + * @param session the SSL session containing the certificates with the ACTUAL hostname/ipaddress. |
| 133 | + * @return true if the hostname matches, false otherwise. |
| 134 | + */ |
| 135 | + public boolean verify(String hostname, SSLSession session) { |
| 136 | + if (hostnameMatches(hostname, session)) { |
| 137 | + return true; |
| 138 | + } else { |
| 139 | + return extraHostnameVerifier != null && extraHostnameVerifier.verify(hostname, session); |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments