source: libabac/abac_verifier.c @ d2b198c

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

Another checkpoint. We can now read mnemonics from credentials.

  • Property mode set to 100644
File size: 10.4 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, 
204        abac_credential_t **cred_ret, abac_keyid_map_t *km) {
205    abac_role_t *head_role = NULL;
206    abac_role_t *tail_role = NULL;
207    abac_id_cert_t *issuer=NULL;
208    abac_keyid_map_t *local_names = NULL;
209    int ret;
210
211    char *attr_string=abac_attribute_role_string(cert);
212
213    if(attr_string == NULL) {
214       ret = ABAC_CERT_INVALID;
215       goto error;
216    }
217
218    // split into head/tail parts
219    char *head_tail[2];
220    ret = 2;
221    abac_split(attr_string, "<-", head_tail, &ret);
222    if (ret != 2) {
223        ret = ABAC_CERT_INVALID;
224        goto error;
225    }
226
227    // must be a role
228    head_role = abac_role_from_string(head_tail[0]);
229    if (head_role == NULL) goto error;
230    if (!abac_role_is_role(head_role)) {
231        ret = ABAC_CERT_INVALID;
232        goto error;
233    }
234
235    // make sure the tail's valid too
236    tail_role = abac_role_from_string(head_tail[1]);
237    if (tail_role == NULL) {
238        ret = ABAC_CERT_INVALID;
239        goto error;
240    }
241
242    char *principal = abac_role_principal(head_role);
243    issuer=_lookup_cert(id_certs,principal);
244    if (issuer == NULL) {
245        ret = ABAC_CERT_MISSING_ISSUER;
246        goto error;
247    }
248
249    // check if issuer is still valid
250    if (!abac_id_still_valid(issuer->cert)) {
251        ret = ABAC_CERT_INVALID_ISSUER;
252        goto error;
253    }
254
255    // make sure principal match up with keyid
256    if(!abac_id_has_keyid(issuer->cert,principal)) {
257        ret = ABAC_CERT_BAD_PRINCIPAL;
258        goto error;
259    }
260
261    if ( (local_names = abac_attribute_get_keyid_map(cert)) && km ) 
262        abac_keyid_map_merge(km, local_names, 1);
263   
264    // at this point we know we have a good attribute cert
265    abac_credential_t *cred = abac_xmalloc(sizeof(abac_credential_t));
266    cred->head = head_role;
267    cred->tail = tail_role;
268    cred->cert = cert;
269    /* acme's cert */
270    cred->issuer = abac_id_dup(issuer->cert);
271    cred->refcount = 1;
272    *cred_ret = cred;
273
274    return ABAC_CERT_SUCCESS;
275
276error:
277    if (head_role != NULL) abac_role_free(head_role);
278    if (tail_role != NULL) abac_role_free(tail_role);
279
280    return ret;
281}
282
283static int _load_attribute_certs(abac_list_t *id_certs,abac_list_t *attr_list, 
284        abac_list_t *cred_list, abac_keyid_map_t *km) {
285
286    int sz=abac_list_size(attr_list);
287    abac_credential_t *cred_ret=NULL;
288    abac_attribute_t *attr;
289    if(sz) { 
290        abac_list_foreach(attr_list, attr,
291            int ret=_load_attribute_cert(id_certs, attr, &cred_ret, km);
292            if(ret==ABAC_CERT_SUCCESS) {
293                abac_list_add(cred_list, cred_ret);
294            }
295        );
296    }
297    return sz;
298}
299
300/**
301 * Load an attribute cert from a file.
302 */
303int abac_verifier_load_attribute_cert_file(abac_list_t *id_certs,char *filename, abac_list_t *cred_list, abac_keyid_map_t *km) {
304
305    // load the cert
306    abac_list_t *attr_list = abac_attribute_certs_from_file(id_certs,filename);
307
308    if (abac_list_size(attr_list) == 0)
309        return ABAC_CERT_INVALID;
310
311    int ret=_load_attribute_certs(id_certs,attr_list, cred_list, km);
312
313    if(ret) return ABAC_CERT_SUCCESS;
314        return ABAC_CERT_INVALID;
315}
316
317/**
318 * Load an attribute cert from a chunk.
319 */
320int abac_verifier_load_attribute_cert_chunk(abac_list_t *id_certs,abac_chunk_t chunk, abac_list_t *cred_list, abac_keyid_map_t *km) {
321
322    // load the cert
323    abac_list_t *attr_list = abac_attribute_certs_from_chunk(id_certs,chunk);
324
325    if (abac_list_size(attr_list) == 0)
326        return ABAC_CERT_INVALID;
327
328    int ret=_load_attribute_certs(id_certs,attr_list,cred_list, km);
329
330    if(ret) return ABAC_CERT_SUCCESS;
331        return ABAC_CERT_INVALID;
332}
333
334/**
335 * Return the head role.
336 */
337abac_role_t *abac_credential_head(abac_credential_t *cred) {
338    return cred->head;
339}
340
341/**
342 * Return the tail role.
343 */
344abac_role_t *abac_credential_tail(abac_credential_t *cred) {
345    return cred->tail;
346}
347
348/**
349 * Return the xml chunk of the attribute cert.
350 */
351abac_chunk_t abac_credential_attribute_cert(abac_credential_t *cred) {
352    assert(cred); assert(cred->cert);
353    return abac_attribute_cert(cred->cert);
354}
355
356/**
357 * Return the chunk of the issuer cert.
358 */
359abac_chunk_t abac_credential_issuer_cert(abac_credential_t *cred) {
360    assert(cred); assert(cred->issuer);
361    abac_chunk_t ret= abac_id_in_PEM(cred->issuer);
362    return ret;
363}
364
365/**
366 * Increase the ref count of a credential.
367 */
368abac_credential_t *abac_credential_dup(abac_credential_t *cred) {
369    assert(cred != NULL);
370
371    ++cred->refcount;
372    return cred;
373}
374
375abac_id_cert_t *abac_id_cert_dup(abac_id_cert_t *id_cert) {
376    assert(id_cert != NULL && id_cert->keyid!=NULL);
377
378    abac_id_cert_t *new_cert = abac_xmalloc(sizeof(abac_id_cert_t));
379    new_cert->keyid=abac_xstrdup(id_cert->keyid);
380    new_cert->cert=abac_id_dup(id_cert->cert);
381 
382    return new_cert;
383}
384
385
386/**
387 * Decrease the reference count of a credential, freeing it when it reaches 0.
388 */
389void abac_credential_free(abac_credential_t *cred) {
390    if (cred == NULL)
391        return;
392
393    --cred->refcount;
394    if (cred->refcount > 0)
395        return;
396
397    abac_role_free(cred->head);
398    abac_role_free(cred->tail);
399    abac_attribute_free(cred->cert);
400    abac_id_free(cred->issuer);
401
402    free(cred);
403}
404
405/**
406 * remove an id cert, (not reference counted, so abac_id_certs are
407 * deep-copied)
408 */
409void abac_id_cert_free(abac_id_cert_t *id_cert) {
410    if (id_cert==NULL)
411        return;
412
413    abac_id_free(id_cert->cert);
414    if(id_cert->keyid) free(id_cert->keyid);
415    free(id_cert);
416}
417
418char *abac_id_cert_keyid(abac_id_cert_t *id_cert)
419{
420    assert(id_cert);
421    return id_cert->keyid;
422}
423
424
425
Note: See TracBrowser for help on using the repository browser.