1 | package net.deterlab.abac; |
---|
2 | |
---|
3 | import edu.uci.ics.jung.graph.*; |
---|
4 | import edu.uci.ics.jung.graph.util.*; |
---|
5 | |
---|
6 | import java.io.*; |
---|
7 | import java.util.*; |
---|
8 | import java.util.regex.*; |
---|
9 | import java.util.zip.*; |
---|
10 | import java.security.*; |
---|
11 | import java.security.cert.*; |
---|
12 | |
---|
13 | import org.bouncycastle.asn1.*; |
---|
14 | import org.bouncycastle.asn1.x509.*; |
---|
15 | import org.bouncycastle.x509.*; |
---|
16 | import org.bouncycastle.openssl.*; |
---|
17 | import org.bouncycastle.jce.provider.BouncyCastleProvider; |
---|
18 | |
---|
19 | /** |
---|
20 | * Represents a global graph of credentials in the form of principals and |
---|
21 | * attributes. Contains the identities and credentials that can be used in a |
---|
22 | * proof. |
---|
23 | * @author <a href="http://abac.deterlab.net">ISI ABAC team</a> |
---|
24 | * @version 1.5 |
---|
25 | */ |
---|
26 | public class KeyIDMap { |
---|
27 | /** Translation from nickname to issuer pubkey identifier */ |
---|
28 | protected Map<String, String> nicknames; |
---|
29 | /** Translation from issuer pubkey identifier to nickname */ |
---|
30 | protected Map<String, String> keys; |
---|
31 | |
---|
32 | /** |
---|
33 | * Create an empty Context. |
---|
34 | */ |
---|
35 | public KeyIDMap() { |
---|
36 | nicknames = new TreeMap<String, String>(); |
---|
37 | keys = new TreeMap<String, String>(); |
---|
38 | } |
---|
39 | |
---|
40 | /** |
---|
41 | * Create a KeyIDMap from another KeyIDMap |
---|
42 | * @param k the KeyIDMap to copy |
---|
43 | */ |
---|
44 | public KeyIDMap(KeyIDMap k) { |
---|
45 | nicknames = new TreeMap<String, String>(k.nicknames); |
---|
46 | keys = new TreeMap<String, String>(k.keys); |
---|
47 | } |
---|
48 | |
---|
49 | /** |
---|
50 | * Add a mapping from keyid to nickname and back. If the keyid is already |
---|
51 | * assigned a nickname, this fails. If the nickname is already assigned to |
---|
52 | * another key, it is disambiguated from other known nicknames. The |
---|
53 | * nickname that is assigned is returned, or null on failure. |
---|
54 | * @param keyid a String, the keyid to map |
---|
55 | * @param nick a String, the nickname to assign |
---|
56 | * @return a String, the nickname actually assigned |
---|
57 | */ |
---|
58 | public String addNickname(String keyid, String nick) { |
---|
59 | String name = nick; |
---|
60 | int n= 1; |
---|
61 | |
---|
62 | if ( keyid == null || nick == null) return null; |
---|
63 | if ( keys.containsKey(keyid) ) return null; |
---|
64 | |
---|
65 | while (nicknames.containsKey(name)) { |
---|
66 | name = nick + n++; |
---|
67 | } |
---|
68 | nicknames.put(name, keyid); |
---|
69 | keys.put(keyid, name); |
---|
70 | return name; |
---|
71 | } |
---|
72 | |
---|
73 | /** |
---|
74 | * Return the nickname of this keyid, if any. |
---|
75 | * @param keyid the keyID to look up |
---|
76 | * @return the nickname of this keyid, or null if it is unknown. |
---|
77 | */ |
---|
78 | public String keyToNickname(String keyid) { |
---|
79 | return keys.get(keyid); |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * Return the keyis of this nickname, if any. |
---|
84 | * @param nick the nickname to look up |
---|
85 | * @return the keyid of this nickname, or null if it is unknown. |
---|
86 | */ |
---|
87 | public String nicknameToKey(String nick) { |
---|
88 | return nicknames.get(nick); |
---|
89 | } |
---|
90 | |
---|
91 | /** |
---|
92 | * Return all the keyIDs this map knows. |
---|
93 | * @return a Set of Strings containing all the keyIDs this map knows. |
---|
94 | */ |
---|
95 | public Set<String> getKeys() { |
---|
96 | return keys.keySet(); |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * Remove the mapping associated with this keyid. |
---|
101 | * @param keyid a String to remove from both maps as a keyis |
---|
102 | * @return a boolean, true if the keyid was found and removed |
---|
103 | */ |
---|
104 | public boolean clearKey(String keyid) { |
---|
105 | String nick = keys.get(keyid); |
---|
106 | |
---|
107 | if ( nick == null ) return false; |
---|
108 | |
---|
109 | nicknames.remove(nick); |
---|
110 | keys.remove(keyid); |
---|
111 | return true; |
---|
112 | } |
---|
113 | |
---|
114 | /** |
---|
115 | * Remove the mapping associated with this nickname. |
---|
116 | * @param nick a String to remove from both maps as a nickname |
---|
117 | * @return a boolean, true if the nickname was found and removed |
---|
118 | */ |
---|
119 | public boolean clearNickname(String nick) { |
---|
120 | String keyid = nicknames.get(nick); |
---|
121 | |
---|
122 | if ( keyid == null ) return false; |
---|
123 | |
---|
124 | nicknames.remove(nick); |
---|
125 | keys.remove(keyid); |
---|
126 | return true; |
---|
127 | } |
---|
128 | |
---|
129 | /** |
---|
130 | * Merge the mapping into this one. If overwrite is true, entries in km |
---|
131 | * overwrite entries in this. |
---|
132 | * @param km the KeyIDMap to merge |
---|
133 | * @param overwrite a boolean, true if km entries overwrite this map |
---|
134 | */ |
---|
135 | public void merge(KeyIDMap km, boolean overwrite) { |
---|
136 | for (String k : km.getKeys()) { |
---|
137 | if (keyToNickname(k) != null) { |
---|
138 | if ( overwrite) clearKey(k); |
---|
139 | else continue; |
---|
140 | } |
---|
141 | addNickname(k, km.keyToNickname(k)); |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | /** |
---|
146 | * Translate either keys to nicknames or vice versa. Break the string into |
---|
147 | * space separated tokens and then each of them into period separated |
---|
148 | * strings. If any of the smallest strings is in the map, replace it with |
---|
149 | * the value. |
---|
150 | * @param is the string to manipulate |
---|
151 | * @param m the Map containing translations |
---|
152 | * @return the string after modification |
---|
153 | */ |
---|
154 | protected String replace(String is, Map<String, String> m) { |
---|
155 | String rv = ""; |
---|
156 | for (String tok: is.split(" ")) { |
---|
157 | String term = ""; |
---|
158 | for (String s: tok.split("\\.")) { |
---|
159 | String next = m.containsKey(s) ? m.get(s) : s; |
---|
160 | |
---|
161 | if (term.isEmpty()) term = next; |
---|
162 | else term += "." + next; |
---|
163 | } |
---|
164 | if (rv.isEmpty()) rv = term; |
---|
165 | else rv += " " + term; |
---|
166 | } |
---|
167 | return rv; |
---|
168 | } |
---|
169 | |
---|
170 | /** |
---|
171 | * Expand menmonic names in a Role string, e.g. the CN of the issuer |
---|
172 | * certificate, into the full key ID. Used internally by Roles to provide |
---|
173 | * transparent use of mnemonics |
---|
174 | * @param s the string to expand |
---|
175 | * @return the String after expansion. |
---|
176 | */ |
---|
177 | public String expandKeyID(String s) { return replace(s, nicknames); } |
---|
178 | /** |
---|
179 | * Convert key IDs to menmonic names in a Role string. The inverse of |
---|
180 | * expandKeyID. |
---|
181 | * @param s the string to expand |
---|
182 | * @return the String after expansion. |
---|
183 | */ |
---|
184 | public String expandNickname(String s) { return replace(s, keys); } |
---|
185 | |
---|
186 | } |
---|