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 | **/ |
---|
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 | |
---|
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_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 | |
---|
49 | abac_id_t *chocolate_id = abac_id_from_file(argv[4]); |
---|
50 | rc=abac_context_load_id_id(ctx, chocolate_id); |
---|
51 | |
---|
52 | abac_aspect_t *head=abac_role_create(abac_id_keyid(id),"delicious"); |
---|
53 | abac_aspect_t *tail=abac_role_principal_create(abac_id_keyid(chocolate_id)); |
---|
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 | /* make a query */ |
---|
65 | abac_aspect_t *q=abac_role_create(abac_id_keyid(id),"delicious"); |
---|
66 | abac_aspect_t *w=abac_role_principal_create(abac_id_keyid(chocolate_id)); |
---|
67 | char *with=abac_aspect_typed_string(w); |
---|
68 | char *query=abac_aspect_typed_string(q); |
---|
69 | |
---|
70 | printf("query %s\n", query); |
---|
71 | printf("with %s\n", with); |
---|
72 | |
---|
73 | if(query !=NULL && with!=NULL) { |
---|
74 | credentials = abac_context_query(ctx, |
---|
75 | query, with, |
---|
76 | &success); |
---|
77 | if (success) |
---|
78 | puts("prover success!!"); |
---|
79 | else puts("prover failed!!"); |
---|
80 | |
---|
81 | if (credentials != NULL && success) { |
---|
82 | puts("credentials needed :"); |
---|
83 | for (i = 0; credentials[i] != NULL; ++i) { |
---|
84 | cred = credentials[i]; |
---|
85 | abac_print_cred_info(cred,NULL); |
---|
86 | } |
---|
87 | } |
---|
88 | if(credentials) |
---|
89 | abac_context_credentials_free(credentials); |
---|
90 | } |
---|
91 | |
---|
92 | abac_context_free(ctx); |
---|
93 | return 0; |
---|
94 | } |
---|