Blame SOURCES/jakarta-commons-httpclient-CVE-2014-3577.patch

b03f6a
diff --git a/src/java/org/apache/commons/httpclient/protocol/SSLProtocolSocketFactory.java b/src/java/org/apache/commons/httpclient/protocol/SSLProtocolSocketFactory.java
b03f6a
index fa0acc7..e6ce513 100644
b03f6a
--- a/src/java/org/apache/commons/httpclient/protocol/SSLProtocolSocketFactory.java
b03f6a
+++ b/src/java/org/apache/commons/httpclient/protocol/SSLProtocolSocketFactory.java
b03f6a
@@ -44,9 +44,15 @@ import java.util.Iterator;
b03f6a
 import java.util.LinkedList;
b03f6a
 import java.util.List;
b03f6a
 import java.util.Locale;
b03f6a
-import java.util.StringTokenizer;
b03f6a
+import java.util.NoSuchElementException;
b03f6a
 import java.util.regex.Pattern;
b03f6a
 
b03f6a
+import javax.naming.InvalidNameException;
b03f6a
+import javax.naming.NamingException;
b03f6a
+import javax.naming.directory.Attribute;
b03f6a
+import javax.naming.directory.Attributes;
b03f6a
+import javax.naming.ldap.LdapName;
b03f6a
+import javax.naming.ldap.Rdn;
b03f6a
 import javax.net.ssl.SSLException;
b03f6a
 import javax.net.ssl.SSLSession;
b03f6a
 import javax.net.ssl.SSLSocket;
b03f6a
@@ -424,28 +430,39 @@ public class SSLProtocolSocketFactory implements SecureProtocolSocketFactory {
b03f6a
 		return dots;
b03f6a
 	}
b03f6a
 
b03f6a
-	private static String getCN(X509Certificate cert) {
b03f6a
-        // Note:  toString() seems to do a better job than getName()
b03f6a
-        //
b03f6a
-        // For example, getName() gives me this:
b03f6a
-        // 1.2.840.113549.1.9.1=#16166a756c6975736461766965734063756362632e636f6d
b03f6a
-        //
b03f6a
-        // whereas toString() gives me this:
b03f6a
-        // EMAILADDRESS=juliusdavies@cucbc.com        
b03f6a
-		String subjectPrincipal = cert.getSubjectX500Principal().toString();
b03f6a
-		
b03f6a
-		return getCN(subjectPrincipal);
b03f6a
-
b03f6a
+	private static String getCN(final X509Certificate cert) {
b03f6a
+		final String subjectPrincipal = cert.getSubjectX500Principal().toString();
b03f6a
+		try {
b03f6a
+			return extractCN(subjectPrincipal);
b03f6a
+		} catch (SSLException ex) {
b03f6a
+			return null;
b03f6a
+		}
b03f6a
 	}
b03f6a
-	private static String getCN(String subjectPrincipal) {
b03f6a
-		StringTokenizer st = new StringTokenizer(subjectPrincipal, ",");
b03f6a
-		while(st.hasMoreTokens()) {
b03f6a
-			String tok = st.nextToken().trim();
b03f6a
-			if (tok.length() > 3) {
b03f6a
-				if (tok.substring(0, 3).equalsIgnoreCase("CN=")) {
b03f6a
-					return tok.substring(3);
b03f6a
+
b03f6a
+	private static String extractCN(final String subjectPrincipal) throws SSLException {
b03f6a
+		if (subjectPrincipal == null) {
b03f6a
+			return null;
b03f6a
+		}
b03f6a
+		try {
b03f6a
+			final LdapName subjectDN = new LdapName(subjectPrincipal);
b03f6a
+			final List<Rdn> rdns = subjectDN.getRdns();
b03f6a
+			for (int i = rdns.size() - 1; i >= 0; i--) {
b03f6a
+				final Rdn rds = rdns.get(i);
b03f6a
+				final Attributes attributes = rds.toAttributes();
b03f6a
+				final Attribute cn = attributes.get("cn");
b03f6a
+				if (cn != null) {
b03f6a
+					try {
b03f6a
+						final Object value = cn.get();
b03f6a
+						if (value != null) {
b03f6a
+							return value.toString();
b03f6a
+						}
b03f6a
+					} catch (NoSuchElementException ignore) {
b03f6a
+					} catch (NamingException ignore) {
b03f6a
+					}
b03f6a
 				}
b03f6a
 			}
b03f6a
+		} catch (InvalidNameException e) {
b03f6a
+			throw new SSLException(subjectPrincipal + " is not a valid X500 distinguished name");
b03f6a
 		}
b03f6a
 		return null;
b03f6a
 	}