source: libabac/abac_verifier.c @ 461541a

abac0-leakabac0-meimei-idmei-rt0-nmei_rt0tvf-new-xml
Last change on this file since 461541a was 461541a, checked in by Mei <mei@…>, 11 years ago

1) updated original rt0 to remove libstrongswan dependency

a) identity credential being made/accessed with openssl api calls

(X509/EVP_PKEY pem)

b) attribute credential being made/access via xmlsec1 (custom XML

structure)

2) refactored libcreddy into libabac and now one ABAC namespace for

libabac

3) added attribute_rule suboption to creddy's attribute as another way

to insert access rule

4) added some regression tests into example directory
5) updated some docs.

  • Property mode set to 100644
File size: 7.6 KB
Line 
1
2/* abac_verifier.c */
3#include <assert.h>
4
5#include "libabac_common.h"
6#include "abac.h"
7
8#include "abac_list.h"
9#include "uthash.h"
10#include "abac_util.h"
11
12typedef struct _abac_id_cert_t {
13    char *keyid; // using cert's keyid
14    abac_id_t *cert;
15
16    UT_hash_handle hh;
17} abac_id_cert_t;
18
19struct _abac_credential_t {
20    abac_role_t *head;
21    abac_role_t *tail;
22    abac_id_t *issuer; /* Acme of Acme.customer <- Coyote */
23    abac_attribute_t *cert;
24
25    int refcount;
26};
27
28/* hash table of all the principal id credentials */
29abac_id_cert_t *id_certs = NULL;
30
31/***********************************************************************/
32// convert a chunk to a lowercase binary string
33// malloc's the string
34static char *_chunk_to_string(abac_chunk_t chunk) {
35    int i;
36
37    char *ret = abac_xmalloc(chunk.len * 2 + 1);
38
39    for (i = 0; i < chunk.len; ++i)
40        sprintf(ret + 2 * i, "%02x", chunk.ptr[i]);
41
42    return ret;
43}
44
45// verify that cert was issued by issuer
46// cert and issuer can be the same, in which case the self-sig is validated
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 * Init the verifier subsystem.
57 */
58void abac_verifier_init(void) {
59    init_openssl();
60    init_xmlsec();
61}
62
63/**
64 * Uninitialize the system, free up any allocated memory.
65 */
66void abac_verifier_deinit(void) {
67    abac_id_cert_t *id;
68
69    while ((id = id_certs) != NULL) {
70        HASH_DEL(id_certs, id);
71
72        free(id->keyid);
73        abac_id_free(id->cert);
74        free(id);
75    }
76    deinit_xmlsec();
77    deinit_openssl();
78}
79
80/**
81 * Load an ID certificate.
82 */
83static int _load_id(abac_id_t *cert) {
84    abac_id_cert_t *id_cert = NULL;
85    char *keyid = NULL;
86    int ret;
87
88    assert(cert);
89
90    // get the key ID
91    keyid = abac_id_keyid(cert);
92
93    // if we already have this cert 'error' with success
94    HASH_FIND_STR(id_certs, keyid, id_cert);
95    if (id_cert != NULL) {
96        ret = ABAC_CERT_SUCCESS;
97        goto error;
98    }
99
100    ret = abac_id_still_valid(cert);
101    if (!ret) {
102        ret = ABAC_CERT_INVALID;
103        goto error;
104    }
105
106    // success, add the key to the map of certificates
107    id_cert = abac_xmalloc(sizeof(abac_id_cert_t));
108    id_cert->keyid = abac_xstrdup(keyid);
109    id_cert->cert = cert;
110    HASH_ADD_KEYPTR(hh, id_certs, id_cert->keyid, strlen(id_cert->keyid), id_cert);
111
112    return ABAC_CERT_SUCCESS;
113
114error:
115    if (keyid != NULL) free(keyid);
116    return ret;
117}
118
119abac_id_t *abac_verifier_lookup_id(char *keyid)
120{
121    abac_id_cert_t *id_cert;
122    HASH_FIND_STR(id_certs, keyid, id_cert);
123    if(id_cert == NULL) return NULL;
124    return id_cert->cert;
125}
126
127/**
128 * Load an ID cert from a file.
129 */
130int abac_verifier_load_id_file(char *filename) {
131    // load the cert
132    abac_id_t *cert = abac_id_from_file(filename);
133
134    if (cert == NULL)
135        return ABAC_CERT_INVALID;
136
137    return _load_id(cert);
138}
139
140/**
141 * Load an ID cert from a chunk.
142 */
143int abac_verifier_load_id_chunk(abac_chunk_t chunk) {
144
145    // load the cert
146    abac_id_t *cert= abac_id_from_chunk(chunk);
147
148    if (cert == NULL)
149        return ABAC_CERT_INVALID;
150    return _load_id(cert);
151}
152
153/**
154 * Load an attribute cert.
155 * Returns true only if the certificate is valid and is issued by the proper
156 * authority.
157 */
158static int _load_attribute_cert(abac_attribute_t *cert, abac_credential_t **cred_ret) {
159    abac_role_t *head_role = NULL;
160    abac_role_t *tail_role = NULL;
161    abac_id_cert_t *issuer=NULL;
162    int ret;
163
164    char *attr_string=abac_attribute_role_string(cert);
165
166    if(attr_string == NULL) {
167       ret = ABAC_CERT_INVALID;
168       goto error;
169    }
170
171    // split into head/tail parts
172    char *head_tail[2];
173    abac_split(attr_string, "<-", head_tail, &ret);
174    if (ret != 2) {
175        ret = ABAC_CERT_INVALID;
176        goto error;
177    }
178
179    // must be a role
180    head_role = abac_role_from_string(head_tail[0]);
181    if (head_role == NULL) goto error;
182    if (!abac_role_is_role(head_role)) {
183        ret = ABAC_CERT_INVALID;
184        goto error;
185    }
186
187    // make sure the tail's valid too
188    tail_role = abac_role_from_string(head_tail[1]);
189    if (tail_role == NULL) {
190        ret = ABAC_CERT_INVALID;
191        goto error;
192    }
193
194    char *principal = abac_role_principal(head_role);
195    HASH_FIND_STR(id_certs, principal, issuer);
196    if (issuer == NULL) {
197        ret = ABAC_CERT_MISSING_ISSUER;
198        goto error;
199    }
200
201    // check if issuer is still valid
202    if (!abac_id_still_valid(issuer->cert)) {
203        ret = ABAC_CERT_INVALID_ISSUER;
204        goto error;
205    }
206
207    // make sure principal match up with keyid
208    if(!abac_id_has_keyid(issuer->cert,principal)) {
209        ret = ABAC_CERT_BAD_PRINCIPAL;
210        goto error;
211    }
212   
213    // at this point we know we have a good attribute cert
214    abac_credential_t *cred = abac_xmalloc(sizeof(abac_credential_t));
215    cred->head = head_role;
216    cred->tail = tail_role;
217    cred->cert = cert;
218    /* acme's cert */
219    cred->issuer = abac_id_dup(issuer->cert);
220    cred->refcount = 1;
221    *cred_ret = cred;
222
223    return ABAC_CERT_SUCCESS;
224
225error:
226    if (head_role != NULL) abac_role_free(head_role);
227    if (tail_role != NULL) abac_role_free(tail_role);
228
229    return ret;
230}
231
232static int _load_attribute_certs(abac_list_t *attr_list, abac_list_t *cred_list) {
233
234    int sz=abac_list_size(attr_list);
235    abac_credential_t *cred_ret=NULL;
236    abac_attribute_t *attr;
237    if(sz) { 
238        abac_list_foreach(attr_list, attr,
239            int ret=_load_attribute_cert(attr, &cred_ret);
240            if(ret==ABAC_CERT_SUCCESS) {
241                abac_list_add(cred_list, cred_ret);
242            }
243        );
244    }
245    return sz;
246}
247
248/**
249 * Load an attribute cert from a file.
250 */
251int abac_verifier_load_attribute_cert_file(char *filename, abac_list_t *cred_list) {
252
253    // load the cert
254    abac_list_t *attr_list = abac_attribute_certs_from_file(filename);
255
256    if (abac_list_size(attr_list) == 0)
257        return ABAC_CERT_INVALID;
258
259    int ret=_load_attribute_certs(attr_list, cred_list);
260
261    if(ret) return ABAC_CERT_SUCCESS;
262        return ABAC_CERT_INVALID;
263}
264
265/**
266 * Load an attribute cert from a chunk.
267 */
268int abac_verifier_load_attribute_cert_chunk(abac_chunk_t chunk, abac_list_t *cred_list) {
269
270    // load the cert
271    abac_list_t *attr_list = abac_attribute_certs_from_chunk(chunk);
272
273    if (abac_list_size(attr_list) == 0)
274        return ABAC_CERT_INVALID;
275    return _load_attribute_certs(attr_list,cred_list);
276}
277
278/**
279 * Return the head role.
280 */
281abac_role_t *abac_credential_head(abac_credential_t *cred) {
282    return cred->head;
283}
284
285/**
286 * Return the tail role.
287 */
288abac_role_t *abac_credential_tail(abac_credential_t *cred) {
289    return cred->tail;
290}
291
292/**
293 * Return the xml chunk of the attribute cert.
294 */
295abac_chunk_t abac_credential_attribute_cert(abac_credential_t *cred) {
296    assert(cred); assert(cred->cert);
297    return abac_attribute_cert(cred->cert);
298}
299
300/**
301 * Return the chunk of the issuer cert.
302 */
303abac_chunk_t abac_credential_issuer_cert(abac_credential_t *cred) {
304    assert(cred); assert(cred->issuer);
305    abac_chunk_t ret= abac_id_in_PEM(cred->issuer);
306    return ret;
307}
308
309/**
310 * Increase the ref count of a credential.
311 */
312abac_credential_t *abac_credential_dup(abac_credential_t *cred) {
313    assert(cred != NULL);
314
315    ++cred->refcount;
316    return cred;
317}
318
319/**
320 * Decrease the reference count of a credential, freeing it when it reaches 0.
321 */
322void abac_credential_free(abac_credential_t *cred) {
323    if (cred == NULL)
324        return;
325
326    --cred->refcount;
327    if (cred->refcount > 0)
328        return;
329
330    abac_role_free(cred->head);
331    abac_role_free(cred->tail);
332    abac_attribute_free(cred->cert);
333    abac_id_free(cred->issuer);
334
335    free(cred);
336}
Note: See TracBrowser for help on using the repository browser.