source: libabac/abac_verifier.c @ 9f78e4c

abac0-leakabac0-meitvf-new-xml
Last change on this file since 9f78e4c was 9f78e4c, checked in by Ted Faber <faber@…>, 11 years ago

Modify abac_split to be more safe and callers of it to use the modified interface

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