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

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