source: libabac/abac_verifier.c @ b73c5d05

abac0-leakabac0-meimei-idtvf-new-xml
Last change on this file since b73c5d05 was 4721618, checked in by Mei <mei@…>, 11 years ago

1) tested out python and perl test scripts along with

abac_chunk_t calls in libabac's abac.hh

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