source: libabac/abac.h @ 63dcd99

abac0-leak
Last change on this file since 63dcd99 was 4f79997, checked in by Mei <mei@…>, 11 years ago

1) add a new scaling test -haystack/ralphs
2) tweak some libabac code here and there

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