[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 | } |
---|
| 508 | /** |
---|
| 509 | * Put the Identity into the set of ids used to validate certificates. |
---|
| 510 | * Also put the keyID and name into the translation mappings used by Roles |
---|
| 511 | * to pretty print. In the role mapping, if multiple ids use the same |
---|
| 512 | * common name they are disambiguated. Only one entry for keyid is |
---|
| 513 | * allowed. |
---|
[6432e35] | 514 | * @param id the Identity to add |
---|
[418b586] | 515 | */ |
---|
| 516 | protected void addIdentity(Identity id) { |
---|
[388a3d7] | 517 | m_identities.add(id); |
---|
[418b586] | 518 | if (id.getName() != null && id.getKeyID() != null) { |
---|
| 519 | if ( !keys.containsKey(id.getKeyID()) ) { |
---|
| 520 | String name = id.getName(); |
---|
| 521 | int n= 1; |
---|
| 522 | |
---|
| 523 | while (nicknames.containsKey(name)) { |
---|
| 524 | name = id.getName() + n++; |
---|
| 525 | } |
---|
| 526 | nicknames.put(name, id.getKeyID()); |
---|
| 527 | keys.put(id.getKeyID(), name); |
---|
| 528 | } |
---|
| 529 | } |
---|
| 530 | } |
---|
| 531 | /** |
---|
| 532 | * Translate either keys to nicknames or vice versa. Break the string into |
---|
| 533 | * space separated tokens and then each of them into period separated |
---|
| 534 | * strings. If any of the smallest strings is in the map, replace it with |
---|
| 535 | * the value. |
---|
[e36ea1d] | 536 | * @param is the string to manipulate |
---|
| 537 | * @param m the Map containing translations |
---|
| 538 | * @return the string after modification |
---|
[418b586] | 539 | */ |
---|
| 540 | protected String replace(String is, Map<String, String> m) { |
---|
| 541 | String rv = ""; |
---|
| 542 | for (String tok: is.split(" ")) { |
---|
| 543 | String term = ""; |
---|
| 544 | for (String s: tok.split("\\.")) { |
---|
| 545 | String next = m.containsKey(s) ? m.get(s) : s; |
---|
| 546 | |
---|
| 547 | if (term.isEmpty()) term = next; |
---|
| 548 | else term += "." + next; |
---|
| 549 | } |
---|
| 550 | if (rv.isEmpty()) rv = term; |
---|
| 551 | else rv += " " + term; |
---|
| 552 | } |
---|
| 553 | return rv; |
---|
| 554 | } |
---|
| 555 | |
---|
[e36ea1d] | 556 | /** |
---|
| 557 | * Expand menmonic names in a Role string, e.g. the CN of the issuer |
---|
| 558 | * certificate, into the full key ID. Used internally by Roles to provide |
---|
| 559 | * transparent use of mnemonics |
---|
| 560 | * @param s the string to expand |
---|
| 561 | * @return the String after expansion. |
---|
| 562 | */ |
---|
| 563 | String expandKeyID(String s) { return replace(s, nicknames); } |
---|
| 564 | /** |
---|
| 565 | * Convert key IDs to menmonic names in a Role string. The inverse of |
---|
| 566 | * expandKeyID. |
---|
| 567 | * @param s the string to expand |
---|
| 568 | * @return the String after expansion. |
---|
| 569 | */ |
---|
| 570 | String expandNickname(String s) { return replace(s, keys); } |
---|
[418b586] | 571 | |
---|
[39526ce] | 572 | /** |
---|
| 573 | * Read the current ZipEntry's bytes from z. Tedious because there's no |
---|
| 574 | * way to reliably tell how big the entry is, so we have to rely on a |
---|
| 575 | * simple expanding array read of the bytes. |
---|
| 576 | */ |
---|
| 577 | protected byte[] readCurrentZipEntry(ZipInputStream z) throws IOException { |
---|
| 578 | final int bsize = 4096; |
---|
| 579 | byte[] buf = new byte[bsize]; |
---|
| 580 | byte[] rv = new byte[0]; |
---|
| 581 | int r = 0; |
---|
| 582 | |
---|
| 583 | // z.read returns -1 at the end of entry |
---|
| 584 | while ((r = z.read(buf, 0, bsize)) != -1 ) { |
---|
| 585 | byte[] b = new byte[rv.length + r]; |
---|
| 586 | |
---|
| 587 | System.arraycopy(rv, 0, b, 0, rv.length); |
---|
| 588 | System.arraycopy(buf, 0, b, rv.length, r); |
---|
| 589 | rv = b; |
---|
| 590 | } |
---|
| 591 | return rv; |
---|
| 592 | } |
---|
| 593 | |
---|
[418b586] | 594 | /** |
---|
| 595 | * Import a zip file. First import all the identities |
---|
| 596 | * (pem), then the credentials (der) into the credential graph then any |
---|
| 597 | * alias files into the two maps. If keys is not null, any key pairs in |
---|
| 598 | * PEM files are put in there. If errors is not null, errors reading files |
---|
[6432e35] | 599 | * are added indexed by filename. This is a jabac extension. |
---|
[39526ce] | 600 | * @param s the InputStream to read |
---|
[e36ea1d] | 601 | * @param keys a Collection into which to insert unmatched keys |
---|
| 602 | * @param errors a Map from entry name to generated exception |
---|
| 603 | * @throws IOException if the file is unreadable. Per entry exceptions are |
---|
| 604 | * returned in the errors parameter. |
---|
[418b586] | 605 | */ |
---|
[39526ce] | 606 | public void load_zip(InputStream s, Collection<KeyPair> keys, |
---|
[418b586] | 607 | Map<String, Exception> errors) throws IOException { |
---|
[39526ce] | 608 | Map<String, byte[]> derEntries = new HashMap<String, byte[]>(); |
---|
[418b586] | 609 | Map<String, Identity> ids = new TreeMap<String, Identity>(); |
---|
| 610 | Map<String, KeyPair> kps = new TreeMap<String, KeyPair>(); |
---|
[39526ce] | 611 | int entries = 0; |
---|
[418b586] | 612 | |
---|
[39526ce] | 613 | ZipInputStream z = new ZipInputStream(s); |
---|
[418b586] | 614 | |
---|
[39526ce] | 615 | for (ZipEntry ze = z.getNextEntry(); ze != null; ze = z.getNextEntry()){ |
---|
[418b586] | 616 | try { |
---|
[39526ce] | 617 | entries++; |
---|
| 618 | byte[] buf = readCurrentZipEntry(z); |
---|
[418b586] | 619 | PEMReader r = new PEMReader( |
---|
[39526ce] | 620 | new InputStreamReader(new ByteArrayInputStream(buf))); |
---|
[418b586] | 621 | Object o = readPEM(r); |
---|
| 622 | |
---|
| 623 | if ( o != null ) { |
---|
| 624 | if (o instanceof Identity) { |
---|
| 625 | Identity i = (Identity) o; |
---|
| 626 | String kid = i.getKeyID(); |
---|
| 627 | |
---|
| 628 | if (kps.containsKey(kid) ) { |
---|
| 629 | i.setKeyPair(kps.get(kid)); |
---|
| 630 | kps.remove(kid); |
---|
| 631 | } |
---|
| 632 | else if (i.getKeyPair() == null ) |
---|
| 633 | ids.put(i.getKeyID(), i); |
---|
| 634 | |
---|
[6432e35] | 635 | load_id_chunk(i); |
---|
[418b586] | 636 | } |
---|
| 637 | else if (o instanceof KeyPair ) { |
---|
| 638 | KeyPair kp = (KeyPair) o; |
---|
[0595372] | 639 | String kid = extractKeyID(kp.getPublic()); |
---|
[418b586] | 640 | |
---|
| 641 | if (ids.containsKey(kid)) { |
---|
| 642 | Identity i = ids.get(kid); |
---|
| 643 | |
---|
| 644 | i.setKeyPair(kp); |
---|
| 645 | ids.remove(kid); |
---|
| 646 | } |
---|
| 647 | else { |
---|
| 648 | kps.put(kid, kp); |
---|
| 649 | } |
---|
| 650 | } |
---|
| 651 | } |
---|
| 652 | else { |
---|
| 653 | // Not a PEM file |
---|
[39526ce] | 654 | derEntries.put(ze.getName(),buf); |
---|
[418b586] | 655 | continue; |
---|
| 656 | } |
---|
| 657 | } |
---|
| 658 | catch (Exception e ) { |
---|
[39526ce] | 659 | if (errors != null ) errors.put(ze.getName(), e); |
---|
[418b586] | 660 | } |
---|
| 661 | } |
---|
| 662 | |
---|
[39526ce] | 663 | for ( String k: derEntries.keySet() ) { |
---|
[418b586] | 664 | try { |
---|
[d06c453] | 665 | Credential[] creds = credentialFactory.parseCredential( |
---|
[39526ce] | 666 | new ByteArrayInputStream(derEntries.get(k)), |
---|
[3612811] | 667 | m_identities); |
---|
| 668 | for (Credential c: creds) |
---|
| 669 | add_credential(c); |
---|
[418b586] | 670 | } |
---|
| 671 | catch (Exception e ) { |
---|
[39526ce] | 672 | if (errors != null ) errors.put(k, e); |
---|
[418b586] | 673 | } |
---|
| 674 | } |
---|
[39526ce] | 675 | |
---|
| 676 | if (entries == 0) |
---|
| 677 | throw new IOException("Not a ZIP file (or empty ZIP file)"); |
---|
| 678 | } |
---|
| 679 | /** |
---|
| 680 | * Equivalent to load_zip(s, null, null). |
---|
| 681 | * @param s the InputStream to read |
---|
| 682 | * @throws IOException if the file is unreadable. To see per-entry |
---|
| 683 | * exceptions use a signature with the errors parameter |
---|
| 684 | */ |
---|
| 685 | public void load_zip(InputStream s) |
---|
| 686 | throws IOException { |
---|
| 687 | load_zip(s, null, null); |
---|
| 688 | } |
---|
| 689 | /** |
---|
| 690 | * Equivalent to load_zip(s, null, errors). |
---|
| 691 | * @param s the InputStream to read |
---|
| 692 | * @param errors a Map from entry name to generated exception |
---|
| 693 | * @throws IOException if the file is unreadable. Per entry exceptions are |
---|
| 694 | * returned in the errors parameter. |
---|
| 695 | */ |
---|
| 696 | public void load_zip(InputStream s, |
---|
| 697 | Map<String, Exception> errors) throws IOException { |
---|
| 698 | load_zip(s, null, errors); |
---|
[418b586] | 699 | } |
---|
[e36ea1d] | 700 | /** |
---|
[39526ce] | 701 | * Equivalent to load_zip(s, keys, null). |
---|
| 702 | * @param s the InputStream to read |
---|
| 703 | * @param keys a Collection into which to insert unmatched keys |
---|
| 704 | * @throws IOException if the file is unreadable. To see per-entry |
---|
| 705 | * exceptions use a signature with the errors parameter |
---|
| 706 | */ |
---|
| 707 | public void load_zip(InputStream s, |
---|
| 708 | Collection<KeyPair> keys) throws IOException { |
---|
| 709 | load_zip(s, keys, null); |
---|
| 710 | } |
---|
| 711 | |
---|
| 712 | /** |
---|
| 713 | * Loads a zip file. Equivalent to |
---|
| 714 | * load_zip(new FileInputStream(zf), keys, errors). |
---|
| 715 | * @param zf the File to read |
---|
| 716 | * @param keys a Collection into which to insert unmatched keys |
---|
| 717 | * @param errors a Map from entry name to generated exception |
---|
| 718 | * @throws IOException if the file is unreadable. Per entry exceptions are |
---|
| 719 | * returned in the errors parameter. |
---|
| 720 | */ |
---|
| 721 | public void load_zip(File zf, Collection<KeyPair> keys, |
---|
| 722 | Map<String, Exception> errors) throws IOException { |
---|
| 723 | load_zip(new FileInputStream(zf), keys, errors); |
---|
| 724 | } |
---|
| 725 | /** |
---|
| 726 | * Equivalent to load_zip(d, null, null). |
---|
[e36ea1d] | 727 | * @param d the File to read |
---|
| 728 | * @throws IOException if the file is unreadable. To see per-entry |
---|
| 729 | * exceptions use a signature with the errors parameter |
---|
| 730 | */ |
---|
[39526ce] | 731 | public void load_zip(File d) |
---|
[418b586] | 732 | throws IOException { |
---|
[39526ce] | 733 | load_zip(d, null, null); |
---|
[418b586] | 734 | } |
---|
[e36ea1d] | 735 | /** |
---|
[39526ce] | 736 | * Equivalent to load_zip(d, null, errors). |
---|
[e36ea1d] | 737 | * @param d the File to read |
---|
| 738 | * @param errors a Map from entry name to generated exception |
---|
| 739 | * @throws IOException if the file is unreadable. Per entry exceptions are |
---|
| 740 | * returned in the errors parameter. |
---|
| 741 | */ |
---|
[39526ce] | 742 | public void load_zip(File d, |
---|
[418b586] | 743 | Map<String, Exception> errors) throws IOException { |
---|
[39526ce] | 744 | load_zip(d, null, errors); |
---|
[418b586] | 745 | } |
---|
[e36ea1d] | 746 | /** |
---|
[39526ce] | 747 | * Equivalent to load_zip(d, keys, null). |
---|
[e36ea1d] | 748 | * @param d the File to read |
---|
| 749 | * @param keys a Collection into which to insert unmatched keys |
---|
| 750 | * @throws IOException if the file is unreadable. To see per-entry |
---|
| 751 | * exceptions use a signature with the errors parameter |
---|
| 752 | */ |
---|
[39526ce] | 753 | public void load_zip(File d, |
---|
[418b586] | 754 | Collection<KeyPair> keys) throws IOException { |
---|
[39526ce] | 755 | load_zip(d, keys, null); |
---|
[418b586] | 756 | } |
---|
| 757 | |
---|
[e36ea1d] | 758 | /** |
---|
| 759 | * Read a PEM file that contains an X509 Certificate, a key pair, or both. |
---|
| 760 | * If a cert is present it is converted into an Identity. A key pair is |
---|
| 761 | * returned as a java.security.KeyPair and both are returned as an Identity |
---|
| 762 | * with an associated key pair. |
---|
| 763 | * @param r a PEMReader from which to read |
---|
| 764 | * @return an object encoding the contents (as above) |
---|
| 765 | * @throws IOException for an unreadable or badly formated input |
---|
| 766 | */ |
---|
[418b586] | 767 | protected Object readPEM(PEMReader r) throws IOException { |
---|
| 768 | Identity i = null; |
---|
| 769 | KeyPair keys = null; |
---|
| 770 | Object o = null; |
---|
| 771 | |
---|
| 772 | while ( (o = r.readObject()) != null ) { |
---|
| 773 | if (o instanceof X509Certificate) { |
---|
| 774 | if ( i == null ) { |
---|
| 775 | try { |
---|
| 776 | i = new Identity((X509Certificate)o); |
---|
| 777 | } |
---|
| 778 | catch (Exception e) { |
---|
| 779 | // Translate Idenitiy exceptions to IOException |
---|
| 780 | throw new IOException(e); |
---|
| 781 | } |
---|
| 782 | if (keys != null ) { |
---|
| 783 | i.setKeyPair(keys); |
---|
| 784 | keys = null; |
---|
| 785 | } |
---|
| 786 | } |
---|
| 787 | else throw new IOException("Two certificates"); |
---|
| 788 | } |
---|
| 789 | else if (o instanceof KeyPair ) { |
---|
| 790 | if ( i != null ) i.setKeyPair((KeyPair) o); |
---|
| 791 | else keys = (KeyPair) o; |
---|
| 792 | } |
---|
| 793 | else { |
---|
| 794 | throw new IOException("Unexpected PEM object: " + |
---|
| 795 | o.getClass().getName()); |
---|
| 796 | } |
---|
| 797 | } |
---|
| 798 | |
---|
| 799 | if ( i != null ) return i; |
---|
| 800 | else if ( keys != null) return keys; |
---|
| 801 | else return null; |
---|
| 802 | } |
---|
| 803 | |
---|
| 804 | /** |
---|
| 805 | * Import a directory full of files. First import all the identities |
---|
| 806 | * (pem), then the credentials (der) into the credential graph then any |
---|
| 807 | * alias files into the two maps. If keys is not null, any key pairs in |
---|
| 808 | * PEM files are put in there. If errors is not null, errors reading files |
---|
[6432e35] | 809 | * are added indexed by filename. This behaves slightly differently from |
---|
| 810 | * the load_directory description in the general libabac documentation. |
---|
[e36ea1d] | 811 | * @param d the File to read. If it is a directory its contents are read |
---|
| 812 | * @param keys a Collection into which to insert unmatched keys |
---|
| 813 | * @param errors a Map from entry name to generated exception |
---|
| 814 | * @throws IOException if the file is unreadable. Per file exceptions are |
---|
| 815 | * returned in the errors parameter. |
---|
[418b586] | 816 | */ |
---|
[6432e35] | 817 | public void load_directory(File d, Collection<KeyPair> keys, |
---|
[418b586] | 818 | Map<String, Exception> errors) { |
---|
| 819 | Vector<File> derFiles = new Vector<File>(); |
---|
| 820 | Collection<File> files = new Vector<File>(); |
---|
| 821 | Map<String, Identity> ids = new TreeMap<String, Identity>(); |
---|
| 822 | Map<String, KeyPair> kps = new TreeMap<String, KeyPair>(); |
---|
| 823 | |
---|
| 824 | if (d.isDirectory() ) |
---|
| 825 | for (File f : d.listFiles()) |
---|
| 826 | files.add(f); |
---|
| 827 | else files.add(d); |
---|
| 828 | |
---|
| 829 | for (File f: files ) { |
---|
| 830 | try { |
---|
| 831 | PEMReader r = new PEMReader(new FileReader(f)); |
---|
| 832 | Object o = readPEM(r); |
---|
| 833 | |
---|
| 834 | if ( o != null ) { |
---|
| 835 | if (o instanceof Identity) { |
---|
| 836 | Identity i = (Identity) o; |
---|
| 837 | String kid = i.getKeyID(); |
---|
| 838 | |
---|
| 839 | if (kps.containsKey(kid) ) { |
---|
| 840 | i.setKeyPair(kps.get(kid)); |
---|
| 841 | kps.remove(kid); |
---|
| 842 | } |
---|
| 843 | else if (i.getKeyPair() == null ) |
---|
| 844 | ids.put(i.getKeyID(), i); |
---|
| 845 | |
---|
[6432e35] | 846 | load_id_chunk(i); |
---|
[418b586] | 847 | } |
---|
| 848 | else if (o instanceof KeyPair ) { |
---|
| 849 | KeyPair kp = (KeyPair) o; |
---|
[0595372] | 850 | String kid = extractKeyID(kp.getPublic()); |
---|
[418b586] | 851 | |
---|
| 852 | if (ids.containsKey(kid)) { |
---|
| 853 | Identity i = ids.get(kid); |
---|
| 854 | |
---|
| 855 | i.setKeyPair(kp); |
---|
| 856 | ids.remove(kid); |
---|
| 857 | } |
---|
| 858 | else { |
---|
| 859 | kps.put(kid, kp); |
---|
| 860 | } |
---|
| 861 | } |
---|
| 862 | } |
---|
| 863 | else { |
---|
| 864 | // Not a PEM file |
---|
| 865 | derFiles.add(f); |
---|
| 866 | continue; |
---|
| 867 | } |
---|
| 868 | } |
---|
| 869 | catch (Exception e ) { |
---|
| 870 | if (errors != null ) errors.put(f.getName(), e); |
---|
| 871 | } |
---|
| 872 | } |
---|
| 873 | |
---|
| 874 | for ( File f : derFiles ) { |
---|
| 875 | try { |
---|
[d06c453] | 876 | Credential[] creds = credentialFactory.parseCredential(f, |
---|
[3612811] | 877 | m_identities); |
---|
| 878 | for (Credential c: creds) |
---|
| 879 | add_credential(c); |
---|
[418b586] | 880 | } |
---|
| 881 | catch (Exception e ) { |
---|
| 882 | if (errors != null ) errors.put(f.getName(), e); |
---|
| 883 | } |
---|
| 884 | } |
---|
| 885 | } |
---|
[e36ea1d] | 886 | /** |
---|
[6432e35] | 887 | * Equivalent to load_directory(d, null, null). |
---|
[e36ea1d] | 888 | * @param d the File to read. If it is a directory its contents are read |
---|
| 889 | * @throws IOException if the file is unreadable. To see per-file |
---|
| 890 | * exceptions use a signature with the errors parameter. |
---|
| 891 | */ |
---|
[6432e35] | 892 | public void load_directory(File d) { |
---|
| 893 | load_directory(d, null, null); |
---|
[418b586] | 894 | } |
---|
[e36ea1d] | 895 | /** |
---|
[6432e35] | 896 | * Equivalent to load_directory(d, null, null). |
---|
[e36ea1d] | 897 | * @param d the File to read. If it is a directory its contents are read |
---|
| 898 | * @param errors a Map from entry name to generated exception |
---|
| 899 | * @throws IOException if the file is unreadable. Per file exceptions are |
---|
| 900 | * returned in the errors parameter. |
---|
| 901 | */ |
---|
[6432e35] | 902 | public void load_directory(File d, Map<String, Exception> errors) { |
---|
| 903 | load_directory(d, null, errors); |
---|
[418b586] | 904 | } |
---|
[e36ea1d] | 905 | /** |
---|
[6432e35] | 906 | * Equivalent to load_directory(d, null, null). |
---|
[e36ea1d] | 907 | * @param d the File to read. If it is a directory its contents are read |
---|
| 908 | * @param keys a Collection into which to insert unmatched keys |
---|
| 909 | * @throws IOException if the file is unreadable. To see per-file |
---|
| 910 | * exceptions use a signature with the errors parameter. |
---|
| 911 | */ |
---|
[6432e35] | 912 | public void load_directory(File d, Collection<KeyPair> keys) { |
---|
| 913 | load_directory(d, keys, null); |
---|
[418b586] | 914 | } |
---|
| 915 | |
---|
[ad24705] | 916 | /** |
---|
| 917 | * Load from a simple rt0 text format. A jabac extension. The format is |
---|
| 918 | * <br/> |
---|
| 919 | * # comments<br/> |
---|
| 920 | * role <- role<br/> |
---|
| 921 | * <br/> |
---|
| 922 | * |
---|
| 923 | * Spaces are not significant around the arrow and the tail can be as long |
---|
| 924 | * as needed. |
---|
[39526ce] | 925 | * @param s the InputStream to load |
---|
| 926 | * @throws IOException if there is an error getting the file open or in |
---|
| 927 | * format |
---|
[ad24705] | 928 | */ |
---|
[39526ce] | 929 | public void load_rt0(InputStream s) |
---|
[ad24705] | 930 | throws IOException { |
---|
| 931 | Pattern comment = Pattern.compile("(^\\s*#|^\\s*$)"); |
---|
[39526ce] | 932 | Pattern rule = Pattern.compile("([\\w\\.]+)\\s*<-+\\s*(.+)"); |
---|
| 933 | LineNumberReader r = new LineNumberReader(new InputStreamReader(s)); |
---|
[ad24705] | 934 | String line = null; |
---|
| 935 | |
---|
| 936 | while ((line = r.readLine()) != null) { |
---|
| 937 | Matcher cm = comment.matcher(line); |
---|
| 938 | Matcher rm = rule.matcher(line); |
---|
| 939 | |
---|
| 940 | if (cm.find()) continue; |
---|
| 941 | if (rm.find()) |
---|
[3612811] | 942 | add_credential(new InternalCredential(new Role(rm.group(1)), |
---|
[ad24705] | 943 | new Role(rm.group(2)))); |
---|
[39526ce] | 944 | else |
---|
| 945 | throw new RuntimeException("Unexpected format: line " + |
---|
| 946 | r.getLineNumber()); |
---|
[ad24705] | 947 | } |
---|
| 948 | } |
---|
| 949 | /** |
---|
[39526ce] | 950 | * Equivalent to load_rt0(new FileInputStream(f) |
---|
[ad24705] | 951 | * @param f the File to load |
---|
| 952 | * @throws IOException if there is an error getting the file open |
---|
| 953 | */ |
---|
[39526ce] | 954 | public void load_rt0(File f) throws IOException { |
---|
| 955 | load_rt0(new FileInputStream(f)); |
---|
[ad24705] | 956 | } |
---|
| 957 | |
---|
| 958 | |
---|
[e36ea1d] | 959 | /** |
---|
| 960 | * Write the certificates that make up the context as a zip file, with an |
---|
[3d13073] | 961 | * entry for each credential or identity. The files are all zipped in a |
---|
| 962 | * directory derived from the filename. |
---|
[39526ce] | 963 | * @param s the OutputStream to write |
---|
[e36ea1d] | 964 | * @param allIDs a boolean, if true write certificates for all Identities, |
---|
| 965 | * whether used in signing a credential or not. |
---|
| 966 | * @param withPrivateKeys a boolean, if true write the Identities as PEM |
---|
| 967 | * file containing both the certificate and the private keys. |
---|
[3d13073] | 968 | * @throws IOException if there is a problem writing the file. |
---|
[e36ea1d] | 969 | */ |
---|
[39526ce] | 970 | public void write_zip(OutputStream s, boolean allIDs, |
---|
| 971 | boolean withPrivateKeys) throws IOException { |
---|
| 972 | ZipOutputStream z = new ZipOutputStream(s); |
---|
[388a3d7] | 973 | Set<Identity> ids = allIDs ? m_identities : new TreeSet<Identity>(); |
---|
[39526ce] | 974 | String baseDir = "creds"; |
---|
[b6e7c18] | 975 | int idx = baseDir.indexOf('.'); |
---|
| 976 | |
---|
| 977 | |
---|
| 978 | if (idx != -1) |
---|
| 979 | baseDir = baseDir.substring(0, idx); |
---|
[418b586] | 980 | |
---|
| 981 | int n = 0; |
---|
| 982 | for (Credential c: credentials()) { |
---|
[b6e7c18] | 983 | z.putNextEntry(new ZipEntry(baseDir + File.separator + |
---|
| 984 | "attr" + n++ + ".der")); |
---|
[418b586] | 985 | c.write(z); |
---|
| 986 | z.closeEntry(); |
---|
[d69593c] | 987 | if ( c.issuer() != null && !allIDs) ids.add(c.issuer()); |
---|
[418b586] | 988 | } |
---|
| 989 | for (Identity i: ids) { |
---|
[b6e7c18] | 990 | z.putNextEntry(new ZipEntry(baseDir + File.separator + |
---|
| 991 | i.getName() + ".pem")); |
---|
[418b586] | 992 | i.write(z); |
---|
| 993 | if (withPrivateKeys) |
---|
| 994 | i.writePrivateKey(z); |
---|
| 995 | z.closeEntry(); |
---|
| 996 | } |
---|
| 997 | z.close(); |
---|
| 998 | } |
---|
[39526ce] | 999 | /** |
---|
| 1000 | * Equivalent to |
---|
| 1001 | * write_zip(new FileOutputStream(f), allIDs, withPrivateKeys). |
---|
| 1002 | * @param f the File to write |
---|
| 1003 | * @param allIDs a boolean, if true write certificates for all Identities, |
---|
| 1004 | * whether used in signing a credential or not. |
---|
| 1005 | * @param withPrivateKeys a boolean, if true write the Identities as PEM |
---|
| 1006 | * file containing both the certificate and the private keys. |
---|
| 1007 | * @throws IOException if there is a problem writing the file. |
---|
| 1008 | */ |
---|
| 1009 | public void write_zip(File f, boolean allIDs, boolean withPrivateKeys) |
---|
| 1010 | throws IOException { |
---|
| 1011 | write_zip(new FileOutputStream(f), allIDs, withPrivateKeys); |
---|
| 1012 | } |
---|
[418b586] | 1013 | |
---|
[ad24705] | 1014 | /** |
---|
| 1015 | * Write to a simple rt0 text format. A jabac extension. |
---|
| 1016 | * The format is |
---|
| 1017 | * <br/> |
---|
| 1018 | * role <- role<br/> |
---|
| 1019 | * <br/> |
---|
| 1020 | * |
---|
| 1021 | * @param w a Writer to print on |
---|
| 1022 | * @param useKeyIDs a boolean, true to print key IDs not mnemonics |
---|
| 1023 | */ |
---|
| 1024 | public void write_rt0(Writer w, boolean useKeyIDs) { |
---|
| 1025 | PrintWriter pw = w instanceof PrintWriter ? |
---|
| 1026 | (PrintWriter) w : new PrintWriter(w); |
---|
| 1027 | |
---|
| 1028 | for (Credential c: credentials()) |
---|
| 1029 | pw.println(useKeyIDs ? c.toString() : c.simpleString(this)); |
---|
| 1030 | pw.flush(); |
---|
| 1031 | } |
---|
| 1032 | |
---|
| 1033 | /** |
---|
| 1034 | * Call write_rt0 on a FileWriter derived from f. |
---|
| 1035 | * @param f the File to write to |
---|
| 1036 | * @param useKeyIDs a boolean, true to print key IDs not mnemonics |
---|
| 1037 | * @throws IOException if there is a problem writing the file. |
---|
| 1038 | */ |
---|
| 1039 | public void write_rt0(File f, boolean useKeyIDs) throws IOException { |
---|
| 1040 | write_rt0(new FileWriter(f), useKeyIDs); |
---|
| 1041 | } |
---|
| 1042 | |
---|
| 1043 | /** |
---|
| 1044 | * Equivalent to write_rt0(w, false); |
---|
| 1045 | * @param w a Writer to print on |
---|
| 1046 | */ |
---|
| 1047 | public void write_rt0(Writer w) { write_rt0(w, false); } |
---|
| 1048 | |
---|
| 1049 | /** |
---|
| 1050 | * Equivalent to write_rt0(f, false); |
---|
| 1051 | * @param f the File to write to |
---|
| 1052 | * @throws IOException if there is a problem writing the file. |
---|
| 1053 | */ |
---|
| 1054 | public void write_rt0(File f) throws IOException { |
---|
| 1055 | write_rt0(new FileWriter(f), false); |
---|
| 1056 | } |
---|
| 1057 | |
---|
[d06c453] | 1058 | /** |
---|
| 1059 | * Return this Context's CredentialFactory. |
---|
| 1060 | * @return this Context's CredentialFactory. |
---|
| 1061 | */ |
---|
| 1062 | public CredentialFactory getCredentialFactory() { |
---|
| 1063 | return credentialFactory; |
---|
| 1064 | } |
---|
| 1065 | |
---|
| 1066 | /** |
---|
| 1067 | * Set this Context's CredentialFactory. |
---|
| 1068 | * @param cf the new CredentialFactoty |
---|
| 1069 | */ |
---|
| 1070 | public void setCredentialFactory(CredentialFactory cf) { |
---|
| 1071 | credentialFactory = cf; |
---|
| 1072 | } |
---|
| 1073 | |
---|
[aaadefd] | 1074 | /** |
---|
| 1075 | * Return a new credential supported by this Context. It is not inserted |
---|
| 1076 | * in the Context. |
---|
| 1077 | * @param head a Role, the head of the encoded ABAC statement |
---|
| 1078 | * @param tail a Role, the tail of the decoded ABAC statement |
---|
| 1079 | * @return a Credential encoding that ABAC statement |
---|
| 1080 | */ |
---|
| 1081 | public Credential newCredential(Role head, Role tail) { |
---|
| 1082 | return credentialFactory.generateCredential(head, tail); |
---|
| 1083 | } |
---|
| 1084 | |
---|
[0595372] | 1085 | /** |
---|
[e36ea1d] | 1086 | * Get to the SHA1 hash of the key. Used by Roles and Identities to get a |
---|
| 1087 | * key ID. |
---|
| 1088 | * @param k the PublicKey to get the ID from. |
---|
| 1089 | * @return a String with the key identifier |
---|
[0595372] | 1090 | */ |
---|
[e36ea1d] | 1091 | static String extractKeyID(PublicKey k) { |
---|
[0595372] | 1092 | SubjectPublicKeyInfo ki = extractSubjectPublicKeyInfo(k); |
---|
| 1093 | SubjectKeyIdentifier id = |
---|
| 1094 | SubjectKeyIdentifier.createSHA1KeyIdentifier(ki); |
---|
| 1095 | |
---|
| 1096 | // Now format it into a string for keeps |
---|
| 1097 | Formatter fmt = new Formatter(new StringWriter()); |
---|
| 1098 | for (byte b: id.getKeyIdentifier()) |
---|
| 1099 | fmt.format("%02x", b); |
---|
| 1100 | return fmt.out().toString(); |
---|
| 1101 | } |
---|
| 1102 | |
---|
| 1103 | /** |
---|
| 1104 | * Extratct the SubjectPublicKeyInfo. Useful for some other encryptions, |
---|
| 1105 | * notably Certificate.make_cert(). |
---|
[e36ea1d] | 1106 | * @param k the PublicKey to get the ID from. |
---|
| 1107 | * @return a String with the key identifier |
---|
[0595372] | 1108 | */ |
---|
[e36ea1d] | 1109 | static SubjectPublicKeyInfo extractSubjectPublicKeyInfo( |
---|
[0595372] | 1110 | PublicKey k) { |
---|
| 1111 | ASN1Sequence seq = null; |
---|
| 1112 | try { |
---|
| 1113 | seq = (ASN1Sequence) new ASN1InputStream( |
---|
| 1114 | k.getEncoded()).readObject(); |
---|
| 1115 | } |
---|
| 1116 | catch (IOException ie) { |
---|
| 1117 | // Badly formatted key?? |
---|
| 1118 | return null; |
---|
| 1119 | } |
---|
| 1120 | return new SubjectPublicKeyInfo(seq); |
---|
| 1121 | } |
---|
| 1122 | |
---|
[418b586] | 1123 | } |
---|