J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2001-2005 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package com.sun.net.ssl.internal.www.protocol.https; |
| 27 | |
| 28 | import java.net.URL; |
| 29 | import java.net.Proxy; |
| 30 | import java.io.IOException; |
| 31 | import java.util.Collection; |
| 32 | import java.util.List; |
| 33 | import java.util.Iterator; |
| 34 | |
| 35 | import java.security.Principal; |
| 36 | import java.security.cert.*; |
| 37 | |
| 38 | import javax.security.auth.x500.X500Principal; |
| 39 | import javax.security.auth.kerberos.KerberosPrincipal; |
| 40 | |
| 41 | import sun.security.util.HostnameChecker; |
| 42 | import sun.security.util.DerValue; |
| 43 | import sun.security.x509.X500Name; |
| 44 | |
| 45 | import sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection; |
| 46 | |
| 47 | /** |
| 48 | * This class was introduced to provide an additional level of |
| 49 | * abstraction between javax.net.ssl.HttpURLConnection and |
| 50 | * com.sun.net.ssl.HttpURLConnection objects. <p> |
| 51 | * |
| 52 | * javax.net.ssl.HttpURLConnection is used in the new sun.net version |
| 53 | * of protocol implementation (this one) |
| 54 | * com.sun.net.ssl.HttpURLConnection is used in the com.sun version. |
| 55 | * |
| 56 | */ |
| 57 | public class DelegateHttpsURLConnection extends AbstractDelegateHttpsURLConnection { |
| 58 | |
| 59 | // we need a reference to the HttpsURLConnection to get |
| 60 | // the properties set there |
| 61 | // we also need it to be public so that it can be referenced |
| 62 | // from sun.net.www.protocol.http.HttpURLConnection |
| 63 | // this is for ResponseCache.put(URI, URLConnection) |
| 64 | // second parameter needs to be cast to javax.net.ssl.HttpsURLConnection |
| 65 | // instead of AbstractDelegateHttpsURLConnection |
| 66 | public com.sun.net.ssl.HttpsURLConnection httpsURLConnection; |
| 67 | |
| 68 | DelegateHttpsURLConnection(URL url, |
| 69 | sun.net.www.protocol.http.Handler handler, |
| 70 | com.sun.net.ssl.HttpsURLConnection httpsURLConnection) |
| 71 | throws IOException { |
| 72 | this(url, null, handler, httpsURLConnection); |
| 73 | } |
| 74 | |
| 75 | DelegateHttpsURLConnection(URL url, Proxy p, |
| 76 | sun.net.www.protocol.http.Handler handler, |
| 77 | com.sun.net.ssl.HttpsURLConnection httpsURLConnection) |
| 78 | throws IOException { |
| 79 | super(url, p, handler); |
| 80 | this.httpsURLConnection = httpsURLConnection; |
| 81 | } |
| 82 | |
| 83 | protected javax.net.ssl.SSLSocketFactory getSSLSocketFactory() { |
| 84 | return httpsURLConnection.getSSLSocketFactory(); |
| 85 | } |
| 86 | |
| 87 | protected javax.net.ssl.HostnameVerifier getHostnameVerifier() { |
| 88 | // note: getHostnameVerifier() never returns null |
| 89 | return new VerifierWrapper(httpsURLConnection.getHostnameVerifier()); |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * Called by layered delegator's finalize() method to handle closing |
| 94 | * the underlying object. |
| 95 | */ |
| 96 | protected void dispose() throws Throwable { |
| 97 | super.finalize(); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | class VerifierWrapper implements javax.net.ssl.HostnameVerifier { |
| 102 | |
| 103 | private com.sun.net.ssl.HostnameVerifier verifier; |
| 104 | |
| 105 | VerifierWrapper(com.sun.net.ssl.HostnameVerifier verifier) { |
| 106 | this.verifier = verifier; |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * In com.sun.net.ssl.HostnameVerifier the method is defined |
| 111 | * as verify(String urlHostname, String certHostname). |
| 112 | * This means we need to extract the hostname from the certificate |
| 113 | * in this wrapper |
| 114 | */ |
| 115 | public boolean verify(String hostname, javax.net.ssl.SSLSession session) { |
| 116 | try { |
| 117 | String serverName; |
| 118 | Principal principal = getPeerPrincipal(session); |
| 119 | if (principal instanceof KerberosPrincipal) { |
| 120 | serverName = |
| 121 | HostnameChecker.getServerName((KerberosPrincipal)principal); |
| 122 | } else { |
| 123 | Certificate[] serverChain = session.getPeerCertificates(); |
| 124 | if ((serverChain == null) || (serverChain.length == 0)) { |
| 125 | return false; |
| 126 | } |
| 127 | if (serverChain[0] instanceof X509Certificate == false) { |
| 128 | return false; |
| 129 | } |
| 130 | X509Certificate serverCert = (X509Certificate)serverChain[0]; |
| 131 | serverName = getServername(serverCert); |
| 132 | } |
| 133 | if (serverName == null) { |
| 134 | return false; |
| 135 | } |
| 136 | return verifier.verify(hostname, serverName); |
| 137 | } catch (javax.net.ssl.SSLPeerUnverifiedException e) { |
| 138 | return false; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * Get the peer principal from the session |
| 144 | */ |
| 145 | private Principal getPeerPrincipal(javax.net.ssl.SSLSession session) |
| 146 | throws javax.net.ssl.SSLPeerUnverifiedException |
| 147 | { |
| 148 | Principal principal; |
| 149 | try { |
| 150 | principal = session.getPeerPrincipal(); |
| 151 | } catch (AbstractMethodError e) { |
| 152 | // if the provider does not support it, return null, since |
| 153 | // we need it only for Kerberos. |
| 154 | principal = null; |
| 155 | } |
| 156 | return principal; |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * Extract the name of the SSL server from the certificate. |
| 161 | * |
| 162 | * Note this code is essentially a subset of the hostname extraction |
| 163 | * code in HostnameChecker. |
| 164 | */ |
| 165 | private static String getServername(X509Certificate peerCert) { |
| 166 | try { |
| 167 | // compare to subjectAltNames if dnsName is present |
| 168 | Collection subjAltNames = peerCert.getSubjectAlternativeNames(); |
| 169 | if (subjAltNames != null) { |
| 170 | for (Iterator itr = subjAltNames.iterator(); itr.hasNext(); ) { |
| 171 | List next = (List)itr.next(); |
| 172 | if (((Integer)next.get(0)).intValue() == 2) { |
| 173 | // compare dNSName with host in url |
| 174 | String dnsName = ((String)next.get(1)); |
| 175 | return dnsName; |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | // else check against common name in the subject field |
| 181 | X500Name subject = HostnameChecker.getSubjectX500Name(peerCert); |
| 182 | |
| 183 | DerValue derValue = subject.findMostSpecificAttribute |
| 184 | (X500Name.commonName_oid); |
| 185 | if (derValue != null) { |
| 186 | try { |
| 187 | String name = derValue.getAsString(); |
| 188 | return name; |
| 189 | } catch (IOException e) { |
| 190 | // ignore |
| 191 | } |
| 192 | } |
| 193 | } catch (java.security.cert.CertificateException e) { |
| 194 | // ignore |
| 195 | } |
| 196 | return null; |
| 197 | } |
| 198 | |
| 199 | } |