source: libabac/abac_verifier.c @ 9ac7fb4

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

1) work around some compilation warnings

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