source: libabac/abac_verifier.c @ 7764378

abac0-leak
Last change on this file since 7764378 was 7764378, checked in by Mei <mei@…>, 11 years ago

1) tweak according valgrind's leak report

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