|
|
f20e4f |
Index: oac.hc3x/trunk/src/java/org/apache/commons/httpclient/protocol/SSLProtocolSocketFactory.java
|
|
|
f20e4f |
===================================================================
|
|
|
f20e4f |
diff -u -N -r608014 -r1422573
|
|
|
f20e4f |
--- oac.hc3x/trunk/src/java/org/apache/commons/httpclient/protocol/SSLProtocolSocketFactory.java (.../SSLProtocolSocketFactory.java) (revision 608014)
|
|
|
f20e4f |
+++ oac.hc3x/trunk/src/java/org/apache/commons/httpclient/protocol/SSLProtocolSocketFactory.java (.../SSLProtocolSocketFactory.java) (revision 1422573)
|
|
|
f20e4f |
@@ -31,10 +31,25 @@
|
|
|
f20e4f |
package org.apache.commons.httpclient.protocol;
|
|
|
f20e4f |
|
|
|
f20e4f |
import java.io.IOException;
|
|
|
f20e4f |
+import java.io.InputStream;
|
|
|
f20e4f |
import java.net.InetAddress;
|
|
|
f20e4f |
import java.net.Socket;
|
|
|
f20e4f |
import java.net.UnknownHostException;
|
|
|
f20e4f |
+import java.security.cert.Certificate;
|
|
|
f20e4f |
+import java.security.cert.CertificateParsingException;
|
|
|
f20e4f |
+import java.security.cert.X509Certificate;
|
|
|
f20e4f |
+import java.util.Arrays;
|
|
|
f20e4f |
+import java.util.Collection;
|
|
|
f20e4f |
+import java.util.Iterator;
|
|
|
f20e4f |
+import java.util.LinkedList;
|
|
|
f20e4f |
+import java.util.List;
|
|
|
f20e4f |
+import java.util.Locale;
|
|
|
f20e4f |
+import java.util.StringTokenizer;
|
|
|
f20e4f |
+import java.util.regex.Pattern;
|
|
|
f20e4f |
|
|
|
f20e4f |
+import javax.net.ssl.SSLException;
|
|
|
f20e4f |
+import javax.net.ssl.SSLSession;
|
|
|
f20e4f |
+import javax.net.ssl.SSLSocket;
|
|
|
f20e4f |
import javax.net.ssl.SSLSocketFactory;
|
|
|
f20e4f |
|
|
|
f20e4f |
import org.apache.commons.httpclient.ConnectTimeoutException;
|
|
|
f20e4f |
@@ -55,6 +70,11 @@
|
|
|
f20e4f |
*/
|
|
|
f20e4f |
private static final SSLProtocolSocketFactory factory = new SSLProtocolSocketFactory();
|
|
|
f20e4f |
|
|
|
f20e4f |
+ // This is a a sorted list, if you insert new elements do it orderdered.
|
|
|
f20e4f |
+ private final static String[] BAD_COUNTRY_2LDS =
|
|
|
f20e4f |
+ {"ac", "co", "com", "ed", "edu", "go", "gouv", "gov", "info",
|
|
|
f20e4f |
+ "lg", "ne", "net", "or", "org"};
|
|
|
f20e4f |
+
|
|
|
f20e4f |
/**
|
|
|
f20e4f |
* Gets an singleton instance of the SSLProtocolSocketFactory.
|
|
|
f20e4f |
* @return a SSLProtocolSocketFactory
|
|
|
f20e4f |
@@ -79,12 +99,14 @@
|
|
|
f20e4f |
InetAddress clientHost,
|
|
|
f20e4f |
int clientPort)
|
|
|
f20e4f |
throws IOException, UnknownHostException {
|
|
|
f20e4f |
- return SSLSocketFactory.getDefault().createSocket(
|
|
|
f20e4f |
+ Socket sslSocket = SSLSocketFactory.getDefault().createSocket(
|
|
|
f20e4f |
host,
|
|
|
f20e4f |
port,
|
|
|
f20e4f |
clientHost,
|
|
|
f20e4f |
clientPort
|
|
|
f20e4f |
);
|
|
|
f20e4f |
+ verifyHostName(host, (SSLSocket) sslSocket);
|
|
|
f20e4f |
+ return sslSocket;
|
|
|
f20e4f |
}
|
|
|
f20e4f |
|
|
|
f20e4f |
/**
|
|
|
f20e4f |
@@ -124,16 +146,19 @@
|
|
|
f20e4f |
}
|
|
|
f20e4f |
int timeout = params.getConnectionTimeout();
|
|
|
f20e4f |
if (timeout == 0) {
|
|
|
f20e4f |
- return createSocket(host, port, localAddress, localPort);
|
|
|
f20e4f |
+ Socket sslSocket = createSocket(host, port, localAddress, localPort);
|
|
|
f20e4f |
+ verifyHostName(host, (SSLSocket) sslSocket);
|
|
|
f20e4f |
+ return sslSocket;
|
|
|
f20e4f |
} else {
|
|
|
f20e4f |
// To be eventually deprecated when migrated to Java 1.4 or above
|
|
|
f20e4f |
- Socket socket = ReflectionSocketFactory.createSocket(
|
|
|
f20e4f |
+ Socket sslSocket = ReflectionSocketFactory.createSocket(
|
|
|
f20e4f |
"javax.net.ssl.SSLSocketFactory", host, port, localAddress, localPort, timeout);
|
|
|
f20e4f |
- if (socket == null) {
|
|
|
f20e4f |
- socket = ControllerThreadSocketFactory.createSocket(
|
|
|
f20e4f |
+ if (sslSocket == null) {
|
|
|
f20e4f |
+ sslSocket = ControllerThreadSocketFactory.createSocket(
|
|
|
f20e4f |
this, host, port, localAddress, localPort, timeout);
|
|
|
f20e4f |
}
|
|
|
f20e4f |
- return socket;
|
|
|
f20e4f |
+ verifyHostName(host, (SSLSocket) sslSocket);
|
|
|
f20e4f |
+ return sslSocket;
|
|
|
f20e4f |
}
|
|
|
f20e4f |
}
|
|
|
f20e4f |
|
|
|
f20e4f |
@@ -142,10 +167,12 @@
|
|
|
f20e4f |
*/
|
|
|
f20e4f |
public Socket createSocket(String host, int port)
|
|
|
f20e4f |
throws IOException, UnknownHostException {
|
|
|
f20e4f |
- return SSLSocketFactory.getDefault().createSocket(
|
|
|
f20e4f |
+ Socket sslSocket = SSLSocketFactory.getDefault().createSocket(
|
|
|
f20e4f |
host,
|
|
|
f20e4f |
port
|
|
|
f20e4f |
);
|
|
|
f20e4f |
+ verifyHostName(host, (SSLSocket) sslSocket);
|
|
|
f20e4f |
+ return sslSocket;
|
|
|
f20e4f |
}
|
|
|
f20e4f |
|
|
|
f20e4f |
/**
|
|
|
f20e4f |
@@ -157,15 +184,273 @@
|
|
|
f20e4f |
int port,
|
|
|
f20e4f |
boolean autoClose)
|
|
|
f20e4f |
throws IOException, UnknownHostException {
|
|
|
f20e4f |
- return ((SSLSocketFactory) SSLSocketFactory.getDefault()).createSocket(
|
|
|
f20e4f |
+ Socket sslSocket = ((SSLSocketFactory) SSLSocketFactory.getDefault()).createSocket(
|
|
|
f20e4f |
socket,
|
|
|
f20e4f |
host,
|
|
|
f20e4f |
port,
|
|
|
f20e4f |
autoClose
|
|
|
f20e4f |
);
|
|
|
f20e4f |
+ verifyHostName(host, (SSLSocket) sslSocket);
|
|
|
f20e4f |
+ return sslSocket;
|
|
|
f20e4f |
}
|
|
|
f20e4f |
+
|
|
|
f20e4f |
|
|
|
f20e4f |
+
|
|
|
f20e4f |
+
|
|
|
f20e4f |
/**
|
|
|
f20e4f |
+ * Verifies that the given hostname in certicifate is the hostname we are trying to connect to
|
|
|
f20e4f |
+ * http://www.cvedetails.com/cve/CVE-2012-5783/
|
|
|
f20e4f |
+ * @param host
|
|
|
f20e4f |
+ * @param ssl
|
|
|
f20e4f |
+ * @throws IOException
|
|
|
f20e4f |
+ */
|
|
|
f20e4f |
+
|
|
|
f20e4f |
+ private static void verifyHostName(String host, SSLSocket ssl)
|
|
|
f20e4f |
+ throws IOException {
|
|
|
f20e4f |
+ if (host == null) {
|
|
|
f20e4f |
+ throw new IllegalArgumentException("host to verify was null");
|
|
|
f20e4f |
+ }
|
|
|
f20e4f |
+
|
|
|
f20e4f |
+ SSLSession session = ssl.getSession();
|
|
|
f20e4f |
+ if (session == null) {
|
|
|
f20e4f |
+ // In our experience this only happens under IBM 1.4.x when
|
|
|
f20e4f |
+ // spurious (unrelated) certificates show up in the server's chain.
|
|
|
f20e4f |
+ // Hopefully this will unearth the real problem:
|
|
|
f20e4f |
+ InputStream in = ssl.getInputStream();
|
|
|
f20e4f |
+ in.available();
|
|
|
f20e4f |
+ /*
|
|
|
f20e4f |
+ If you're looking at the 2 lines of code above because you're
|
|
|
f20e4f |
+ running into a problem, you probably have two options:
|
|
|
f20e4f |
+
|
|
|
f20e4f |
+ #1. Clean up the certificate chain that your server
|
|
|
f20e4f |
+ is presenting (e.g. edit "/etc/apache2/server.crt" or
|
|
|
f20e4f |
+ wherever it is your server's certificate chain is
|
|
|
f20e4f |
+ defined).
|
|
|
f20e4f |
+
|
|
|
f20e4f |
+ OR
|
|
|
f20e4f |
+
|
|
|
f20e4f |
+ #2. Upgrade to an IBM 1.5.x or greater JVM, or switch to a
|
|
|
f20e4f |
+ non-IBM JVM.
|
|
|
f20e4f |
+ */
|
|
|
f20e4f |
+
|
|
|
f20e4f |
+ // If ssl.getInputStream().available() didn't cause an exception,
|
|
|
f20e4f |
+ // maybe at least now the session is available?
|
|
|
f20e4f |
+ session = ssl.getSession();
|
|
|
f20e4f |
+ if (session == null) {
|
|
|
f20e4f |
+ // If it's still null, probably a startHandshake() will
|
|
|
f20e4f |
+ // unearth the real problem.
|
|
|
f20e4f |
+ ssl.startHandshake();
|
|
|
f20e4f |
+
|
|
|
f20e4f |
+ // Okay, if we still haven't managed to cause an exception,
|
|
|
f20e4f |
+ // might as well go for the NPE. Or maybe we're okay now?
|
|
|
f20e4f |
+ session = ssl.getSession();
|
|
|
f20e4f |
+ }
|
|
|
f20e4f |
+ }
|
|
|
f20e4f |
+
|
|
|
f20e4f |
+ Certificate[] certs = session.getPeerCertificates();
|
|
|
f20e4f |
+ verifyHostName(host.trim().toLowerCase(Locale.US), (X509Certificate) certs[0]);
|
|
|
f20e4f |
+ }
|
|
|
f20e4f |
+ /**
|
|
|
f20e4f |
+ * Extract the names from the certificate and tests host matches one of them
|
|
|
f20e4f |
+ * @param host
|
|
|
f20e4f |
+ * @param cert
|
|
|
f20e4f |
+ * @throws SSLException
|
|
|
f20e4f |
+ */
|
|
|
f20e4f |
+
|
|
|
f20e4f |
+ private static void verifyHostName(final String host, X509Certificate cert)
|
|
|
f20e4f |
+ throws SSLException {
|
|
|
f20e4f |
+ // I'm okay with being case-insensitive when comparing the host we used
|
|
|
f20e4f |
+ // to establish the socket to the hostname in the certificate.
|
|
|
f20e4f |
+ // Don't trim the CN, though.
|
|
|
f20e4f |
+
|
|
|
f20e4f |
+ String cn = getCN(cert);
|
|
|
f20e4f |
+ String[] subjectAlts = getDNSSubjectAlts(cert);
|
|
|
f20e4f |
+ verifyHostName(host, cn.toLowerCase(Locale.US), subjectAlts);
|
|
|
f20e4f |
+
|
|
|
f20e4f |
+ }
|
|
|
f20e4f |
+
|
|
|
f20e4f |
+ /**
|
|
|
f20e4f |
+ * Extract all alternative names from a certificate.
|
|
|
f20e4f |
+ * @param cert
|
|
|
f20e4f |
+ * @return
|
|
|
f20e4f |
+ */
|
|
|
f20e4f |
+ private static String[] getDNSSubjectAlts(X509Certificate cert) {
|
|
|
f20e4f |
+ LinkedList subjectAltList = new LinkedList();
|
|
|
f20e4f |
+ Collection c = null;
|
|
|
f20e4f |
+ try {
|
|
|
f20e4f |
+ c = cert.getSubjectAlternativeNames();
|
|
|
f20e4f |
+ } catch (CertificateParsingException cpe) {
|
|
|
f20e4f |
+ // Should probably log.debug() this?
|
|
|
f20e4f |
+ cpe.printStackTrace();
|
|
|
f20e4f |
+ }
|
|
|
f20e4f |
+ if (c != null) {
|
|
|
f20e4f |
+ Iterator it = c.iterator();
|
|
|
f20e4f |
+ while (it.hasNext()) {
|
|
|
f20e4f |
+ List list = (List) it.next();
|
|
|
f20e4f |
+ int type = ((Integer) list.get(0)).intValue();
|
|
|
f20e4f |
+ // If type is 2, then we've got a dNSName
|
|
|
f20e4f |
+ if (type == 2) {
|
|
|
f20e4f |
+ String s = (String) list.get(1);
|
|
|
f20e4f |
+ subjectAltList.add(s);
|
|
|
f20e4f |
+ }
|
|
|
f20e4f |
+ }
|
|
|
f20e4f |
+ }
|
|
|
f20e4f |
+ if (!subjectAltList.isEmpty()) {
|
|
|
f20e4f |
+ String[] subjectAlts = new String[subjectAltList.size()];
|
|
|
f20e4f |
+ subjectAltList.toArray(subjectAlts);
|
|
|
f20e4f |
+ return subjectAlts;
|
|
|
f20e4f |
+ } else {
|
|
|
f20e4f |
+ return new String[0];
|
|
|
f20e4f |
+ }
|
|
|
f20e4f |
+
|
|
|
f20e4f |
+ }
|
|
|
f20e4f |
+ /**
|
|
|
f20e4f |
+ * Verifies
|
|
|
f20e4f |
+ * @param host
|
|
|
f20e4f |
+ * @param cn
|
|
|
f20e4f |
+ * @param subjectAlts
|
|
|
f20e4f |
+ * @throws SSLException
|
|
|
f20e4f |
+ */
|
|
|
f20e4f |
+
|
|
|
f20e4f |
+ private static void verifyHostName(final String host, String cn, String[] subjectAlts)throws SSLException{
|
|
|
f20e4f |
+ StringBuffer cnTested = new StringBuffer();
|
|
|
f20e4f |
+
|
|
|
f20e4f |
+ for (int i = 0; i < subjectAlts.length; i++){
|
|
|
f20e4f |
+ String name = subjectAlts[i];
|
|
|
f20e4f |
+ if (name != null) {
|
|
|
f20e4f |
+ name = name.toLowerCase();
|
|
|
f20e4f |
+ if (verifyHostName(host, name)){
|
|
|
f20e4f |
+ return;
|
|
|
f20e4f |
+ }
|
|
|
f20e4f |
+ cnTested.append("/").append(name);
|
|
|
f20e4f |
+ }
|
|
|
f20e4f |
+ }
|
|
|
f20e4f |
+ if (cn != null && verifyHostName(host, cn)){
|
|
|
f20e4f |
+ return;
|
|
|
f20e4f |
+ }
|
|
|
f20e4f |
+ cnTested.append("/").append(cn);
|
|
|
f20e4f |
+ throw new SSLException("hostname in certificate didn't match: <"
|
|
|
f20e4f |
+ + host + "> != <" + cnTested + ">");
|
|
|
f20e4f |
+
|
|
|
f20e4f |
+ }
|
|
|
f20e4f |
+
|
|
|
f20e4f |
+ private static boolean verifyHostName(final String host, final String cn){
|
|
|
f20e4f |
+ if (doWildCard(cn) && !isIPAddress(host)) {
|
|
|
f20e4f |
+ return matchesWildCard(cn, host);
|
|
|
f20e4f |
+ }
|
|
|
f20e4f |
+ return host.equalsIgnoreCase(cn);
|
|
|
f20e4f |
+ }
|
|
|
f20e4f |
+ private static boolean doWildCard(String cn) {
|
|
|
f20e4f |
+ // Contains a wildcard
|
|
|
f20e4f |
+ // wildcard in the first block
|
|
|
f20e4f |
+ // not an ipaddress (ip addres must explicitily be equal)
|
|
|
f20e4f |
+ // not using 2nd level common tld : ex: not for *.co.uk
|
|
|
f20e4f |
+ String parts[] = cn.split("\\.");
|
|
|
f20e4f |
+ return parts.length >= 3 &&
|
|
|
f20e4f |
+ parts[0].endsWith("*") &&
|
|
|
f20e4f |
+ acceptableCountryWildcard(cn) &&
|
|
|
f20e4f |
+ !isIPAddress(cn);
|
|
|
f20e4f |
+ }
|
|
|
f20e4f |
+
|
|
|
f20e4f |
+
|
|
|
f20e4f |
+ private static final Pattern IPV4_PATTERN =
|
|
|
f20e4f |
+ Pattern.compile("^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$");
|
|
|
f20e4f |
+
|
|
|
f20e4f |
+ private static final Pattern IPV6_STD_PATTERN =
|
|
|
f20e4f |
+ Pattern.compile("^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$");
|
|
|
f20e4f |
+
|
|
|
f20e4f |
+ private static final Pattern IPV6_HEX_COMPRESSED_PATTERN =
|
|
|
f20e4f |
+ Pattern.compile("^((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)::((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)$");
|
|
|
f20e4f |
+
|
|
|
f20e4f |
+
|
|
|
f20e4f |
+ private static boolean isIPAddress(final String hostname) {
|
|
|
f20e4f |
+ return hostname != null
|
|
|
f20e4f |
+ && (
|
|
|
f20e4f |
+ IPV4_PATTERN.matcher(hostname).matches()
|
|
|
f20e4f |
+ || IPV6_STD_PATTERN.matcher(hostname).matches()
|
|
|
f20e4f |
+ || IPV6_HEX_COMPRESSED_PATTERN.matcher(hostname).matches()
|
|
|
f20e4f |
+ );
|
|
|
f20e4f |
+
|
|
|
f20e4f |
+ }
|
|
|
f20e4f |
+
|
|
|
f20e4f |
+ private static boolean acceptableCountryWildcard(final String cn) {
|
|
|
f20e4f |
+ // The CN better have at least two dots if it wants wildcard action,
|
|
|
f20e4f |
+ // but can't be [*.co.uk] or [*.co.jp] or [*.org.uk], etc...
|
|
|
f20e4f |
+ // The [*.co.uk] problem is an interesting one. Should we just
|
|
|
f20e4f |
+ // hope that CA's would never foolishly allow such a
|
|
|
f20e4f |
+ // certificate to happen?
|
|
|
f20e4f |
+
|
|
|
f20e4f |
+ String[] parts = cn.split("\\.");
|
|
|
f20e4f |
+ // Only checks for 3 levels, with country code of 2 letters.
|
|
|
f20e4f |
+ if (parts.length > 3 || parts[parts.length - 1].length() != 2) {
|
|
|
f20e4f |
+ return true;
|
|
|
f20e4f |
+ }
|
|
|
f20e4f |
+ String countryCode = parts[parts.length - 2];
|
|
|
f20e4f |
+ return Arrays.binarySearch(BAD_COUNTRY_2LDS, countryCode) < 0;
|
|
|
f20e4f |
+ }
|
|
|
f20e4f |
+
|
|
|
f20e4f |
+ private static boolean matchesWildCard(final String cn,
|
|
|
f20e4f |
+ final String hostName) {
|
|
|
f20e4f |
+ String parts[] = cn.split("\\.");
|
|
|
f20e4f |
+ boolean match = false;
|
|
|
f20e4f |
+ String firstpart = parts[0];
|
|
|
f20e4f |
+ if (firstpart.length() > 1) {
|
|
|
f20e4f |
+ // server∗
|
|
|
f20e4f |
+ // e.g. server
|
|
|
f20e4f |
+ String prefix = firstpart.substring(0, firstpart.length() - 1);
|
|
|
f20e4f |
+ // skipwildcard part from cn
|
|
|
f20e4f |
+ String suffix = cn.substring(firstpart.length());
|
|
|
f20e4f |
+ // skip wildcard part from host
|
|
|
f20e4f |
+ String hostSuffix = hostName.substring(prefix.length());
|
|
|
f20e4f |
+ match = hostName.startsWith(prefix) && hostSuffix.endsWith(suffix);
|
|
|
f20e4f |
+ } else {
|
|
|
f20e4f |
+ match = hostName.endsWith(cn.substring(1));
|
|
|
f20e4f |
+ }
|
|
|
f20e4f |
+ if (match) {
|
|
|
f20e4f |
+ // I f we're in strict mode ,
|
|
|
f20e4f |
+ // [ ∗.foo.com] is not allowed to match [a.b.foo.com]
|
|
|
f20e4f |
+ match = countDots(hostName) == countDots(cn);
|
|
|
f20e4f |
+ }
|
|
|
f20e4f |
+ return match;
|
|
|
f20e4f |
+ }
|
|
|
f20e4f |
+
|
|
|
f20e4f |
+ private static int countDots(final String data) {
|
|
|
f20e4f |
+ int dots = 0;
|
|
|
f20e4f |
+ for (int i = 0; i < data.length(); i++) {
|
|
|
f20e4f |
+ if (data.charAt(i) == '.') {
|
|
|
f20e4f |
+ dots += 1;
|
|
|
f20e4f |
+ }
|
|
|
f20e4f |
+ }
|
|
|
f20e4f |
+ return dots;
|
|
|
f20e4f |
+ }
|
|
|
f20e4f |
+
|
|
|
f20e4f |
+ private static String getCN(X509Certificate cert) {
|
|
|
f20e4f |
+ // Note: toString() seems to do a better job than getName()
|
|
|
f20e4f |
+ //
|
|
|
f20e4f |
+ // For example, getName() gives me this:
|
|
|
f20e4f |
+ // 1.2.840.113549.1.9.1=#16166a756c6975736461766965734063756362632e636f6d
|
|
|
f20e4f |
+ //
|
|
|
f20e4f |
+ // whereas toString() gives me this:
|
|
|
f20e4f |
+ // EMAILADDRESS=juliusdavies@cucbc.com
|
|
|
f20e4f |
+ String subjectPrincipal = cert.getSubjectX500Principal().toString();
|
|
|
f20e4f |
+
|
|
|
f20e4f |
+ return getCN(subjectPrincipal);
|
|
|
f20e4f |
+
|
|
|
f20e4f |
+ }
|
|
|
f20e4f |
+ private static String getCN(String subjectPrincipal) {
|
|
|
f20e4f |
+ StringTokenizer st = new StringTokenizer(subjectPrincipal, ",");
|
|
|
f20e4f |
+ while(st.hasMoreTokens()) {
|
|
|
f20e4f |
+ String tok = st.nextToken().trim();
|
|
|
f20e4f |
+ if (tok.length() > 3) {
|
|
|
f20e4f |
+ if (tok.substring(0, 3).equalsIgnoreCase("CN=")) {
|
|
|
f20e4f |
+ return tok.substring(3);
|
|
|
f20e4f |
+ }
|
|
|
f20e4f |
+ }
|
|
|
f20e4f |
+ }
|
|
|
f20e4f |
+ return null;
|
|
|
f20e4f |
+ }
|
|
|
f20e4f |
+
|
|
|
f20e4f |
+ /**
|
|
|
f20e4f |
* All instances of SSLProtocolSocketFactory are the same.
|
|
|
f20e4f |
*/
|
|
|
f20e4f |
public boolean equals(Object obj) {
|