source: libabac/abac_verifier.c @ f2622ee

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

1) ran with valgrind and did some leak patching

  • Property mode set to 100644
File size: 10.9 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
[ec550f7]188/**
189 * Load an ID cert from a char ptr of a X509 pem data
[f2622ee]190 * this is called from parse_privilege/parse_abac
[ec550f7]191 */
[831da18]192int abac_verifier_load_id_chars(abac_list_t *id_certs,char *naked_pem, 
[94605f2]193        abac_keyid_map_t *km) {
[bec30b5]194    /* if id_certs is NULL, don't even try to load it */
195    if(id_certs == NULL) return ABAC_CERT_SUCCESS;
[ec550f7]196    /* make a well formed pem from this */
197    char *new_naked_pem=abac_xstrdup(naked_pem);
198    char *pem=make_pem_from_naked_pem(new_naked_pem);
199    int len=strlen(pem);
200    free(new_naked_pem);
[f2622ee]201
202    abac_chunk_t chunk = { pem, len }; 
203    int rc=abac_verifier_load_id_chunk(id_certs,chunk, km);
204/* ??? MEI */
205    abac_chunk_free(chunk);
206    return rc;
207   
[ec550f7]208}
[5450aeb]209/**
210 * Load an attribute cert.
211 * Returns true only if the certificate is valid and is issued by the proper
212 * authority.
213 */
[d2b198c]214static int _load_attribute_cert(abac_list_t *id_certs,abac_attribute_t *cert, 
215        abac_credential_t **cred_ret, abac_keyid_map_t *km) {
[1743825]216    abac_role_t *head_role = NULL;
217    abac_role_t *tail_role = NULL;
[461541a]218    abac_id_cert_t *issuer=NULL;
[d2b198c]219    abac_keyid_map_t *local_names = NULL;
[461541a]220    int ret;
[5450aeb]221
[461541a]222    char *attr_string=abac_attribute_role_string(cert);
[5450aeb]223
[461541a]224    if(attr_string == NULL) {
225       ret = ABAC_CERT_INVALID;
226       goto error;
[0779c99]227    }
[5450aeb]228
229    // split into head/tail parts
[342e28f]230    char *head_tail[2];
[9f78e4c]231    ret = 2;
[461541a]232    abac_split(attr_string, "<-", head_tail, &ret);
[0779c99]233    if (ret != 2) {
234        ret = ABAC_CERT_INVALID;
235        goto error;
236    }
[5450aeb]237
238    // must be a role
[342e28f]239    head_role = abac_role_from_string(head_tail[0]);
[5450aeb]240    if (head_role == NULL) goto error;
[0779c99]241    if (!abac_role_is_role(head_role)) {
242        ret = ABAC_CERT_INVALID;
243        goto error;
244    }
[5450aeb]245
[9a411d7]246    // make sure the tail's valid too
247    tail_role = abac_role_from_string(head_tail[1]);
248    if (tail_role == NULL) {
249        ret = ABAC_CERT_INVALID;
250        goto error;
[0779c99]251    }
[5450aeb]252
[dcc1a8e]253    char *principal = abac_role_principal(head_role);
[bec30b5]254    issuer=_lookup_cert(id_certs,principal);
[0779c99]255    if (issuer == NULL) {
256        ret = ABAC_CERT_MISSING_ISSUER;
257        goto error;
258    }
[5450aeb]259
[461541a]260    // check if issuer is still valid
261    if (!abac_id_still_valid(issuer->cert)) {
262        ret = ABAC_CERT_INVALID_ISSUER;
[0779c99]263        goto error;
264    }
[5450aeb]265
[461541a]266    // make sure principal match up with keyid
267    if(!abac_id_has_keyid(issuer->cert,principal)) {
268        ret = ABAC_CERT_BAD_PRINCIPAL;
269        goto error;
270    }
[d2b198c]271
272    if ( (local_names = abac_attribute_get_keyid_map(cert)) && km ) 
273        abac_keyid_map_merge(km, local_names, 1);
[461541a]274   
[5450aeb]275    // at this point we know we have a good attribute cert
[401a054]276    abac_credential_t *cred = abac_xmalloc(sizeof(abac_credential_t));
277    cred->head = head_role;
278    cred->tail = tail_role;
279    cred->cert = cert;
[f2622ee]280
[461541a]281    /* acme's cert */
282    cred->issuer = abac_id_dup(issuer->cert);
[fbb591e]283    cred->refcount = 1;
[0779c99]284    *cred_ret = cred;
[5450aeb]285
[f2622ee]286    free(attr_string);
287
[0779c99]288    return ABAC_CERT_SUCCESS;
[5450aeb]289
290error:
[f2622ee]291    free(attr_string);
[dcc1a8e]292    if (head_role != NULL) abac_role_free(head_role);
293    if (tail_role != NULL) abac_role_free(tail_role);
[5450aeb]294
[0779c99]295    return ret;
[5450aeb]296}
[5f99fbc]297
[d2b198c]298static int _load_attribute_certs(abac_list_t *id_certs,abac_list_t *attr_list, 
299        abac_list_t *cred_list, abac_keyid_map_t *km) {
[461541a]300
301    int sz=abac_list_size(attr_list);
302    abac_credential_t *cred_ret=NULL;
303    abac_attribute_t *attr;
304    if(sz) { 
305        abac_list_foreach(attr_list, attr,
[f2622ee]306            /* attr is being used to build cred_ret, so, don't remove it */
[d2b198c]307            int ret=_load_attribute_cert(id_certs, attr, &cred_ret, km);
[461541a]308            if(ret==ABAC_CERT_SUCCESS) {
309                abac_list_add(cred_list, cred_ret);
310            }
311        );
312    }
[f2622ee]313/* ??? MEI, attr is being reused, just free the list ptr */
314    abac_list_free(attr_list);
[461541a]315    return sz;
316}
317
[e898049]318/**
319 * Load an attribute cert from a file.
320 */
[d2b198c]321int abac_verifier_load_attribute_cert_file(abac_list_t *id_certs,char *filename, abac_list_t *cred_list, abac_keyid_map_t *km) {
[461541a]322
[e898049]323    // load the cert
[bec30b5]324    abac_list_t *attr_list = abac_attribute_certs_from_file(id_certs,filename);
[461541a]325
326    if (abac_list_size(attr_list) == 0)
327        return ABAC_CERT_INVALID;
328
[d2b198c]329    int ret=_load_attribute_certs(id_certs,attr_list, cred_list, km);
[461541a]330
331    if(ret) return ABAC_CERT_SUCCESS;
[0779c99]332        return ABAC_CERT_INVALID;
[e898049]333}
334
335/**
336 * Load an attribute cert from a chunk.
337 */
[d2b198c]338int 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]339
[e898049]340    // load the cert
[bec30b5]341    abac_list_t *attr_list = abac_attribute_certs_from_chunk(id_certs,chunk);
[461541a]342
343    if (abac_list_size(attr_list) == 0)
[0779c99]344        return ABAC_CERT_INVALID;
[4721618]345
[d2b198c]346    int ret=_load_attribute_certs(id_certs,attr_list,cred_list, km);
[4721618]347
348    if(ret) return ABAC_CERT_SUCCESS;
349        return ABAC_CERT_INVALID;
[e898049]350}
351
[5f99fbc]352/**
353 * Return the head role.
354 */
[401a054]355abac_role_t *abac_credential_head(abac_credential_t *cred) {
356    return cred->head;
[5f99fbc]357}
358
359/**
360 * Return the tail role.
361 */
[401a054]362abac_role_t *abac_credential_tail(abac_credential_t *cred) {
363    return cred->tail;
[5f99fbc]364}
[85cdf53]365
366/**
[461541a]367 * Return the xml chunk of the attribute cert.
[85cdf53]368 */
[401a054]369abac_chunk_t abac_credential_attribute_cert(abac_credential_t *cred) {
[461541a]370    assert(cred); assert(cred->cert);
371    return abac_attribute_cert(cred->cert);
[85cdf53]372}
373
374/**
[461541a]375 * Return the chunk of the issuer cert.
[85cdf53]376 */
[401a054]377abac_chunk_t abac_credential_issuer_cert(abac_credential_t *cred) {
[461541a]378    assert(cred); assert(cred->issuer);
379    abac_chunk_t ret= abac_id_in_PEM(cred->issuer);
[9efbfbf]380    return ret;
[85cdf53]381}
[186cb75]382
383/**
[fbb591e]384 * Increase the ref count of a credential.
[186cb75]385 */
[401a054]386abac_credential_t *abac_credential_dup(abac_credential_t *cred) {
387    assert(cred != NULL);
[186cb75]388
[fbb591e]389    ++cred->refcount;
390    return cred;
[186cb75]391}
392
[bec30b5]393abac_id_cert_t *abac_id_cert_dup(abac_id_cert_t *id_cert) {
394    assert(id_cert != NULL && id_cert->keyid!=NULL);
395
396    abac_id_cert_t *new_cert = abac_xmalloc(sizeof(abac_id_cert_t));
397    new_cert->keyid=abac_xstrdup(id_cert->keyid);
398    new_cert->cert=abac_id_dup(id_cert->cert);
399 
400    return new_cert;
401}
402
403
[186cb75]404/**
[fbb591e]405 * Decrease the reference count of a credential, freeing it when it reaches 0.
[186cb75]406 */
[401a054]407void abac_credential_free(abac_credential_t *cred) {
408    if (cred == NULL)
[186cb75]409        return;
410
[fbb591e]411    --cred->refcount;
412    if (cred->refcount > 0)
413        return;
414
[401a054]415    abac_role_free(cred->head);
416    abac_role_free(cred->tail);
[461541a]417    abac_attribute_free(cred->cert);
418    abac_id_free(cred->issuer);
[186cb75]419
[401a054]420    free(cred);
[186cb75]421}
[bec30b5]422
423/**
424 * remove an id cert, (not reference counted, so abac_id_certs are
425 * deep-copied)
426 */
427void abac_id_cert_free(abac_id_cert_t *id_cert) {
428    if (id_cert==NULL)
429        return;
430
431    abac_id_free(id_cert->cert);
432    if(id_cert->keyid) free(id_cert->keyid);
433    free(id_cert);
434}
435
436char *abac_id_cert_keyid(abac_id_cert_t *id_cert)
437{
438    assert(id_cert);
439    return id_cert->keyid;
440}
441
[4f79997]442char *abac_id_cert_cn(abac_id_cert_t *id_cert)
443{
444    assert(id_cert);
445    return abac_id_cn(id_cert->cert);
446}
447
[bec30b5]448
449
Note: See TracBrowser for help on using the repository browser.