source: libabac/abac_verifier.c @ 8fa2c49

mei_rt2mei_rt2_fix_1meiyap-rt1rt2
Last change on this file since 8fa2c49 was 36b100a, checked in by Mei <mei@…>, 12 years ago

1) fix up fruits_rt2_typed example to test different types of strings

as oset term

2) fix the abac_pl_yap.c's _abac_yap_query so the first term if given

a literal string will use ReadBuffer? instead of MkAtomTerm?

  • Property mode set to 100644
File size: 17.6 KB
Line 
1
2// include the GNU extension of asprintf
3#define _GNU_SOURCE
4
5#include <assert.h>
6#include <stdio.h>
7
8#include <library.h>
9#include <credentials/certificates/certificate.h>
10#include <credentials/certificates/x509.h>
11#include <credentials/certificates/ac.h>
12
13#include "abac_verifier.h"
14
15#include "abac_list.h"
16#include "abac_util.h"
17#include "abac_rt.h"
18
19extern abac_role_t *abac_yy_get_rule_head_role();
20extern abac_role_t *abac_yy_get_rule_tail_role();
21extern abac_oset_t *abac_yy_get_rule_head_oset();
22extern abac_oset_t *abac_yy_get_rule_tail_oset();
23extern int abac_yy_get_rule_is_oset();
24extern abac_list_t *abac_yy_get_rule_clauses();
25extern char* abac_decode_string(char *);
26extern void abac_yy_free_rule_clauses();
27extern char *generate_pl_type_clause(char *, int);
28
29
30/* keeping this a fixed enum is to have tighter control of
31   what are acceptabletypes of keyid are we allowing */
32typedef enum _keyidtype_t {
33    e_KEYID = 1,
34} keyidtype_t;
35
36/* keyidname[keyidtype] */
37static char * const keyidname[] =
38{
39  "badidtype", "keyid", 0
40};
41
42typedef enum _credtype_t {
43   e_CREDTYPE_ROLE =1,
44   e_CREDTYPE_OSET =2,
45} credtype_t;
46
47static int keyidname_cnt=1;
48
49struct _abac_id_cert_t {
50    char *keyid;
51    char *cn;
52    int type; /* keyidtype */
53    certificate_t *cert;
54    char *clause;
55
56    UT_hash_handle hh;
57};
58
59/* can store either role or oset */
60struct _abac_credential_t {
61    int type;
62    void *head;
63    void *tail;
64    certificate_t *cert;
65    certificate_t *issuer;
66    abac_list_t *clauses;
67
68    int refcount;
69};
70
71typedef struct _abac_str_cred_t {
72    char *strid;
73    abac_credential_t *cred;
74
75    UT_hash_handle hh;
76} abac_str_cred_t;
77
78abac_id_cert_t *id_certs = NULL;
79abac_str_cred_t *str_creds = NULL;
80
81static int debug=0;
82static void check_id_cert(abac_id_cert_t *id_cert);
83extern void abac_print_role_string_with_condition(abac_role_t *role, FILE*);
84
85//
86void abac_print_cred_info(abac_credential_t *cred, FILE *fp)
87{
88    if(fp == NULL)
89        fp=stdout;
90
91    if(debug) {
92        fprintf(fp,"---> printing out credential info cred(%d)..\n", (int) cred);
93        if(cred->head) {
94            fprintf(fp,"  head cred(%d):\n",(int)cred->head);
95            if(cred->type == e_CREDTYPE_ROLE)
96               abac_print_role_string_with_condition((abac_role_t*)cred->head,fp);
97               else
98                   abac_print_oset_string_with_condition((abac_oset_t*)cred->head,fp);
99        }
100        if(cred->tail) {
101            fprintf(fp,"\n  tail cred(%d):\n",(int)cred->tail);
102            if(cred->type == e_CREDTYPE_ROLE)
103                abac_print_role_string_with_condition((abac_role_t*)cred->tail,fp);
104                else
105                    abac_print_oset_string_with_condition((abac_oset_t*)cred->tail,fp);
106        }
107
108        abac_list_t *clauses=cred->clauses;
109        if (clauses != NULL) {
110            char *cur=NULL;
111            abac_list_foreach(clauses, cur,
112                fprintf(fp,"\n  a clause(%d):\n",(int)cur);
113                if(cur)
114                    fprintf(fp,"strlen(%d)loc(%d)(%s)\n", strlen(cur),(int)cur, cur);
115            );
116        }
117        } else {
118            fprintf(fp," ");
119            if(cred->type == e_CREDTYPE_ROLE)
120                abac_print_role_string_with_condition((abac_role_t *)cred->head,fp);
121                else abac_print_oset_string_with_condition((abac_oset_t *)cred->head,fp);
122            fprintf(fp," <- ");
123            if(cred->type == e_CREDTYPE_ROLE)
124                abac_print_role_string_with_condition((abac_role_t *)cred->tail,fp);
125                else abac_print_oset_string_with_condition((abac_oset_t *)cred->tail,fp);
126            fprintf(fp,"\n");
127   }
128}
129
130//
131int abac_verify_keyid_type(char *type) {
132    int i;
133
134    if (type == NULL)
135        return 0;
136
137    for (i = 1; i <= keyidname_cnt ; i++)
138        if(strcmp(type,keyidname[i])==0)
139            return i;
140    return 0;
141}
142
143char *abac_keyid_type(int i)
144{
145    return keyidname[i];
146}
147
148// convert a chunk to a lowercase binary string
149// malloc's the string
150static char *_chunk_to_string(chunk_t chunk) {
151    int i;
152
153    char *ret = abac_xmalloc(chunk.len * 2 + 1);
154
155    for (i = 0; i < chunk.len; ++i)
156        sprintf(ret + 2 * i, "%02x", chunk.ptr[i]);
157
158    return ret;
159}
160
161// verify that cert was issued by issuer
162// cert and issuer can be the same, in which case the self-sig is validated
163static int _verify_signature(certificate_t *issuer, certificate_t *cert) {
164    if (cert->issued_by(cert, issuer))
165        if (cert->get_validity(cert, NULL, NULL, NULL))
166            return 1;
167    return 0;
168}
169
170/**
171 * Init the verifier subsystem.
172 */
173void abac_verifier_init(void) {
174    // silence all debugging
175
176    if (!library_init(NULL))
177        exit(SS_RC_LIBSTRONGSWAN_INTEGRITY);
178
179    if (!lib->plugins->load(lib->plugins, NULL,
180            lib->settings->get_str(lib->settings, "pki.load", PLUGINS)))
181        exit(SS_RC_INITIALIZATION_FAILED);
182}
183
184/**
185 * Uninitialize the system, free up any allocated memory.
186 */
187void abac_verifier_deinit(void) {
188    abac_id_cert_t *id;
189    while ((id = id_certs) != NULL) {
190        HASH_DEL(id_certs, id);
191
192        free(id->keyid);
193        free(id->cn);
194        id->cert->destroy(id->cert);
195        free(id);
196    }
197
198    abac_str_cred_t *id2;
199    while ((id2 = str_creds) != NULL) {
200        HASH_DEL(str_creds, id2);
201
202        free(id2->strid);
203        free(id2);
204    }
205
206    library_deinit();
207}
208
209/**
210 * Load an ID certificate.
211 */
212static int _load_id(certificate_t *cert, abac_id_cert_t **id_cert_ret) {
213    abac_id_cert_t *id_cert = NULL;
214    char *keyid = NULL;
215    chunk_t id;
216    int ret;
217    x509_t *x509 = (x509_t *)cert;
218
219    assert(cert != NULL);
220
221    // get the key ID
222    id = x509->get_subjectKeyIdentifier(x509);
223    keyid = _chunk_to_string(id);
224
225    /* Mike said this is the way it is */
226    char *str;
227    int rv = asprintf(&str, "%Y", cert->get_issuer(cert));
228
229    /* add p to cn name here */
230    char *cn=(char *)abac_xmalloc(strlen(str)+2);
231    cn[0]='p';
232    int n=sscanf(str,"CN=%s", &(cn[1]));
233    if ( n!=1 ) { 
234        ret = ABAC_CERT_BAD_CN;
235        goto error;
236    }
237
238    if(debug) {
239        printf ("DEBUG:keyid %s \n", keyid);
240        printf( "DEBUG:issuer '%s' \n", str);
241        printf ("DEBUG:cn %s \n", cn);
242    }
243    free(str);
244
245    // if we already have this cert 'error' with success
246    HASH_FIND_STR(id_certs, keyid, id_cert);
247    if (id_cert != NULL) {
248        ret = ABAC_CERT_SUCCESS;
249        goto error;
250    }
251
252    // validate sig
253    ret = _verify_signature(cert, cert);
254    if (!ret) {
255        ret = ABAC_CERT_BAD_SIG;
256        goto error;
257    }
258
259    // success, add the key to the map of certificates
260    id_cert = abac_xmalloc(sizeof(abac_id_cert_t));
261    id_cert->keyid = keyid;
262    id_cert->cert = cert;
263    id_cert->cn = cn;
264    id_cert->type = e_KEYID;
265    id_cert->clause = generate_pl_type_clause(cn, id_cert->type);
266
267    HASH_ADD_KEYPTR(hh, id_certs, id_cert->keyid, strlen(id_cert->keyid), id_cert);
268
269    *id_cert_ret=id_cert;
270
271    return ABAC_CERT_SUCCESS;
272
273error:
274    if (keyid != NULL) free(keyid);
275    if (cn != NULL) free(cn);
276
277    return ret;
278}
279
280static void check_id_cert(abac_id_cert_t *id_cert)
281{
282    if(id_cert) {
283        printf("checking on this id_cert %d\n", (int)id_cert);
284        printf("  --> sha is (%s)\n", id_cert->keyid);
285        printf("  --> cn is (%s)\n", id_cert->cn);
286    }
287}
288
289/**
290 * Load an attribute cert as string.
291 * have minimum syntax & validity check
292 */
293static int _load_attribute_string(char* attr_string) {
294    printf("NOT implemented yet!!!");
295    return ABAC_CERT_INVALID;
296}
297
298/**
299 * Load an ID cert from a file.
300 */
301int abac_verifier_load_id_file(char *filename, abac_id_cert_t **id_cert_ret) {
302    if (lib == NULL)
303        errx(1, "looks like you didn't call libabac_init() (lib is NULL)");
304
305    // load the cert
306    certificate_t *cert = lib->creds->create(
307        lib->creds, CRED_CERTIFICATE, CERT_X509,
308        BUILD_FROM_FILE, filename,
309        BUILD_X509_FLAG, X509_AA, // attribute authority, dumb
310        BUILD_END
311    );
312    if (cert == NULL)
313        return ABAC_CERT_INVALID;
314    return _load_id(cert,id_cert_ret);
315}
316
317/**
318 * Load an ID cert from a chunk.
319 */
320int abac_verifier_load_id_chunk(chunk_t chunk, abac_id_cert_t **id_cert_ret) {
321    // load the cert
322    certificate_t *cert = lib->creds->create(
323        lib->creds, CRED_CERTIFICATE, CERT_X509,
324        BUILD_BLOB_ASN1_DER, chunk,
325        BUILD_X509_FLAG, X509_AA, // attribute authority, dumb
326        BUILD_END
327    );
328    if (cert == NULL)
329        return ABAC_CERT_INVALID;
330    return _load_id(cert,id_cert_ret);
331}
332
333
334void abac_print_clauses(abac_list_t *clauses, FILE *fp)
335{
336    if (clauses != NULL) {
337        char *cur;
338        abac_list_foreach(clauses, cur,
339            if(cur) {
340                if(fp)
341                    fprintf (fp,"a clause, %d(%s)\n", (int)cur,cur);
342                    else printf ("a clause, %d(%s)\n", (int)cur,cur);
343            }
344        );
345    }
346}
347
348static int _verify_valid_role_credential(certificate_t *cert,
349abac_credential_t **cred_ret, char *encoded_attr_string)
350{
351    abac_role_t *head_role = NULL;
352    abac_role_t *tail_role = NULL;
353    abac_list_t *clauses=NULL;
354    abac_id_cert_t *issuer;
355    int ret, i;
356
357    // get the attr
358    head_role = abac_yy_get_rule_head_role();
359    tail_role = abac_yy_get_rule_tail_role();
360    clauses = abac_yy_get_rule_clauses();
361
362    // get the issuer based on keyid
363    char *principalname = abac_role_principal(head_role);
364    HASH_FIND_STR(id_certs, principalname, issuer);
365    if (issuer == NULL) {
366        ret = ABAC_CERT_MISSING_ISSUER;
367        goto error;
368    }
369
370    // make sure the issuer's signed it
371    ret = _verify_signature(issuer->cert, cert);
372    if (!ret) {
373        abac_yy_set_error_code(ABAC_RT_CERT_BAD_SIG);
374        goto error;
375    }
376
377    // at this point we know we have a good attribute cert
378    abac_credential_t *cred = abac_xmalloc(sizeof(abac_credential_t));
379    cred->type= e_CREDTYPE_ROLE;
380    cred->head = head_role;
381    cred->tail = tail_role;
382    cred->cert = cert;
383    cred->issuer = issuer->cert->get_ref(issuer->cert);
384    cred->clauses = clauses;
385    cred->refcount = 1;
386    *cred_ret = cred;
387
388    // success, add the key to the map of certificates
389    abac_str_cred_t *str_cred;
390    str_cred=abac_xmalloc(sizeof(abac_str_cred_t));
391    str_cred->strid=strdup(encoded_attr_string);
392    str_cred->cred=cred;
393    HASH_ADD_KEYPTR(hh, str_creds, str_cred->strid, strlen(str_cred->strid), str_cred);
394
395    return ABAC_CERT_SUCCESS;
396
397error:
398    if (head_role) abac_role_free(head_role);
399    if (tail_role) abac_role_free(tail_role);
400    abac_yy_free_rule_clauses();
401
402    return ret;
403}
404
405static int _verify_valid_oset_credential(certificate_t *cert,
406abac_credential_t **cred_ret, char *encoded_attr_string)
407{
408    abac_oset_t *head_oset = NULL;
409    abac_oset_t *tail_oset = NULL;
410    abac_list_t *clauses=NULL;
411    abac_id_cert_t *issuer;
412    int ret, i;
413
414    // get the attr
415    head_oset = abac_yy_get_rule_head_oset();
416    tail_oset = abac_yy_get_rule_tail_oset();
417    clauses = abac_yy_get_rule_clauses();
418
419    // get the issuer based on keyid
420    char *principalname = abac_oset_principal(head_oset);
421    HASH_FIND_STR(id_certs, principalname, issuer);
422    if (issuer == NULL) {
423        ret = ABAC_CERT_MISSING_ISSUER;
424        goto error;
425    }
426
427    // make sure the issuer's signed it
428    ret = _verify_signature(issuer->cert, cert);
429    if (!ret) {
430        abac_yy_set_error_code(ABAC_RT_CERT_BAD_SIG);
431        goto error;
432    }
433
434    // at this point we know we have a good attribute cert
435    abac_credential_t *cred = abac_xmalloc(sizeof(abac_credential_t));
436    cred->type= e_CREDTYPE_OSET;
437    cred->head = head_oset;
438    cred->tail = tail_oset;
439    cred->cert = cert;
440    cred->issuer = issuer->cert->get_ref(issuer->cert);
441    cred->clauses = clauses;
442    cred->refcount = 1;
443    *cred_ret = cred;
444
445    // success, add the key to the map of certificates
446    abac_str_cred_t *str_cred;
447    str_cred=abac_xmalloc(sizeof(abac_str_cred_t));
448    str_cred->strid=strdup(encoded_attr_string);
449    str_cred->cred=cred;
450    HASH_ADD_KEYPTR(hh, str_creds, str_cred->strid, strlen(str_cred->strid), str_cred);
451
452    return ABAC_CERT_SUCCESS;
453
454error:
455    if (head_oset) abac_oset_free(head_oset);
456    if (tail_oset) abac_oset_free(tail_oset);
457    abac_yy_free_rule_clauses();
458
459    return ret;
460}
461/**
462 * Load an attribute cert.
463 * Returns true only if the certificate is valid and is issued by the proper
464 * authority.
465 */
466static int _load_attribute_cert(certificate_t *cert, abac_credential_t **cred_ret) {
467    ietf_attributes_t *attr_cert = NULL;
468    abac_id_cert_t *issuer;
469    int ret, i;
470
471    // get the attr
472    ac_t *ac = (ac_t *)cert;
473    attr_cert = ac->get_groups(ac);
474    if (attr_cert == NULL) {
475        ret = ABAC_CERT_INVALID;
476        goto error;
477    }
478
479    char *encoded_attr_string=attr_cert->get_string(attr_cert);
480    char *attr_string = abac_decode_string(encoded_attr_string);
481    if(debug)
482         printf("string to be yyparse..(%s)\n",attr_string);
483
484    if (attr_string == NULL) {
485        ret = ABAC_CERT_INVALID;
486        goto error;
487    }
488
489    /* call into yacc parser */
490    abac_reset_yyfptr(attr_string);
491    abac_yy_init();
492    int rc=yyparse();
493    if (rc) {
494        ret = ABAC_CERT_INVALID;
495        goto error;
496    }
497
498    if(abac_yy_get_rule_is_oset()) {
499        ret=_verify_valid_oset_credential(cert,cred_ret,encoded_attr_string);
500        } else {
501            ret=_verify_valid_role_credential(cert,cred_ret,encoded_attr_string);
502    }
503
504    // free up some crap
505    attr_cert->destroy(attr_cert);
506
507    return ABAC_CERT_SUCCESS;
508
509error:
510    if (cert) cert->destroy(cert);
511    if (attr_cert) attr_cert->destroy(attr_cert);
512    return ret;
513}
514
515/**
516 * Load an attribute cert from a file.
517 */
518int abac_verifier_load_attribute_cert_file(char *filename, abac_credential_t **cred) {
519    // load the cert
520    certificate_t *cert = lib->creds->create(
521        lib->creds, CRED_CERTIFICATE, CERT_X509_AC,
522        BUILD_FROM_FILE, filename,
523        BUILD_END
524    );
525    if (cert == NULL)
526        return ABAC_CERT_INVALID;
527    return _load_attribute_cert(cert, cred);
528}
529
530/**
531 * Load attribute cert rules from a file. (this should only be used to test)
532 */
533int abac_verifier_load_certs_file(char *filename) {
534    size_t nbytes=0;
535    char *input=NULL;
536    FILE *fp;
537    int ret;
538
539    fp=fopen(filename, "r");
540
541    if(fp == NULL)
542        return ABAC_CERT_INVALID;
543
544    while( (ret=getline(&input, &nbytes, fp)) != -1 ) {
545        ret=_load_attribute_string(input);
546        free(input);
547        if(ret != ABAC_CERT_SUCCESS )
548            fclose(fp);
549            return ret;
550    }
551    fclose(fp);
552    return ABAC_CERT_SUCCESS;
553}
554
555
556/**
557 * Load an attribute cert from a chunk.
558 */
559int abac_verifier_load_attribute_cert_chunk(chunk_t chunk, abac_credential_t **cred) {
560    // load the cert
561    certificate_t *cert = lib->creds->create(
562        lib->creds, CRED_CERTIFICATE, CERT_X509_AC,
563        BUILD_BLOB_ASN1_DER, chunk,
564        BUILD_END
565    );
566    if (cert == NULL)
567        return ABAC_CERT_INVALID;
568    return _load_attribute_cert(cert, cred);
569}
570
571/**
572 * Return the encoding of the attribute cert.
573 */
574abac_chunk_t abac_credential_attribute_cert(abac_credential_t *cred) {
575    chunk_t encoding = cred->cert->get_encoding(cred->cert);
576    abac_chunk_t ret = { encoding.ptr, encoding.len };
577    return ret;
578}
579
580/**
581 * Return the encoding of the issuer cert.
582 */
583abac_chunk_t abac_credential_issuer_cert(abac_credential_t *cred) {
584    chunk_t encoding = cred->issuer->get_encoding(cred->issuer);
585    abac_chunk_t ret = { encoding.ptr, encoding.len };
586    return ret;
587}
588
589/**
590 * Return the clause of the cert
591 */
592abac_list_t *abac_credential_clauses(abac_credential_t *cred) {
593    return cred->clauses;
594}
595
596
597/**
598 * Increase the ref count of a credential.
599 */
600abac_credential_t *abac_credential_dup(abac_credential_t *cred) {
601    assert(cred != NULL);
602
603    ++cred->refcount;
604    return cred;
605}
606
607/**
608 *  lookup for a credential.
609 */
610abac_credential_t *abac_credential_lookup(char* cred_string)
611{
612    if(debug)
613        printf("DEBUG:doing abac_crendential_lookup ... (%s)\n", cred_string);
614    abac_str_cred_t *str_cred;
615    HASH_FIND_STR(str_creds, cred_string, str_cred);
616    if (str_cred == NULL) {
617        if(debug) printf("DEBUG:NOT FOUND..\n");
618        return NULL;
619    }
620    if(debug) printf("DEBUG:FOUND..(%d)\n", (int) str_cred->cred);
621    return str_cred->cred;
622}
623
624/**
625 * Decrease the reference count of a credential, freeing it when it reaches 0.
626 */
627void abac_credential_free(abac_credential_t *cred)
628{
629    if(debug)
630        printf("DEBUG:freeing cred(%d)clause(%d)\n", (int)cred, (int)cred->clauses);
631
632    if (cred == NULL)
633        return;
634
635    --cred->refcount;
636    if (cred->refcount > 0)
637        return;
638
639    if(cred->type == e_CREDTYPE_ROLE) {
640        abac_role_free((abac_role_t *)cred->head);
641        abac_role_free((abac_role_t *)cred->tail);
642        } else {
643            abac_oset_free((abac_oset_t *)cred->head);
644            abac_oset_free((abac_oset_t *)cred->tail);
645    }
646    cred->cert->destroy(cred->cert);
647    cred->issuer->destroy(cred->issuer); // reference counted
648    /* no need to freeyap_clauses here */
649    free(cred);
650}
651
652char *abac_id_clause(abac_id_cert_t *id_cert)
653{
654    if(id_cert)
655        return id_cert->clause;
656    return NULL;
657}
658
659/* retrieve the cn that is associated with this sha_string */
660char *abac_cn_with_sha(char *sha_string) {
661
662    // get the issuer based on keyid
663    abac_id_cert_t *id_cert;
664    HASH_FIND_STR(id_certs, sha_string, id_cert);
665    if (id_cert == NULL) {
666        return NULL;
667    }
668    if(debug)
669        check_id_cert(id_cert);
670    return id_cert->cn;
671}
672
673/* retrieve the cn that is associated with the principal of this role */
674char *abac_cn_with_role(abac_role_t *role) {
675
676    char *principal = abac_role_principal(role);
677    return abac_cn_with_sha(principal);
678}
679
680/* retrieve the cn that is associated with the principal of this oset */
681char *abac_cn_with_oset(abac_oset_t *oset) {
682    char *principal = abac_oset_principal(oset);
683    return abac_cn_with_sha(principal);
684}
685
Note: See TracBrowser for help on using the repository browser.