1 /*
2  * @(#) $Id: SecurityInfo.java,v 1.3 2003/07/08 08:13:53 pankaj Exp $
3  *
4  * Copyright (c) 2002-03 by Pankaj Kumar (http://www.pankaj-k.net). 
5  * All rights reserved.
6  *
7  * The license governing the use of this file can be found in the 
8  * root directory of the containing software.
9  */
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){ // key is of form:: <svc>.<alg>
89                    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){  // key is of form:: Alg.Alias.<svc>.<alg>
98                    svc = comps[2];
99                    alg1 = comps[3];
00                    alg2 = value;
01                } else {                // Ignore other entries.
02                    continue;
03                }
04                Vector algs = (Vector)svcmap.get(svc);
05                if (algs == null){  // Found a new service.
06                    algs = new Vector();
07
08                    algs.add(new AlgInfo(alg1, alg2, pname, pvalue));
09                    svcmap.put(svc, algs);
10                } else {            // Exisiting service
11                    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) // New Algorithm
17                        algs.add(new AlgInfo(alg1, alg2, pname, pvalue));
18                }
19            }
20        }
21    }
22    public ProviderInfo[] providers = null; // array of ProviderInfo
23
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}