1
10package org.jstk.crypt;
11
12import java.util.*;
13import java.security.*;
14
15public class SecurityInfo {
16 public static class AlgInfo {
17 HashSet aliases;
18 Properties props;
19 public AlgInfo(String alg1, String alg2, String pn, String pv){
20 aliases = new HashSet();
21 aliases.add(alg1);
22 if (alg2 != null)
23 aliases.add(alg2);
24 addProperty(pn, pv);
25 }
26 public boolean addConditionally(String alg1, String alg2, String pn, String pv){
27 if (aliases.contains(alg1)){
28 if (alg2 != null)
29 aliases.add(alg2);
30 addProperty(pn, pv);
31 return true;
32 } else if (alg2 != null && aliases.contains(alg2)){
33 aliases.add(alg1);
34 addProperty(pn, pv);
35 return true;
36 }
37 return false;
38 }
39
40 public void addProperty(String pn, String pv){
41 if (pn != null){
42 if (props == null)
43 props = new Properties();
44 props.setProperty(pn, pv);
45 }
46 }
47
48 public String toString(){
49 StringBuffer sb = new StringBuffer();
50 Iterator aitr = aliases.iterator();
51 boolean firstAlias = true;
52 while (aitr.hasNext()){
53 String alg = (String)aitr.next();
54 if (!firstAlias)
55 sb.append("|");
56 sb.append(alg);
57 firstAlias = false;
58 }
59 return sb.toString();
60 }
61 }
62
63 public static class ProviderInfo {
64 String name;
65 double version;
66 String info;
67 Properties props;
68 HashMap svcmap;
69 public ProviderInfo(Provider provider){
70 name = provider.getName();
71 version = provider.getVersion();
72 info = provider.getInfo();
73
74 props = new Properties();
75 svcmap = new HashMap();
76 Iterator itr = provider.entrySet().iterator();
77 while (itr.hasNext()){
78 Map.Entry ent = (Map.Entry)itr.next();
79 String key = (String)ent.getKey();
80 String value = (String)ent.getValue();
81 props.setProperty(key, value);
82
83 String[] comps = key.split("\\.");
84 String svc = null;
85 String alg1 = null;
86 String alg2 = null;
87 String pname = null, pvalue=null;
88 if (comps.length == 2){ svc = comps[0];
90 alg1 = comps[1];
91 String[] subcomps = alg1.split(" ");
92 if (subcomps.length > 1){
93 alg1 = subcomps[0];
94 pname = subcomps[1];
95 pvalue = value;
96 }
97 } else if (comps.length == 4){ svc = comps[2];
99 alg1 = comps[3];
00 alg2 = value;
01 } else { continue;
03 }
04 Vector algs = (Vector)svcmap.get(svc);
05 if (algs == null){ algs = new Vector();
07
08 algs.add(new AlgInfo(alg1, alg2, pname, pvalue));
09 svcmap.put(svc, algs);
10 } else { boolean found = false;
12 for (int i = 0; i < algs.size() && !found; i++){
13 AlgInfo ai = (AlgInfo)algs.elementAt(i);
14 found = ai.addConditionally(alg1, alg2, pname, pvalue);
15 }
16 if (!found) algs.add(new AlgInfo(alg1, alg2, pname, pvalue));
18 }
19 }
20 }
21 }
22 public ProviderInfo[] providers = null;
24 public SecurityInfo(){
25 Provider[] ps = Security.getProviders();
26 providers = new ProviderInfo[ps.length];
27 for (int i = 0; i < ps.length; i++){
28 providers[i] = new ProviderInfo(ps[i]);
29 }
30 }
31}