Index: libabac/abac_verifier.c
===================================================================
--- libabac/abac_verifier.c	(revision 9b43fc3d618a238405947779470f1a092ba98648)
+++ libabac/abac_verifier.c	(revision ba6027a0fac78c4a2e819aabd3ccc80779620acc)
@@ -27,5 +27,5 @@
 
 static int debug=0;
- 
+
 /* from abac_attribute.c */
 extern char *get_cred_encoding(abac_attribute_t *ptr);
@@ -41,4 +41,6 @@
 
 extern void abac_print_aspect_string_with_condition(abac_aspect_t *role, FILE*);
+
+extern int abac_id_load_privkey_chunk(abac_id_t *ptr, chunk_t keychunk);
 
 struct _abac_id_credential_t {
@@ -263,6 +265,6 @@
     HASH_ADD_KEYPTR(hh, id_creds, id_cred->hashkeyid, strlen(id_cred->hashkeyid), id_cred);
     if(debug)
-        printf("-->adding into id_creds, (%s)..cnt(%d)\n",
-                                   id_cred->hashkeyid, HASH_COUNT(id_creds));
+        printf("-->adding into id_creds, (%s)..cnt(%d)(%d)\n",
+                                   id_cred->hashkeyid, HASH_COUNT(id_creds),(int)id_cred);
 
     assert(id_hashkeyid_list);
@@ -399,5 +401,7 @@
  * Load an ID cert from a file.
  */
-int abac_verifier_load_id_file_key_file(char *filename, char *keyfilename, abac_id_credential_t **id_cred_ret) {
+int abac_verifier_load_id_file_key_file(char *filename, char *keyfilename,
+abac_id_credential_t **id_cred_ret)
+{
     abac_id_t *id=NULL;
     if (lib == NULL)
@@ -427,4 +431,42 @@
         if(debug) printf("loading... %s\n", keyfilename);
         int keyrc=abac_id_load_privkey_file(id, keyfilename);
+        if(debug) {
+           if(keyrc == 1) printf("..load_id_file: load(%s) with a private key\n",filename);
+               else printf("..load_id_file: load(%s) without a private key\n",filename);
+        }
+    }
+    return ABAC_CERT_SUCCESS;
+}
+
+int abac_verifier_load_enc_id_file_key_file(char *filename, char *keyfilename,
+char *pfile, abac_id_credential_t **id_cred_ret)
+{
+    abac_id_t *id=NULL;
+    if (lib == NULL)
+        errx(1, "looks like you didn't call libabac_init() (lib is NULL)");
+
+    if(debug) printf("loading id file... %s\n", filename);
+
+    // load the cert, with public key
+    certificate_t *cert = lib->creds->create(
+        lib->creds, CRED_CERTIFICATE, CERT_X509,
+        BUILD_FROM_FILE, filename,
+        BUILD_X509_FLAG, X509_AA, // attribute authority, dumb
+        BUILD_END
+    );
+
+    if (cert == NULL)
+        return ABAC_CERT_INVALID;
+
+    int rc=_load_id(&id,cert,id_cred_ret);
+    if(rc==ABAC_CERT_EXISTS) {
+        if(debug) printf("abac_verifier_load_id_files: id already exists\n");
+        return ABAC_CERT_SUCCESS;
+    } 
+
+    /* try to load the private key if it is there */
+    if((rc==ABAC_CERT_SUCCESS) && keyfilename!=NULL && file_exist(keyfilename)) {
+        if(debug) printf("loading... %s\n", keyfilename);
+        int keyrc=abac_id_load_enc_privkey_file(id, keyfilename, pfile);
         if(debug) {
            if(keyrc == 1) printf("..load_id_file: load(%s) with a private key\n",filename);
@@ -503,4 +545,27 @@
     return rc;
 }
+
+int abac_verifier_load_id_enc_privkey_chunk(chunk_t chunk, chunk_t privkey_chunk,
+char* pfile, abac_id_credential_t **id_cred_ret) {
+
+    abac_id_t *id=NULL;
+    // load the cert
+    certificate_t *cert = lib->creds->create(
+        lib->creds, CRED_CERTIFICATE, CERT_X509,
+        BUILD_BLOB_ASN1_DER, chunk,
+        BUILD_X509_FLAG, X509_AA, // attribute authority, dumb
+        BUILD_END
+    );
+    if (cert == NULL)
+        return ABAC_CERT_INVALID;
+
+    int rc=_load_id(&id, cert,id_cred_ret);
+    if(rc == ABAC_CERT_SUCCESS) {
+        rc=abac_id_load_enc_privkey_chunk(id, privkey_chunk, pfile);
+    }
+
+    return rc;
+}
+
 
 
@@ -644,4 +709,80 @@
 
 
+int abac_verifier_load_enc_idkey_file(char *filename, char *pfile, 
+abac_id_credential_t **id_cred_ret)
+{
+    struct stat sb;
+    int fd;
+    int rc;
+
+    fd = open(filename, O_RDONLY);
+    if (fd == -1) { return 1; }
+    if(stat(filename, &sb) == -1) {
+        close(fd);
+        return 1;
+    }
+    char* data = (char *)mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
+    if(data == MAP_FAILED) {
+        close(fd);
+        return 1;
+    }
+    close(fd);
+
+    chunk_t key_pem_chunk=_extract_chunk(data,
+               "-----BEGIN RSA PRIVATE KEY-----","-----END RSA PRIVATE KEY-----");
+    chunk_t cert_pem_chunk=_extract_chunk(data,
+                   "-----BEGIN CERTIFICATE-----","-----END CERTIFICATE-----");
+
+    /* key=0, cert=0 */
+    if(key_pem_chunk.len==0 && cert_pem_chunk.len==0) {
+        if(debug) printf("abac_verifier_load_idkey_file: no key or id in idkey_file\n");
+        goto error;
+    }
+
+    /* key=0, cert=1 */
+    if(key_pem_chunk.len==0) {
+        if(debug) printf("abac_verifier_load_idkey_file: no key in idkey_file\n");
+        munmap(data, sb.st_size);
+        chunk_free(&key_pem_chunk);
+        return abac_verifier_load_id_file_key_file(filename,NULL,id_cred_ret);
+    }
+
+    /* key=1, cert=1 */
+    if(cert_pem_chunk.len!=0) {
+        if(debug) {
+            printf("loading cert pem chunk %d\n", cert_pem_chunk.len);
+            printf("(%s)\n",cert_pem_chunk.ptr);
+        }
+        rc=abac_verifier_load_id_pem_chunk(cert_pem_chunk, id_cred_ret);
+        if(rc != 0) {
+            if(debug) printf("abac_verifier_load_idkey_file: fail to load the id\n");
+            goto error;
+        }
+    /* key=1, cert=0 */
+        } else {
+            if(debug) printf("abac_verifier_load_idkey_file: there is no id info in there\n");
+            goto error;
+    }
+    
+    /* handle the key_pem_chunk */
+    if(debug)  {
+        printf("loading key pem chunk %d\n", key_pem_chunk.len);
+        printf("(%s)\n",key_pem_chunk.ptr);
+        printf("(%d)\n",strlen(key_pem_chunk.ptr));
+    }
+    abac_id_t *id=abac_id_credential_id(*id_cred_ret);
+    rc=abac_id_load_enc_privkey_chunk(id, key_pem_chunk, pfile);
+    if(rc==0) {
+        printf("abac_verifier_load_idkey_file:failed to load priv key!!!\n");
+        goto error;
+    }
+
+    munmap(data, sb.st_size);
+    return 0;
+
+error:
+    munmap(data, sb.st_size);
+    return 1;
+}
 
 void abac_print_clauses(abac_list_t *clauses, FILE *fp)
@@ -662,4 +803,6 @@
 abac_id_credential_t *abac_id_credential_dup(abac_id_credential_t *ptr) {
     assert(ptr != NULL);
+    if(debug)
+       printf("calling abac_id_credential_dup, increment id ref count\n");
     abac_id_dup(ptr->id);
     return ptr;
