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

f20e4f
diff --git a/src/java/org/apache/commons/httpclient/protocol/SSLProtocolSocketFactory.java b/src/java/org/apache/commons/httpclient/protocol/SSLProtocolSocketFactory.java
f20e4f
index fa0acc7..e6ce513 100644
f20e4f
--- a/src/java/org/apache/commons/httpclient/protocol/SSLProtocolSocketFactory.java
f20e4f
+++ b/src/java/org/apache/commons/httpclient/protocol/SSLProtocolSocketFactory.java
f20e4f
@@ -44,9 +44,15 @@ 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.NoSuchElementException;
f20e4f
 import java.util.regex.Pattern;
f20e4f
 
f20e4f
+import javax.naming.InvalidNameException;
f20e4f
+import javax.naming.NamingException;
f20e4f
+import javax.naming.directory.Attribute;
f20e4f
+import javax.naming.directory.Attributes;
f20e4f
+import javax.naming.ldap.LdapName;
f20e4f
+import javax.naming.ldap.Rdn;
f20e4f
 import javax.net.ssl.SSLException;
f20e4f
 import javax.net.ssl.SSLSession;
f20e4f
 import javax.net.ssl.SSLSocket;
f20e4f
@@ -424,28 +430,39 @@ public class SSLProtocolSocketFactory implements SecureProtocolSocketFactory {
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
+	private static String getCN(final X509Certificate cert) {
f20e4f
+		final String subjectPrincipal = cert.getSubjectX500Principal().toString();
f20e4f
+		try {
f20e4f
+			return extractCN(subjectPrincipal);
f20e4f
+		} catch (SSLException ex) {
f20e4f
+			return null;
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
+	private static String extractCN(final String subjectPrincipal) throws SSLException {
f20e4f
+		if (subjectPrincipal == null) {
f20e4f
+			return null;
f20e4f
+		}
f20e4f
+		try {
f20e4f
+			final LdapName subjectDN = new LdapName(subjectPrincipal);
f20e4f
+			final List<Rdn> rdns = subjectDN.getRdns();
f20e4f
+			for (int i = rdns.size() - 1; i >= 0; i--) {
f20e4f
+				final Rdn rds = rdns.get(i);
f20e4f
+				final Attributes attributes = rds.toAttributes();
f20e4f
+				final Attribute cn = attributes.get("cn");
f20e4f
+				if (cn != null) {
f20e4f
+					try {
f20e4f
+						final Object value = cn.get();
f20e4f
+						if (value != null) {
f20e4f
+							return value.toString();
f20e4f
+						}
f20e4f
+					} catch (NoSuchElementException ignore) {
f20e4f
+					} catch (NamingException ignore) {
f20e4f
+					}
f20e4f
 				}
f20e4f
 			}
f20e4f
+		} catch (InvalidNameException e) {
f20e4f
+			throw new SSLException(subjectPrincipal + " is not a valid X500 distinguished name");
f20e4f
 		}
f20e4f
 		return null;
f20e4f
 	}