]> source.dussan.org Git - gitblit.git/commitdiff
Parse X509 DN with LdapName 49/249/1
authorJames Moger <james.moger@gitblit.com>
Fri, 22 May 2015 15:25:37 +0000 (11:25 -0400)
committerJames Moger <james.moger@gitblit.com>
Fri, 22 May 2015 15:25:37 +0000 (11:25 -0400)
src/main/java/com/gitblit/utils/X509Utils.java

index fc0b797d298db2aedfe0100e313c052f99c1b894..a2650be44d35b9ee54e8645ffb0f5acab22b4bd2 100644 (file)
@@ -61,6 +61,7 @@ import java.util.zip.ZipEntry;
 import java.util.zip.ZipOutputStream;\r
 \r
 import javax.crypto.Cipher;\r
+import javax.naming.ldap.LdapName;\r
 \r
 import org.bouncycastle.asn1.ASN1ObjectIdentifier;\r
 import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;\r
@@ -1117,17 +1118,18 @@ public class X509Utils {
        }\r
 \r
        public static X509Metadata getMetadata(X509Certificate cert) {\r
-               // manually split DN into OID components\r
-               // this is instead of parsing with LdapName which:\r
-               // (1) I don't trust the order of values\r
-               // (2) it filters out values like EMAILADDRESS\r
-               String dn = cert.getSubjectDN().getName();\r
                Map<String, String> oids = new HashMap<String, String>();\r
-               for (String kvp : dn.split(",")) {\r
-                       String [] val = kvp.trim().split("=");\r
-                       String oid = val[0].toUpperCase().trim();\r
-                       String data = val[1].trim();\r
-                       oids.put(oid, data);\r
+               try {\r
+                       String dn = cert.getSubjectDN().getName();\r
+                       LdapName ldapName = new LdapName(dn);\r
+                       for (int i = 0; i < ldapName.size(); i++) {\r
+                               String [] val = ldapName.get(i).trim().split("=", 2);\r
+                               String oid = val[0].toUpperCase().trim();\r
+                               String data = val[1].trim();\r
+                               oids.put(oid, data);\r
+                       }\r
+               } catch (Exception e) {\r
+                       throw new RuntimeException(e);\r
                }\r
 \r
                X509Metadata metadata = new X509Metadata(oids.get("CN"), "whocares");\r