source: libabac/abac_verifier.c @ bec30b5

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

1) change abac_context_load_directory to check on every regular files

and try to extract id id/privkey and then attribute in turn.

2) move id_certs to be context based instead of shared globally

  • Property mode set to 100644
File size: 9.6 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 "abac_util.h"
9
10struct _abac_id_cert_t {
11    char *keyid; // using cert's keyid
12    abac_id_t *cert;
13};
14
15struct _abac_credential_t {
16    abac_role_t *head;
17    abac_role_t *tail;
18    abac_id_t *issuer; /* Acme of Acme.customer <- Coyote */
19    abac_attribute_t *cert;
20
21    int refcount;
22};
23
24extern int abac_id_pass_privkey_from_id(abac_id_t *to_id, abac_id_t *from_id);
25extern char *make_pem_from_naked_pem(char *naked_pem);
26
27/***********************************************************************/
28// convert a chunk to a lowercase binary string
29// malloc's the string
30/*** not used,
31static char *_chunk_to_string(abac_chunk_t chunk) {
32    int i;
33
34    char *ret = abac_xmalloc(chunk.len * 2 + 1);
35
36    for (i = 0; i < chunk.len; ++i)
37        sprintf(ret + 2 * i, "%02x", chunk.ptr[i]);
38
39    return ret;
40}
41***/
42
43// verify that cert was issued by issuer
44// cert and issuer can be the same, in which case the self-sig is validated
45/*** not used
46static int _verify_cert(abac_id_t *cert) {
47
48    assert(cert);
49    if(!abac_id_still_valid(cert))
50        return 0;
51    return 1;
52}
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    deinit_xmlsec();
68    deinit_openssl();
69}
70
71static abac_id_cert_t *_lookup_cert(abac_list_t *id_certs, char *keyid)
72{
73    abac_id_cert_t *id_cert=NULL;
74    if(id_certs != NULL) {
75        abac_list_foreach(id_certs, id_cert,
76            if(strcmp(keyid,id_cert->keyid)==0)
77               return id_cert;
78        );
79    }
80    return NULL;
81}
82
83/**
84 * Load an ID certificate.
85 */
86static int _load_id(abac_list_t *id_certs, 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    id_cert=_lookup_cert(id_certs, keyid);
98
99    if (id_cert != NULL) {
100        /* special case, if
101           existing id does not have private key and
102           incoming does, then need to bring that bit of
103           information in */
104           if(abac_id_has_privkey(cert) && 
105                       !abac_id_has_privkey(id_cert->cert)) {
106               abac_id_pass_privkey_from_id(id_cert->cert, cert);
107           }
108        ret = ABAC_CERT_SUCCESS;
109        goto error;
110    }
111
112    ret = abac_id_still_valid(cert);
113    if (!ret) {
114        ret = ABAC_CERT_INVALID;
115        goto error;
116    }
117
118    // success, add the key to the map of certificates
119    id_cert = abac_xmalloc(sizeof(abac_id_cert_t));
120    id_cert->keyid = abac_xstrdup(keyid);
121    id_cert->cert = cert;
122    abac_list_add(id_certs, id_cert); 
123
124    return ABAC_CERT_SUCCESS;
125
126error:
127    if (keyid != NULL) free(keyid);
128    return ret;
129}
130
131abac_id_t *abac_verifier_lookup_id(abac_list_t *id_certs, char *keyid)
132{
133    abac_id_cert_t *id_cert=_lookup_cert(id_certs,keyid);
134    if(id_cert == NULL) return NULL;
135    return id_cert->cert;
136}
137
138/**
139 * Load an ID cert from a file.
140 */
141int abac_verifier_load_id_file(abac_list_t *id_certs,char *filename) {
142    // load the cert
143    abac_id_t *cert = abac_id_from_file(filename);
144
145    if (cert == NULL)
146        return ABAC_CERT_INVALID;
147
148    return _load_id(id_certs,cert);
149}
150
151/**
152 * Load an ID cert from a chunk.
153 */
154int abac_verifier_load_id_chunk(abac_list_t *id_certs,abac_chunk_t chunk) {
155
156    // load the cert
157    abac_id_t *cert= abac_id_from_chunk(chunk);
158
159    if (cert == NULL)
160        return ABAC_CERT_INVALID;
161    return _load_id(id_certs,cert);
162}
163
164/**
165 * Load an ID cert from a char ptr of a X509 pem data
166 * this is called from parse_privilege(..)
167 */
168int abac_verifier_load_id_chars(void *id_certs,char *naked_pem) {
169    /* if id_certs is NULL, don't even try to load it */
170    if(id_certs == NULL) return ABAC_CERT_SUCCESS;
171    /* make a well formed pem from this */
172    char *new_naked_pem=abac_xstrdup(naked_pem);
173    char *pem=make_pem_from_naked_pem(new_naked_pem);
174    int len=strlen(pem);
175    abac_chunk_t chunk = { pem, len }; 
176    free(new_naked_pem);
177    return abac_verifier_load_id_chunk((abac_list_t *)id_certs,chunk);
178}
179/**
180 * Load an attribute cert.
181 * Returns true only if the certificate is valid and is issued by the proper
182 * authority.
183 */
184static int _load_attribute_cert(abac_list_t *id_certs,abac_attribute_t *cert, abac_credential_t **cred_ret) {
185    abac_role_t *head_role = NULL;
186    abac_role_t *tail_role = NULL;
187    abac_id_cert_t *issuer=NULL;
188    int ret;
189
190    char *attr_string=abac_attribute_role_string(cert);
191
192    if(attr_string == NULL) {
193       ret = ABAC_CERT_INVALID;
194       goto error;
195    }
196
197    // split into head/tail parts
198    char *head_tail[2];
199    abac_split(attr_string, "<-", head_tail, &ret);
200    if (ret != 2) {
201        ret = ABAC_CERT_INVALID;
202        goto error;
203    }
204
205    // must be a role
206    head_role = abac_role_from_string(head_tail[0]);
207    if (head_role == NULL) goto error;
208    if (!abac_role_is_role(head_role)) {
209        ret = ABAC_CERT_INVALID;
210        goto error;
211    }
212
213    // make sure the tail's valid too
214    tail_role = abac_role_from_string(head_tail[1]);
215    if (tail_role == NULL) {
216        ret = ABAC_CERT_INVALID;
217        goto error;
218    }
219
220    char *principal = abac_role_principal(head_role);
221    issuer=_lookup_cert(id_certs,principal);
222    if (issuer == NULL) {
223        ret = ABAC_CERT_MISSING_ISSUER;
224        goto error;
225    }
226
227    // check if issuer is still valid
228    if (!abac_id_still_valid(issuer->cert)) {
229        ret = ABAC_CERT_INVALID_ISSUER;
230        goto error;
231    }
232
233    // make sure principal match up with keyid
234    if(!abac_id_has_keyid(issuer->cert,principal)) {
235        ret = ABAC_CERT_BAD_PRINCIPAL;
236        goto error;
237    }
238   
239    // at this point we know we have a good attribute cert
240    abac_credential_t *cred = abac_xmalloc(sizeof(abac_credential_t));
241    cred->head = head_role;
242    cred->tail = tail_role;
243    cred->cert = cert;
244    /* acme's cert */
245    cred->issuer = abac_id_dup(issuer->cert);
246    cred->refcount = 1;
247    *cred_ret = cred;
248
249    return ABAC_CERT_SUCCESS;
250
251error:
252    if (head_role != NULL) abac_role_free(head_role);
253    if (tail_role != NULL) abac_role_free(tail_role);
254
255    return ret;
256}
257
258static int _load_attribute_certs(abac_list_t *id_certs,abac_list_t *attr_list, abac_list_t *cred_list) {
259
260    int sz=abac_list_size(attr_list);
261    abac_credential_t *cred_ret=NULL;
262    abac_attribute_t *attr;
263    if(sz) { 
264        abac_list_foreach(attr_list, attr,
265            int ret=_load_attribute_cert(id_certs, attr, &cred_ret);
266            if(ret==ABAC_CERT_SUCCESS) {
267                abac_list_add(cred_list, cred_ret);
268            }
269        );
270    }
271    return sz;
272}
273
274/**
275 * Load an attribute cert from a file.
276 */
277int abac_verifier_load_attribute_cert_file(abac_list_t *id_certs,char *filename, abac_list_t *cred_list) {
278
279    // load the cert
280    abac_list_t *attr_list = abac_attribute_certs_from_file(id_certs,filename);
281
282    if (abac_list_size(attr_list) == 0)
283        return ABAC_CERT_INVALID;
284
285    int ret=_load_attribute_certs(id_certs,attr_list, cred_list);
286
287    if(ret) return ABAC_CERT_SUCCESS;
288        return ABAC_CERT_INVALID;
289}
290
291/**
292 * Load an attribute cert from a chunk.
293 */
294int abac_verifier_load_attribute_cert_chunk(abac_list_t *id_certs,abac_chunk_t chunk, abac_list_t *cred_list) {
295
296    // load the cert
297    abac_list_t *attr_list = abac_attribute_certs_from_chunk(id_certs,chunk);
298
299    if (abac_list_size(attr_list) == 0)
300        return ABAC_CERT_INVALID;
301
302    int ret=_load_attribute_certs(id_certs,attr_list,cred_list);
303
304    if(ret) return ABAC_CERT_SUCCESS;
305        return ABAC_CERT_INVALID;
306}
307
308/**
309 * Return the head role.
310 */
311abac_role_t *abac_credential_head(abac_credential_t *cred) {
312    return cred->head;
313}
314
315/**
316 * Return the tail role.
317 */
318abac_role_t *abac_credential_tail(abac_credential_t *cred) {
319    return cred->tail;
320}
321
322/**
323 * Return the xml chunk of the attribute cert.
324 */
325abac_chunk_t abac_credential_attribute_cert(abac_credential_t *cred) {
326    assert(cred); assert(cred->cert);
327    return abac_attribute_cert(cred->cert);
328}
329
330/**
331 * Return the chunk of the issuer cert.
332 */
333abac_chunk_t abac_credential_issuer_cert(abac_credential_t *cred) {
334    assert(cred); assert(cred->issuer);
335    abac_chunk_t ret= abac_id_in_PEM(cred->issuer);
336    return ret;
337}
338
339/**
340 * Increase the ref count of a credential.
341 */
342abac_credential_t *abac_credential_dup(abac_credential_t *cred) {
343    assert(cred != NULL);
344
345    ++cred->refcount;
346    return cred;
347}
348
349abac_id_cert_t *abac_id_cert_dup(abac_id_cert_t *id_cert) {
350    assert(id_cert != NULL && id_cert->keyid!=NULL);
351
352    abac_id_cert_t *new_cert = abac_xmalloc(sizeof(abac_id_cert_t));
353    new_cert->keyid=abac_xstrdup(id_cert->keyid);
354    new_cert->cert=abac_id_dup(id_cert->cert);
355 
356    return new_cert;
357}
358
359
360/**
361 * Decrease the reference count of a credential, freeing it when it reaches 0.
362 */
363void abac_credential_free(abac_credential_t *cred) {
364    if (cred == NULL)
365        return;
366
367    --cred->refcount;
368    if (cred->refcount > 0)
369        return;
370
371    abac_role_free(cred->head);
372    abac_role_free(cred->tail);
373    abac_attribute_free(cred->cert);
374    abac_id_free(cred->issuer);
375
376    free(cred);
377}
378
379/**
380 * remove an id cert, (not reference counted, so abac_id_certs are
381 * deep-copied)
382 */
383void abac_id_cert_free(abac_id_cert_t *id_cert) {
384    if (id_cert==NULL)
385        return;
386
387    abac_id_free(id_cert->cert);
388    if(id_cert->keyid) free(id_cert->keyid);
389    free(id_cert);
390}
391
392char *abac_id_cert_keyid(abac_id_cert_t *id_cert)
393{
394    assert(id_cert);
395    return id_cert->keyid;
396}
397
398
399
Note: See TracBrowser for help on using the repository browser.