[java-xmltooling COMMIT] in /branches/REL_1/src: main/java/org/opensaml/xml/security/x509/X509Util.java test/java/org...

noreply at shibboleth.net noreply at shibboleth.net
Wed Sep 17 19:05:42 EDT 2014


Author: putmanb
Date: Wed Sep 17 19:05:42 2014
New Revision: 830

URL: http://svn.shibboleth.net/view/java-xmltooling?rev=830&view=rev
Log:
JXT-114: X509Util getCommonNames returns a list in the reverse order of what is expected 

Modified:
    branches/REL_1/src/main/java/org/opensaml/xml/security/x509/X509Util.java
    branches/REL_1/src/test/java/org/opensaml/xml/security/x509/X509UtilTest.java

Modified: branches/REL_1/src/main/java/org/opensaml/xml/security/x509/X509Util.java
URL: http://svn.shibboleth.net/view/java-xmltooling/branches/REL_1/src/main/java/org/opensaml/xml/security/x509/X509Util.java?rev=830&r1=829&r2=830&view=diff
==============================================================================
--- branches/REL_1/src/main/java/org/opensaml/xml/security/x509/X509Util.java (original)
+++ branches/REL_1/src/main/java/org/opensaml/xml/security/x509/X509Util.java Wed Sep 17 19:05:42 2014
@@ -32,6 +32,7 @@
 import java.security.cert.X509CRL;
 import java.security.cert.X509Certificate;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.LinkedList;
 import java.util.List;
 
@@ -136,12 +137,17 @@
     }
 
     /**
-     * Gets the commons names that appear within the given distinguished name. The returned list provides the names in
-     * the order they appeared in the DN.
+     * Gets the commons names that appear within the given distinguished name. 
+     * 
+     * <p>
+     * The returned list provides the names in the order they appeared in the DN, according to 
+     * RFC 1779/2253 encoding. In this encoding the "most specific" name would typically appear
+     * in the left-most position, and would appear first in the returned list.
+     * </p>
      * 
      * @param dn the DN to extract the common names from
      * 
-     * @return the common names that appear in the DN in the order they appear or null if the given DN is null
+     * @return the common names that appear in the DN in the order they appear, or null if the given DN is null
      */
     public static List<String> getCommonNames(X500Principal dn) {
         Logger log = getLogger();
@@ -188,6 +194,10 @@
 
             asn1Stream.close();
 
+            // Reverse the order so that the most-specific CN is first in the list, 
+            // consistent with RFC 1779/2253 RDN ordering.
+            Collections.reverse(commonNames);
+            
             return commonNames;
 
         } catch (IOException e) {

Modified: branches/REL_1/src/test/java/org/opensaml/xml/security/x509/X509UtilTest.java
URL: http://svn.shibboleth.net/view/java-xmltooling/branches/REL_1/src/test/java/org/opensaml/xml/security/x509/X509UtilTest.java?rev=830&r1=829&r2=830&view=diff
==============================================================================
--- branches/REL_1/src/test/java/org/opensaml/xml/security/x509/X509UtilTest.java (original)
+++ branches/REL_1/src/test/java/org/opensaml/xml/security/x509/X509UtilTest.java Wed Sep 17 19:05:42 2014
@@ -303,22 +303,31 @@
         commonNames = X509Util.getCommonNames(new X500Principal("cn=foo.example.org"));
         assertNotNull(commonNames);
         assertEquals(1, commonNames.size());
-        assertTrue(commonNames.contains("foo.example.org"));
+        assertEquals("foo.example.org", commonNames.get(0));
         
         // 2 components, 1 cn
         commonNames = X509Util.getCommonNames(new X500Principal("cn=foo.example.org, o=MyOrg"));
         assertNotNull(commonNames);
         assertEquals(1, commonNames.size());
-        assertTrue(commonNames.contains("foo.example.org"));
+        assertEquals("foo.example.org", commonNames.get(0));
         
         // 2 components each with cn
         commonNames = X509Util.getCommonNames(new X500Principal("cn=foo.example.org, cn=MyOrg"));
         assertNotNull(commonNames);
         assertEquals(2, commonNames.size());
-        assertTrue(commonNames.contains("foo.example.org"));
-        assertTrue(commonNames.contains("MyOrg"));
-        
-        // 2 components, one of them with multiple cn AVAs
+        assertEquals("foo.example.org", commonNames.get(0));
+        assertEquals("MyOrg", commonNames.get(1));
+        
+        // 4 components, 3 cn 
+        commonNames = X509Util.getCommonNames(new X500Principal("cn=foo.example.org, cn=WebServers, cn=Hosts, o=MyOrg"));
+        assertNotNull(commonNames);
+        assertEquals(3, commonNames.size());
+        assertEquals("foo.example.org", commonNames.get(0));
+        assertEquals("WebServers", commonNames.get(1));
+        assertEquals("Hosts", commonNames.get(2));
+        
+        // 2 components, one of them with multiple cn AVAs. 
+        // Note: The set of AVAs in a DN component is unordered, so can't test returned ordering.
         commonNames = X509Util.getCommonNames(new X500Principal("cn=foo.example.org+cn=bar.example.org+cn=baz.example.org, o=MyOrg"));
         assertNotNull(commonNames);
         assertEquals(3, commonNames.size());
@@ -327,6 +336,7 @@
         assertTrue(commonNames.contains("baz.example.org"));
         

[... 30 lines stripped ...]


More information about the commits mailing list