View Javadoc

1   /*
2    * CertToolkit.java
3    *
4    * Created on May 11, 2005, 12:00 PM
5    *
6    * To change this template, choose Tools | Options and locate the template under
7    * the Source Creation and Management node. Right-click the template and choose
8    * Open. You can then make changes to the template in the Source Editor.
9    */
10  
11  package gov.bnl.gums.admin;
12  
13  import java.util.Collections;
14  import java.util.Iterator;
15  import java.util.List;
16  import java.util.ArrayList;
17  import java.security.cert.X509Certificate;
18  
19  /***
20   *
21   * @author carcassi
22   */
23  public class CertToolkit {
24      
25      private  CertToolkit() {
26      }
27      
28      public static String getUserDN(X509Certificate cert) {
29          if (cert == null) return null;
30          String commaDN = cert.getSubjectX500Principal().toString();
31          // TODO Probably should check the issuer cert is also a proxy
32          // Old style proxy are recognized by proxy in the DN
33          if (commaDN.toLowerCase().indexOf("proxy") != -1) {
34              commaDN = cert.getIssuerX500Principal().toString();
35          }
36          // New style proxy are recognized by presence of extension
37          if (cert.getExtensionValue("1.3.6.1.5.5.7") != null) {
38              commaDN = cert.getIssuerX500Principal().toString();
39          }
40          // New proxy implementation is bogus, and uses a different extension
41          if (cert.getExtensionValue("1.3.6.1.4.1.3536.1.222") != null) {
42              commaDN = cert.getIssuerX500Principal().toString();
43          }
44          return convertDN(commaDN);
45      }
46      
47      public static String convertDN(String commaDN) {
48          List pieces = new ArrayList();
49          while (commaDN.indexOf(", ") != -1) {
50              int pos = commaDN.indexOf(", ");
51              pieces.add(commaDN.substring(0, pos));
52              commaDN = commaDN.substring(pos+2);
53          }
54          pieces.add(commaDN);
55          Collections.reverse(pieces);
56          Iterator iter = pieces.iterator();
57          StringBuffer DN = new StringBuffer();
58          while (iter.hasNext()) {
59              DN.append("/");
60              DN.append((String) iter.next());
61          }
62          return DN.toString();
63      }
64  }