1 | #ifndef __ABAC_H__ |
---|
2 | #define __ABAC_H__ |
---|
3 | |
---|
4 | #include <time.h> // for struct tm |
---|
5 | #include <stdio.h> // for FILE |
---|
6 | |
---|
7 | typedef struct _abac_context_t abac_context_t; |
---|
8 | typedef struct _abac_credential_t abac_credential_t; |
---|
9 | typedef struct _abac_id_cert_t abac_id_cert_t; |
---|
10 | typedef struct _abac_role_t abac_role_t; |
---|
11 | |
---|
12 | typedef struct _abac_id_t abac_id_t; |
---|
13 | typedef struct _abac_attribute_t abac_attribute_t; |
---|
14 | |
---|
15 | typedef struct _abac_list_t ABAC_LIST_T; |
---|
16 | |
---|
17 | #ifndef __ABAC_CHUNK_T__ |
---|
18 | #define __ABAC_CHUNK_T__ |
---|
19 | typedef struct _abac_chunk_t { |
---|
20 | unsigned char *ptr; |
---|
21 | int len; |
---|
22 | } abac_chunk_t; |
---|
23 | |
---|
24 | void abac_chunk_free(abac_chunk_t); |
---|
25 | abac_chunk_t abac_chunk_dup(char *, int); |
---|
26 | abac_chunk_t abac_chunk_new(char *, int); |
---|
27 | int abac_chunk_null(abac_chunk_t); |
---|
28 | void abac_chunk_show(abac_chunk_t); |
---|
29 | |
---|
30 | #endif /* __ABAC_CHUNK_T__ */ |
---|
31 | |
---|
32 | typedef struct abac_keyid_mapping_t abac_keyid_mapping_t; |
---|
33 | typedef struct abac_keyid_map_t abac_keyid_map_t; |
---|
34 | |
---|
35 | /* |
---|
36 | * ABAC functions, operating on an ABAC context. |
---|
37 | */ |
---|
38 | abac_context_t *abac_context_new(void); |
---|
39 | abac_context_t *abac_context_dup(abac_context_t *ctx); |
---|
40 | void abac_context_free(abac_context_t *ctx); |
---|
41 | |
---|
42 | /* see the bottom of the file for possible return codes */ |
---|
43 | int abac_context_load_id_file(abac_context_t *ctx, char *filename); |
---|
44 | int abac_context_load_id_chunk(abac_context_t *ctx, abac_chunk_t cert); |
---|
45 | int abac_context_load_id_id(abac_context_t *ctx, abac_id_t *cert); |
---|
46 | int abac_context_load_attribute_file(abac_context_t *ctx, char *filename); |
---|
47 | int abac_context_load_attribute_chunk(abac_context_t *ctx, abac_chunk_t cert); |
---|
48 | |
---|
49 | /* load an entire directory full of certs */ |
---|
50 | void abac_context_load_directory(abac_context_t *ctx, char *path); |
---|
51 | |
---|
52 | /* abac query, returns a NULL-terminated array of credentials on success, NULL on fail */ |
---|
53 | abac_credential_t **abac_context_query(abac_context_t *ctx, char *role, char *principal, int *success); |
---|
54 | |
---|
55 | /* get all the credentials from the context, returns a NULL-terminated array of credentials */ |
---|
56 | abac_credential_t **abac_context_credentials(abac_context_t *ctx); |
---|
57 | |
---|
58 | /* get all the principals from the context, returns a NULL-terminated array of credentials */ |
---|
59 | abac_id_cert_t **abac_context_principals(abac_context_t *ctx); |
---|
60 | |
---|
61 | /* use this to free the results of either of the previous two functions */ |
---|
62 | void abac_context_credentials_free(abac_credential_t **credentials); |
---|
63 | /* Used to pretty print */ |
---|
64 | int abac_context_set_nickname(abac_context_t *ctxt, char *key, char*nick); |
---|
65 | char *abac_context_expand_key(abac_context_t *ctxt, char *s ); |
---|
66 | char *abac_context_expand_nickname(abac_context_t *ctxt, char *s ); |
---|
67 | abac_keyid_map_t *abac_context_get_keyid_map(abac_context_t *ctxt); |
---|
68 | |
---|
69 | /* |
---|
70 | * Operations on credentials |
---|
71 | */ |
---|
72 | abac_role_t *abac_credential_head(abac_credential_t *cred); |
---|
73 | abac_role_t *abac_credential_tail(abac_credential_t *cred); |
---|
74 | abac_chunk_t abac_credential_attribute_cert(abac_credential_t *cred); |
---|
75 | abac_chunk_t abac_credential_issuer_cert(abac_credential_t *cred); |
---|
76 | |
---|
77 | abac_credential_t *abac_credential_dup(abac_credential_t *cred); |
---|
78 | void abac_credential_free(abac_credential_t *cred); |
---|
79 | char *abac_id_cert_keyid(abac_id_cert_t *); |
---|
80 | |
---|
81 | /* |
---|
82 | * Operations on roles. |
---|
83 | */ |
---|
84 | abac_role_t *abac_role_principal_new(char *principal); |
---|
85 | abac_role_t *abac_role_role_new(char *principal, char *abac_role_name); |
---|
86 | abac_role_t *abac_role_linking_new(char *principal, char *linked_role, char *abac_role_name); |
---|
87 | |
---|
88 | void abac_role_free(abac_role_t *role); |
---|
89 | |
---|
90 | abac_role_t *abac_role_from_string(char *string); |
---|
91 | abac_role_t *abac_role_dup(abac_role_t *role); |
---|
92 | |
---|
93 | int abac_role_is_principal(abac_role_t *role); |
---|
94 | int abac_role_is_role(abac_role_t *role); |
---|
95 | int abac_role_is_linking(abac_role_t *role); |
---|
96 | int abac_role_is_intersection(abac_role_t *role); |
---|
97 | |
---|
98 | char *abac_role_string(abac_role_t *role); |
---|
99 | char *abac_role_short_string(abac_role_t *role, abac_context_t *ctxt); |
---|
100 | char *abac_role_linked_role(abac_role_t *role); |
---|
101 | char *abac_role_linking_role(abac_role_t *role); |
---|
102 | char *abac_role_role_name(abac_role_t *role); |
---|
103 | char *abac_role_principal(abac_role_t *role); |
---|
104 | |
---|
105 | char *abac_role_attr_key(abac_role_t *head_role, abac_role_t *tail_role); |
---|
106 | |
---|
107 | /* |
---|
108 | * Operations on ID |
---|
109 | */ |
---|
110 | // create an ID from an X.509 certificate |
---|
111 | abac_id_t *abac_id_from_file(char *); |
---|
112 | |
---|
113 | // create an ID from a X.509 certificate PEM chunk |
---|
114 | abac_id_t *abac_id_from_chunk(abac_chunk_t chunk); |
---|
115 | |
---|
116 | // load an X.509 private key from a file |
---|
117 | int abac_id_privkey_from_file(abac_id_t *id, char *filename); |
---|
118 | |
---|
119 | // load an X.509 private key from a chunk |
---|
120 | int abac_id_privkey_from_chunk(abac_id_t *id, abac_chunk_t chunk); |
---|
121 | |
---|
122 | // generate an ID |
---|
123 | // returns one of ABAC_SUCCESS or ABAC_GENERATE_* (see top) |
---|
124 | int abac_id_generate(abac_id_t **ret, char *cn, long validity); |
---|
125 | |
---|
126 | // generate an ID using supplied private key |
---|
127 | // returns one of ABAC_SUCCESS or ABAC_GENERATE_* (see top) |
---|
128 | int abac_id_generate_with_key(abac_id_t **ret, char *cn, long validity, char *keyfile); |
---|
129 | |
---|
130 | // get the SHA1 keyid, pointer is valid for the lifetime of the object |
---|
131 | char *abac_id_keyid(abac_id_t *id); |
---|
132 | |
---|
133 | // get the CN of keyid, pointer is valid for the lifetime of the object |
---|
134 | char *abac_id_cn(abac_id_t *id); |
---|
135 | |
---|
136 | // get the name of the issuer |
---|
137 | // caller must free the returned string |
---|
138 | char *abac_id_issuer(abac_id_t *id); |
---|
139 | |
---|
140 | // get the DN of the subject |
---|
141 | // caller must free the returned string |
---|
142 | char *abac_id_subject(abac_id_t *id); |
---|
143 | |
---|
144 | // check if the cert is still valid |
---|
145 | int abac_id_still_valid(abac_id_t *id); |
---|
146 | |
---|
147 | // check if the principal cert's keyid is specified |
---|
148 | int abac_id_has_keyid(abac_id_t *id, char *); |
---|
149 | |
---|
150 | // check if the cert is has a private key |
---|
151 | int abac_id_has_privkey(abac_id_t *id); |
---|
152 | |
---|
153 | // get the validity period of the cert |
---|
154 | int abac_id_validity(abac_id_t *id, struct tm *not_before, struct tm *not_after); |
---|
155 | |
---|
156 | // default filename for the cert: ${CN}_ID.pem |
---|
157 | // caller must free the returned string |
---|
158 | char *abac_id_cert_filename(abac_id_t *id); |
---|
159 | |
---|
160 | // write the cert fo an open file pointer |
---|
161 | int abac_id_write_cert(abac_id_t *id, FILE *out); |
---|
162 | |
---|
163 | // default filename for the private key: ${CN}_key.pem |
---|
164 | // caller must free the return value |
---|
165 | char *abac_id_privkey_filename(abac_id_t *id); |
---|
166 | |
---|
167 | // write the private key to a file |
---|
168 | // it is recommended that you open this file mode 0600 |
---|
169 | // returns false if there's no private key loaded |
---|
170 | int abac_id_write_privkey(abac_id_t *id, FILE *out); |
---|
171 | |
---|
172 | // get a chunk representing the cert |
---|
173 | // you must free the ptr of the chunk when done |
---|
174 | abac_chunk_t abac_id_cert_chunk(abac_id_t *id); |
---|
175 | |
---|
176 | // get a chunk representing the private key of the id |
---|
177 | abac_chunk_t abac_id_privkey_chunk(abac_id_t *id); |
---|
178 | |
---|
179 | // dup an ID (increases its refcount) |
---|
180 | abac_id_t *abac_id_dup(abac_id_t *id); |
---|
181 | |
---|
182 | // destroy the id |
---|
183 | // decreases refcount and destroys when it hits 0 |
---|
184 | void abac_id_free(abac_id_t *id); |
---|
185 | |
---|
186 | /* |
---|
187 | * Operations on Attribute |
---|
188 | */ |
---|
189 | // |
---|
190 | // Here's the skinny: |
---|
191 | // Attribute cert objects don't contain an actual cert until they're baked. |
---|
192 | // First you construct the object using abac_attribute_create, then you add |
---|
193 | // subjects to it using abac_attribute_{principal,role,linking_role}. |
---|
194 | // Finally you bake it. Once you've done that, you can keep it as XML chunk |
---|
195 | // or write it to a file. |
---|
196 | // |
---|
197 | |
---|
198 | // create an attribute cert |
---|
199 | // validity is in days |
---|
200 | // returns one of CREDDY_SUCCESS or CREDDY_ATTRIBUTE_* (see top) |
---|
201 | int abac_attribute_create(abac_attribute_t **attr, abac_id_t *issuer, char *role, long validity); |
---|
202 | |
---|
203 | // add a head string to the cert |
---|
204 | void abac_attribute_set_head(abac_attribute_t *attr, char *string); |
---|
205 | |
---|
206 | // return the head string of the attribute |
---|
207 | char *abac_attribute_get_head(abac_attribute_t *); |
---|
208 | |
---|
209 | // add a principal subject to the cert |
---|
210 | int abac_attribute_principal(abac_attribute_t *attr, char *keyid); |
---|
211 | |
---|
212 | // add a role subject |
---|
213 | int abac_attribute_role(abac_attribute_t *attr, char *keyid, char *role); |
---|
214 | |
---|
215 | // add a linking role subject |
---|
216 | int abac_attribute_linking_role(abac_attribute_t *attr, char *keyid, char *role, char *linked); |
---|
217 | |
---|
218 | // create the attribute cert once all the subjects have been added |
---|
219 | // can return 0 if there are no subjects or there's a problem building the cert |
---|
220 | int abac_attribute_bake(abac_attribute_t *attr); |
---|
221 | int abac_attribute_bake_context(abac_attribute_t *attr, abac_context_t *ctxt); |
---|
222 | |
---|
223 | // returns true iff the cert's been baked |
---|
224 | int abac_attribute_baked(abac_attribute_t *attr); |
---|
225 | |
---|
226 | // write the cert to a file. returns 0 if the cert hasn't been baked |
---|
227 | int abac_attribute_write(abac_attribute_t *attr, FILE *out); |
---|
228 | int abac_attribute_write_file(abac_attribute_t *attr, const char *fname); |
---|
229 | |
---|
230 | /* |
---|
231 | * Return the number of tail strings |
---|
232 | */ |
---|
233 | int abac_attribute_get_ntails(abac_attribute_t *a); |
---|
234 | |
---|
235 | /* |
---|
236 | * Return the nth tail string or NULL if it is undefined |
---|
237 | */ |
---|
238 | char *abac_attribute_get_tail_n(abac_attribute_t *a, int n); |
---|
239 | |
---|
240 | // get chunked cert |
---|
241 | // returns 0 if the cert isn't baked |
---|
242 | abac_chunk_t abac_attribute_cert_chunk(abac_attribute_t *); |
---|
243 | |
---|
244 | // destroy the cert |
---|
245 | void abac_attribute_free(abac_attribute_t *); |
---|
246 | |
---|
247 | // load a list of attr cert from file (aborts on fail) |
---|
248 | ABAC_LIST_T *abac_attribute_certs_from_file(ABAC_LIST_T *,char *); |
---|
249 | |
---|
250 | // load a list of attr cert from chunk (aborts on fail) |
---|
251 | ABAC_LIST_T *abac_attribute_certs_from_chunk(ABAC_LIST_T *,abac_chunk_t); |
---|
252 | |
---|
253 | // get the attribute role string |
---|
254 | char *abac_attribute_role_string(abac_attribute_t *attr); |
---|
255 | |
---|
256 | // get the issuer id of an attribute |
---|
257 | abac_id_t *abac_attribute_issuer_id(abac_attribute_t *ptr); |
---|
258 | |
---|
259 | // get the attribute output format |
---|
260 | char *abac_attribute_get_output_format(abac_attribute_t *); |
---|
261 | |
---|
262 | // set the attribute output format |
---|
263 | // Valid formats GENIv1.0, GENIv1.1 |
---|
264 | void abac_attribute_set_output_format(abac_attribute_t *, char *); |
---|
265 | |
---|
266 | // get the validity period of the attribute cert |
---|
267 | int abac_attribute_validity(abac_attribute_t *attr, struct tm *not_before, struct tm *not_after); |
---|
268 | abac_keyid_map_t *abac_attribute_get_keyid_map(abac_attribute_t *); |
---|
269 | |
---|
270 | // return the principal from an attribute's role string |
---|
271 | // callee must free the space |
---|
272 | char *abac_attribute_get_principal(abac_attribute_t *attr); |
---|
273 | |
---|
274 | // check if the attribute cert is still valid |
---|
275 | int abac_attribute_still_valid(abac_attribute_t *attr); |
---|
276 | |
---|
277 | /* abac name mappings. These are used internally, mostly */ |
---|
278 | abac_keyid_mapping_t *abac_keyid_mapping_new(char *k, char *v); |
---|
279 | void abac_keyid_mapping_free(abac_keyid_mapping_t *m); |
---|
280 | abac_keyid_map_t *abac_keyid_map_new(); |
---|
281 | abac_keyid_map_t *abac_keyid_map_dup(abac_keyid_map_t *); |
---|
282 | abac_keyid_map_t *abac_keyid_map_clone(abac_keyid_map_t *); |
---|
283 | void abac_keyid_map_free(abac_keyid_map_t *m); |
---|
284 | char *abac_keyid_map_key_to_nickname(abac_keyid_map_t *m, char *key); |
---|
285 | char *abac_keyid_map_nickname_to_key(abac_keyid_map_t *m, char *nick); |
---|
286 | int abac_keyid_map_remove_keyid(abac_keyid_map_t *m, char *key); |
---|
287 | int abac_keyid_map_add_nickname(abac_keyid_map_t *m, char *key, char *nick); |
---|
288 | void abac_keyid_map_merge(abac_keyid_map_t *d, abac_keyid_map_t *s, |
---|
289 | int overwrite); |
---|
290 | char *abac_keyid_map_expand_key(abac_keyid_map_t *m, char *s); |
---|
291 | char *abac_keyid_map_expand_nickname(abac_keyid_map_t *m, char *s); |
---|
292 | /* |
---|
293 | * Return code for libabac |
---|
294 | */ |
---|
295 | #define ABAC_RC_SUCCESS 0 |
---|
296 | #define ABAC_RC_FAILURE 1 |
---|
297 | |
---|
298 | /* |
---|
299 | * Error codes for loading certificates. |
---|
300 | */ |
---|
301 | #define ABAC_CERT_SUCCESS 0 // certificate loaded, all is well |
---|
302 | #define ABAC_CERT_INVALID -1 // invalid format; also file not found |
---|
303 | #define ABAC_CERT_BAD_SIG -2 // invalid signature |
---|
304 | #define ABAC_CERT_MISSING_ISSUER -3 // missing ID cert that issued the attribute cert |
---|
305 | #define ABAC_CERT_BAD_PRINCIPAL -4 // Principal of attribute cert issuer has mismatch keyid |
---|
306 | #define ABAC_CERT_INVALID_ISSUER -5 // Issuer of attribute cert is invalid |
---|
307 | #define ABAC_CERT_SIGNER_NOKEY -6 // Signer of attribute cert is missing private key |
---|
308 | |
---|
309 | /* |
---|
310 | * Error codes for IDs and Attributes |
---|
311 | */ |
---|
312 | #define ABAC_SUCCESS 0 |
---|
313 | #define ABAC_FAILURE 1 /* catch all */ |
---|
314 | #define ABAC_GENERATE_INVALID_CN -1 |
---|
315 | #define ABAC_GENERATE_INVALID_VALIDITY -2 |
---|
316 | #define ABAC_ATTRIBUTE_ISSUER_NOKEY -3 |
---|
317 | #define ABAC_ATTRIBUTE_INVALID_ROLE -4 |
---|
318 | #define ABAC_ATTRIBUTE_INVALID_VALIDITY -5 |
---|
319 | #define ABAC_ATTRIBUTE_INVALID_ISSUER -6 |
---|
320 | |
---|
321 | |
---|
322 | |
---|
323 | #endif /* __ABAC_H__ */ |
---|