source: libabac/abac_verifier.c @ ea38d81

abac0-leak
Last change on this file since ea38d81 was ea38d81, checked in by Ted Faber <faber@…>, 11 years ago

Memory leak in queries, and a couple other incidental ones.

  • Property mode set to 100644
File size: 10.4 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 */
[94605f2]87static int _load_id(abac_list_t *id_certs, abac_id_t *cert, 
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
[461541a]94    assert(cert);
[5450aeb]95
[2ef48fa]96    // get the key ID
[461541a]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 */
107           if(abac_id_has_privkey(cert) && 
108                       !abac_id_has_privkey(id_cert->cert)) {
109               abac_id_pass_privkey_from_id(id_cert->cert, cert);
110           }
[327e808]111        ret = ABAC_CERT_SUCCESS;
112        goto error;
113    }
114
[461541a]115    ret = abac_id_still_valid(cert);
[0779c99]116    if (!ret) {
[461541a]117        ret = ABAC_CERT_INVALID;
[0779c99]118        goto error;
119    }
[5450aeb]120
121    // success, add the key to the map of certificates
[327e808]122    id_cert = abac_xmalloc(sizeof(abac_id_cert_t));
[461541a]123    id_cert->keyid = abac_xstrdup(keyid);
[5450aeb]124    id_cert->cert = cert;
[bec30b5]125    abac_list_add(id_certs, id_cert); 
[94605f2]126    /* Add the new id and issuer to the keyid <-> name map */
127    if ( km && keyid && cert ) {
128        if ( (nick= abac_id_issuer(cert)) ) {
129            /* If the issuer starts with /CN=, as many do,
130             * trim the /CN= off */
131            if (!strncmp(nick, "/CN=", 4) && strlen(nick) > 4) 
132                abac_keyid_map_add_nickname(km, keyid, nick+4);
133            else
134                abac_keyid_map_add_nickname(km, keyid, nick);
135            free(nick);
136        }
137    }
138
[5450aeb]139
[0779c99]140    return ABAC_CERT_SUCCESS;
[5450aeb]141
142error:
[ea38d81]143    // No one owns cert, so delete it.
144    if (cert != NULL) free(cert);
[327e808]145    if (keyid != NULL) free(keyid);
[0779c99]146    return ret;
[5450aeb]147}
148
[bec30b5]149abac_id_t *abac_verifier_lookup_id(abac_list_t *id_certs, char *keyid)
[461541a]150{
[bec30b5]151    abac_id_cert_t *id_cert=_lookup_cert(id_certs,keyid);
[461541a]152    if(id_cert == NULL) return NULL;
153    return id_cert->cert;
154}
155
[e898049]156/**
157 * Load an ID cert from a file.
158 */
[94605f2]159int abac_verifier_load_id_file(abac_list_t *id_certs, char *filename,
160        abac_keyid_map_t *km) {
[e898049]161    // load the cert
[461541a]162    abac_id_t *cert = abac_id_from_file(filename);
163
[e898049]164    if (cert == NULL)
[0779c99]165        return ABAC_CERT_INVALID;
[461541a]166
[94605f2]167    return _load_id(id_certs,cert, km);
[e898049]168}
169
170/**
171 * Load an ID cert from a chunk.
172 */
[94605f2]173int abac_verifier_load_id_chunk(abac_list_t *id_certs,abac_chunk_t chunk, 
174        abac_keyid_map_t *km) {
[461541a]175
[e898049]176    // load the cert
[461541a]177    abac_id_t *cert= abac_id_from_chunk(chunk);
178
[e898049]179    if (cert == NULL)
[0779c99]180        return ABAC_CERT_INVALID;
[94605f2]181    return _load_id(id_certs,cert, km);
[e898049]182}
183
[ec550f7]184/**
185 * Load an ID cert from a char ptr of a X509 pem data
186 * this is called from parse_privilege(..)
187 */
[94605f2]188int abac_verifier_load_id_chars(void *id_certs,char *naked_pem, 
189        abac_keyid_map_t *km) {
[bec30b5]190    /* if id_certs is NULL, don't even try to load it */
191    if(id_certs == NULL) return ABAC_CERT_SUCCESS;
[ec550f7]192    /* make a well formed pem from this */
193    char *new_naked_pem=abac_xstrdup(naked_pem);
194    char *pem=make_pem_from_naked_pem(new_naked_pem);
195    int len=strlen(pem);
196    abac_chunk_t chunk = { pem, len }; 
197    free(new_naked_pem);
[94605f2]198    return abac_verifier_load_id_chunk((abac_list_t *)id_certs,chunk, km);
[ec550f7]199}
[5450aeb]200/**
201 * Load an attribute cert.
202 * Returns true only if the certificate is valid and is issued by the proper
203 * authority.
204 */
[d2b198c]205static int _load_attribute_cert(abac_list_t *id_certs,abac_attribute_t *cert, 
206        abac_credential_t **cred_ret, abac_keyid_map_t *km) {
[1743825]207    abac_role_t *head_role = NULL;
208    abac_role_t *tail_role = NULL;
[461541a]209    abac_id_cert_t *issuer=NULL;
[d2b198c]210    abac_keyid_map_t *local_names = NULL;
[461541a]211    int ret;
[5450aeb]212
[461541a]213    char *attr_string=abac_attribute_role_string(cert);
[5450aeb]214
[461541a]215    if(attr_string == NULL) {
216       ret = ABAC_CERT_INVALID;
217       goto error;
[0779c99]218    }
[5450aeb]219
220    // split into head/tail parts
[342e28f]221    char *head_tail[2];
[9f78e4c]222    ret = 2;
[461541a]223    abac_split(attr_string, "<-", head_tail, &ret);
[0779c99]224    if (ret != 2) {
225        ret = ABAC_CERT_INVALID;
226        goto error;
227    }
[5450aeb]228
229    // must be a role
[342e28f]230    head_role = abac_role_from_string(head_tail[0]);
[5450aeb]231    if (head_role == NULL) goto error;
[0779c99]232    if (!abac_role_is_role(head_role)) {
233        ret = ABAC_CERT_INVALID;
234        goto error;
235    }
[5450aeb]236
[9a411d7]237    // make sure the tail's valid too
238    tail_role = abac_role_from_string(head_tail[1]);
239    if (tail_role == NULL) {
240        ret = ABAC_CERT_INVALID;
241        goto error;
[0779c99]242    }
[5450aeb]243
[dcc1a8e]244    char *principal = abac_role_principal(head_role);
[bec30b5]245    issuer=_lookup_cert(id_certs,principal);
[0779c99]246    if (issuer == NULL) {
247        ret = ABAC_CERT_MISSING_ISSUER;
248        goto error;
249    }
[5450aeb]250
[461541a]251    // check if issuer is still valid
252    if (!abac_id_still_valid(issuer->cert)) {
253        ret = ABAC_CERT_INVALID_ISSUER;
[0779c99]254        goto error;
255    }
[5450aeb]256
[461541a]257    // make sure principal match up with keyid
258    if(!abac_id_has_keyid(issuer->cert,principal)) {
259        ret = ABAC_CERT_BAD_PRINCIPAL;
260        goto error;
261    }
[d2b198c]262
263    if ( (local_names = abac_attribute_get_keyid_map(cert)) && km ) 
264        abac_keyid_map_merge(km, local_names, 1);
[461541a]265   
[5450aeb]266    // at this point we know we have a good attribute cert
[401a054]267    abac_credential_t *cred = abac_xmalloc(sizeof(abac_credential_t));
268    cred->head = head_role;
269    cred->tail = tail_role;
270    cred->cert = cert;
[461541a]271    /* acme's cert */
272    cred->issuer = abac_id_dup(issuer->cert);
[fbb591e]273    cred->refcount = 1;
[0779c99]274    *cred_ret = cred;
[5450aeb]275
[0779c99]276    return ABAC_CERT_SUCCESS;
[5450aeb]277
278error:
[dcc1a8e]279    if (head_role != NULL) abac_role_free(head_role);
280    if (tail_role != NULL) abac_role_free(tail_role);
[5450aeb]281
[0779c99]282    return ret;
[5450aeb]283}
[5f99fbc]284
[d2b198c]285static int _load_attribute_certs(abac_list_t *id_certs,abac_list_t *attr_list, 
286        abac_list_t *cred_list, abac_keyid_map_t *km) {
[461541a]287
288    int sz=abac_list_size(attr_list);
289    abac_credential_t *cred_ret=NULL;
290    abac_attribute_t *attr;
291    if(sz) { 
292        abac_list_foreach(attr_list, attr,
[d2b198c]293            int ret=_load_attribute_cert(id_certs, attr, &cred_ret, km);
[461541a]294            if(ret==ABAC_CERT_SUCCESS) {
295                abac_list_add(cred_list, cred_ret);
296            }
297        );
298    }
299    return sz;
300}
301
[e898049]302/**
303 * Load an attribute cert from a file.
304 */
[d2b198c]305int abac_verifier_load_attribute_cert_file(abac_list_t *id_certs,char *filename, abac_list_t *cred_list, abac_keyid_map_t *km) {
[461541a]306
[e898049]307    // load the cert
[bec30b5]308    abac_list_t *attr_list = abac_attribute_certs_from_file(id_certs,filename);
[461541a]309
310    if (abac_list_size(attr_list) == 0)
311        return ABAC_CERT_INVALID;
312
[d2b198c]313    int ret=_load_attribute_certs(id_certs,attr_list, cred_list, km);
[461541a]314
315    if(ret) return ABAC_CERT_SUCCESS;
[0779c99]316        return ABAC_CERT_INVALID;
[e898049]317}
318
319/**
320 * Load an attribute cert from a chunk.
321 */
[d2b198c]322int 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]323
[e898049]324    // load the cert
[bec30b5]325    abac_list_t *attr_list = abac_attribute_certs_from_chunk(id_certs,chunk);
[461541a]326
327    if (abac_list_size(attr_list) == 0)
[0779c99]328        return ABAC_CERT_INVALID;
[4721618]329
[d2b198c]330    int ret=_load_attribute_certs(id_certs,attr_list,cred_list, km);
[4721618]331
332    if(ret) return ABAC_CERT_SUCCESS;
333        return ABAC_CERT_INVALID;
[e898049]334}
335
[5f99fbc]336/**
337 * Return the head role.
338 */
[401a054]339abac_role_t *abac_credential_head(abac_credential_t *cred) {
340    return cred->head;
[5f99fbc]341}
342
343/**
344 * Return the tail role.
345 */
[401a054]346abac_role_t *abac_credential_tail(abac_credential_t *cred) {
347    return cred->tail;
[5f99fbc]348}
[85cdf53]349
350/**
[461541a]351 * Return the xml chunk of the attribute cert.
[85cdf53]352 */
[401a054]353abac_chunk_t abac_credential_attribute_cert(abac_credential_t *cred) {
[461541a]354    assert(cred); assert(cred->cert);
355    return abac_attribute_cert(cred->cert);
[85cdf53]356}
357
358/**
[461541a]359 * Return the chunk of the issuer cert.
[85cdf53]360 */
[401a054]361abac_chunk_t abac_credential_issuer_cert(abac_credential_t *cred) {
[461541a]362    assert(cred); assert(cred->issuer);
363    abac_chunk_t ret= abac_id_in_PEM(cred->issuer);
[9efbfbf]364    return ret;
[85cdf53]365}
[186cb75]366
367/**
[fbb591e]368 * Increase the ref count of a credential.
[186cb75]369 */
[401a054]370abac_credential_t *abac_credential_dup(abac_credential_t *cred) {
371    assert(cred != NULL);
[186cb75]372
[fbb591e]373    ++cred->refcount;
374    return cred;
[186cb75]375}
376
[bec30b5]377abac_id_cert_t *abac_id_cert_dup(abac_id_cert_t *id_cert) {
378    assert(id_cert != NULL && id_cert->keyid!=NULL);
379
380    abac_id_cert_t *new_cert = abac_xmalloc(sizeof(abac_id_cert_t));
381    new_cert->keyid=abac_xstrdup(id_cert->keyid);
382    new_cert->cert=abac_id_dup(id_cert->cert);
383 
384    return new_cert;
385}
386
387
[186cb75]388/**
[fbb591e]389 * Decrease the reference count of a credential, freeing it when it reaches 0.
[186cb75]390 */
[401a054]391void abac_credential_free(abac_credential_t *cred) {
392    if (cred == NULL)
[186cb75]393        return;
394
[fbb591e]395    --cred->refcount;
396    if (cred->refcount > 0)
397        return;
398
[401a054]399    abac_role_free(cred->head);
400    abac_role_free(cred->tail);
[461541a]401    abac_attribute_free(cred->cert);
402    abac_id_free(cred->issuer);
[186cb75]403
[401a054]404    free(cred);
[186cb75]405}
[bec30b5]406
407/**
408 * remove an id cert, (not reference counted, so abac_id_certs are
409 * deep-copied)
410 */
411void abac_id_cert_free(abac_id_cert_t *id_cert) {
412    if (id_cert==NULL)
413        return;
414
415    abac_id_free(id_cert->cert);
416    if(id_cert->keyid) free(id_cert->keyid);
417    free(id_cert);
418}
419
420char *abac_id_cert_keyid(abac_id_cert_t *id_cert)
421{
422    assert(id_cert);
423    return id_cert->keyid;
424}
425
426
427
Note: See TracBrowser for help on using the repository browser.