Index: java/GraphTest.java
===================================================================
--- java/GraphTest.java	(revision 7ad007621e30cc878a97c60e34b9495a98452fed)
+++ java/GraphTest.java	(revision be05757f1e4d406274cd7d5a6a42e81181b0c726)
@@ -10,9 +10,5 @@
 import net.deterlab.abac.Identity;
 
-import org.bouncycastle.openssl.PEMReader;
-import org.bouncycastle.jce.provider.X509CertificateObject;
 import java.security.KeyPair;
-import java.security.PublicKey;
-// import org.bouncycastle.util.io.pem.PemObject;
 
 
@@ -22,15 +18,4 @@
  */
 public class GraphTest {
-
-    /**
-     * Import a credential from a der file and add it to the credential graph.
-     */
-    protected static void importCred(File f, CredentialGraph g) 
-	throws Exception {
-	Credential c = new Credential(f);
-	g.add_credential(c);
-    }
-
-
     /**
      * Import a directory full of files, using the suffixes as determinants.
@@ -39,32 +24,6 @@
      */
     protected static void importDir(File d, CredentialGraph g) {
-	Vector<File> ids = new Vector<File>();
-	Vector<File> creds = new Vector<File>();
+    }
 
-	for (File f: d.listFiles()) {
-	    if (f.getPath().endsWith(".pem")) ids.add(f);
-	    else if (f.getPath().endsWith(".der") ) creds.add(f);
-	    else System.out.println(f + " of unknown type");
-	}
-	for (File f: ids ){
-	    try {
-		Identity id = new Identity(f);
-
-		Credential.addIdentity(id);
-	    }
-	    catch (Exception e) {
-		System.err.println("Cannot add " + f + ": " + e);
-	    }
-	}
-
-	for (File f: creds) {
-	    try {
-		importCred(f, g);
-	    }
-	    catch (Exception e) {
-		System.err.println("Cannot add " + f + ": " + e);
-	    }
-	}
-    }
     public static void main(String[] args) throws IOException {
         if (args.length < 3) {
@@ -75,6 +34,6 @@
 
         CredentialGraph graph = new CredentialGraph();
-	TreeMap<String, String> nicknames = new TreeMap<String, String>();
-	TreeMap<String, String> keys = new TreeMap<String, String>();
+	Vector<KeyPair> kp = new Vector<KeyPair>();
+	Map<File, Exception> errs = new HashMap<File, Exception>();
 
 	for (int i= 0; i < args.length-2; i++) {
@@ -83,11 +42,10 @@
 	    try {
 		if (f.isDirectory()) 
-		    importDir(f, graph);
-		else if (f.getPath().endsWith(".pem")) {
-		    Identity id = new Identity(f);
-		    Credential.addIdentity(id);
-		}
+		    for (Credential c :Credential.readDirectory(f, kp, errs)) 
+			graph.add_credential(c);
+		else if (f.getPath().endsWith(".pem")) 
+		    Credential.addIdentity(new Identity(f));
 		else if (f.getPath().endsWith(".der"))
-		    importCred(f, graph);
+		    graph.add_credential(new Credential(f));
 		else
 		    System.out.println(f + " of unknown type");
@@ -97,4 +55,7 @@
 	    }
 	}
+
+	for (KeyPair k: kp) System.err.println(k);
+	for (File f: errs.keySet()) System.err.println(f + " " + errs.get(f));
 
         //
Index: java/net/deterlab/abac/Credential.java
===================================================================
--- java/net/deterlab/abac/Credential.java	(revision 7ad007621e30cc878a97c60e34b9495a98452fed)
+++ java/net/deterlab/abac/Credential.java	(revision be05757f1e4d406274cd7d5a6a42e81181b0c726)
@@ -53,4 +53,6 @@
         m_ac = (X509V2AttributeCertificate)parser.engineRead();
 	m_id = null;
+
+	if ( m_ac == null ) throw new IOException("Invalid Credential Format");
 
 	for (Identity id: s_ids) {
@@ -204,8 +206,75 @@
     public boolean hasCertificate() { return m_ac != null; }
 
-    private Role m_head, m_tail;
-
-    private X509V2AttributeCertificate m_ac;
-    private Identity m_id;
+    /**
+     * Import a directory full of files.  First import all the identities
+     * (pem), then the credentials (der) into the credential graph then any
+     * alias files into the two maps.  If keys is not null, any key pairs in
+     * PEM files are put in there.  If errors is not null, errors reading files
+     * are added indexed by filename.
+     */
+    static public Collection<Credential> readDirectory(File d, 
+	    Collection<KeyPair> keys, Map<File, Exception> errors) {
+	Vector<Credential> creds = new Vector<Credential>();
+	Vector<File> derFiles = new Vector<File>();
+
+	for (File f: d.listFiles() ) {
+	    Object o = null;
+	    try {
+		o = new PEMReader(new FileReader(f)).readObject();
+	    }
+	    catch (IOException e) { 
+		// PEMReader couldn't deal, so we assume it's a DER
+		derFiles.add(f);
+		continue;
+	    }
+	    try {
+		if ( o == null ) {
+		    // This shouldn't be, but assume it's a DER
+		    derFiles.add(f);
+		}
+		else if (o instanceof X509CertificateObject) {
+		    Credential.addIdentity(
+			    new Identity((X509CertificateObject)o));
+		}
+		else if (o instanceof KeyPair ) {
+		    if ( keys != null ) keys.add((KeyPair) o);
+		}
+		else {
+		    throw new IOException("Unexpected PEM object: " + 
+			    o.getClass().getName());
+		}
+	    }
+	    catch (Exception e ) {
+		if (errors != null ) errors.put(f, e);
+	    }
+	}
+
+	for ( File f : derFiles ) {
+	    try {
+		creds.add(new Credential(f));
+	    }
+	    catch (Exception e ) {
+		if (errors != null ) errors.put(f, e);
+	    }
+	}
+	return creds;
+    }
+
+    static public Collection<Credential> readDirectory(File d) {
+	return readDirectory(d, null, null);
+    }
+    static public Collection<Credential> readDirectory(File d, 
+	    Map<File, Exception> errors) {
+	return readDirectory(d, null, errors);
+    }
+    static public Collection<Credential> readDirectory(File d, 
+	    Collection<KeyPair> keys) {
+	return readDirectory(d, keys, null);
+    }
+
+private Role m_head, m_tail;
+
+private X509V2AttributeCertificate m_ac;
+private Identity m_id;
 
     public static void addIdentity(Identity id) { 
