6671a8
From 6f55656e288808437389f7d733e9a466fa5f0e2b Mon Sep 17 00:00:00 2001
6671a8
From: Michal Srb <msrb@redhat.com>
6671a8
Date: Tue, 12 Aug 2014 16:14:06 +0200
6671a8
Subject: [PATCH] Fix CVE-2014-3577
6671a8
6671a8
---
6671a8
 .../protocol/SSLProtocolSocketFactory.java         | 57 ++++++++++++++--------
6671a8
 1 file changed, 37 insertions(+), 20 deletions(-)
6671a8
6671a8
diff --git a/src/java/org/apache/commons/httpclient/protocol/SSLProtocolSocketFactory.java b/src/java/org/apache/commons/httpclient/protocol/SSLProtocolSocketFactory.java
6671a8
index fa0acc7..e6ce513 100644
6671a8
--- a/src/java/org/apache/commons/httpclient/protocol/SSLProtocolSocketFactory.java
6671a8
+++ b/src/java/org/apache/commons/httpclient/protocol/SSLProtocolSocketFactory.java
6671a8
@@ -44,9 +44,15 @@ import java.util.Iterator;
6671a8
 import java.util.LinkedList;
6671a8
 import java.util.List;
6671a8
 import java.util.Locale;
6671a8
-import java.util.StringTokenizer;
6671a8
+import java.util.NoSuchElementException;
6671a8
 import java.util.regex.Pattern;
6671a8
 
6671a8
+import javax.naming.InvalidNameException;
6671a8
+import javax.naming.NamingException;
6671a8
+import javax.naming.directory.Attribute;
6671a8
+import javax.naming.directory.Attributes;
6671a8
+import javax.naming.ldap.LdapName;
6671a8
+import javax.naming.ldap.Rdn;
6671a8
 import javax.net.ssl.SSLException;
6671a8
 import javax.net.ssl.SSLSession;
6671a8
 import javax.net.ssl.SSLSocket;
6671a8
@@ -424,28 +430,39 @@ public class SSLProtocolSocketFactory implements SecureProtocolSocketFactory {
6671a8
 		return dots;
6671a8
 	}
6671a8
 
6671a8
-	private static String getCN(X509Certificate cert) {
6671a8
-        // Note:  toString() seems to do a better job than getName()
6671a8
-        //
6671a8
-        // For example, getName() gives me this:
6671a8
-        // 1.2.840.113549.1.9.1=#16166a756c6975736461766965734063756362632e636f6d
6671a8
-        //
6671a8
-        // whereas toString() gives me this:
6671a8
-        // EMAILADDRESS=juliusdavies@cucbc.com        
6671a8
-		String subjectPrincipal = cert.getSubjectX500Principal().toString();
6671a8
-		
6671a8
-		return getCN(subjectPrincipal);
6671a8
-
6671a8
+	private static String getCN(final X509Certificate cert) {
6671a8
+		final String subjectPrincipal = cert.getSubjectX500Principal().toString();
6671a8
+		try {
6671a8
+			return extractCN(subjectPrincipal);
6671a8
+		} catch (SSLException ex) {
6671a8
+			return null;
6671a8
+		}
6671a8
 	}
6671a8
-	private static String getCN(String subjectPrincipal) {
6671a8
-		StringTokenizer st = new StringTokenizer(subjectPrincipal, ",");
6671a8
-		while(st.hasMoreTokens()) {
6671a8
-			String tok = st.nextToken().trim();
6671a8
-			if (tok.length() > 3) {
6671a8
-				if (tok.substring(0, 3).equalsIgnoreCase("CN=")) {
6671a8
-					return tok.substring(3);
6671a8
+
6671a8
+	private static String extractCN(final String subjectPrincipal) throws SSLException {
6671a8
+		if (subjectPrincipal == null) {
6671a8
+			return null;
6671a8
+		}
6671a8
+		try {
6671a8
+			final LdapName subjectDN = new LdapName(subjectPrincipal);
6671a8
+			final List<Rdn> rdns = subjectDN.getRdns();
6671a8
+			for (int i = rdns.size() - 1; i >= 0; i--) {
6671a8
+				final Rdn rds = rdns.get(i);
6671a8
+				final Attributes attributes = rds.toAttributes();
6671a8
+				final Attribute cn = attributes.get("cn");
6671a8
+				if (cn != null) {
6671a8
+					try {
6671a8
+						final Object value = cn.get();
6671a8
+						if (value != null) {
6671a8
+							return value.toString();
6671a8
+						}
6671a8
+					} catch (NoSuchElementException ignore) {
6671a8
+					} catch (NamingException ignore) {
6671a8
+					}
6671a8
 				}
6671a8
 			}
6671a8
+		} catch (InvalidNameException e) {
6671a8
+			throw new SSLException(subjectPrincipal + " is not a valid X500 distinguished name");
6671a8
 		}
6671a8
 		return null;
6671a8
 	}
6671a8
-- 
6671a8
1.9.3
6671a8