896142
From 3e3515b42a5c782219ba898f9cb79812c8895349 Mon Sep 17 00:00:00 2001
896142
From: Michal Srb <msrb@redhat.com>
896142
Date: Tue, 12 Aug 2014 14:07:29 +0200
896142
Subject: [PATCH] Fix CVE-2014-3577
896142
896142
---
896142
 .../org/apache/http/conn/ssl/AbstractVerifier.java | 87 +++++++++++-----------
896142
 1 file changed, 44 insertions(+), 43 deletions(-)
896142
896142
diff --git a/httpclient/src/main/java/org/apache/http/conn/ssl/AbstractVerifier.java b/httpclient/src/main/java/org/apache/http/conn/ssl/AbstractVerifier.java
896142
index a7cad68..7245781 100644
896142
--- a/httpclient/src/main/java/org/apache/http/conn/ssl/AbstractVerifier.java
896142
+++ b/httpclient/src/main/java/org/apache/http/conn/ssl/AbstractVerifier.java
896142
@@ -28,7 +28,6 @@
896142
 package org.apache.http.conn.ssl;
896142
 
896142
 import org.apache.http.annotation.Immutable;
896142
-
896142
 import org.apache.http.conn.util.InetAddressUtils;
896142
 
896142
 import java.io.IOException;
896142
@@ -36,14 +35,21 @@ import java.io.InputStream;
896142
 import java.security.cert.Certificate;
896142
 import java.security.cert.CertificateParsingException;
896142
 import java.security.cert.X509Certificate;
896142
+import java.util.ArrayList;
896142
 import java.util.Arrays;
896142
 import java.util.Collection;
896142
 import java.util.Iterator;
896142
 import java.util.LinkedList;
896142
 import java.util.List;
896142
 import java.util.Locale;
896142
-import java.util.StringTokenizer;
896142
-
896142
+import java.util.NoSuchElementException;
896142
+
896142
+import javax.naming.InvalidNameException;
896142
+import javax.naming.NamingException;
896142
+import javax.naming.directory.Attribute;
896142
+import javax.naming.directory.Attributes;
896142
+import javax.naming.ldap.LdapName;
896142
+import javax.naming.ldap.Rdn;
896142
 import javax.net.ssl.SSLException;
896142
 import javax.net.ssl.SSLSession;
896142
 import javax.net.ssl.SSLSocket;
896142
@@ -142,7 +148,8 @@ public abstract class AbstractVerifier implements X509HostnameVerifier {
896142
 
896142
     public final void verify(String host, X509Certificate cert)
896142
           throws SSLException {
896142
-        String[] cns = getCNs(cert);
896142
+        final String subjectPrincipal = cert.getSubjectX500Principal().toString();
896142
+        final String[] cns = extractCNs(subjectPrincipal);
896142
         String[] subjectAlts = getSubjectAlts(cert, host);
896142
         verify(host, cns, subjectAlts);
896142
     }
896142
@@ -236,48 +243,42 @@ public abstract class AbstractVerifier implements X509HostnameVerifier {
896142
         return Arrays.binarySearch(BAD_COUNTRY_2LDS, parts[1]) < 0;
896142
     }
896142
 
896142
-    public static String[] getCNs(X509Certificate cert) {
896142
-        LinkedList<String> cnList = new LinkedList<String>();
896142
-        /*
896142
-          Sebastian Hauer's original StrictSSLProtocolSocketFactory used
896142
-          getName() and had the following comment:
896142
-
896142
-                Parses a X.500 distinguished name for the value of the
896142
-                "Common Name" field.  This is done a bit sloppy right
896142
-                 now and should probably be done a bit more according to
896142
-                RFC 2253.
896142
-
896142
-           I've noticed that toString() seems to do a better job than
896142
-           getName() on these X500Principal objects, so I'm hoping that
896142
-           addresses Sebastian's concern.
896142
-
896142
-           For example, getName() gives me this:
896142
-           1.2.840.113549.1.9.1=#16166a756c6975736461766965734063756362632e636f6d
896142
-
896142
-           whereas toString() gives me this:
896142
-           EMAILADDRESS=juliusdavies@cucbc.com
896142
-
896142
-           Looks like toString() even works with non-ascii domain names!
896142
-           I tested it with "花子.co.jp" and it worked fine.
896142
-        */
896142
-
896142
-        String subjectPrincipal = cert.getSubjectX500Principal().toString();
896142
-        StringTokenizer st = new StringTokenizer(subjectPrincipal, ",");
896142
-        while(st.hasMoreTokens()) {
896142
-            String tok = st.nextToken().trim();
896142
-            if (tok.length() > 3) {
896142
-                if (tok.substring(0, 3).equalsIgnoreCase("CN=")) {
896142
-                    cnList.add(tok.substring(3));
896142
-                }
896142
-            }
896142
+    public static String[] getCNs(final X509Certificate cert) {
896142
+        final String subjectPrincipal = cert.getSubjectX500Principal().toString();
896142
+        try {
896142
+            return extractCNs(subjectPrincipal);
896142
+        } catch (SSLException ex) {
896142
+            return null;
896142
         }
896142
-        if(!cnList.isEmpty()) {
896142
-            String[] cns = new String[cnList.size()];
896142
-            cnList.toArray(cns);
896142
-            return cns;
896142
-        } else {
896142
+    }
896142
+
896142
+    static String[] extractCNs(final String subjectPrincipal) throws SSLException {
896142
+        if (subjectPrincipal == null) {
896142
             return null;
896142
         }
896142
+        final List<String> cns = new ArrayList<String>();
896142
+        try {
896142
+            final LdapName subjectDN = new LdapName(subjectPrincipal);
896142
+            final List<Rdn> rdns = subjectDN.getRdns();
896142
+            for (int i = rdns.size() - 1; i >= 0; i--) {
896142
+                final Rdn rds = rdns.get(i);
896142
+                final Attributes attributes = rds.toAttributes();
896142
+                final Attribute cn = attributes.get("cn");
896142
+                if (cn != null) {
896142
+                    try {
896142
+                        final Object value = cn.get();
896142
+                        if (value != null) {
896142
+                            cns.add(value.toString());
896142
+                        }
896142
+                    } catch (NoSuchElementException ignore) {
896142
+                    } catch (NamingException ignore) {
896142
+                    }
896142
+                }
896142
+            }
896142
+        } catch (InvalidNameException e) {
896142
+            throw new SSLException(subjectPrincipal + " is not a valid X500 distinguished name");
896142
+        }
896142
+        return cns.isEmpty() ? null : cns.toArray(new String[cns.size()]);
896142
     }
896142
 
896142
     /**
896142
-- 
896142
1.9.3
896142