[646e57e] | 1 | /** |
---|
| 2 | attr_abac.c |
---|
| 3 | |
---|
| 4 | To demonstrate how to use ABAC's api in C with partial proof result |
---|
| 5 | |
---|
| 6 | call: attr_abac_partial IceCream_ID.pem IceCream_private.pem IceCream_attr.der |
---|
| 7 | |
---|
| 8 | pre-condition: generate IceCream_ID.pem and IceCream_private.pem with |
---|
| 9 | creddy --generate --cn IceCream |
---|
| 10 | generate Chocolate_ID.pem and Chocolate_private.pem with |
---|
| 11 | creddy --generate --cn IceCream |
---|
| 12 | |
---|
| 13 | This program will generate an attribute rule, write it out to an external |
---|
| 14 | file and also load it into the context (prolog db) |
---|
| 15 | [keyid:IceCream].delicious <- [Keyid:Chocolate] |
---|
| 16 | |
---|
| 17 | Then, a invalid query is made against the context to see if partial proof is |
---|
| 18 | working in c |
---|
| 19 | |
---|
| 20 | **/ |
---|
| 21 | |
---|
| 22 | #include <err.h> |
---|
| 23 | #include <stdio.h> |
---|
| 24 | #include <assert.h> |
---|
| 25 | |
---|
| 26 | #include <abac.h> |
---|
| 27 | |
---|
| 28 | extern void abac_print_cred_info(abac_credential_t*, FILE*); |
---|
| 29 | extern char *abac_id_keyid(abac_id_t *id); |
---|
| 30 | extern abac_attribute_t *abac_attribute_add_tail(abac_attribute_t *ptr, abac_aspect_t *); |
---|
| 31 | |
---|
| 32 | int main(int argc, char **argv) { |
---|
| 33 | int i, success=0; |
---|
| 34 | abac_credential_t *cred=NULL; |
---|
| 35 | abac_credential_t **credentials=NULL; |
---|
| 36 | |
---|
| 37 | abac_context_t *ctx = abac_context_new(); |
---|
| 38 | |
---|
| 39 | if(argc != 5) return 1; |
---|
| 40 | |
---|
| 41 | /* build up structure */ |
---|
| 42 | abac_id_t *id =NULL; |
---|
| 43 | id = abac_id_from_file(argv[1]); |
---|
| 44 | int rc=abac_id_load_privkey_file(id,argv[2]); |
---|
| 45 | |
---|
| 46 | rc=abac_context_load_id_privkey_chunk(ctx, |
---|
| 47 | abac_id_cert_chunk(id), |
---|
| 48 | abac_id_privkey_chunk(id)); |
---|
| 49 | |
---|
| 50 | abac_id_t *chocolate_id = abac_id_from_file(argv[4]); |
---|
| 51 | rc=abac_context_load_id_id(ctx, chocolate_id); |
---|
| 52 | |
---|
| 53 | abac_aspect_t *head=abac_role_create(abac_id_keyid(id),"delicious"); |
---|
| 54 | abac_aspect_t *tail=abac_role_principal_create(abac_id_keyid(chocolate_id)); |
---|
| 55 | abac_attribute_t *attr; |
---|
| 56 | rc=abac_attribute_create(&attr, head, NULL, 1800); |
---|
| 57 | abac_attribute_add_tail(attr, tail); |
---|
| 58 | rc=abac_attribute_bake(attr); |
---|
| 59 | |
---|
| 60 | printf(" attribute being made : %s\n",abac_attribute_string(attr)); |
---|
| 61 | |
---|
| 62 | abac_attribute_write_cert_fname(attr,argv[3]); |
---|
| 63 | abac_context_load_attribute_chunk(ctx,abac_attribute_cert_chunk(attr)); |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | /* make a query */ |
---|
| 67 | abac_aspect_t *q=abac_role_create(abac_id_keyid(id),"yummy"); |
---|
| 68 | abac_aspect_t *w=abac_role_principal_create(abac_id_keyid(chocolate_id)); |
---|
| 69 | char *with=abac_aspect_typed_string(w); |
---|
| 70 | char *query=abac_aspect_typed_string(q); |
---|
| 71 | |
---|
| 72 | printf("query %s\n", query); |
---|
| 73 | printf("with %s\n", with); |
---|
| 74 | |
---|
| 75 | if(query !=NULL && with!=NULL) { |
---|
| 76 | credentials = abac_context_query(ctx, |
---|
| 77 | query, with, |
---|
| 78 | &success); |
---|
| 79 | if (success) |
---|
| 80 | puts("prover success!!"); |
---|
| 81 | else puts("prover failed!!"); |
---|
| 82 | |
---|
| 83 | if (credentials != NULL && success) { |
---|
| 84 | puts("credentials needed :"); |
---|
| 85 | for (i = 0; credentials[i] != NULL; ++i) { |
---|
| 86 | cred = credentials[i]; |
---|
| 87 | abac_print_cred_info(cred,NULL); |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | if(credentials) |
---|
| 91 | abac_context_credentials_free(credentials); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | abac_context_free(ctx); |
---|
| 95 | return 0; |
---|
| 96 | } |
---|