[418b586] | 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.*; |
---|
[ad24705] | 8 | import java.util.regex.*; |
---|
[418b586] | 9 | import java.util.zip.*; |
---|
| 10 | import java.security.*; |
---|
| 11 | import java.security.cert.*; |
---|
| 12 | |
---|
[0595372] | 13 | import org.bouncycastle.asn1.*; |
---|
| 14 | import org.bouncycastle.asn1.x509.*; |
---|
[418b586] | 15 | import org.bouncycastle.x509.*; |
---|
| 16 | import org.bouncycastle.openssl.*; |
---|
[238717d] | 17 | import org.bouncycastle.jce.provider.BouncyCastleProvider; |
---|
[418b586] | 18 | |
---|
| 19 | /** |
---|
| 20 | * Represents a global graph of credentials in the form of principals and |
---|
[e36ea1d] | 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> |
---|
[a1a9a47] | 24 | * @version 1.5 |
---|
[418b586] | 25 | */ |
---|
| 26 | public class Context { |
---|
[e36ea1d] | 27 | /** Certificate imported successfully */ |
---|
| 28 | public static final int ABAC_CERT_SUCCESS = 0; |
---|
| 29 | /** Certificate import failed, invalid certificate */ |
---|
| 30 | public static final int ABAC_CERT_INVALID = -1; |
---|
| 31 | /** Certificate import failed, signature filed */ |
---|
| 32 | public static final int ABAC_CERT_BAD_SIG = -2; |
---|
| 33 | /** Certificate import failed, unknown issuer */ |
---|
| 34 | public static final int ABAC_CERT_MISSING_ISSUER = -3; |
---|
| 35 | |
---|
| 36 | /** Internal graph representation */ |
---|
[418b586] | 37 | protected Graph<Role,Credential> g; |
---|
[e36ea1d] | 38 | /** Set of edges in the graph that were added by the logic. */ |
---|
[418b586] | 39 | protected Set<Credential> derived_edges; |
---|
[e36ea1d] | 40 | /** Internal persistent query object */ |
---|
[418b586] | 41 | protected Query pq; |
---|
[e36ea1d] | 42 | /** True when the graph has been changed since the last set of implied |
---|
| 43 | * edges were calculated. */ |
---|
[418b586] | 44 | protected boolean dirty; |
---|
[e36ea1d] | 45 | /** Set of identities known to this Context. */ |
---|
[388a3d7] | 46 | protected Set<Identity> m_identities; |
---|
[d06c453] | 47 | /** The CredentialFactory for this Context */ |
---|
| 48 | protected CredentialFactory credentialFactory; |
---|
[418b586] | 49 | |
---|
[4fd9436] | 50 | /** Translation of nicknames to keyids */ |
---|
| 51 | protected KeyIDMap keyMap; |
---|
[418b586] | 52 | |
---|
[238717d] | 53 | /** True once BouncyCastle has been loaded. */ |
---|
| 54 | static boolean providerLoaded = false; |
---|
| 55 | |
---|
| 56 | /** |
---|
| 57 | * Load the BouncyCastle provider, necessary for ABAC crypto (shouldn't |
---|
| 58 | * need to be called directly). This is called from the Context static |
---|
| 59 | * constructor and static constructors in other ABAC classes that use |
---|
| 60 | * BouncyCastle crypto (which loads a Context, which calls it as well) to |
---|
| 61 | * make sure crypto is available. |
---|
| 62 | */ |
---|
| 63 | static void loadBouncyCastle() { |
---|
| 64 | if ( !providerLoaded ) { |
---|
| 65 | AccessController.doPrivileged(new PrivilegedAction<Object>() { |
---|
| 66 | public Object run() { |
---|
| 67 | Security.addProvider(new BouncyCastleProvider()); |
---|
| 68 | return null; |
---|
| 69 | } |
---|
| 70 | }); |
---|
| 71 | providerLoaded = true; |
---|
| 72 | } |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | /** Load the BouncyCastle provider. */ |
---|
| 76 | static { loadBouncyCastle(); }; |
---|
| 77 | |
---|
[e36ea1d] | 78 | /** |
---|
| 79 | * The result of a query on this context. The credentials form a partial |
---|
| 80 | * or total proof, and success indicates whether the proof succeeded. |
---|
| 81 | * @author <a href="http://abac.deterlab.net">ISI ABAC team</a> |
---|
[a1a9a47] | 82 | * @version 1.5 |
---|
[e36ea1d] | 83 | */ |
---|
[1a80501] | 84 | public class QueryResult { |
---|
[e36ea1d] | 85 | /** Credentials returned */ |
---|
[1a80501] | 86 | protected Collection<Credential> creds; |
---|
[e36ea1d] | 87 | /** True if the proof succeeded. */ |
---|
[1a80501] | 88 | protected boolean success; |
---|
| 89 | |
---|
[e36ea1d] | 90 | /** |
---|
| 91 | * Construct a result from components. |
---|
| 92 | * @param c the collection of credentials in the proof |
---|
| 93 | * @param s a boolean, true if the query succeeded. |
---|
| 94 | */ |
---|
| 95 | QueryResult(Collection<Credential> c, boolean s) { |
---|
[1a80501] | 96 | creds = c; |
---|
| 97 | success = s; |
---|
| 98 | } |
---|
| 99 | |
---|
[e36ea1d] | 100 | /** |
---|
| 101 | * Empty constructor |
---|
| 102 | */ |
---|
[1a80501] | 103 | public QueryResult() { |
---|
| 104 | creds = new TreeSet<Credential>(); |
---|
| 105 | success = false; |
---|
| 106 | } |
---|
| 107 | |
---|
[e36ea1d] | 108 | /** |
---|
| 109 | * Return the credentials in the proof. |
---|
| 110 | * @return the collection of credentials |
---|
| 111 | */ |
---|
[1a80501] | 112 | public Collection<Credential> getCredentials() { return creds; } |
---|
[e36ea1d] | 113 | /** |
---|
| 114 | * Return the success in the proof. |
---|
| 115 | * @return the boolean, true on success |
---|
| 116 | */ |
---|
[1a80501] | 117 | public boolean getSuccess() { return success; } |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | |
---|
[e36ea1d] | 121 | /** |
---|
| 122 | * Create an empty Context. |
---|
| 123 | */ |
---|
[418b586] | 124 | public Context() { |
---|
| 125 | /* create the graph */ |
---|
| 126 | g = Graphs.<Role,Credential>synchronizedDirectedGraph( |
---|
| 127 | new DirectedSparseGraph<Role,Credential>()); |
---|
| 128 | derived_edges = new HashSet<Credential>(); |
---|
| 129 | pq = new Query(g); |
---|
| 130 | dirty = false; |
---|
[388a3d7] | 131 | m_identities = new TreeSet<Identity>(); |
---|
[4fd9436] | 132 | keyMap = new KeyIDMap(); |
---|
[d06c453] | 133 | try { |
---|
| 134 | credentialFactory = new CredentialFactory(); |
---|
| 135 | } |
---|
| 136 | catch (ABACException ignored) { } |
---|
[418b586] | 137 | } |
---|
| 138 | |
---|
[e36ea1d] | 139 | /** |
---|
| 140 | * Create a context from another context. |
---|
| 141 | * @param c the Context to copy |
---|
| 142 | */ |
---|
[418b586] | 143 | public Context(Context c) { |
---|
| 144 | this(); |
---|
[388a3d7] | 145 | for (Identity i: c.m_identities) |
---|
[6432e35] | 146 | load_id_chunk(i); |
---|
[418b586] | 147 | for (Credential cr: c.credentials()) |
---|
[6432e35] | 148 | load_attribute_chunk(cr); |
---|
[418b586] | 149 | derive_implied_edges(); |
---|
[d06c453] | 150 | try { |
---|
| 151 | credentialFactory = (CredentialFactory) c.credentialFactory.clone(); |
---|
| 152 | } |
---|
| 153 | catch (CloneNotSupportedException ignored) { } |
---|
[418b586] | 154 | } |
---|
| 155 | |
---|
[83cdf0f] | 156 | /** |
---|
| 157 | * Create a Context from a collection of Credentials. A jabac extension. |
---|
| 158 | * @param creds the collection of credentials |
---|
| 159 | */ |
---|
| 160 | public Context(Collection<Credential> creds) { |
---|
| 161 | this(); |
---|
| 162 | for (Credential c: creds) { |
---|
| 163 | Identity i = c.issuer(); |
---|
| 164 | |
---|
| 165 | if (i != null ) load_id_chunk(i); |
---|
| 166 | load_attribute_chunk(c); |
---|
| 167 | } |
---|
| 168 | } |
---|
| 169 | |
---|
[e36ea1d] | 170 | /** |
---|
| 171 | * Load an Identity from a file. |
---|
| 172 | * @param fn a String containing the file name. |
---|
| 173 | * @return one of the static int return codes. |
---|
| 174 | */ |
---|
[6432e35] | 175 | public int load_id_file(String fn) { return load_id_chunk(new File(fn)); } |
---|
[e36ea1d] | 176 | /** |
---|
| 177 | * Load an Identity from a file. |
---|
| 178 | * @param fn a File containing the file name. |
---|
| 179 | * @return one of the static int return codes. |
---|
| 180 | */ |
---|
[6432e35] | 181 | public int load_id_file(File fn) { return load_id_chunk(fn); } |
---|
[e36ea1d] | 182 | /** |
---|
| 183 | * Load an Identity from an object. Supported objects are an Identity, a |
---|
| 184 | * String, a File, or a java.security.cert.X509Certificate. A string |
---|
| 185 | * creates an new identity, and the others are derived from the contents of |
---|
| 186 | * the data or file. |
---|
| 187 | * @param c an object convertable to an identity as above. |
---|
| 188 | * @return one of the static int return codes. |
---|
| 189 | */ |
---|
[6432e35] | 190 | public int load_id_chunk(Object c) { |
---|
[418b586] | 191 | try { |
---|
| 192 | if (c instanceof Identity) |
---|
| 193 | addIdentity((Identity) c); |
---|
| 194 | else if (c instanceof String) |
---|
| 195 | addIdentity(new Identity((String) c)); |
---|
| 196 | else if (c instanceof File) |
---|
| 197 | addIdentity(new Identity((File) c)); |
---|
| 198 | else if (c instanceof X509Certificate) |
---|
| 199 | addIdentity(new Identity((X509Certificate) c)); |
---|
| 200 | else |
---|
| 201 | return ABAC_CERT_INVALID; |
---|
| 202 | } |
---|
[8fdbdac] | 203 | catch (BadSignatureException sig) { |
---|
[418b586] | 204 | return ABAC_CERT_BAD_SIG; |
---|
| 205 | } |
---|
| 206 | catch (Exception e) { |
---|
| 207 | return ABAC_CERT_INVALID; |
---|
| 208 | } |
---|
| 209 | return ABAC_CERT_SUCCESS; |
---|
| 210 | } |
---|
| 211 | |
---|
[e36ea1d] | 212 | /** |
---|
| 213 | * Load an attribute certificate from a file. |
---|
| 214 | * @param fn a String containing the file name. |
---|
| 215 | * @return one of the static int return codes. |
---|
| 216 | */ |
---|
[6432e35] | 217 | public int load_attribute_file(String fn) { |
---|
| 218 | return load_attribute_chunk(new File(fn)); |
---|
[418b586] | 219 | } |
---|
| 220 | |
---|
[e36ea1d] | 221 | /** |
---|
| 222 | * Load an attribute certificate from a file. |
---|
| 223 | * @param fn a File containing the file name. |
---|
| 224 | * @return one of the static int return codes. |
---|
| 225 | */ |
---|
[6432e35] | 226 | public int load_attribute_file(File fn) { return load_attribute_chunk(fn); } |
---|
[418b586] | 227 | |
---|
[e36ea1d] | 228 | /** |
---|
[7f16578] | 229 | * Load an Credential from an object. Supported objects are a Credential, a |
---|
[e36ea1d] | 230 | * String, a File, or an org.bouncycastle.x509.X509V2AttributeCertificate. |
---|
| 231 | * A string creates an new Credential, and the others are derived from the |
---|
| 232 | * contents of the data or file. |
---|
| 233 | * @param c an object convertable to a Credential as above. |
---|
| 234 | * @return one of the static int return codes. |
---|
| 235 | */ |
---|
[6432e35] | 236 | public int load_attribute_chunk(Object c) { |
---|
[418b586] | 237 | try { |
---|
[7f16578] | 238 | Credential[] creds = null; |
---|
| 239 | |
---|
| 240 | if (c instanceof Credential) { |
---|
[418b586] | 241 | add_credential((Credential) c); |
---|
[7f16578] | 242 | return ABAC_CERT_SUCCESS; |
---|
| 243 | } else if (c instanceof String) { |
---|
[d06c453] | 244 | creds = credentialFactory.parseCredential( |
---|
[f84d71e] | 245 | (String) c, m_identities); |
---|
[7f16578] | 246 | } else if (c instanceof File) { |
---|
[d06c453] | 247 | creds = credentialFactory.parseCredential( |
---|
[f84d71e] | 248 | (File) c, m_identities); |
---|
[7f16578] | 249 | } else if ( c instanceof X509V2AttributeCertificate) { |
---|
[3612811] | 250 | add_credential(new X509Credential((X509V2AttributeCertificate)c, |
---|
[388a3d7] | 251 | m_identities)); |
---|
[7f16578] | 252 | return ABAC_CERT_SUCCESS; |
---|
| 253 | } else return ABAC_CERT_INVALID; |
---|
| 254 | |
---|
| 255 | if ( creds == null ) |
---|
[418b586] | 256 | return ABAC_CERT_INVALID; |
---|
[7f16578] | 257 | |
---|
| 258 | for (Credential cc: creds ) |
---|
| 259 | add_credential(cc); |
---|
[418b586] | 260 | } |
---|
[3612811] | 261 | catch (MissingIssuerException sig) { |
---|
[388a3d7] | 262 | return ABAC_CERT_MISSING_ISSUER ; |
---|
| 263 | } |
---|
[3612811] | 264 | catch (BadSignatureException sig) { |
---|
[418b586] | 265 | return ABAC_CERT_BAD_SIG; |
---|
| 266 | } |
---|
[3612811] | 267 | catch (CertInvalidException e) { |
---|
| 268 | return ABAC_CERT_INVALID; |
---|
| 269 | } |
---|
| 270 | catch (ABACException ae) { |
---|
[418b586] | 271 | return ABAC_CERT_INVALID; |
---|
| 272 | } |
---|
| 273 | return ABAC_CERT_SUCCESS; |
---|
| 274 | } |
---|
| 275 | |
---|
[e36ea1d] | 276 | /** |
---|
| 277 | * Determine if prinicpal possesses role in the current context. If so, |
---|
| 278 | * return a proof of that, otherwise return a partial proof of it. |
---|
| 279 | * @param role a String encoding the role to check for. |
---|
| 280 | * @param principal a String with the principal ID in it. |
---|
| 281 | * @return a Context.QueryResult containing the result. |
---|
| 282 | */ |
---|
[1a80501] | 283 | public QueryResult query(String role, String principal) { |
---|
[418b586] | 284 | derive_implied_edges(); |
---|
| 285 | |
---|
| 286 | Query q = new Query(g); |
---|
| 287 | Graph<Role, Credential> rg = q.run(role, principal); |
---|
[343b9e3] | 288 | TreeSet<Credential> tr = new TreeSet<Credential>(); |
---|
[1a80501] | 289 | |
---|
[343b9e3] | 290 | for ( Credential c: rg.getEdges()) |
---|
[736a573] | 291 | tr.add(c); |
---|
[343b9e3] | 292 | |
---|
| 293 | return new QueryResult(tr, q.successful()); |
---|
[418b586] | 294 | } |
---|
| 295 | |
---|
| 296 | /** |
---|
[e36ea1d] | 297 | * Return a collection of the credentials in the graph.s |
---|
| 298 | * @return a collection of the credentials in the graph. |
---|
[418b586] | 299 | */ |
---|
| 300 | public Collection<Credential> credentials() { |
---|
| 301 | Collection<Credential> creds = new HashSet<Credential>(); |
---|
| 302 | |
---|
[ad24705] | 303 | // only non-derived edges |
---|
[418b586] | 304 | for (Credential cred : g.getEdges()) |
---|
[ad24705] | 305 | if (!derived_edges.contains(cred)) |
---|
[418b586] | 306 | creds.add(cred); |
---|
| 307 | |
---|
| 308 | return creds; |
---|
| 309 | } |
---|
| 310 | |
---|
[388a3d7] | 311 | /** |
---|
| 312 | * Return all the Identities known in this context. A jabac extension. |
---|
| 313 | * @return all the Identities known in this context. |
---|
| 314 | */ |
---|
| 315 | public Collection<Identity> identities() { |
---|
| 316 | return m_identities; |
---|
| 317 | } |
---|
| 318 | |
---|
[e36ea1d] | 319 | /** |
---|
| 320 | * Returns true if the given Identity is known in this Context. A jabac |
---|
| 321 | * extension. |
---|
| 322 | * @param i the Identity to look for |
---|
| 323 | * @return a boolean, true if the Identity is known. |
---|
| 324 | */ |
---|
[238717d] | 325 | public boolean knowsIdentity(Identity i) { return m_identities.contains(i);} |
---|
[e36ea1d] | 326 | /** |
---|
| 327 | * Returns true if an Identity with the given string representation is |
---|
| 328 | * known in this Context. A jabac extension. |
---|
| 329 | * @param k the string representing the Identity to look for |
---|
| 330 | * @return a boolean, true if the Identity is known. |
---|
| 331 | */ |
---|
[53f5c27] | 332 | public boolean knowsKeyID(String k) { |
---|
| 333 | boolean known = false; |
---|
[388a3d7] | 334 | for (Identity i: m_identities) |
---|
[53f5c27] | 335 | if (k.equals(i.getKeyID())) return true; |
---|
| 336 | return false; |
---|
[418b586] | 337 | } |
---|
| 338 | |
---|
[53f5c27] | 339 | |
---|
[418b586] | 340 | /** |
---|
| 341 | * Add a credential to the graph. |
---|
[e36ea1d] | 342 | * @param cred the Credential to add |
---|
[418b586] | 343 | */ |
---|
[56ec930] | 344 | protected void add_credential(Credential cred) { |
---|
[418b586] | 345 | Role tail = cred.tail(); |
---|
| 346 | Role head = cred.head(); |
---|
| 347 | |
---|
[f25a7ff] | 348 | // explicitly add the vertices, to avoid a null pointer exception |
---|
[418b586] | 349 | if ( !g.containsVertex(head)) |
---|
| 350 | g.addVertex(head); |
---|
| 351 | if ( !g.containsVertex(tail)) |
---|
| 352 | g.addVertex(tail); |
---|
| 353 | |
---|
| 354 | if (!g.containsEdge(cred)) |
---|
| 355 | g.addEdge(cred, tail, head); |
---|
| 356 | |
---|
| 357 | // add the prereqs of an intersection to the graph |
---|
[8fdbdac] | 358 | if (tail.is_intersection()) { |
---|
| 359 | try { |
---|
| 360 | for (Role prereq : tail.prereqs()) |
---|
| 361 | g.addVertex(prereq); |
---|
| 362 | } catch (ABACException ignored) { } |
---|
| 363 | } |
---|
[418b586] | 364 | |
---|
[120b7d8] | 365 | /* If the credential includes new names for identities, incorporate |
---|
| 366 | * them. */ |
---|
| 367 | if ( cred instanceof MapsKeyIDs ) { |
---|
| 368 | MapsKeyIDs km = (MapsKeyIDs) cred; |
---|
| 369 | |
---|
| 370 | keyMap.merge(km.getMapping(), true); |
---|
| 371 | } |
---|
| 372 | |
---|
| 373 | |
---|
[418b586] | 374 | dirty = true; |
---|
| 375 | } |
---|
| 376 | |
---|
| 377 | /** |
---|
| 378 | * Remove a credential from the graph. |
---|
[e36ea1d] | 379 | * @param cred the Credential to remove |
---|
[418b586] | 380 | */ |
---|
[56ec930] | 381 | protected void remove_credential(Credential cred) { |
---|
[418b586] | 382 | if (g.containsEdge(cred)) |
---|
| 383 | g.removeEdge(cred); |
---|
| 384 | dirty = true; |
---|
| 385 | } |
---|
| 386 | |
---|
| 387 | /** |
---|
| 388 | * Add a role w/o an edge |
---|
[e36ea1d] | 389 | * @param v the Role to add |
---|
[418b586] | 390 | */ |
---|
[56ec930] | 391 | protected void add_vertex(Role v) { |
---|
[418b586] | 392 | if (!g.containsVertex(v)) { |
---|
| 393 | g.addVertex(v); |
---|
| 394 | dirty = true; |
---|
| 395 | } |
---|
| 396 | } |
---|
| 397 | |
---|
[e36ea1d] | 398 | /** |
---|
| 399 | * Remove a role and connected edges. |
---|
| 400 | * @param v the Role to remove |
---|
| 401 | */ |
---|
[56ec930] | 402 | protected void remove_vertex(Role v) { |
---|
[418b586] | 403 | if (g.containsVertex(v)) { |
---|
| 404 | g.removeVertex(v); |
---|
| 405 | dirty = true; |
---|
| 406 | } |
---|
| 407 | } |
---|
| 408 | |
---|
| 409 | /** |
---|
| 410 | * Derive the implied edges in the graph, according to RT0 derivation rules. |
---|
| 411 | * They are added to this graph. See "Distributed Credential Chain Discovery |
---|
| 412 | * in Trust Management" by Ninghui Li et al. for details. Note that a |
---|
| 413 | * derived linking edge can imply a new intersection edge and vice versa. |
---|
| 414 | * Therefore we iteratively derive edges, giving up when an iteration |
---|
| 415 | * produces 0 new edges. |
---|
| 416 | */ |
---|
| 417 | protected synchronized void derive_implied_edges() { |
---|
| 418 | // nothing to do on a clean graph |
---|
| 419 | if (!dirty) |
---|
| 420 | return; |
---|
| 421 | |
---|
| 422 | clear_old_edges(); |
---|
| 423 | |
---|
| 424 | // iteratively derive links. continue as long as new links are added |
---|
| 425 | while (derive_links_iter() > 0) |
---|
| 426 | ; |
---|
| 427 | dirty = false; |
---|
| 428 | } |
---|
| 429 | |
---|
| 430 | /** |
---|
| 431 | * Single iteration of deriving implied edges. Returns the number of new |
---|
| 432 | * links added. |
---|
[e36ea1d] | 433 | * @return the number of new links added. |
---|
[418b586] | 434 | */ |
---|
| 435 | protected int derive_links_iter() { |
---|
| 436 | int count = 0; |
---|
| 437 | |
---|
| 438 | /* for every node in the graph.. */ |
---|
| 439 | for (Role vertex : g.getVertices()) { |
---|
| 440 | if (vertex.is_intersection()) { |
---|
| 441 | // for each prereq edge: |
---|
| 442 | // find set of principals that have the prereq |
---|
[f25a7ff] | 443 | // find the intersection of all sets (i.e., principals |
---|
| 444 | // that satisfy all prereqs) |
---|
[418b586] | 445 | // for each principal in intersection: |
---|
| 446 | // add derived edge |
---|
| 447 | |
---|
| 448 | Set<Role> principals = null; |
---|
[8fdbdac] | 449 | try { |
---|
| 450 | for (Role prereq : vertex.prereqs()) { |
---|
| 451 | Set<Role> cur_principals = pq.find_principals(prereq); |
---|
| 452 | |
---|
| 453 | if (principals == null) |
---|
| 454 | principals = cur_principals; |
---|
| 455 | else |
---|
| 456 | // no, they couldn't just call it "intersection" |
---|
| 457 | principals.retainAll(cur_principals); |
---|
| 458 | |
---|
| 459 | if (principals.size() == 0) |
---|
| 460 | break; |
---|
| 461 | } |
---|
| 462 | } |
---|
| 463 | catch (ABACException ignored) { } |
---|
[418b586] | 464 | |
---|
| 465 | // add em |
---|
| 466 | for (Role principal : principals) |
---|
| 467 | if (add_derived_edge(vertex, principal)) |
---|
| 468 | ++count; |
---|
| 469 | } |
---|
| 470 | |
---|
| 471 | else if (vertex.is_linking()) { |
---|
| 472 | // make the rest of the code a bit clearer |
---|
| 473 | Role A_r1_r2 = vertex; |
---|
| 474 | |
---|
| 475 | Role A_r1 = new Role(A_r1_r2.A_r1()); |
---|
| 476 | String r2 = A_r1_r2.r2(); |
---|
| 477 | |
---|
| 478 | /* locate the node A.r1 */ |
---|
[f25a7ff] | 479 | if (!g.containsVertex(A_r1)) continue; |
---|
[418b586] | 480 | |
---|
| 481 | /* for each B that satisfies A_r1 */ |
---|
| 482 | for (Role principal : pq.find_principals(A_r1)) { |
---|
| 483 | Role B_r2 = new Role(principal + "." + r2); |
---|
| 484 | if (!g.containsVertex(B_r2)) continue; |
---|
| 485 | |
---|
| 486 | if (add_derived_edge(A_r1_r2, B_r2)) |
---|
| 487 | ++count; |
---|
| 488 | } |
---|
| 489 | } |
---|
| 490 | } |
---|
| 491 | |
---|
| 492 | return count; |
---|
| 493 | } |
---|
| 494 | |
---|
| 495 | /** |
---|
| 496 | * Add a derived edge in the graph. Returns true only if the edge does not |
---|
| 497 | * exist. |
---|
[461edba] | 498 | * @param head the head of the link to add |
---|
| 499 | * @param tail the tail of the link to add |
---|
[e36ea1d] | 500 | * @return a boolean, true if an edge has been added |
---|
[418b586] | 501 | */ |
---|
| 502 | protected boolean add_derived_edge(Role head, Role tail) { |
---|
| 503 | // edge exists: return false |
---|
| 504 | if (g.findEdge(tail, head) != null) |
---|
| 505 | return false; |
---|
| 506 | |
---|
| 507 | // add the new edge |
---|
[3612811] | 508 | Credential derived_edge = new InternalCredential(head, tail); |
---|
[418b586] | 509 | derived_edges.add(derived_edge); |
---|
| 510 | g.addEdge(derived_edge, tail, head); |
---|
| 511 | |
---|
| 512 | return true; |
---|
| 513 | } |
---|
| 514 | |
---|
| 515 | /** |
---|
| 516 | * Clear the derived edges that currently exist in the graph. This is done |
---|
| 517 | * before the edges are rederived. The derived edges in filtered graphs are |
---|
| 518 | * also cleared. |
---|
| 519 | */ |
---|
| 520 | protected void clear_old_edges() { |
---|
| 521 | for (Credential i: derived_edges) |
---|
| 522 | g.removeEdge(i); |
---|
| 523 | derived_edges = new HashSet<Credential>(); |
---|
| 524 | } |
---|
[3f928b0] | 525 | |
---|
[418b586] | 526 | /** |
---|
| 527 | * Put the Identity into the set of ids used to validate certificates. |
---|
| 528 | * Also put the keyID and name into the translation mappings used by Roles |
---|
| 529 | * to pretty print. In the role mapping, if multiple ids use the same |
---|
| 530 | * common name they are disambiguated. Only one entry for keyid is |
---|
| 531 | * allowed. |
---|
[6432e35] | 532 | * @param id the Identity to add |
---|
[418b586] | 533 | */ |
---|
| 534 | protected void addIdentity(Identity id) { |
---|
[3f928b0] | 535 | if (m_identities.contains(id)) |
---|
[7f614c1] | 536 | return; |
---|
[388a3d7] | 537 | m_identities.add(id); |
---|
[4fd9436] | 538 | if (id.getName() != null && id.getKeyID() != null) |
---|
| 539 | keyMap.addNickname(id.getKeyID(), id.getName()); |
---|
[418b586] | 540 | } |
---|
| 541 | |
---|
[e36ea1d] | 542 | /** |
---|
| 543 | * Expand menmonic names in a Role string, e.g. the CN of the issuer |
---|
| 544 | * certificate, into the full key ID. Used internally by Roles to provide |
---|
| 545 | * transparent use of mnemonics |
---|
| 546 | * @param s the string to expand |
---|
| 547 | * @return the String after expansion. |
---|
| 548 | */ |
---|
[4fd9436] | 549 | String expandKeyID(String s) { return keyMap.expandKeyID(s); } |
---|
| 550 | |
---|
[e36ea1d] | 551 | /** |
---|
| 552 | * Convert key IDs to menmonic names in a Role string. The inverse of |
---|
| 553 | * expandKeyID. |
---|
| 554 | * @param s the string to expand |
---|
| 555 | * @return the String after expansion. |
---|
| 556 | */ |
---|
[4fd9436] | 557 | String expandNickname(String s) { return keyMap.expandNickname(s); } |
---|
[418b586] | 558 | |
---|
[39526ce] | 559 | /** |
---|
| 560 | * Read the current ZipEntry's bytes from z. Tedious because there's no |
---|
| 561 | * way to reliably tell how big the entry is, so we have to rely on a |
---|
| 562 | * simple expanding array read of the bytes. |
---|
[461edba] | 563 | * @param z the stream to operate on |
---|
| 564 | * @return the raw data bytes |
---|
| 565 | * @throws IOException if reading fails |
---|
[39526ce] | 566 | */ |
---|
| 567 | protected byte[] readCurrentZipEntry(ZipInputStream z) throws IOException { |
---|
| 568 | final int bsize = 4096; |
---|
| 569 | byte[] buf = new byte[bsize]; |
---|
| 570 | byte[] rv = new byte[0]; |
---|
| 571 | int r = 0; |
---|
| 572 | |
---|
| 573 | // z.read returns -1 at the end of entry |
---|
| 574 | while ((r = z.read(buf, 0, bsize)) != -1 ) { |
---|
| 575 | byte[] b = new byte[rv.length + r]; |
---|
| 576 | |
---|
| 577 | System.arraycopy(rv, 0, b, 0, rv.length); |
---|
| 578 | System.arraycopy(buf, 0, b, rv.length, r); |
---|
| 579 | rv = b; |
---|
| 580 | } |
---|
| 581 | return rv; |
---|
| 582 | } |
---|
| 583 | |
---|
[418b586] | 584 | /** |
---|
| 585 | * Import a zip file. First import all the identities |
---|
| 586 | * (pem), then the credentials (der) into the credential graph then any |
---|
| 587 | * alias files into the two maps. If keys is not null, any key pairs in |
---|
| 588 | * PEM files are put in there. If errors is not null, errors reading files |
---|
[6432e35] | 589 | * are added indexed by filename. This is a jabac extension. |
---|
[39526ce] | 590 | * @param s the InputStream to read |
---|
[e36ea1d] | 591 | * @param keys a Collection into which to insert unmatched keys |
---|
| 592 | * @param errors a Map from entry name to generated exception |
---|
| 593 | * @throws IOException if the file is unreadable. Per entry exceptions are |
---|
| 594 | * returned in the errors parameter. |
---|
[418b586] | 595 | */ |
---|
[39526ce] | 596 | public void load_zip(InputStream s, Collection<KeyPair> keys, |
---|
[418b586] | 597 | Map<String, Exception> errors) throws IOException { |
---|
[39526ce] | 598 | Map<String, byte[]> derEntries = new HashMap<String, byte[]>(); |
---|
[418b586] | 599 | Map<String, Identity> ids = new TreeMap<String, Identity>(); |
---|
| 600 | Map<String, KeyPair> kps = new TreeMap<String, KeyPair>(); |
---|
[39526ce] | 601 | int entries = 0; |
---|
[418b586] | 602 | |
---|
[39526ce] | 603 | ZipInputStream z = new ZipInputStream(s); |
---|
[418b586] | 604 | |
---|
[39526ce] | 605 | for (ZipEntry ze = z.getNextEntry(); ze != null; ze = z.getNextEntry()){ |
---|
[418b586] | 606 | try { |
---|
[39526ce] | 607 | entries++; |
---|
| 608 | byte[] buf = readCurrentZipEntry(z); |
---|
[418b586] | 609 | PEMReader r = new PEMReader( |
---|
[39526ce] | 610 | new InputStreamReader(new ByteArrayInputStream(buf))); |
---|
[418b586] | 611 | Object o = readPEM(r); |
---|
| 612 | |
---|
| 613 | if ( o != null ) { |
---|
| 614 | if (o instanceof Identity) { |
---|
| 615 | Identity i = (Identity) o; |
---|
| 616 | String kid = i.getKeyID(); |
---|
| 617 | |
---|
| 618 | if (kps.containsKey(kid) ) { |
---|
| 619 | i.setKeyPair(kps.get(kid)); |
---|
| 620 | kps.remove(kid); |
---|
| 621 | } |
---|
| 622 | else if (i.getKeyPair() == null ) |
---|
| 623 | ids.put(i.getKeyID(), i); |
---|
| 624 | |
---|
[6432e35] | 625 | load_id_chunk(i); |
---|
[418b586] | 626 | } |
---|
| 627 | else if (o instanceof KeyPair ) { |
---|
| 628 | KeyPair kp = (KeyPair) o; |
---|
[0595372] | 629 | String kid = extractKeyID(kp.getPublic()); |
---|
[418b586] | 630 | |
---|
| 631 | if (ids.containsKey(kid)) { |
---|
| 632 | Identity i = ids.get(kid); |
---|
| 633 | |
---|
| 634 | i.setKeyPair(kp); |
---|
| 635 | ids.remove(kid); |
---|
| 636 | } |
---|
| 637 | else { |
---|
| 638 | kps.put(kid, kp); |
---|
| 639 | } |
---|
| 640 | } |
---|
| 641 | } |
---|
| 642 | else { |
---|
| 643 | // Not a PEM file |
---|
[39526ce] | 644 | derEntries.put(ze.getName(),buf); |
---|
[418b586] | 645 | continue; |
---|
| 646 | } |
---|
| 647 | } |
---|
| 648 | catch (Exception e ) { |
---|
[39526ce] | 649 | if (errors != null ) errors.put(ze.getName(), e); |
---|
[418b586] | 650 | } |
---|
| 651 | } |
---|
| 652 | |
---|
[39526ce] | 653 | for ( String k: derEntries.keySet() ) { |
---|
[418b586] | 654 | try { |
---|
[d06c453] | 655 | Credential[] creds = credentialFactory.parseCredential( |
---|
[39526ce] | 656 | new ByteArrayInputStream(derEntries.get(k)), |
---|
[3612811] | 657 | m_identities); |
---|
| 658 | for (Credential c: creds) |
---|
| 659 | add_credential(c); |
---|
[418b586] | 660 | } |
---|
| 661 | catch (Exception e ) { |
---|
[39526ce] | 662 | if (errors != null ) errors.put(k, e); |
---|
[418b586] | 663 | } |
---|
| 664 | } |
---|
[39526ce] | 665 | |
---|
| 666 | if (entries == 0) |
---|
| 667 | throw new IOException("Not a ZIP file (or empty ZIP file)"); |
---|
| 668 | } |
---|
| 669 | /** |
---|
| 670 | * Equivalent to load_zip(s, null, null). |
---|
| 671 | * @param s the InputStream to read |
---|
| 672 | * @throws IOException if the file is unreadable. To see per-entry |
---|
| 673 | * exceptions use a signature with the errors parameter |
---|
| 674 | */ |
---|
| 675 | public void load_zip(InputStream s) |
---|
| 676 | throws IOException { |
---|
| 677 | load_zip(s, null, null); |
---|
| 678 | } |
---|
| 679 | /** |
---|
| 680 | * Equivalent to load_zip(s, null, errors). |
---|
| 681 | * @param s the InputStream to read |
---|
| 682 | * @param errors a Map from entry name to generated exception |
---|
| 683 | * @throws IOException if the file is unreadable. Per entry exceptions are |
---|
| 684 | * returned in the errors parameter. |
---|
| 685 | */ |
---|
| 686 | public void load_zip(InputStream s, |
---|
| 687 | Map<String, Exception> errors) throws IOException { |
---|
| 688 | load_zip(s, null, errors); |
---|
[418b586] | 689 | } |
---|
[e36ea1d] | 690 | /** |
---|
[39526ce] | 691 | * Equivalent to load_zip(s, keys, null). |
---|
| 692 | * @param s the InputStream to read |
---|
| 693 | * @param keys a Collection into which to insert unmatched keys |
---|
| 694 | * @throws IOException if the file is unreadable. To see per-entry |
---|
| 695 | * exceptions use a signature with the errors parameter |
---|
| 696 | */ |
---|
| 697 | public void load_zip(InputStream s, |
---|
| 698 | Collection<KeyPair> keys) throws IOException { |
---|
| 699 | load_zip(s, keys, null); |
---|
| 700 | } |
---|
| 701 | |
---|
| 702 | /** |
---|
| 703 | * Loads a zip file. Equivalent to |
---|
| 704 | * load_zip(new FileInputStream(zf), keys, errors). |
---|
| 705 | * @param zf the File to read |
---|
| 706 | * @param keys a Collection into which to insert unmatched keys |
---|
| 707 | * @param errors a Map from entry name to generated exception |
---|
| 708 | * @throws IOException if the file is unreadable. Per entry exceptions are |
---|
| 709 | * returned in the errors parameter. |
---|
| 710 | */ |
---|
| 711 | public void load_zip(File zf, Collection<KeyPair> keys, |
---|
| 712 | Map<String, Exception> errors) throws IOException { |
---|
| 713 | load_zip(new FileInputStream(zf), keys, errors); |
---|
| 714 | } |
---|
| 715 | /** |
---|
| 716 | * Equivalent to load_zip(d, null, null). |
---|
[e36ea1d] | 717 | * @param d the File to read |
---|
| 718 | * @throws IOException if the file is unreadable. To see per-entry |
---|
| 719 | * exceptions use a signature with the errors parameter |
---|
| 720 | */ |
---|
[39526ce] | 721 | public void load_zip(File d) |
---|
[418b586] | 722 | throws IOException { |
---|
[39526ce] | 723 | load_zip(d, null, null); |
---|
[418b586] | 724 | } |
---|
[e36ea1d] | 725 | /** |
---|
[39526ce] | 726 | * Equivalent to load_zip(d, null, errors). |
---|
[e36ea1d] | 727 | * @param d the File to read |
---|
| 728 | * @param errors a Map from entry name to generated exception |
---|
| 729 | * @throws IOException if the file is unreadable. Per entry exceptions are |
---|
| 730 | * returned in the errors parameter. |
---|
| 731 | */ |
---|
[39526ce] | 732 | public void load_zip(File d, |
---|
[418b586] | 733 | Map<String, Exception> errors) throws IOException { |
---|
[39526ce] | 734 | load_zip(d, null, errors); |
---|
[418b586] | 735 | } |
---|
[e36ea1d] | 736 | /** |
---|
[39526ce] | 737 | * Equivalent to load_zip(d, keys, null). |
---|
[e36ea1d] | 738 | * @param d the File to read |
---|
| 739 | * @param keys a Collection into which to insert unmatched keys |
---|
| 740 | * @throws IOException if the file is unreadable. To see per-entry |
---|
| 741 | * exceptions use a signature with the errors parameter |
---|
| 742 | */ |
---|
[39526ce] | 743 | public void load_zip(File d, |
---|
[418b586] | 744 | Collection<KeyPair> keys) throws IOException { |
---|
[39526ce] | 745 | load_zip(d, keys, null); |
---|
[418b586] | 746 | } |
---|
| 747 | |
---|
[e36ea1d] | 748 | /** |
---|
| 749 | * Read a PEM file that contains an X509 Certificate, a key pair, or both. |
---|
| 750 | * If a cert is present it is converted into an Identity. A key pair is |
---|
| 751 | * returned as a java.security.KeyPair and both are returned as an Identity |
---|
| 752 | * with an associated key pair. |
---|
| 753 | * @param r a PEMReader from which to read |
---|
| 754 | * @return an object encoding the contents (as above) |
---|
| 755 | * @throws IOException for an unreadable or badly formated input |
---|
| 756 | */ |
---|
[418b586] | 757 | protected Object readPEM(PEMReader r) throws IOException { |
---|
| 758 | Identity i = null; |
---|
| 759 | KeyPair keys = null; |
---|
| 760 | Object o = null; |
---|
| 761 | |
---|
| 762 | while ( (o = r.readObject()) != null ) { |
---|
| 763 | if (o instanceof X509Certificate) { |
---|
| 764 | if ( i == null ) { |
---|
| 765 | try { |
---|
| 766 | i = new Identity((X509Certificate)o); |
---|
| 767 | } |
---|
| 768 | catch (Exception e) { |
---|
| 769 | // Translate Idenitiy exceptions to IOException |
---|
| 770 | throw new IOException(e); |
---|
| 771 | } |
---|
| 772 | if (keys != null ) { |
---|
| 773 | i.setKeyPair(keys); |
---|
| 774 | keys = null; |
---|
| 775 | } |
---|
| 776 | } |
---|
| 777 | else throw new IOException("Two certificates"); |
---|
| 778 | } |
---|
| 779 | else if (o instanceof KeyPair ) { |
---|
| 780 | if ( i != null ) i.setKeyPair((KeyPair) o); |
---|
| 781 | else keys = (KeyPair) o; |
---|
| 782 | } |
---|
| 783 | else { |
---|
| 784 | throw new IOException("Unexpected PEM object: " + |
---|
| 785 | o.getClass().getName()); |
---|
| 786 | } |
---|
| 787 | } |
---|
| 788 | |
---|
| 789 | if ( i != null ) return i; |
---|
| 790 | else if ( keys != null) return keys; |
---|
| 791 | else return null; |
---|
| 792 | } |
---|
| 793 | |
---|
| 794 | /** |
---|
| 795 | * Import a directory full of files. First import all the identities |
---|
| 796 | * (pem), then the credentials (der) into the credential graph then any |
---|
| 797 | * alias files into the two maps. If keys is not null, any key pairs in |
---|
| 798 | * PEM files are put in there. If errors is not null, errors reading files |
---|
[6432e35] | 799 | * are added indexed by filename. This behaves slightly differently from |
---|
| 800 | * the load_directory description in the general libabac documentation. |
---|
[e36ea1d] | 801 | * @param d the File to read. If it is a directory its contents are read |
---|
| 802 | * @param keys a Collection into which to insert unmatched keys |
---|
| 803 | * @param errors a Map from entry name to generated exception |
---|
[418b586] | 804 | */ |
---|
[6432e35] | 805 | public void load_directory(File d, Collection<KeyPair> keys, |
---|
[418b586] | 806 | Map<String, Exception> errors) { |
---|
| 807 | Vector<File> derFiles = new Vector<File>(); |
---|
| 808 | Collection<File> files = new Vector<File>(); |
---|
| 809 | Map<String, Identity> ids = new TreeMap<String, Identity>(); |
---|
| 810 | Map<String, KeyPair> kps = new TreeMap<String, KeyPair>(); |
---|
| 811 | |
---|
| 812 | if (d.isDirectory() ) |
---|
| 813 | for (File f : d.listFiles()) |
---|
| 814 | files.add(f); |
---|
| 815 | else files.add(d); |
---|
| 816 | |
---|
| 817 | for (File f: files ) { |
---|
| 818 | try { |
---|
| 819 | PEMReader r = new PEMReader(new FileReader(f)); |
---|
| 820 | Object o = readPEM(r); |
---|
| 821 | |
---|
| 822 | if ( o != null ) { |
---|
| 823 | if (o instanceof Identity) { |
---|
| 824 | Identity i = (Identity) o; |
---|
| 825 | String kid = i.getKeyID(); |
---|
| 826 | |
---|
| 827 | if (kps.containsKey(kid) ) { |
---|
| 828 | i.setKeyPair(kps.get(kid)); |
---|
| 829 | kps.remove(kid); |
---|
| 830 | } |
---|
| 831 | else if (i.getKeyPair() == null ) |
---|
| 832 | ids.put(i.getKeyID(), i); |
---|
| 833 | |
---|
[6432e35] | 834 | load_id_chunk(i); |
---|
[418b586] | 835 | } |
---|
| 836 | else if (o instanceof KeyPair ) { |
---|
| 837 | KeyPair kp = (KeyPair) o; |
---|
[0595372] | 838 | String kid = extractKeyID(kp.getPublic()); |
---|
[418b586] | 839 | |
---|
| 840 | if (ids.containsKey(kid)) { |
---|
| 841 | Identity i = ids.get(kid); |
---|
| 842 | |
---|
| 843 | i.setKeyPair(kp); |
---|
| 844 | ids.remove(kid); |
---|
| 845 | } |
---|
| 846 | else { |
---|
| 847 | kps.put(kid, kp); |
---|
| 848 | } |
---|
| 849 | } |
---|
| 850 | } |
---|
| 851 | else { |
---|
| 852 | // Not a PEM file |
---|
| 853 | derFiles.add(f); |
---|
| 854 | continue; |
---|
| 855 | } |
---|
| 856 | } |
---|
| 857 | catch (Exception e ) { |
---|
| 858 | if (errors != null ) errors.put(f.getName(), e); |
---|
| 859 | } |
---|
| 860 | } |
---|
| 861 | |
---|
| 862 | for ( File f : derFiles ) { |
---|
| 863 | try { |
---|
[d06c453] | 864 | Credential[] creds = credentialFactory.parseCredential(f, |
---|
[3612811] | 865 | m_identities); |
---|
| 866 | for (Credential c: creds) |
---|
| 867 | add_credential(c); |
---|
[418b586] | 868 | } |
---|
| 869 | catch (Exception e ) { |
---|
| 870 | if (errors != null ) errors.put(f.getName(), e); |
---|
| 871 | } |
---|
| 872 | } |
---|
| 873 | } |
---|
[e36ea1d] | 874 | /** |
---|
[6432e35] | 875 | * Equivalent to load_directory(d, null, null). |
---|
[e36ea1d] | 876 | * @param d the File to read. If it is a directory its contents are read |
---|
| 877 | */ |
---|
[6432e35] | 878 | public void load_directory(File d) { |
---|
| 879 | load_directory(d, null, null); |
---|
[418b586] | 880 | } |
---|
[e36ea1d] | 881 | /** |
---|
[6432e35] | 882 | * Equivalent to load_directory(d, null, null). |
---|
[e36ea1d] | 883 | * @param d the File to read. If it is a directory its contents are read |
---|
| 884 | * @param errors a Map from entry name to generated exception |
---|
| 885 | */ |
---|
[6432e35] | 886 | public void load_directory(File d, Map<String, Exception> errors) { |
---|
| 887 | load_directory(d, null, errors); |
---|
[418b586] | 888 | } |
---|
[e36ea1d] | 889 | /** |
---|
[6432e35] | 890 | * Equivalent to load_directory(d, null, null). |
---|
[e36ea1d] | 891 | * @param d the File to read. If it is a directory its contents are read |
---|
| 892 | * @param keys a Collection into which to insert unmatched keys |
---|
| 893 | */ |
---|
[6432e35] | 894 | public void load_directory(File d, Collection<KeyPair> keys) { |
---|
| 895 | load_directory(d, keys, null); |
---|
[418b586] | 896 | } |
---|
| 897 | |
---|
[ad24705] | 898 | /** |
---|
| 899 | * Load from a simple rt0 text format. A jabac extension. The format is |
---|
[461edba] | 900 | * <pre> |
---|
| 901 | * # comments |
---|
| 902 | * role <- role |
---|
| 903 | * </pre> |
---|
[ad24705] | 904 | * |
---|
| 905 | * Spaces are not significant around the arrow and the tail can be as long |
---|
| 906 | * as needed. |
---|
[39526ce] | 907 | * @param s the InputStream to load |
---|
| 908 | * @throws IOException if there is an error getting the file open or in |
---|
| 909 | * format |
---|
[ad24705] | 910 | */ |
---|
[39526ce] | 911 | public void load_rt0(InputStream s) |
---|
[ad24705] | 912 | throws IOException { |
---|
| 913 | Pattern comment = Pattern.compile("(^\\s*#|^\\s*$)"); |
---|
[39526ce] | 914 | Pattern rule = Pattern.compile("([\\w\\.]+)\\s*<-+\\s*(.+)"); |
---|
| 915 | LineNumberReader r = new LineNumberReader(new InputStreamReader(s)); |
---|
[ad24705] | 916 | String line = null; |
---|
| 917 | |
---|
| 918 | while ((line = r.readLine()) != null) { |
---|
| 919 | Matcher cm = comment.matcher(line); |
---|
| 920 | Matcher rm = rule.matcher(line); |
---|
| 921 | |
---|
| 922 | if (cm.find()) continue; |
---|
| 923 | if (rm.find()) |
---|
[3612811] | 924 | add_credential(new InternalCredential(new Role(rm.group(1)), |
---|
[ad24705] | 925 | new Role(rm.group(2)))); |
---|
[39526ce] | 926 | else |
---|
[3a83fda] | 927 | throw new IOException("Unexpected format: line " + |
---|
[39526ce] | 928 | r.getLineNumber()); |
---|
[ad24705] | 929 | } |
---|
| 930 | } |
---|
| 931 | /** |
---|
[39526ce] | 932 | * Equivalent to load_rt0(new FileInputStream(f) |
---|
[ad24705] | 933 | * @param f the File to load |
---|
| 934 | * @throws IOException if there is an error getting the file open |
---|
| 935 | */ |
---|
[39526ce] | 936 | public void load_rt0(File f) throws IOException { |
---|
| 937 | load_rt0(new FileInputStream(f)); |
---|
[ad24705] | 938 | } |
---|
| 939 | |
---|
| 940 | |
---|
[e36ea1d] | 941 | /** |
---|
| 942 | * Write the certificates that make up the context as a zip file, with an |
---|
[3d13073] | 943 | * entry for each credential or identity. The files are all zipped in a |
---|
| 944 | * directory derived from the filename. |
---|
[39526ce] | 945 | * @param s the OutputStream to write |
---|
[e36ea1d] | 946 | * @param allIDs a boolean, if true write certificates for all Identities, |
---|
| 947 | * whether used in signing a credential or not. |
---|
| 948 | * @param withPrivateKeys a boolean, if true write the Identities as PEM |
---|
| 949 | * file containing both the certificate and the private keys. |
---|
[3d13073] | 950 | * @throws IOException if there is a problem writing the file. |
---|
[e36ea1d] | 951 | */ |
---|
[39526ce] | 952 | public void write_zip(OutputStream s, boolean allIDs, |
---|
| 953 | boolean withPrivateKeys) throws IOException { |
---|
| 954 | ZipOutputStream z = new ZipOutputStream(s); |
---|
[388a3d7] | 955 | Set<Identity> ids = allIDs ? m_identities : new TreeSet<Identity>(); |
---|
[39526ce] | 956 | String baseDir = "creds"; |
---|
[b6e7c18] | 957 | int idx = baseDir.indexOf('.'); |
---|
| 958 | |
---|
| 959 | |
---|
| 960 | if (idx != -1) |
---|
| 961 | baseDir = baseDir.substring(0, idx); |
---|
[418b586] | 962 | |
---|
| 963 | int n = 0; |
---|
| 964 | for (Credential c: credentials()) { |
---|
[b6e7c18] | 965 | z.putNextEntry(new ZipEntry(baseDir + File.separator + |
---|
[343b9e3] | 966 | "attr" + n++ + c.getSuffix())); |
---|
[418b586] | 967 | c.write(z); |
---|
| 968 | z.closeEntry(); |
---|
[d69593c] | 969 | if ( c.issuer() != null && !allIDs) ids.add(c.issuer()); |
---|
[418b586] | 970 | } |
---|
| 971 | for (Identity i: ids) { |
---|
[b6e7c18] | 972 | z.putNextEntry(new ZipEntry(baseDir + File.separator + |
---|
| 973 | i.getName() + ".pem")); |
---|
[418b586] | 974 | i.write(z); |
---|
| 975 | if (withPrivateKeys) |
---|
| 976 | i.writePrivateKey(z); |
---|
| 977 | z.closeEntry(); |
---|
| 978 | } |
---|
| 979 | z.close(); |
---|
| 980 | } |
---|
[39526ce] | 981 | /** |
---|
| 982 | * Equivalent to |
---|
| 983 | * write_zip(new FileOutputStream(f), allIDs, withPrivateKeys). |
---|
| 984 | * @param f the File to write |
---|
| 985 | * @param allIDs a boolean, if true write certificates for all Identities, |
---|
| 986 | * whether used in signing a credential or not. |
---|
| 987 | * @param withPrivateKeys a boolean, if true write the Identities as PEM |
---|
| 988 | * file containing both the certificate and the private keys. |
---|
| 989 | * @throws IOException if there is a problem writing the file. |
---|
| 990 | */ |
---|
| 991 | public void write_zip(File f, boolean allIDs, boolean withPrivateKeys) |
---|
| 992 | throws IOException { |
---|
| 993 | write_zip(new FileOutputStream(f), allIDs, withPrivateKeys); |
---|
| 994 | } |
---|
[418b586] | 995 | |
---|
[ad24705] | 996 | /** |
---|
| 997 | * Write to a simple rt0 text format. A jabac extension. |
---|
| 998 | * The format is |
---|
[461edba] | 999 | * <pre> |
---|
| 1000 | * role <- role |
---|
| 1001 | * </pre> |
---|
[ad24705] | 1002 | * |
---|
| 1003 | * @param w a Writer to print on |
---|
| 1004 | * @param useKeyIDs a boolean, true to print key IDs not mnemonics |
---|
| 1005 | */ |
---|
| 1006 | public void write_rt0(Writer w, boolean useKeyIDs) { |
---|
| 1007 | PrintWriter pw = w instanceof PrintWriter ? |
---|
| 1008 | (PrintWriter) w : new PrintWriter(w); |
---|
| 1009 | |
---|
| 1010 | for (Credential c: credentials()) |
---|
| 1011 | pw.println(useKeyIDs ? c.toString() : c.simpleString(this)); |
---|
| 1012 | pw.flush(); |
---|
| 1013 | } |
---|
| 1014 | |
---|
| 1015 | /** |
---|
| 1016 | * Call write_rt0 on a FileWriter derived from f. |
---|
| 1017 | * @param f the File to write to |
---|
| 1018 | * @param useKeyIDs a boolean, true to print key IDs not mnemonics |
---|
| 1019 | * @throws IOException if there is a problem writing the file. |
---|
| 1020 | */ |
---|
| 1021 | public void write_rt0(File f, boolean useKeyIDs) throws IOException { |
---|
| 1022 | write_rt0(new FileWriter(f), useKeyIDs); |
---|
| 1023 | } |
---|
| 1024 | |
---|
| 1025 | /** |
---|
| 1026 | * Equivalent to write_rt0(w, false); |
---|
| 1027 | * @param w a Writer to print on |
---|
| 1028 | */ |
---|
| 1029 | public void write_rt0(Writer w) { write_rt0(w, false); } |
---|
| 1030 | |
---|
| 1031 | /** |
---|
| 1032 | * Equivalent to write_rt0(f, false); |
---|
| 1033 | * @param f the File to write to |
---|
| 1034 | * @throws IOException if there is a problem writing the file. |
---|
| 1035 | */ |
---|
| 1036 | public void write_rt0(File f) throws IOException { |
---|
| 1037 | write_rt0(new FileWriter(f), false); |
---|
| 1038 | } |
---|
| 1039 | |
---|
[d06c453] | 1040 | /** |
---|
| 1041 | * Return this Context's CredentialFactory. |
---|
| 1042 | * @return this Context's CredentialFactory. |
---|
| 1043 | */ |
---|
| 1044 | public CredentialFactory getCredentialFactory() { |
---|
| 1045 | return credentialFactory; |
---|
| 1046 | } |
---|
| 1047 | |
---|
| 1048 | /** |
---|
| 1049 | * Set this Context's CredentialFactory. |
---|
| 1050 | * @param cf the new CredentialFactoty |
---|
| 1051 | */ |
---|
| 1052 | public void setCredentialFactory(CredentialFactory cf) { |
---|
| 1053 | credentialFactory = cf; |
---|
| 1054 | } |
---|
| 1055 | |
---|
[aaadefd] | 1056 | /** |
---|
| 1057 | * Return a new credential supported by this Context. It is not inserted |
---|
[79f516b] | 1058 | * in the Context, but will have access to the context's keyid aliases. |
---|
[aaadefd] | 1059 | * @param head a Role, the head of the encoded ABAC statement |
---|
| 1060 | * @param tail a Role, the tail of the decoded ABAC statement |
---|
| 1061 | * @return a Credential encoding that ABAC statement |
---|
| 1062 | */ |
---|
| 1063 | public Credential newCredential(Role head, Role tail) { |
---|
[79f516b] | 1064 | return credentialFactory.generateCredential(head, tail, keyMap); |
---|
[aaadefd] | 1065 | } |
---|
| 1066 | |
---|
[0595372] | 1067 | /** |
---|
[e36ea1d] | 1068 | * Get to the SHA1 hash of the key. Used by Roles and Identities to get a |
---|
| 1069 | * key ID. |
---|
| 1070 | * @param k the PublicKey to get the ID from. |
---|
| 1071 | * @return a String with the key identifier |
---|
[0595372] | 1072 | */ |
---|
[e36ea1d] | 1073 | static String extractKeyID(PublicKey k) { |
---|
[0595372] | 1074 | SubjectPublicKeyInfo ki = extractSubjectPublicKeyInfo(k); |
---|
| 1075 | SubjectKeyIdentifier id = |
---|
| 1076 | SubjectKeyIdentifier.createSHA1KeyIdentifier(ki); |
---|
| 1077 | |
---|
| 1078 | // Now format it into a string for keeps |
---|
| 1079 | Formatter fmt = new Formatter(new StringWriter()); |
---|
| 1080 | for (byte b: id.getKeyIdentifier()) |
---|
| 1081 | fmt.format("%02x", b); |
---|
| 1082 | return fmt.out().toString(); |
---|
| 1083 | } |
---|
| 1084 | |
---|
| 1085 | /** |
---|
| 1086 | * Extratct the SubjectPublicKeyInfo. Useful for some other encryptions, |
---|
| 1087 | * notably Certificate.make_cert(). |
---|
[e36ea1d] | 1088 | * @param k the PublicKey to get the ID from. |
---|
| 1089 | * @return a String with the key identifier |
---|
[0595372] | 1090 | */ |
---|
[e36ea1d] | 1091 | static SubjectPublicKeyInfo extractSubjectPublicKeyInfo( |
---|
[0595372] | 1092 | PublicKey k) { |
---|
| 1093 | ASN1Sequence seq = null; |
---|
| 1094 | try { |
---|
| 1095 | seq = (ASN1Sequence) new ASN1InputStream( |
---|
| 1096 | k.getEncoded()).readObject(); |
---|
| 1097 | } |
---|
| 1098 | catch (IOException ie) { |
---|
| 1099 | // Badly formatted key?? |
---|
| 1100 | return null; |
---|
| 1101 | } |
---|
| 1102 | return new SubjectPublicKeyInfo(seq); |
---|
| 1103 | } |
---|
| 1104 | |
---|
[418b586] | 1105 | } |
---|