Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | * ==================================================================== |
michael@0 | 3 | * Licensed to the Apache Software Foundation (ASF) under one |
michael@0 | 4 | * or more contributor license agreements. See the NOTICE file |
michael@0 | 5 | * distributed with this work for additional information |
michael@0 | 6 | * regarding copyright ownership. The ASF licenses this file |
michael@0 | 7 | * to you under the Apache License, Version 2.0 (the |
michael@0 | 8 | * "License"); you may not use this file except in compliance |
michael@0 | 9 | * with the License. You may obtain a copy of the License at |
michael@0 | 10 | * |
michael@0 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 12 | * |
michael@0 | 13 | * Unless required by applicable law or agreed to in writing, |
michael@0 | 14 | * software distributed under the License is distributed on an |
michael@0 | 15 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
michael@0 | 16 | * KIND, either express or implied. See the License for the |
michael@0 | 17 | * specific language governing permissions and limitations |
michael@0 | 18 | * under the License. |
michael@0 | 19 | * ==================================================================== |
michael@0 | 20 | * |
michael@0 | 21 | * This software consists of voluntary contributions made by many |
michael@0 | 22 | * individuals on behalf of the Apache Software Foundation. For more |
michael@0 | 23 | * information on the Apache Software Foundation, please see |
michael@0 | 24 | * <http://www.apache.org/>. |
michael@0 | 25 | * |
michael@0 | 26 | */ |
michael@0 | 27 | |
michael@0 | 28 | package ch.boye.httpclientandroidlib.conn.ssl; |
michael@0 | 29 | |
michael@0 | 30 | import ch.boye.httpclientandroidlib.annotation.Immutable; |
michael@0 | 31 | |
michael@0 | 32 | import ch.boye.httpclientandroidlib.conn.util.InetAddressUtils; |
michael@0 | 33 | |
michael@0 | 34 | import java.io.IOException; |
michael@0 | 35 | import java.io.InputStream; |
michael@0 | 36 | import java.security.cert.Certificate; |
michael@0 | 37 | import java.security.cert.CertificateParsingException; |
michael@0 | 38 | import java.security.cert.X509Certificate; |
michael@0 | 39 | import java.util.Arrays; |
michael@0 | 40 | import java.util.Collection; |
michael@0 | 41 | import java.util.Iterator; |
michael@0 | 42 | import java.util.LinkedList; |
michael@0 | 43 | import java.util.List; |
michael@0 | 44 | import java.util.Locale; |
michael@0 | 45 | import java.util.StringTokenizer; |
michael@0 | 46 | import java.util.logging.Logger; |
michael@0 | 47 | import java.util.logging.Level; |
michael@0 | 48 | |
michael@0 | 49 | import javax.net.ssl.SSLException; |
michael@0 | 50 | import javax.net.ssl.SSLSession; |
michael@0 | 51 | import javax.net.ssl.SSLSocket; |
michael@0 | 52 | |
michael@0 | 53 | /** |
michael@0 | 54 | * Abstract base class for all standard {@link X509HostnameVerifier} |
michael@0 | 55 | * implementations. |
michael@0 | 56 | * |
michael@0 | 57 | * @since 4.0 |
michael@0 | 58 | */ |
michael@0 | 59 | @Immutable |
michael@0 | 60 | public abstract class AbstractVerifier implements X509HostnameVerifier { |
michael@0 | 61 | |
michael@0 | 62 | /** |
michael@0 | 63 | * This contains a list of 2nd-level domains that aren't allowed to |
michael@0 | 64 | * have wildcards when combined with country-codes. |
michael@0 | 65 | * For example: [*.co.uk]. |
michael@0 | 66 | * <p/> |
michael@0 | 67 | * The [*.co.uk] problem is an interesting one. Should we just hope |
michael@0 | 68 | * that CA's would never foolishly allow such a certificate to happen? |
michael@0 | 69 | * Looks like we're the only implementation guarding against this. |
michael@0 | 70 | * Firefox, Curl, Sun Java 1.4, 5, 6 don't bother with this check. |
michael@0 | 71 | */ |
michael@0 | 72 | private final static String[] BAD_COUNTRY_2LDS = |
michael@0 | 73 | { "ac", "co", "com", "ed", "edu", "go", "gouv", "gov", "info", |
michael@0 | 74 | "lg", "ne", "net", "or", "org" }; |
michael@0 | 75 | |
michael@0 | 76 | static { |
michael@0 | 77 | // Just in case developer forgot to manually sort the array. :-) |
michael@0 | 78 | Arrays.sort(BAD_COUNTRY_2LDS); |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | public AbstractVerifier() { |
michael@0 | 82 | super(); |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | public final void verify(String host, SSLSocket ssl) |
michael@0 | 86 | throws IOException { |
michael@0 | 87 | if(host == null) { |
michael@0 | 88 | throw new NullPointerException("host to verify is null"); |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | SSLSession session = ssl.getSession(); |
michael@0 | 92 | if(session == null) { |
michael@0 | 93 | // In our experience this only happens under IBM 1.4.x when |
michael@0 | 94 | // spurious (unrelated) certificates show up in the server' |
michael@0 | 95 | // chain. Hopefully this will unearth the real problem: |
michael@0 | 96 | InputStream in = ssl.getInputStream(); |
michael@0 | 97 | in.available(); |
michael@0 | 98 | /* |
michael@0 | 99 | If you're looking at the 2 lines of code above because |
michael@0 | 100 | you're running into a problem, you probably have two |
michael@0 | 101 | options: |
michael@0 | 102 | |
michael@0 | 103 | #1. Clean up the certificate chain that your server |
michael@0 | 104 | is presenting (e.g. edit "/etc/apache2/server.crt" |
michael@0 | 105 | or wherever it is your server's certificate chain |
michael@0 | 106 | is defined). |
michael@0 | 107 | |
michael@0 | 108 | OR |
michael@0 | 109 | |
michael@0 | 110 | #2. Upgrade to an IBM 1.5.x or greater JVM, or switch |
michael@0 | 111 | to a non-IBM JVM. |
michael@0 | 112 | */ |
michael@0 | 113 | |
michael@0 | 114 | // If ssl.getInputStream().available() didn't cause an |
michael@0 | 115 | // exception, maybe at least now the session is available? |
michael@0 | 116 | session = ssl.getSession(); |
michael@0 | 117 | if(session == null) { |
michael@0 | 118 | // If it's still null, probably a startHandshake() will |
michael@0 | 119 | // unearth the real problem. |
michael@0 | 120 | ssl.startHandshake(); |
michael@0 | 121 | |
michael@0 | 122 | // Okay, if we still haven't managed to cause an exception, |
michael@0 | 123 | // might as well go for the NPE. Or maybe we're okay now? |
michael@0 | 124 | session = ssl.getSession(); |
michael@0 | 125 | } |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | Certificate[] certs = session.getPeerCertificates(); |
michael@0 | 129 | X509Certificate x509 = (X509Certificate) certs[0]; |
michael@0 | 130 | verify(host, x509); |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | public final boolean verify(String host, SSLSession session) { |
michael@0 | 134 | try { |
michael@0 | 135 | Certificate[] certs = session.getPeerCertificates(); |
michael@0 | 136 | X509Certificate x509 = (X509Certificate) certs[0]; |
michael@0 | 137 | verify(host, x509); |
michael@0 | 138 | return true; |
michael@0 | 139 | } |
michael@0 | 140 | catch(SSLException e) { |
michael@0 | 141 | return false; |
michael@0 | 142 | } |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | public final void verify(String host, X509Certificate cert) |
michael@0 | 146 | throws SSLException { |
michael@0 | 147 | String[] cns = getCNs(cert); |
michael@0 | 148 | String[] subjectAlts = getSubjectAlts(cert, host); |
michael@0 | 149 | verify(host, cns, subjectAlts); |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | public final void verify(final String host, final String[] cns, |
michael@0 | 153 | final String[] subjectAlts, |
michael@0 | 154 | final boolean strictWithSubDomains) |
michael@0 | 155 | throws SSLException { |
michael@0 | 156 | |
michael@0 | 157 | // Build the list of names we're going to check. Our DEFAULT and |
michael@0 | 158 | // STRICT implementations of the HostnameVerifier only use the |
michael@0 | 159 | // first CN provided. All other CNs are ignored. |
michael@0 | 160 | // (Firefox, wget, curl, Sun Java 1.4, 5, 6 all work this way). |
michael@0 | 161 | LinkedList<String> names = new LinkedList<String>(); |
michael@0 | 162 | if(cns != null && cns.length > 0 && cns[0] != null) { |
michael@0 | 163 | names.add(cns[0]); |
michael@0 | 164 | } |
michael@0 | 165 | if(subjectAlts != null) { |
michael@0 | 166 | for (String subjectAlt : subjectAlts) { |
michael@0 | 167 | if (subjectAlt != null) { |
michael@0 | 168 | names.add(subjectAlt); |
michael@0 | 169 | } |
michael@0 | 170 | } |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | if(names.isEmpty()) { |
michael@0 | 174 | String msg = "Certificate for <" + host + "> doesn't contain CN or DNS subjectAlt"; |
michael@0 | 175 | throw new SSLException(msg); |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | // StringBuilder for building the error message. |
michael@0 | 179 | StringBuilder buf = new StringBuilder(); |
michael@0 | 180 | |
michael@0 | 181 | // We're can be case-insensitive when comparing the host we used to |
michael@0 | 182 | // establish the socket to the hostname in the certificate. |
michael@0 | 183 | String hostName = host.trim().toLowerCase(Locale.ENGLISH); |
michael@0 | 184 | boolean match = false; |
michael@0 | 185 | for(Iterator<String> it = names.iterator(); it.hasNext();) { |
michael@0 | 186 | // Don't trim the CN, though! |
michael@0 | 187 | String cn = it.next(); |
michael@0 | 188 | cn = cn.toLowerCase(Locale.ENGLISH); |
michael@0 | 189 | // Store CN in StringBuilder in case we need to report an error. |
michael@0 | 190 | buf.append(" <"); |
michael@0 | 191 | buf.append(cn); |
michael@0 | 192 | buf.append('>'); |
michael@0 | 193 | if(it.hasNext()) { |
michael@0 | 194 | buf.append(" OR"); |
michael@0 | 195 | } |
michael@0 | 196 | |
michael@0 | 197 | // The CN better have at least two dots if it wants wildcard |
michael@0 | 198 | // action. It also can't be [*.co.uk] or [*.co.jp] or |
michael@0 | 199 | // [*.org.uk], etc... |
michael@0 | 200 | String parts[] = cn.split("\\."); |
michael@0 | 201 | boolean doWildcard = parts.length >= 3 && |
michael@0 | 202 | parts[0].endsWith("*") && |
michael@0 | 203 | acceptableCountryWildcard(cn) && |
michael@0 | 204 | !isIPAddress(host); |
michael@0 | 205 | |
michael@0 | 206 | if(doWildcard) { |
michael@0 | 207 | if (parts[0].length() > 1) { // e.g. server* |
michael@0 | 208 | String prefix = parts[0].substring(0, parts.length-2); // e.g. server |
michael@0 | 209 | String suffix = cn.substring(parts[0].length()); // skip wildcard part from cn |
michael@0 | 210 | String hostSuffix = hostName.substring(prefix.length()); // skip wildcard part from host |
michael@0 | 211 | match = hostName.startsWith(prefix) && hostSuffix.endsWith(suffix); |
michael@0 | 212 | } else { |
michael@0 | 213 | match = hostName.endsWith(cn.substring(1)); |
michael@0 | 214 | } |
michael@0 | 215 | if(match && strictWithSubDomains) { |
michael@0 | 216 | // If we're in strict mode, then [*.foo.com] is not |
michael@0 | 217 | // allowed to match [a.b.foo.com] |
michael@0 | 218 | match = countDots(hostName) == countDots(cn); |
michael@0 | 219 | } |
michael@0 | 220 | } else { |
michael@0 | 221 | match = hostName.equals(cn); |
michael@0 | 222 | } |
michael@0 | 223 | if(match) { |
michael@0 | 224 | break; |
michael@0 | 225 | } |
michael@0 | 226 | } |
michael@0 | 227 | if(!match) { |
michael@0 | 228 | throw new SSLException("hostname in certificate didn't match: <" + host + "> !=" + buf); |
michael@0 | 229 | } |
michael@0 | 230 | } |
michael@0 | 231 | |
michael@0 | 232 | public static boolean acceptableCountryWildcard(String cn) { |
michael@0 | 233 | String parts[] = cn.split("\\."); |
michael@0 | 234 | if (parts.length != 3 || parts[2].length() != 2) { |
michael@0 | 235 | return true; // it's not an attempt to wildcard a 2TLD within a country code |
michael@0 | 236 | } |
michael@0 | 237 | return Arrays.binarySearch(BAD_COUNTRY_2LDS, parts[1]) < 0; |
michael@0 | 238 | } |
michael@0 | 239 | |
michael@0 | 240 | public static String[] getCNs(X509Certificate cert) { |
michael@0 | 241 | LinkedList<String> cnList = new LinkedList<String>(); |
michael@0 | 242 | /* |
michael@0 | 243 | Sebastian Hauer's original StrictSSLProtocolSocketFactory used |
michael@0 | 244 | getName() and had the following comment: |
michael@0 | 245 | |
michael@0 | 246 | Parses a X.500 distinguished name for the value of the |
michael@0 | 247 | "Common Name" field. This is done a bit sloppy right |
michael@0 | 248 | now and should probably be done a bit more according to |
michael@0 | 249 | <code>RFC 2253</code>. |
michael@0 | 250 | |
michael@0 | 251 | I've noticed that toString() seems to do a better job than |
michael@0 | 252 | getName() on these X500Principal objects, so I'm hoping that |
michael@0 | 253 | addresses Sebastian's concern. |
michael@0 | 254 | |
michael@0 | 255 | For example, getName() gives me this: |
michael@0 | 256 | 1.2.840.113549.1.9.1=#16166a756c6975736461766965734063756362632e636f6d |
michael@0 | 257 | |
michael@0 | 258 | whereas toString() gives me this: |
michael@0 | 259 | EMAILADDRESS=juliusdavies@cucbc.com |
michael@0 | 260 | |
michael@0 | 261 | Looks like toString() even works with non-ascii domain names! |
michael@0 | 262 | I tested it with "花子.co.jp" and it worked fine. |
michael@0 | 263 | */ |
michael@0 | 264 | String subjectPrincipal = cert.getSubjectX500Principal().toString(); |
michael@0 | 265 | StringTokenizer st = new StringTokenizer(subjectPrincipal, ","); |
michael@0 | 266 | while(st.hasMoreTokens()) { |
michael@0 | 267 | String tok = st.nextToken(); |
michael@0 | 268 | int x = tok.indexOf("CN="); |
michael@0 | 269 | if(x >= 0) { |
michael@0 | 270 | cnList.add(tok.substring(x + 3)); |
michael@0 | 271 | } |
michael@0 | 272 | } |
michael@0 | 273 | if(!cnList.isEmpty()) { |
michael@0 | 274 | String[] cns = new String[cnList.size()]; |
michael@0 | 275 | cnList.toArray(cns); |
michael@0 | 276 | return cns; |
michael@0 | 277 | } else { |
michael@0 | 278 | return null; |
michael@0 | 279 | } |
michael@0 | 280 | } |
michael@0 | 281 | |
michael@0 | 282 | /** |
michael@0 | 283 | * Extracts the array of SubjectAlt DNS or IP names from an X509Certificate. |
michael@0 | 284 | * Returns null if there aren't any. |
michael@0 | 285 | * |
michael@0 | 286 | * @param cert X509Certificate |
michael@0 | 287 | * @param hostname |
michael@0 | 288 | * @return Array of SubjectALT DNS or IP names stored in the certificate. |
michael@0 | 289 | */ |
michael@0 | 290 | private static String[] getSubjectAlts( |
michael@0 | 291 | final X509Certificate cert, final String hostname) { |
michael@0 | 292 | int subjectType; |
michael@0 | 293 | if (isIPAddress(hostname)) { |
michael@0 | 294 | subjectType = 7; |
michael@0 | 295 | } else { |
michael@0 | 296 | subjectType = 2; |
michael@0 | 297 | } |
michael@0 | 298 | |
michael@0 | 299 | LinkedList<String> subjectAltList = new LinkedList<String>(); |
michael@0 | 300 | Collection<List<?>> c = null; |
michael@0 | 301 | try { |
michael@0 | 302 | c = cert.getSubjectAlternativeNames(); |
michael@0 | 303 | } |
michael@0 | 304 | catch(CertificateParsingException cpe) { |
michael@0 | 305 | Logger.getLogger(AbstractVerifier.class.getName()) |
michael@0 | 306 | .log(Level.FINE, "Error parsing certificate.", cpe); |
michael@0 | 307 | } |
michael@0 | 308 | if(c != null) { |
michael@0 | 309 | for (List<?> aC : c) { |
michael@0 | 310 | List<?> list = aC; |
michael@0 | 311 | int type = ((Integer) list.get(0)).intValue(); |
michael@0 | 312 | if (type == subjectType) { |
michael@0 | 313 | String s = (String) list.get(1); |
michael@0 | 314 | subjectAltList.add(s); |
michael@0 | 315 | } |
michael@0 | 316 | } |
michael@0 | 317 | } |
michael@0 | 318 | if(!subjectAltList.isEmpty()) { |
michael@0 | 319 | String[] subjectAlts = new String[subjectAltList.size()]; |
michael@0 | 320 | subjectAltList.toArray(subjectAlts); |
michael@0 | 321 | return subjectAlts; |
michael@0 | 322 | } else { |
michael@0 | 323 | return null; |
michael@0 | 324 | } |
michael@0 | 325 | } |
michael@0 | 326 | |
michael@0 | 327 | /** |
michael@0 | 328 | * Extracts the array of SubjectAlt DNS names from an X509Certificate. |
michael@0 | 329 | * Returns null if there aren't any. |
michael@0 | 330 | * <p/> |
michael@0 | 331 | * Note: Java doesn't appear able to extract international characters |
michael@0 | 332 | * from the SubjectAlts. It can only extract international characters |
michael@0 | 333 | * from the CN field. |
michael@0 | 334 | * <p/> |
michael@0 | 335 | * (Or maybe the version of OpenSSL I'm using to test isn't storing the |
michael@0 | 336 | * international characters correctly in the SubjectAlts?). |
michael@0 | 337 | * |
michael@0 | 338 | * @param cert X509Certificate |
michael@0 | 339 | * @return Array of SubjectALT DNS names stored in the certificate. |
michael@0 | 340 | */ |
michael@0 | 341 | public static String[] getDNSSubjectAlts(X509Certificate cert) { |
michael@0 | 342 | return getSubjectAlts(cert, null); |
michael@0 | 343 | } |
michael@0 | 344 | |
michael@0 | 345 | /** |
michael@0 | 346 | * Counts the number of dots "." in a string. |
michael@0 | 347 | * @param s string to count dots from |
michael@0 | 348 | * @return number of dots |
michael@0 | 349 | */ |
michael@0 | 350 | public static int countDots(final String s) { |
michael@0 | 351 | int count = 0; |
michael@0 | 352 | for(int i = 0; i < s.length(); i++) { |
michael@0 | 353 | if(s.charAt(i) == '.') { |
michael@0 | 354 | count++; |
michael@0 | 355 | } |
michael@0 | 356 | } |
michael@0 | 357 | return count; |
michael@0 | 358 | } |
michael@0 | 359 | |
michael@0 | 360 | private static boolean isIPAddress(final String hostname) { |
michael@0 | 361 | return hostname != null && |
michael@0 | 362 | (InetAddressUtils.isIPv4Address(hostname) || |
michael@0 | 363 | InetAddressUtils.isIPv6Address(hostname)); |
michael@0 | 364 | } |
michael@0 | 365 | |
michael@0 | 366 | } |