1
2
3
4
5
6
7
8
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
32
33 if (commaDN.toLowerCase().indexOf("proxy") != -1) {
34 commaDN = cert.getIssuerX500Principal().toString();
35 }
36
37 if (cert.getExtensionValue("1.3.6.1.5.5.7") != null) {
38 commaDN = cert.getIssuerX500Principal().toString();
39 }
40
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 }