source: libabac/abac.h @ 91a6b20

abac0-leak
Last change on this file since 91a6b20 was f2622ee, checked in by Mei-Hui Su <mei@…>, 11 years ago

1) ran with valgrind and did some leak patching

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