|
10 | 10 | import javax.net.ssl.SSLPeerUnverifiedException;
|
11 | 11 | import javax.net.ssl.SSLSession;
|
12 | 12 | import javax.security.auth.kerberos.KerberosPrincipal;
|
13 |
| -import java.lang.reflect.InvocationTargetException; |
14 |
| -import java.lang.reflect.Method; |
15 | 13 | import java.security.Principal;
|
16 | 14 | import java.security.cert.Certificate;
|
17 | 15 | import java.security.cert.CertificateException;
|
18 | 16 | import java.security.cert.X509Certificate;
|
| 17 | +import java.util.logging.Level; |
| 18 | +import java.util.logging.Logger; |
19 | 19 |
|
20 | 20 | /**
|
21 | 21 | * Uses the internal HostnameChecker to verify the server's hostname matches with the
|
|
31 | 31 | */
|
32 | 32 | public class DefaultHostnameVerifier implements HostnameVerifier {
|
33 | 33 |
|
| 34 | + private HostnameChecker checker; |
| 35 | + |
34 | 36 | private HostnameVerifier extraHostnameVerifier;
|
35 | 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 | + */ |
36 | 44 | public DefaultHostnameVerifier() {
|
| 45 | + this.checker = new ProxyHostnameChecker(); |
37 | 46 | }
|
38 | 47 |
|
39 |
| - public DefaultHostnameVerifier(HostnameVerifier extraHostnameVerifier) { |
40 |
| - this.extraHostnameVerifier = extraHostnameVerifier; |
| 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; |
41 | 55 | }
|
42 | 56 |
|
43 |
| - public final static byte TYPE_TLS = 1; |
44 |
| - |
45 |
| - private Object getHostnameChecker() { |
46 |
| - final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); |
47 |
| - try { |
48 |
| - final Class<Object> hostnameCheckerClass = (Class<Object>) classLoader.loadClass("sun.security.util.HostnameChecker"); |
49 |
| - final Method instanceMethod = hostnameCheckerClass.getMethod("getInstance", Byte.TYPE); |
50 |
| - final Object hostnameChecker = instanceMethod.invoke(null, TYPE_TLS); |
51 |
| - return hostnameChecker; |
52 |
| - } catch (ClassNotFoundException e) { |
53 |
| - throw new IllegalStateException(e); |
54 |
| - } catch (NoSuchMethodException e) { |
55 |
| - throw new IllegalStateException(e); |
56 |
| - } catch (InvocationTargetException e) { |
57 |
| - throw new IllegalStateException(e); |
58 |
| - } catch (IllegalAccessException e) { |
59 |
| - throw new IllegalStateException(e); |
60 |
| - } |
61 |
| - } |
62 |
| - |
63 |
| - private void match(Object checker, String hostname, X509Certificate peerCertificate) throws CertificateException { |
64 |
| - try { |
65 |
| - final Class<?> hostnameCheckerClass = checker.getClass(); |
66 |
| - final Method checkMethod = hostnameCheckerClass.getMethod("match", String.class, X509Certificate.class); |
67 |
| - checkMethod.invoke(checker, hostname, peerCertificate); |
68 |
| - } catch (NoSuchMethodException e) { |
69 |
| - throw new IllegalStateException(e); |
70 |
| - } catch (InvocationTargetException e) { |
71 |
| - Throwable t = e.getCause(); |
72 |
| - if (t instanceof CertificateException) { |
73 |
| - throw (CertificateException) t; |
74 |
| - } else { |
75 |
| - throw new IllegalStateException(e); |
76 |
| - } |
77 |
| - } catch (IllegalAccessException e) { |
78 |
| - throw new IllegalStateException(e); |
79 |
| - } |
| 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; |
80 | 65 | }
|
81 | 66 |
|
82 |
| - private boolean match(Object checker, String hostname, Principal principal) { |
83 |
| - try { |
84 |
| - final Class<?> hostnameCheckerClass = checker.getClass(); |
85 |
| - final Method checkMethod = hostnameCheckerClass.getMethod("match", String.class, Principal.class); |
86 |
| - final boolean result = (Boolean) checkMethod.invoke(null, hostname, principal); |
87 |
| - return result; |
88 |
| - } catch (NoSuchMethodException e) { |
89 |
| - throw new IllegalStateException(e); |
90 |
| - } catch (InvocationTargetException e) { |
91 |
| - throw new IllegalStateException(e); |
92 |
| - } catch (IllegalAccessException e) { |
93 |
| - throw new IllegalStateException(e); |
94 |
| - } |
| 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; |
95 | 76 | }
|
96 | 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 | + */ |
97 | 85 | private boolean hostnameMatches(String hostname, SSLSession session) {
|
98 |
| - boolean validCertificate = false; |
99 |
| - boolean validPrincipal = false; |
| 86 | + log.log(Level.FINE, "hostname = {0}, session = {1}", new Object[] { hostname, Base64.encode(session.getId()) }); |
100 | 87 |
|
101 |
| - final Object checker = getHostnameChecker(); |
102 | 88 | try {
|
103 |
| - |
104 | 89 | final Certificate[] peerCertificates = session.getPeerCertificates();
|
| 90 | + if (peerCertificates.length == 0) { |
| 91 | + log.log(Level.FINE, "No peer certificates"); |
| 92 | + return false; |
| 93 | + } |
105 | 94 |
|
106 |
| - if (peerCertificates.length > 0 && |
107 |
| - peerCertificates[0] instanceof X509Certificate) { |
108 |
| - X509Certificate peerCertificate = |
109 |
| - (X509Certificate) peerCertificates[0]; |
110 |
| - |
| 95 | + if (peerCertificates[0] instanceof X509Certificate) { |
| 96 | + X509Certificate peerCertificate = (X509Certificate) peerCertificates[0]; |
| 97 | + log.log(Level.FINE, "peerCertificate = {0}", peerCertificate); |
111 | 98 | try {
|
112 |
| - match(checker, hostname, peerCertificate); |
113 |
| - // Certificate matches hostname |
114 |
| - validCertificate = true; |
| 99 | + checker.match(hostname, peerCertificate); |
| 100 | + // Certificate matches hostname if no exception is thrown. |
| 101 | + return true; |
115 | 102 | } catch (CertificateException ex) {
|
116 |
| - // Certificate does not match hostname |
| 103 | + log.log(Level.FINE, "Certificate does not match hostname", ex); |
117 | 104 | }
|
118 | 105 | } else {
|
119 |
| - // Peer does not have any certificates or they aren't X.509 |
| 106 | + log.log(Level.FINE, "Peer does not have any certificates or they aren't X.509"); |
120 | 107 | }
|
| 108 | + return false; |
121 | 109 | } catch (SSLPeerUnverifiedException ex) {
|
122 |
| - // Not using certificates for peers, try verifying the principal |
| 110 | + log.log(Level.FINE, "Not using certificates for peers, try verifying the principal"); |
123 | 111 | try {
|
124 | 112 | Principal peerPrincipal = session.getPeerPrincipal();
|
| 113 | + log.log(Level.FINE, "peerPrincipal = {0}", peerPrincipal); |
125 | 114 | if (peerPrincipal instanceof KerberosPrincipal) {
|
126 |
| - validPrincipal = match(checker, hostname, |
127 |
| - (KerberosPrincipal) peerPrincipal); |
| 115 | + return checker.match(hostname, (KerberosPrincipal) peerPrincipal); |
128 | 116 | } else {
|
129 |
| - // Can't verify principal, not Kerberos |
| 117 | + log.log(Level.FINE, "Can't verify principal, not Kerberos"); |
130 | 118 | }
|
131 | 119 | } catch (SSLPeerUnverifiedException ex2) {
|
132 | 120 | // Can't verify principal, no principal
|
| 121 | + log.log(Level.FINE, "Can't verify principal, no principal", ex2); |
133 | 122 | }
|
| 123 | + return false; |
134 | 124 | }
|
135 |
| - return validCertificate || validPrincipal; |
136 | 125 | }
|
137 | 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 | + */ |
138 | 135 | public boolean verify(String hostname, SSLSession session) {
|
139 | 136 | if (hostnameMatches(hostname, session)) {
|
140 | 137 | return true;
|
141 | 138 | } else {
|
142 |
| - if (extraHostnameVerifier != null) { |
143 |
| - return extraHostnameVerifier.verify(hostname, session); |
144 |
| - } else { |
145 |
| - return false; |
146 |
| - } |
| 139 | + return extraHostnameVerifier != null && extraHostnameVerifier.verify(hostname, session); |
147 | 140 | }
|
148 | 141 | }
|
149 | 142 | }
|
0 commit comments