[4721618] | 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 |
---|
| 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 query is made against the context to see if it is populated correctly. |
---|
| 18 | |
---|
| 19 | ./abac_attr IceCream_ID.pem IceCream_private.pem IceCream_attr.der Chocolate_ID.pem |
---|
| 20 | **/ |
---|
| 21 | |
---|
| 22 | #define _GNU_SOURCE |
---|
| 23 | #include <err.h> |
---|
| 24 | #include <stdio.h> |
---|
| 25 | #include <assert.h> |
---|
| 26 | |
---|
| 27 | #include <abac.h> |
---|
| 28 | |
---|
| 29 | extern char *abac_id_keyid(abac_id_t *id); |
---|
| 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 | |
---|
| 38 | if(argc != 5) return 1; |
---|
| 39 | |
---|
| 40 | /* build up structure */ |
---|
| 41 | abac_id_t *id =NULL; |
---|
| 42 | id = abac_id_from_file(argv[1]); |
---|
| 43 | int rc=abac_id_privkey_from_file(id,argv[2]); |
---|
| 44 | |
---|
| 45 | rc=abac_context_load_id_chunk(ctx, abac_id_cert_chunk(id)); |
---|
| 46 | |
---|
| 47 | abac_id_t *chocolate_id = abac_id_from_file(argv[4]); |
---|
| 48 | rc=abac_context_load_id_chunk(ctx, abac_id_cert_chunk(chocolate_id)); |
---|
| 49 | |
---|
| 50 | abac_attribute_t *attr; |
---|
| 51 | rc=abac_attribute_create(&attr, id, "delicious", 1800); |
---|
| 52 | abac_attribute_principal(attr, abac_id_keyid(chocolate_id)); |
---|
| 53 | rc=abac_attribute_bake(attr); |
---|
| 54 | |
---|
| 55 | printf(" attribute being made : %s\n",abac_attribute_role_string(attr)); |
---|
| 56 | |
---|
| 57 | abac_attribute_write_file(attr,argv[3]); |
---|
| 58 | abac_context_load_attribute_chunk(ctx,abac_attribute_cert_chunk(attr)); |
---|
| 59 | |
---|
| 60 | char *tmp=NULL; |
---|
| 61 | asprintf(&tmp,"%s.delicious",abac_id_keyid(id)); |
---|
| 62 | |
---|
| 63 | /* make a query */ |
---|
| 64 | credentials = abac_context_query(ctx, |
---|
| 65 | tmp, abac_id_keyid(chocolate_id), |
---|
| 66 | &success |
---|
| 67 | ); |
---|
| 68 | fprintf(stderr,"query with %s\n",tmp); |
---|
| 69 | fprintf(stderr," for %s\n",abac_id_keyid(chocolate_id)); |
---|
| 70 | int sz=abac_list_size(credentials); |
---|
| 71 | |
---|
| 72 | if (success) |
---|
| 73 | puts("attr success"); |
---|
| 74 | else |
---|
| 75 | puts("fail, here's a partial proof"); |
---|
| 76 | |
---|
| 77 | if (credentials != NULL) |
---|
| 78 | for (i = 0; credentials[i] != NULL; ++i) { |
---|
| 79 | cred = credentials[i]; |
---|
| 80 | printf("credential %s <- %s\n", |
---|
| 81 | abac_role_string(abac_credential_head(cred)), |
---|
| 82 | abac_role_string(abac_credential_tail(cred)) |
---|
| 83 | ); |
---|
| 84 | } |
---|
| 85 | abac_context_credentials_free(credentials); |
---|
| 86 | abac_context_free(ctx); |
---|
| 87 | return 0; |
---|
| 88 | } |
---|