package com.nailabs.abac.credential; import java.io.File; import java.io.FileInputStream; import java.util.Iterator; import java.util.HashSet; import java.util.HashMap; import java.util.Hashtable; import java.util.Collection; import java.security.PublicKey; import edu.stanford.peer.rbtm.util.Predicate; import edu.stanford.peer.rbtm.credential.Entity; import edu.stanford.peer.rbtm.credential.EntityExpression; import edu.stanford.peer.rbtm.credential.Role; import edu.stanford.peer.rbtm.credential.RoleName; import edu.stanford.peer.rbtm.credential.SimpleEntity; import edu.stanford.peer.rbtm.credential.LinkedRole; import edu.stanford.peer.rbtm.credential.StaticCredential; import edu.stanford.peer.rbtm.engine.*; import edu.stanford.rt.credential.*; import edu.stanford.rt.parser.RTParser; /** * An RTML-aware GraphEngine, formerly known as CredentialManager. */ public class RtmlEngine extends GraphEngine { /** internal store of parsed RTML */ protected CredentialStore store; /** static credential to credential domain mapping */ protected HashMap indexByDomains = new HashMap(); /** internal cache of entity hash id to short name mapping */ private static Hashtable entityCache = new Hashtable(10); public RtmlEngine() { super(); } /** default constructor used for the opponents transmitted credentials*/ public RtmlEngine(CredentialStore store) { super(); this.store = store; importDomains(store); } /** predicated constructor used by oppo, sens, and simp frontiers */ public RtmlEngine(CredentialStore store, Predicate p) { super(p); this.store = store; importDomains(store); } /** convenience method for looking up a shortname from a credential hash */ public static String getShortName(String hash) { return (String)entityCache.get(hash); } /** adds a new entity hash to short name (reverse) mapping */ public static void addEntity(String hash, String shortName) { entityCache.put(hash, shortName); } /** add the static credentials which make up a credential domain */ public void addDomain(CredentialDomain domain) { Iterator creds = convert(domain).iterator(); try{ while(creds.hasNext()) { StaticCredential cred = (StaticCredential)creds.next(); HashSet siblings = (HashSet)indexByDomains.get(domain); //System.out.println("Adding " + cred); addCredential(cred); if(siblings == null) { siblings = new HashSet(1); indexByDomains.put(domain, siblings); } siblings.add(cred); } store.addCredentialDomain(domain.getHashID(), domain); } catch(Exception ex) { //ex.printStackTrace(); } } /** * removes a the domain for this credential and any othe credentials in * the same domain. */ public void removeCredential(RtmlCredential cred) { CredentialDomain domain = cred.getCredentialDomain(); HashSet credSet = (HashSet)indexByDomains.get(domain); if(credSet != null) { super.removeCredentials((Collection)credSet); } } /** protected accessor method used for importing a set of cred domains */ protected CredentialStore getCredentialStore() { return store; } /** importing domains is used publicly for credential discovery */ public void importDomains(RtmlEngine engine) { importDomains(engine.getCredentialStore()); } /** internal method for loading the engine from a credential store */ public void importDomains(CredentialStore store) { HashMap domains = store.getCredentialDomains(); Iterator i = domains.values().iterator(); while(i.hasNext()) { //System.out.println("domain = " + i.next().getClass()); addDomain((CredentialDomain)i.next()); } } /** convert a credential domain into a set of static credentials */ public static Collection convert(CredentialDomain domain) { HashSet set = new HashSet(); Iterator roles = domain.roleDefinitionIterator(); int count = 0; while(roles.hasNext()) { //System.out.println("Role Definition # " + ++count); RoleDefinition def = (RoleDefinition)roles.next(); Role head = (Role) RtmlExpression.convert(def.getHead(), domain); EntityExpression body = RtmlExpression.convert(def.getBody(), domain); StaticCredential cred = new RtmlCredential(head, body, domain); //System.out.println("\tcred = " + cred); set.add(cred); } return set; } /** main method for a test application */ public static void main(String argv[]) { if(argv.length < 1) { System.out.println("java com.nailabs.abac.credential.RtmlCredential cred_store.xml"); System.exit(1); } try { RTParser parser = new RTParser(); //System domain is created here. RTContext context = new RTContext(parser); CredentialStore store = new CredentialStore(parser); parser.parseCredentialStore(new FileInputStream(new File(argv[0])), context, store); CredentialDomain domain = store.getCredentialDomain(System.getProperty( "com.nailabs.abac.credential.Hash", "FakeHashCredential01")); convert(domain); } catch(Exception ex) { ex.printStackTrace(); } } }