source: libabac/abac_verifier.c @ bec30b5

abac0-leakabac0-meimei-idtvf-new-xml
Last change on this file since bec30b5 was bec30b5, checked in by Mei <mei@…>, 11 years ago

1) change abac_context_load_directory to check on every regular files

and try to extract id id/privkey and then attribute in turn.

2) move id_certs to be context based instead of shared globally

  • Property mode set to 100644
File size: 9.6 KB
RevLine 
[186cb75]1
[461541a]2/* abac_verifier.c */
3#include <assert.h>
[5450aeb]4
[461541a]5#include "libabac_common.h"
6#include "abac.h"
[342e28f]7#include "abac_list.h"
[3c251d0]8#include "abac_util.h"
[5450aeb]9
[bec30b5]10struct _abac_id_cert_t {
[461541a]11    char *keyid; // using cert's keyid
12    abac_id_t *cert;
[bec30b5]13};
[5450aeb]14
[401a054]15struct _abac_credential_t {
[1743825]16    abac_role_t *head;
17    abac_role_t *tail;
[461541a]18    abac_id_t *issuer; /* Acme of Acme.customer <- Coyote */
19    abac_attribute_t *cert;
[fbb591e]20
21    int refcount;
[5f99fbc]22};
23
[ec550f7]24extern int abac_id_pass_privkey_from_id(abac_id_t *to_id, abac_id_t *from_id);
25extern char *make_pem_from_naked_pem(char *naked_pem);
26
[461541a]27/***********************************************************************/
[5450aeb]28// convert a chunk to a lowercase binary string
29// malloc's the string
[9ac7fb4]30/*** not used,
[461541a]31static char *_chunk_to_string(abac_chunk_t chunk) {
[5450aeb]32    int i;
33
[3c251d0]34    char *ret = abac_xmalloc(chunk.len * 2 + 1);
[5450aeb]35
36    for (i = 0; i < chunk.len; ++i)
37        sprintf(ret + 2 * i, "%02x", chunk.ptr[i]);
38
39    return ret;
40}
[9ac7fb4]41***/
[5450aeb]42
43// verify that cert was issued by issuer
44// cert and issuer can be the same, in which case the self-sig is validated
[9ac7fb4]45/*** not used
[461541a]46static int _verify_cert(abac_id_t *cert) {
47
48    assert(cert);
49    if(!abac_id_still_valid(cert))
50        return 0;
51    return 1;
[5450aeb]52}
[9ac7fb4]53***/
[5450aeb]54
55/**
56 * Init the verifier subsystem.
57 */
[43e3b71]58void abac_verifier_init(void) {
[461541a]59    init_openssl();
60    init_xmlsec();
[5450aeb]61}
62
[186cb75]63/**
64 * Uninitialize the system, free up any allocated memory.
65 */
[43e3b71]66void abac_verifier_deinit(void) {
[461541a]67    deinit_xmlsec();
68    deinit_openssl();
[186cb75]69}
70
[bec30b5]71static abac_id_cert_t *_lookup_cert(abac_list_t *id_certs, char *keyid)
72{
73    abac_id_cert_t *id_cert=NULL;
74    if(id_certs != NULL) {
75        abac_list_foreach(id_certs, id_cert,
76            if(strcmp(keyid,id_cert->keyid)==0)
77               return id_cert;
78        );
79    }
80    return NULL;
81}
82
[5450aeb]83/**
84 * Load an ID certificate.
85 */
[bec30b5]86static int _load_id(abac_list_t *id_certs, abac_id_t *cert) {
[327e808]87    abac_id_cert_t *id_cert = NULL;
[5450aeb]88    char *keyid = NULL;
89    int ret;
90
[461541a]91    assert(cert);
[5450aeb]92
[2ef48fa]93    // get the key ID
[461541a]94    keyid = abac_id_keyid(cert);
[327e808]95
96    // if we already have this cert 'error' with success
[bec30b5]97    id_cert=_lookup_cert(id_certs, keyid);
98
[327e808]99    if (id_cert != NULL) {
[ec550f7]100        /* special case, if
101           existing id does not have private key and
102           incoming does, then need to bring that bit of
103           information in */
104           if(abac_id_has_privkey(cert) && 
105                       !abac_id_has_privkey(id_cert->cert)) {
106               abac_id_pass_privkey_from_id(id_cert->cert, cert);
107           }
[327e808]108        ret = ABAC_CERT_SUCCESS;
109        goto error;
110    }
111
[461541a]112    ret = abac_id_still_valid(cert);
[0779c99]113    if (!ret) {
[461541a]114        ret = ABAC_CERT_INVALID;
[0779c99]115        goto error;
116    }
[5450aeb]117
118    // success, add the key to the map of certificates
[327e808]119    id_cert = abac_xmalloc(sizeof(abac_id_cert_t));
[461541a]120    id_cert->keyid = abac_xstrdup(keyid);
[5450aeb]121    id_cert->cert = cert;
[bec30b5]122    abac_list_add(id_certs, id_cert); 
[5450aeb]123
[0779c99]124    return ABAC_CERT_SUCCESS;
[5450aeb]125
126error:
[327e808]127    if (keyid != NULL) free(keyid);
[0779c99]128    return ret;
[5450aeb]129}
130
[bec30b5]131abac_id_t *abac_verifier_lookup_id(abac_list_t *id_certs, char *keyid)
[461541a]132{
[bec30b5]133    abac_id_cert_t *id_cert=_lookup_cert(id_certs,keyid);
[461541a]134    if(id_cert == NULL) return NULL;
135    return id_cert->cert;
136}
137
[e898049]138/**
139 * Load an ID cert from a file.
140 */
[bec30b5]141int abac_verifier_load_id_file(abac_list_t *id_certs,char *filename) {
[e898049]142    // load the cert
[461541a]143    abac_id_t *cert = abac_id_from_file(filename);
144
[e898049]145    if (cert == NULL)
[0779c99]146        return ABAC_CERT_INVALID;
[461541a]147
[bec30b5]148    return _load_id(id_certs,cert);
[e898049]149}
150
151/**
152 * Load an ID cert from a chunk.
153 */
[bec30b5]154int abac_verifier_load_id_chunk(abac_list_t *id_certs,abac_chunk_t chunk) {
[461541a]155
[e898049]156    // load the cert
[461541a]157    abac_id_t *cert= abac_id_from_chunk(chunk);
158
[e898049]159    if (cert == NULL)
[0779c99]160        return ABAC_CERT_INVALID;
[bec30b5]161    return _load_id(id_certs,cert);
[e898049]162}
163
[ec550f7]164/**
165 * Load an ID cert from a char ptr of a X509 pem data
166 * this is called from parse_privilege(..)
167 */
[bec30b5]168int abac_verifier_load_id_chars(void *id_certs,char *naked_pem) {
169    /* if id_certs is NULL, don't even try to load it */
170    if(id_certs == NULL) return ABAC_CERT_SUCCESS;
[ec550f7]171    /* make a well formed pem from this */
172    char *new_naked_pem=abac_xstrdup(naked_pem);
173    char *pem=make_pem_from_naked_pem(new_naked_pem);
174    int len=strlen(pem);
175    abac_chunk_t chunk = { pem, len }; 
176    free(new_naked_pem);
[bec30b5]177    return abac_verifier_load_id_chunk((abac_list_t *)id_certs,chunk);
[ec550f7]178}
[5450aeb]179/**
180 * Load an attribute cert.
181 * Returns true only if the certificate is valid and is issued by the proper
182 * authority.
183 */
[bec30b5]184static int _load_attribute_cert(abac_list_t *id_certs,abac_attribute_t *cert, abac_credential_t **cred_ret) {
[1743825]185    abac_role_t *head_role = NULL;
186    abac_role_t *tail_role = NULL;
[461541a]187    abac_id_cert_t *issuer=NULL;
188    int ret;
[5450aeb]189
[461541a]190    char *attr_string=abac_attribute_role_string(cert);
[5450aeb]191
[461541a]192    if(attr_string == NULL) {
193       ret = ABAC_CERT_INVALID;
194       goto error;
[0779c99]195    }
[5450aeb]196
197    // split into head/tail parts
[342e28f]198    char *head_tail[2];
[461541a]199    abac_split(attr_string, "<-", head_tail, &ret);
[0779c99]200    if (ret != 2) {
201        ret = ABAC_CERT_INVALID;
202        goto error;
203    }
[5450aeb]204
205    // must be a role
[342e28f]206    head_role = abac_role_from_string(head_tail[0]);
[5450aeb]207    if (head_role == NULL) goto error;
[0779c99]208    if (!abac_role_is_role(head_role)) {
209        ret = ABAC_CERT_INVALID;
210        goto error;
211    }
[5450aeb]212
[9a411d7]213    // make sure the tail's valid too
214    tail_role = abac_role_from_string(head_tail[1]);
215    if (tail_role == NULL) {
216        ret = ABAC_CERT_INVALID;
217        goto error;
[0779c99]218    }
[5450aeb]219
[dcc1a8e]220    char *principal = abac_role_principal(head_role);
[bec30b5]221    issuer=_lookup_cert(id_certs,principal);
[0779c99]222    if (issuer == NULL) {
223        ret = ABAC_CERT_MISSING_ISSUER;
224        goto error;
225    }
[5450aeb]226
[461541a]227    // check if issuer is still valid
228    if (!abac_id_still_valid(issuer->cert)) {
229        ret = ABAC_CERT_INVALID_ISSUER;
[0779c99]230        goto error;
231    }
[5450aeb]232
[461541a]233    // make sure principal match up with keyid
234    if(!abac_id_has_keyid(issuer->cert,principal)) {
235        ret = ABAC_CERT_BAD_PRINCIPAL;
236        goto error;
237    }
238   
[5450aeb]239    // at this point we know we have a good attribute cert
[401a054]240    abac_credential_t *cred = abac_xmalloc(sizeof(abac_credential_t));
241    cred->head = head_role;
242    cred->tail = tail_role;
243    cred->cert = cert;
[461541a]244    /* acme's cert */
245    cred->issuer = abac_id_dup(issuer->cert);
[fbb591e]246    cred->refcount = 1;
[0779c99]247    *cred_ret = cred;
[5450aeb]248
[0779c99]249    return ABAC_CERT_SUCCESS;
[5450aeb]250
251error:
[dcc1a8e]252    if (head_role != NULL) abac_role_free(head_role);
253    if (tail_role != NULL) abac_role_free(tail_role);
[5450aeb]254
[0779c99]255    return ret;
[5450aeb]256}
[5f99fbc]257
[bec30b5]258static int _load_attribute_certs(abac_list_t *id_certs,abac_list_t *attr_list, abac_list_t *cred_list) {
[461541a]259
260    int sz=abac_list_size(attr_list);
261    abac_credential_t *cred_ret=NULL;
262    abac_attribute_t *attr;
263    if(sz) { 
264        abac_list_foreach(attr_list, attr,
[bec30b5]265            int ret=_load_attribute_cert(id_certs, attr, &cred_ret);
[461541a]266            if(ret==ABAC_CERT_SUCCESS) {
267                abac_list_add(cred_list, cred_ret);
268            }
269        );
270    }
271    return sz;
272}
273
[e898049]274/**
275 * Load an attribute cert from a file.
276 */
[bec30b5]277int abac_verifier_load_attribute_cert_file(abac_list_t *id_certs,char *filename, abac_list_t *cred_list) {
[461541a]278
[e898049]279    // load the cert
[bec30b5]280    abac_list_t *attr_list = abac_attribute_certs_from_file(id_certs,filename);
[461541a]281
282    if (abac_list_size(attr_list) == 0)
283        return ABAC_CERT_INVALID;
284
[bec30b5]285    int ret=_load_attribute_certs(id_certs,attr_list, cred_list);
[461541a]286
287    if(ret) return ABAC_CERT_SUCCESS;
[0779c99]288        return ABAC_CERT_INVALID;
[e898049]289}
290
291/**
292 * Load an attribute cert from a chunk.
293 */
[bec30b5]294int abac_verifier_load_attribute_cert_chunk(abac_list_t *id_certs,abac_chunk_t chunk, abac_list_t *cred_list) {
[461541a]295
[e898049]296    // load the cert
[bec30b5]297    abac_list_t *attr_list = abac_attribute_certs_from_chunk(id_certs,chunk);
[461541a]298
299    if (abac_list_size(attr_list) == 0)
[0779c99]300        return ABAC_CERT_INVALID;
[4721618]301
[bec30b5]302    int ret=_load_attribute_certs(id_certs,attr_list,cred_list);
[4721618]303
304    if(ret) return ABAC_CERT_SUCCESS;
305        return ABAC_CERT_INVALID;
[e898049]306}
307
[5f99fbc]308/**
309 * Return the head role.
310 */
[401a054]311abac_role_t *abac_credential_head(abac_credential_t *cred) {
312    return cred->head;
[5f99fbc]313}
314
315/**
316 * Return the tail role.
317 */
[401a054]318abac_role_t *abac_credential_tail(abac_credential_t *cred) {
319    return cred->tail;
[5f99fbc]320}
[85cdf53]321
322/**
[461541a]323 * Return the xml chunk of the attribute cert.
[85cdf53]324 */
[401a054]325abac_chunk_t abac_credential_attribute_cert(abac_credential_t *cred) {
[461541a]326    assert(cred); assert(cred->cert);
327    return abac_attribute_cert(cred->cert);
[85cdf53]328}
329
330/**
[461541a]331 * Return the chunk of the issuer cert.
[85cdf53]332 */
[401a054]333abac_chunk_t abac_credential_issuer_cert(abac_credential_t *cred) {
[461541a]334    assert(cred); assert(cred->issuer);
335    abac_chunk_t ret= abac_id_in_PEM(cred->issuer);
[9efbfbf]336    return ret;
[85cdf53]337}
[186cb75]338
339/**
[fbb591e]340 * Increase the ref count of a credential.
[186cb75]341 */
[401a054]342abac_credential_t *abac_credential_dup(abac_credential_t *cred) {
343    assert(cred != NULL);
[186cb75]344
[fbb591e]345    ++cred->refcount;
346    return cred;
[186cb75]347}
348
[bec30b5]349abac_id_cert_t *abac_id_cert_dup(abac_id_cert_t *id_cert) {
350    assert(id_cert != NULL && id_cert->keyid!=NULL);
351
352    abac_id_cert_t *new_cert = abac_xmalloc(sizeof(abac_id_cert_t));
353    new_cert->keyid=abac_xstrdup(id_cert->keyid);
354    new_cert->cert=abac_id_dup(id_cert->cert);
355 
356    return new_cert;
357}
358
359
[186cb75]360/**
[fbb591e]361 * Decrease the reference count of a credential, freeing it when it reaches 0.
[186cb75]362 */
[401a054]363void abac_credential_free(abac_credential_t *cred) {
364    if (cred == NULL)
[186cb75]365        return;
366
[fbb591e]367    --cred->refcount;
368    if (cred->refcount > 0)
369        return;
370
[401a054]371    abac_role_free(cred->head);
372    abac_role_free(cred->tail);
[461541a]373    abac_attribute_free(cred->cert);
374    abac_id_free(cred->issuer);
[186cb75]375
[401a054]376    free(cred);
[186cb75]377}
[bec30b5]378
379/**
380 * remove an id cert, (not reference counted, so abac_id_certs are
381 * deep-copied)
382 */
383void abac_id_cert_free(abac_id_cert_t *id_cert) {
384    if (id_cert==NULL)
385        return;
386
387    abac_id_free(id_cert->cert);
388    if(id_cert->keyid) free(id_cert->keyid);
389    free(id_cert);
390}
391
392char *abac_id_cert_keyid(abac_id_cert_t *id_cert)
393{
394    assert(id_cert);
395    return id_cert->keyid;
396}
397
398
399
Note: See TracBrowser for help on using the repository browser.