PageRenderTime 62ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/third-party/org-apache-commons-httpclient/patches/LPS-121338.patch

https://github.com/danielreuther/liferay-portal
Patch | 375 lines | 366 code | 9 blank | 0 comment | 0 complexity | 8bc45c86dd2d760b7b81b592ec889ac7 MD5 | raw file
  1. diff --git a/org/apache/commons/httpclient/protocol/SSLProtocolSocketFactory.java b/org/apache/commons/httpclient/protocol/SSLProtocolSocketFactory.java
  2. index f176fed118c6..cce784b19520 100644
  3. --- a/org/apache/commons/httpclient/protocol/SSLProtocolSocketFactory.java
  4. +++ b/org/apache/commons/httpclient/protocol/SSLProtocolSocketFactory.java
  5. @@ -31,10 +31,25 @@
  6. package org.apache.commons.httpclient.protocol;
  7. import java.io.IOException;
  8. +import java.io.InputStream;
  9. import java.net.InetAddress;
  10. import java.net.Socket;
  11. import java.net.UnknownHostException;
  12. +import java.security.cert.Certificate;
  13. +import java.security.cert.CertificateParsingException;
  14. +import java.security.cert.X509Certificate;
  15. +import java.util.Arrays;
  16. +import java.util.Collection;
  17. +import java.util.Iterator;
  18. +import java.util.LinkedList;
  19. +import java.util.List;
  20. +import java.util.Locale;
  21. +import java.util.StringTokenizer;
  22. +import java.util.regex.Pattern;
  23. +import javax.net.ssl.SSLException;
  24. +import javax.net.ssl.SSLSession;
  25. +import javax.net.ssl.SSLSocket;
  26. import javax.net.ssl.SSLSocketFactory;
  27. import org.apache.commons.httpclient.ConnectTimeoutException;
  28. @@ -55,6 +70,11 @@ public class SSLProtocolSocketFactory implements SecureProtocolSocketFactory {
  29. */
  30. private static final SSLProtocolSocketFactory factory = new SSLProtocolSocketFactory();
  31. + // This is a a sorted list, if you insert new elements do it orderdered.
  32. + private final static String[] BAD_COUNTRY_2LDS =
  33. + {"ac", "co", "com", "ed", "edu", "go", "gouv", "gov", "info",
  34. + "lg", "ne", "net", "or", "org"};
  35. +
  36. /**
  37. * Gets an singleton instance of the SSLProtocolSocketFactory.
  38. * @return a SSLProtocolSocketFactory
  39. @@ -79,12 +99,14 @@ public class SSLProtocolSocketFactory implements SecureProtocolSocketFactory {
  40. InetAddress clientHost,
  41. int clientPort)
  42. throws IOException, UnknownHostException {
  43. - return SSLSocketFactory.getDefault().createSocket(
  44. + Socket sslSocket = SSLSocketFactory.getDefault().createSocket(
  45. host,
  46. port,
  47. clientHost,
  48. clientPort
  49. );
  50. + verifyHostName(host, (SSLSocket) sslSocket);
  51. + return sslSocket;
  52. }
  53. /**
  54. @@ -124,16 +146,19 @@ public class SSLProtocolSocketFactory implements SecureProtocolSocketFactory {
  55. }
  56. int timeout = params.getConnectionTimeout();
  57. if (timeout == 0) {
  58. - return createSocket(host, port, localAddress, localPort);
  59. + Socket sslSocket = createSocket(host, port, localAddress, localPort);
  60. + verifyHostName(host, (SSLSocket) sslSocket);
  61. + return sslSocket;
  62. } else {
  63. // To be eventually deprecated when migrated to Java 1.4 or above
  64. - Socket socket = ReflectionSocketFactory.createSocket(
  65. + Socket sslSocket = ReflectionSocketFactory.createSocket(
  66. "javax.net.ssl.SSLSocketFactory", host, port, localAddress, localPort, timeout);
  67. - if (socket == null) {
  68. - socket = ControllerThreadSocketFactory.createSocket(
  69. + if (sslSocket == null) {
  70. + sslSocket = ControllerThreadSocketFactory.createSocket(
  71. this, host, port, localAddress, localPort, timeout);
  72. }
  73. - return socket;
  74. + verifyHostName(host, (SSLSocket) sslSocket);
  75. + return sslSocket;
  76. }
  77. }
  78. @@ -142,10 +167,12 @@ public class SSLProtocolSocketFactory implements SecureProtocolSocketFactory {
  79. */
  80. public Socket createSocket(String host, int port)
  81. throws IOException, UnknownHostException {
  82. - return SSLSocketFactory.getDefault().createSocket(
  83. + Socket sslSocket = SSLSocketFactory.getDefault().createSocket(
  84. host,
  85. port
  86. );
  87. + verifyHostName(host, (SSLSocket) sslSocket);
  88. + return sslSocket;
  89. }
  90. /**
  91. @@ -157,13 +184,271 @@ public class SSLProtocolSocketFactory implements SecureProtocolSocketFactory {
  92. int port,
  93. boolean autoClose)
  94. throws IOException, UnknownHostException {
  95. - return ((SSLSocketFactory) SSLSocketFactory.getDefault()).createSocket(
  96. + Socket sslSocket = ((SSLSocketFactory) SSLSocketFactory.getDefault()).createSocket(
  97. socket,
  98. host,
  99. port,
  100. autoClose
  101. );
  102. + verifyHostName(host, (SSLSocket) sslSocket);
  103. + return sslSocket;
  104. }
  105. +
  106. +
  107. +
  108. +
  109. + /**
  110. + * Verifies that the given hostname in certicifate is the hostname we are trying to connect to
  111. + * http://www.cvedetails.com/cve/CVE-2012-5783/
  112. + * @param host
  113. + * @param ssl
  114. + * @throws IOException
  115. + */
  116. +
  117. + private static void verifyHostName(String host, SSLSocket ssl)
  118. + throws IOException {
  119. + if (host == null) {
  120. + throw new IllegalArgumentException("host to verify was null");
  121. + }
  122. +
  123. + SSLSession session = ssl.getSession();
  124. + if (session == null) {
  125. + // In our experience this only happens under IBM 1.4.x when
  126. + // spurious (unrelated) certificates show up in the server's chain.
  127. + // Hopefully this will unearth the real problem:
  128. + InputStream in = ssl.getInputStream();
  129. + in.available();
  130. + /*
  131. + If you're looking at the 2 lines of code above because you're
  132. + running into a problem, you probably have two options:
  133. +
  134. + #1. Clean up the certificate chain that your server
  135. + is presenting (e.g. edit "/etc/apache2/server.crt" or
  136. + wherever it is your server's certificate chain is
  137. + defined).
  138. +
  139. + OR
  140. +
  141. + #2. Upgrade to an IBM 1.5.x or greater JVM, or switch to a
  142. + non-IBM JVM.
  143. + */
  144. +
  145. + // If ssl.getInputStream().available() didn't cause an exception,
  146. + // maybe at least now the session is available?
  147. + session = ssl.getSession();
  148. + if (session == null) {
  149. + // If it's still null, probably a startHandshake() will
  150. + // unearth the real problem.
  151. + ssl.startHandshake();
  152. +
  153. + // Okay, if we still haven't managed to cause an exception,
  154. + // might as well go for the NPE. Or maybe we're okay now?
  155. + session = ssl.getSession();
  156. + }
  157. + }
  158. +
  159. + Certificate[] certs = session.getPeerCertificates();
  160. + verifyHostName(host.trim().toLowerCase(Locale.US), (X509Certificate) certs[0]);
  161. + }
  162. + /**
  163. + * Extract the names from the certificate and tests host matches one of them
  164. + * @param host
  165. + * @param cert
  166. + * @throws SSLException
  167. + */
  168. +
  169. + private static void verifyHostName(final String host, X509Certificate cert)
  170. + throws SSLException {
  171. + // I'm okay with being case-insensitive when comparing the host we used
  172. + // to establish the socket to the hostname in the certificate.
  173. + // Don't trim the CN, though.
  174. +
  175. + String cn = getCN(cert);
  176. + String[] subjectAlts = getDNSSubjectAlts(cert);
  177. + verifyHostName(host, cn.toLowerCase(Locale.US), subjectAlts);
  178. +
  179. + }
  180. +
  181. + /**
  182. + * Extract all alternative names from a certificate.
  183. + * @param cert
  184. + * @return
  185. + */
  186. + private static String[] getDNSSubjectAlts(X509Certificate cert) {
  187. + LinkedList subjectAltList = new LinkedList();
  188. + Collection c = null;
  189. + try {
  190. + c = cert.getSubjectAlternativeNames();
  191. + } catch (CertificateParsingException cpe) {
  192. + // Should probably log.debug() this?
  193. + cpe.printStackTrace();
  194. + }
  195. + if (c != null) {
  196. + Iterator it = c.iterator();
  197. + while (it.hasNext()) {
  198. + List list = (List) it.next();
  199. + int type = ((Integer) list.get(0)).intValue();
  200. + // If type is 2, then we've got a dNSName
  201. + if (type == 2) {
  202. + String s = (String) list.get(1);
  203. + subjectAltList.add(s);
  204. + }
  205. + }
  206. + }
  207. + if (!subjectAltList.isEmpty()) {
  208. + String[] subjectAlts = new String[subjectAltList.size()];
  209. + subjectAltList.toArray(subjectAlts);
  210. + return subjectAlts;
  211. + } else {
  212. + return new String[0];
  213. + }
  214. +
  215. + }
  216. + /**
  217. + * Verifies
  218. + * @param host
  219. + * @param cn
  220. + * @param subjectAlts
  221. + * @throws SSLException
  222. + */
  223. +
  224. + private static void verifyHostName(final String host, String cn, String[] subjectAlts)throws SSLException{
  225. + StringBuffer cnTested = new StringBuffer();
  226. +
  227. + for (int i = 0; i < subjectAlts.length; i++){
  228. + String name = subjectAlts[i];
  229. + if (name != null) {
  230. + name = name.toLowerCase();
  231. + if (verifyHostName(host, name)){
  232. + return;
  233. + }
  234. + cnTested.append("/").append(name);
  235. + }
  236. + }
  237. + if (cn != null && verifyHostName(host, cn)){
  238. + return;
  239. + }
  240. + cnTested.append("/").append(cn);
  241. + throw new SSLException("hostname in certificate didn't match: <"
  242. + + host + "> != <" + cnTested + ">");
  243. +
  244. + }
  245. +
  246. + private static boolean verifyHostName(final String host, final String cn){
  247. + if (doWildCard(cn) && !isIPAddress(host)) {
  248. + return matchesWildCard(cn, host);
  249. + }
  250. + return host.equalsIgnoreCase(cn);
  251. + }
  252. + private static boolean doWildCard(String cn) {
  253. + // Contains a wildcard
  254. + // wildcard in the first block
  255. + // not an ipaddress (ip addres must explicitily be equal)
  256. + // not using 2nd level common tld : ex: not for *.co.uk
  257. + String parts[] = cn.split("\\.");
  258. + return parts.length >= 3 &&
  259. + parts[0].endsWith("*") &&
  260. + acceptableCountryWildcard(cn) &&
  261. + !isIPAddress(cn);
  262. + }
  263. +
  264. +
  265. + private static final Pattern IPV4_PATTERN =
  266. + 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}$");
  267. +
  268. + private static final Pattern IPV6_STD_PATTERN =
  269. + Pattern.compile("^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$");
  270. +
  271. + private static final Pattern IPV6_HEX_COMPRESSED_PATTERN =
  272. + 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})*)?)$");
  273. +
  274. +
  275. + private static boolean isIPAddress(final String hostname) {
  276. + return hostname != null
  277. + && (
  278. + IPV4_PATTERN.matcher(hostname).matches()
  279. + || IPV6_STD_PATTERN.matcher(hostname).matches()
  280. + || IPV6_HEX_COMPRESSED_PATTERN.matcher(hostname).matches()
  281. + );
  282. +
  283. + }
  284. +
  285. + private static boolean acceptableCountryWildcard(final String cn) {
  286. + // The CN better have at least two dots if it wants wildcard action,
  287. + // but can't be [*.co.uk] or [*.co.jp] or [*.org.uk], etc...
  288. + // The [*.co.uk] problem is an interesting one. Should we just
  289. + // hope that CA's would never foolishly allow such a
  290. + // certificate to happen?
  291. +
  292. + String[] parts = cn.split("\\.");
  293. + // Only checks for 3 levels, with country code of 2 letters.
  294. + if (parts.length > 3 || parts[parts.length - 1].length() != 2) {
  295. + return true;
  296. + }
  297. + String countryCode = parts[parts.length - 2];
  298. + return Arrays.binarySearch(BAD_COUNTRY_2LDS, countryCode) < 0;
  299. + }
  300. +
  301. + private static boolean matchesWildCard(final String cn,
  302. + final String hostName) {
  303. + String parts[] = cn.split("\\.");
  304. + boolean match = false;
  305. + String firstpart = parts[0];
  306. + if (firstpart.length() > 1) {
  307. + // server∗
  308. + // e.g. server
  309. + String prefix = firstpart.substring(0, firstpart.length() - 1);
  310. + // skipwildcard part from cn
  311. + String suffix = cn.substring(firstpart.length());
  312. + // skip wildcard part from host
  313. + String hostSuffix = hostName.substring(prefix.length());
  314. + match = hostName.startsWith(prefix) && hostSuffix.endsWith(suffix);
  315. + } else {
  316. + match = hostName.endsWith(cn.substring(1));
  317. + }
  318. + if (match) {
  319. + // I f we're in strict mode ,
  320. + // [ ∗.foo.com] is not allowed to match [a.b.foo.com]
  321. + match = countDots(hostName) == countDots(cn);
  322. + }
  323. + return match;
  324. + }
  325. +
  326. + private static int countDots(final String data) {
  327. + int dots = 0;
  328. + for (int i = 0; i < data.length(); i++) {
  329. + if (data.charAt(i) == '.') {
  330. + dots += 1;
  331. + }
  332. + }
  333. + return dots;
  334. + }
  335. +
  336. + private static String getCN(X509Certificate cert) {
  337. + // Note: toString() seems to do a better job than getName()
  338. + //
  339. + // For example, getName() gives me this:
  340. + // 1.2.840.113549.1.9.1=#16166a756c6975736461766965734063756362632e636f6d
  341. + //
  342. + // whereas toString() gives me this:
  343. + // EMAILADDRESS=juliusdavies@cucbc.com
  344. + String subjectPrincipal = cert.getSubjectX500Principal().toString();
  345. +
  346. + return getCN(subjectPrincipal);
  347. +
  348. + }
  349. + private static String getCN(String subjectPrincipal) {
  350. + StringTokenizer st = new StringTokenizer(subjectPrincipal, ",");
  351. + while(st.hasMoreTokens()) {
  352. + String tok = st.nextToken().trim();
  353. + if (tok.length() > 3) {
  354. + if (tok.substring(0, 3).equalsIgnoreCase("CN=")) {
  355. + return tok.substring(3);
  356. + }
  357. + }
  358. + }
  359. + return null;
  360. + }
  361. /**
  362. * All instances of SSLProtocolSocketFactory are the same.
  363. @@ -180,3 +465,4 @@ public class SSLProtocolSocketFactory implements SecureProtocolSocketFactory {
  364. }
  365. }
  366. +/* @generated */