source: libabac/abac_verifier.c @ 7764378

abac0-leak
Last change on this file since 7764378 was 7764378, checked in by Mei <mei@…>, 11 years ago

1) tweak according valgrind's leak report

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