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