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