source: libabac/abac_verifier.c @ 12a2fa7

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

checkpoint Lots of the way toward mnemonic names, some parsing fixes

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