/* abac_verifier.c */ #include #include "libabac_common.h" #include "abac.h" #include "abac_list.h" #include "uthash.h" #include "abac_util.h" typedef struct _abac_id_cert_t { char *keyid; // using cert's keyid abac_id_t *cert; UT_hash_handle hh; } abac_id_cert_t; struct _abac_credential_t { abac_role_t *head; abac_role_t *tail; abac_id_t *issuer; /* Acme of Acme.customer <- Coyote */ abac_attribute_t *cert; int refcount; }; /* hash table of all the principal id credentials */ abac_id_cert_t *id_certs = NULL; /***********************************************************************/ // convert a chunk to a lowercase binary string // malloc's the string static char *_chunk_to_string(abac_chunk_t chunk) { int i; char *ret = abac_xmalloc(chunk.len * 2 + 1); for (i = 0; i < chunk.len; ++i) sprintf(ret + 2 * i, "%02x", chunk.ptr[i]); return ret; } // verify that cert was issued by issuer // cert and issuer can be the same, in which case the self-sig is validated static int _verify_cert(abac_id_t *cert) { assert(cert); if(!abac_id_still_valid(cert)) return 0; return 1; } /** * Init the verifier subsystem. */ void abac_verifier_init(void) { init_openssl(); init_xmlsec(); } /** * Uninitialize the system, free up any allocated memory. */ void abac_verifier_deinit(void) { abac_id_cert_t *id; while ((id = id_certs) != NULL) { HASH_DEL(id_certs, id); free(id->keyid); abac_id_free(id->cert); free(id); } deinit_xmlsec(); deinit_openssl(); } /** * Load an ID certificate. */ static int _load_id(abac_id_t *cert) { abac_id_cert_t *id_cert = NULL; char *keyid = NULL; int ret; assert(cert); // get the key ID keyid = abac_id_keyid(cert); // if we already have this cert 'error' with success HASH_FIND_STR(id_certs, keyid, id_cert); if (id_cert != NULL) { ret = ABAC_CERT_SUCCESS; goto error; } ret = abac_id_still_valid(cert); if (!ret) { ret = ABAC_CERT_INVALID; goto error; } // success, add the key to the map of certificates id_cert = abac_xmalloc(sizeof(abac_id_cert_t)); id_cert->keyid = abac_xstrdup(keyid); id_cert->cert = cert; HASH_ADD_KEYPTR(hh, id_certs, id_cert->keyid, strlen(id_cert->keyid), id_cert); return ABAC_CERT_SUCCESS; error: if (keyid != NULL) free(keyid); return ret; } abac_id_t *abac_verifier_lookup_id(char *keyid) { abac_id_cert_t *id_cert; HASH_FIND_STR(id_certs, keyid, id_cert); if(id_cert == NULL) return NULL; return id_cert->cert; } /** * Load an ID cert from a file. */ int abac_verifier_load_id_file(char *filename) { // load the cert abac_id_t *cert = abac_id_from_file(filename); if (cert == NULL) return ABAC_CERT_INVALID; return _load_id(cert); } /** * Load an ID cert from a chunk. */ int abac_verifier_load_id_chunk(abac_chunk_t chunk) { // load the cert abac_id_t *cert= abac_id_from_chunk(chunk); if (cert == NULL) return ABAC_CERT_INVALID; return _load_id(cert); } /** * Load an attribute cert. * Returns true only if the certificate is valid and is issued by the proper * authority. */ static int _load_attribute_cert(abac_attribute_t *cert, abac_credential_t **cred_ret) { abac_role_t *head_role = NULL; abac_role_t *tail_role = NULL; abac_id_cert_t *issuer=NULL; int ret; char *attr_string=abac_attribute_role_string(cert); if(attr_string == NULL) { ret = ABAC_CERT_INVALID; goto error; } // split into head/tail parts char *head_tail[2]; abac_split(attr_string, "<-", head_tail, &ret); if (ret != 2) { ret = ABAC_CERT_INVALID; goto error; } // must be a role head_role = abac_role_from_string(head_tail[0]); if (head_role == NULL) goto error; if (!abac_role_is_role(head_role)) { ret = ABAC_CERT_INVALID; goto error; } // make sure the tail's valid too tail_role = abac_role_from_string(head_tail[1]); if (tail_role == NULL) { ret = ABAC_CERT_INVALID; goto error; } char *principal = abac_role_principal(head_role); HASH_FIND_STR(id_certs, principal, issuer); if (issuer == NULL) { ret = ABAC_CERT_MISSING_ISSUER; goto error; } // check if issuer is still valid if (!abac_id_still_valid(issuer->cert)) { ret = ABAC_CERT_INVALID_ISSUER; goto error; } // make sure principal match up with keyid if(!abac_id_has_keyid(issuer->cert,principal)) { ret = ABAC_CERT_BAD_PRINCIPAL; goto error; } // at this point we know we have a good attribute cert abac_credential_t *cred = abac_xmalloc(sizeof(abac_credential_t)); cred->head = head_role; cred->tail = tail_role; cred->cert = cert; /* acme's cert */ cred->issuer = abac_id_dup(issuer->cert); cred->refcount = 1; *cred_ret = cred; return ABAC_CERT_SUCCESS; error: if (head_role != NULL) abac_role_free(head_role); if (tail_role != NULL) abac_role_free(tail_role); return ret; } static int _load_attribute_certs(abac_list_t *attr_list, abac_list_t *cred_list) { int sz=abac_list_size(attr_list); abac_credential_t *cred_ret=NULL; abac_attribute_t *attr; if(sz) { abac_list_foreach(attr_list, attr, int ret=_load_attribute_cert(attr, &cred_ret); if(ret==ABAC_CERT_SUCCESS) { abac_list_add(cred_list, cred_ret); } ); } return sz; } /** * Load an attribute cert from a file. */ int abac_verifier_load_attribute_cert_file(char *filename, abac_list_t *cred_list) { // load the cert abac_list_t *attr_list = abac_attribute_certs_from_file(filename); if (abac_list_size(attr_list) == 0) return ABAC_CERT_INVALID; int ret=_load_attribute_certs(attr_list, cred_list); if(ret) return ABAC_CERT_SUCCESS; return ABAC_CERT_INVALID; } /** * Load an attribute cert from a chunk. */ int abac_verifier_load_attribute_cert_chunk(abac_chunk_t chunk, abac_list_t *cred_list) { // load the cert abac_list_t *attr_list = abac_attribute_certs_from_chunk(chunk); if (abac_list_size(attr_list) == 0) return ABAC_CERT_INVALID; return _load_attribute_certs(attr_list,cred_list); } /** * Return the head role. */ abac_role_t *abac_credential_head(abac_credential_t *cred) { return cred->head; } /** * Return the tail role. */ abac_role_t *abac_credential_tail(abac_credential_t *cred) { return cred->tail; } /** * Return the xml chunk of the attribute cert. */ abac_chunk_t abac_credential_attribute_cert(abac_credential_t *cred) { assert(cred); assert(cred->cert); return abac_attribute_cert(cred->cert); } /** * Return the chunk of the issuer cert. */ abac_chunk_t abac_credential_issuer_cert(abac_credential_t *cred) { assert(cred); assert(cred->issuer); abac_chunk_t ret= abac_id_in_PEM(cred->issuer); return ret; } /** * Increase the ref count of a credential. */ abac_credential_t *abac_credential_dup(abac_credential_t *cred) { assert(cred != NULL); ++cred->refcount; return cred; } /** * Decrease the reference count of a credential, freeing it when it reaches 0. */ void abac_credential_free(abac_credential_t *cred) { if (cred == NULL) return; --cred->refcount; if (cred->refcount > 0) return; abac_role_free(cred->head); abac_role_free(cred->tail); abac_attribute_free(cred->cert); abac_id_free(cred->issuer); free(cred); }