source: java/net/deterlab/abac/KeyIDMap.java @ a1a9a47

abac0-leakabac0-mei
Last change on this file since a1a9a47 was a1a9a47, checked in by Ted Faber <faber@…>, 11 years ago

Bump version

  • Property mode set to 100644
File size: 5.3 KB
Line 
1package net.deterlab.abac;
2
3import edu.uci.ics.jung.graph.*;
4import edu.uci.ics.jung.graph.util.*;
5
6import java.io.*;
7import java.util.*;
8import java.util.regex.*;
9import java.util.zip.*;
10import java.security.*;
11import java.security.cert.*;
12
13import org.bouncycastle.asn1.*;
14import org.bouncycastle.asn1.x509.*;
15import org.bouncycastle.x509.*;
16import org.bouncycastle.openssl.*;
17import 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 */
26public 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     * @return the nickname of this keyid, or null if it is unknown.
76     */
77    public String keyToNickname(String keyid) {
78        return keys.get(keyid);
79    }
80
81    /**
82     * Return the keyis of this nickname, if any.
83     * @return the keyid of this nickname, or null if it is unknown.
84     */
85    public String nicknameToKey(String nick) {
86        return nicknames.get(nick);
87    }
88
89    /**
90     * Return all the keyIDs this map knows.
91     * @return a Set of Strings containing all the keyIDs this map knows.
92     */
93    public Set<String> getKeys() {
94        return keys.keySet();
95    }
96
97    /**
98     * Remove the mapping associated with this keyid.
99     * @param keyid a String to remove from both maps as a keyis
100     * @return a boolean, true if the keyid was found and removed
101     */
102    public boolean clearKey(String keyid) {
103        String nick = keys.get(keyid);
104
105        if ( nick == null ) return false;
106
107        nicknames.remove(nick);
108        keys.remove(keyid);
109        return true;
110    }
111
112    /**
113     * Remove the mapping associated with this nickname.
114     * @param nick a String to remove from both maps as a nickname
115     * @return a boolean, true if the nickname was found and removed
116     */
117    public boolean clearNickname(String nick) {
118        String keyid = nicknames.get(nick);
119
120        if ( keyid == null ) return false;
121
122        nicknames.remove(nick);
123        keys.remove(keyid);
124        return true;
125    }
126
127    /**
128     * Merge the mapping into this one.  If overwrite is true, entries in km
129     * overwrite entries in this.
130     * @param km the KeyIDMap to merge
131     * @param overwrite a boolean, true if km entries overwrite this map
132     */
133    public void merge(KeyIDMap km, boolean overwrite) {
134        for (String k : km.getKeys()) {
135            if (keyToNickname(k) != null) {
136                if ( overwrite) clearKey(k);
137                else continue;
138            }
139            addNickname(k, km.keyToNickname(k));
140        }
141    }
142
143    /**
144     * Translate either keys to nicknames or vice versa.  Break the string into
145     * space separated tokens and then each of them into period separated
146     * strings.  If any of the smallest strings is in the map, replace it with
147     * the value.
148     * @param is the string to manipulate
149     * @param m the Map containing translations
150     * @return the string after modification
151     */
152    protected String replace(String is, Map<String, String> m) {
153        String rv = "";
154        for (String tok: is.split(" ")) {
155            String term = "";
156            for (String s: tok.split("\\.")) {
157                String next = m.containsKey(s) ? m.get(s) : s;
158
159                if (term.isEmpty()) term = next;
160                else term += "." + next;
161            }
162            if (rv.isEmpty()) rv = term;
163            else rv += " " + term;
164        }
165        return rv;
166    }
167
168    /**
169     * Expand menmonic names in a Role string, e.g. the CN of the issuer
170     * certificate, into the full key ID.  Used internally by Roles to provide
171     * transparent use of mnemonics
172     * @param s the string to expand
173     * @return the String after expansion.
174     */
175    public String expandKeyID(String s) { return replace(s, nicknames); }
176    /**
177     * Convert key IDs to  menmonic names in a Role string.  The inverse of
178     * expandKeyID.
179     * @param s the string to expand
180     * @return the String after expansion.
181     */
182    public String expandNickname(String s) { return replace(s, keys); }
183
184}
Note: See TracBrowser for help on using the repository browser.