source: libabac/abac_verifier.c @ 91a6b20

abac0-leak
Last change on this file since 91a6b20 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
Line 
1
2/* abac_verifier.c */
3#include <assert.h>
4#include <string.h>
5
6#include "libabac_common.h"
7#include "abac.h"
8#include "abac_list.h"
9#include "abac_util.h"
10
11struct _abac_id_cert_t {
12    char *keyid; // using cert's keyid
13    abac_id_t *cert;
14};
15
16struct _abac_credential_t {
17    abac_role_t *head;
18    abac_role_t *tail;
19    abac_id_t *issuer; /* Acme of Acme.customer <- Coyote */
20    abac_attribute_t *cert;
21
22    int refcount;
23};
24
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
28/***********************************************************************/
29// convert a chunk to a lowercase binary string
30// malloc's the string
31/*** not used,
32static char *_chunk_to_string(abac_chunk_t chunk) {
33    int i;
34
35    char *ret = abac_xmalloc(chunk.len * 2 + 1);
36
37    for (i = 0; i < chunk.len; ++i)
38        sprintf(ret + 2 * i, "%02x", chunk.ptr[i]);
39
40    return ret;
41}
42***/
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
46/*** not used
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;
53}
54***/
55
56/**
57 * Init the verifier subsystem.
58 */
59void abac_verifier_init(void) {
60    init_openssl();
61    init_xmlsec();
62}
63
64/**
65 * Uninitialize the system, free up any allocated memory.
66 */
67void abac_verifier_deinit(void) {
68    deinit_xmlsec();
69    deinit_openssl();
70}
71
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
84/**
85 * Load an ID certificate.
86 */
87static int _load_id(abac_list_t *id_certs, abac_id_t **cert, 
88        abac_keyid_map_t *km) {
89    abac_id_cert_t *id_cert = NULL;
90    char *keyid = NULL;
91    char *nick = NULL;
92    int ret;
93
94    assert(*cert);
95
96    // get the key ID
97    keyid = abac_id_keyid(*cert);
98
99    // if we already have this cert 'error' with success
100    id_cert=_lookup_cert(id_certs, keyid);
101
102    if (id_cert != NULL) {
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           }
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; 
115        goto error;
116    }
117
118    ret = abac_id_still_valid(*cert);
119    if (!ret) {
120        ret = ABAC_CERT_INVALID;
121        goto error;
122    }
123
124    // success, add the key to the map of certificates
125    id_cert = abac_xmalloc(sizeof(abac_id_cert_t));
126    id_cert->keyid = abac_xstrdup(keyid);
127    id_cert->cert = *cert;
128    abac_list_add(id_certs, id_cert); 
129    /* Add the new id and issuer to the keyid <-> name map */
130    if ( km && keyid && *cert ) {
131        if ( (nick= abac_id_issuer(*cert)) ) {
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
142
143    return ABAC_CERT_SUCCESS;
144
145error:
146    // No one owns cert, so delete it.
147    if (*cert != NULL) abac_id_free(*cert);
148
149    return ret;
150}
151
152abac_id_t *abac_verifier_lookup_id(abac_list_t *id_certs, char *keyid)
153{
154    abac_id_cert_t *id_cert=_lookup_cert(id_certs,keyid);
155    if(id_cert == NULL) return NULL;
156    return id_cert->cert;
157}
158
159/**
160 * Load an ID cert from a file.
161 */
162int abac_verifier_load_id_file(abac_list_t *id_certs, char *filename,
163        abac_keyid_map_t *km) {
164    // load the cert
165    abac_id_t *cert = abac_id_from_file(filename);
166
167    if (cert == NULL)
168        return ABAC_CERT_INVALID;
169
170    return _load_id(id_certs,&cert, km);
171}
172
173/**
174 * Load an ID cert from a chunk.
175 */
176int abac_verifier_load_id_chunk(abac_list_t *id_certs,abac_chunk_t chunk, 
177        abac_keyid_map_t *km) {
178
179    // load the cert
180    abac_id_t *cert= abac_id_from_chunk(chunk);
181
182    if (cert == NULL)
183        return ABAC_CERT_INVALID;
184
185    return _load_id(id_certs,&cert, km);
186}
187
188/**
189 * Load an ID cert from a char ptr of a X509 pem data
190 * this is called from parse_privilege/parse_abac
191 */
192int abac_verifier_load_id_chars(abac_list_t *id_certs,char *naked_pem, 
193        abac_keyid_map_t *km) {
194    /* if id_certs is NULL, don't even try to load it */
195    if(id_certs == NULL) return ABAC_CERT_SUCCESS;
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);
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   
208}
209/**
210 * Load an attribute cert.
211 * Returns true only if the certificate is valid and is issued by the proper
212 * authority.
213 */
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) {
216    abac_role_t *head_role = NULL;
217    abac_role_t *tail_role = NULL;
218    abac_id_cert_t *issuer=NULL;
219    abac_keyid_map_t *local_names = NULL;
220    int ret;
221
222    char *attr_string=abac_attribute_role_string(cert);
223
224    if(attr_string == NULL) {
225       ret = ABAC_CERT_INVALID;
226       goto error;
227    }
228
229    // split into head/tail parts
230    char *head_tail[2];
231    ret = 2;
232    abac_split(attr_string, "<-", head_tail, &ret);
233    if (ret != 2) {
234        ret = ABAC_CERT_INVALID;
235        goto error;
236    }
237
238    // must be a role
239    head_role = abac_role_from_string(head_tail[0]);
240    if (head_role == NULL) goto error;
241    if (!abac_role_is_role(head_role)) {
242        ret = ABAC_CERT_INVALID;
243        goto error;
244    }
245
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;
251    }
252
253    char *principal = abac_role_principal(head_role);
254    issuer=_lookup_cert(id_certs,principal);
255    if (issuer == NULL) {
256        ret = ABAC_CERT_MISSING_ISSUER;
257        goto error;
258    }
259
260    // check if issuer is still valid
261    if (!abac_id_still_valid(issuer->cert)) {
262        ret = ABAC_CERT_INVALID_ISSUER;
263        goto error;
264    }
265
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    }
271
272    if ( (local_names = abac_attribute_get_keyid_map(cert)) && km ) 
273        abac_keyid_map_merge(km, local_names, 1);
274   
275    // at this point we know we have a good attribute cert
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;
280
281    /* acme's cert */
282    cred->issuer = abac_id_dup(issuer->cert);
283    cred->refcount = 1;
284    *cred_ret = cred;
285
286    free(attr_string);
287
288    return ABAC_CERT_SUCCESS;
289
290error:
291    free(attr_string);
292    if (head_role != NULL) abac_role_free(head_role);
293    if (tail_role != NULL) abac_role_free(tail_role);
294
295    return ret;
296}
297
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) {
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,
306            /* attr is being used to build cred_ret, so, don't remove it */
307            int ret=_load_attribute_cert(id_certs, attr, &cred_ret, km);
308            if(ret==ABAC_CERT_SUCCESS) {
309                abac_list_add(cred_list, cred_ret);
310            }
311        );
312    }
313/* ??? MEI, attr is being reused, just free the list ptr */
314    abac_list_free(attr_list);
315    return sz;
316}
317
318/**
319 * Load an attribute cert from a file.
320 */
321int abac_verifier_load_attribute_cert_file(abac_list_t *id_certs,char *filename, abac_list_t *cred_list, abac_keyid_map_t *km) {
322
323    // load the cert
324    abac_list_t *attr_list = abac_attribute_certs_from_file(id_certs,filename);
325
326    if (abac_list_size(attr_list) == 0)
327        return ABAC_CERT_INVALID;
328
329    int ret=_load_attribute_certs(id_certs,attr_list, cred_list, km);
330
331    if(ret) return ABAC_CERT_SUCCESS;
332        return ABAC_CERT_INVALID;
333}
334
335/**
336 * Load an attribute cert from a chunk.
337 */
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) {
339
340    // load the cert
341    abac_list_t *attr_list = abac_attribute_certs_from_chunk(id_certs,chunk);
342
343    if (abac_list_size(attr_list) == 0)
344        return ABAC_CERT_INVALID;
345
346    int ret=_load_attribute_certs(id_certs,attr_list,cred_list, km);
347
348    if(ret) return ABAC_CERT_SUCCESS;
349        return ABAC_CERT_INVALID;
350}
351
352/**
353 * Return the head role.
354 */
355abac_role_t *abac_credential_head(abac_credential_t *cred) {
356    return cred->head;
357}
358
359/**
360 * Return the tail role.
361 */
362abac_role_t *abac_credential_tail(abac_credential_t *cred) {
363    return cred->tail;
364}
365
366/**
367 * Return the xml chunk of the attribute cert.
368 */
369abac_chunk_t abac_credential_attribute_cert(abac_credential_t *cred) {
370    assert(cred); assert(cred->cert);
371    return abac_attribute_cert(cred->cert);
372}
373
374/**
375 * Return the chunk of the issuer cert.
376 */
377abac_chunk_t abac_credential_issuer_cert(abac_credential_t *cred) {
378    assert(cred); assert(cred->issuer);
379    abac_chunk_t ret= abac_id_in_PEM(cred->issuer);
380    return ret;
381}
382
383/**
384 * Increase the ref count of a credential.
385 */
386abac_credential_t *abac_credential_dup(abac_credential_t *cred) {
387    assert(cred != NULL);
388
389    ++cred->refcount;
390    return cred;
391}
392
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
404/**
405 * Decrease the reference count of a credential, freeing it when it reaches 0.
406 */
407void abac_credential_free(abac_credential_t *cred) {
408    if (cred == NULL)
409        return;
410
411    --cred->refcount;
412    if (cred->refcount > 0)
413        return;
414
415    abac_role_free(cred->head);
416    abac_role_free(cred->tail);
417    abac_attribute_free(cred->cert);
418    abac_id_free(cred->issuer);
419
420    free(cred);
421}
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
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
448
449
Note: See TracBrowser for help on using the repository browser.