source: libabac/abac.h @ 0f8d3ab

abac0-leak
Last change on this file since 0f8d3ab was c5720ad, checked in by Mei <mei@…>, 11 years ago

1) flesh out abac_chunk_t utilities

  • Property mode set to 100644
File size: 11.6 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
24void abac_chunk_free(abac_chunk_t);
25abac_chunk_t abac_chunk_dup(char *, int);
26abac_chunk_t abac_chunk_new(char *, int);
27int abac_chunk_null(abac_chunk_t);
28void abac_chunk_show(abac_chunk_t);
29
30#endif /* __ABAC_CHUNK_T__ */
31
32typedef struct abac_keyid_mapping_t abac_keyid_mapping_t;
33typedef struct abac_keyid_map_t abac_keyid_map_t;
34
35/*
36 * ABAC functions, operating on an ABAC context.
37 */
38abac_context_t *abac_context_new(void);
39abac_context_t *abac_context_dup(abac_context_t *ctx);
40void abac_context_free(abac_context_t *ctx);
41
42/* see the bottom of the file for possible return codes */
43int abac_context_load_id_file(abac_context_t *ctx, char *filename);
44int abac_context_load_id_chunk(abac_context_t *ctx, abac_chunk_t cert);
45int abac_context_load_id_id(abac_context_t *ctx, abac_id_t *cert);
46int abac_context_load_attribute_file(abac_context_t *ctx, char *filename);
47int abac_context_load_attribute_chunk(abac_context_t *ctx, abac_chunk_t cert);
48
49/* load an entire directory full of certs */
50void 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 */
53abac_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 */
56abac_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 */
59abac_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 */
62void abac_context_credentials_free(abac_credential_t **credentials);
63/* Used to pretty print */
64int abac_context_set_nickname(abac_context_t *ctxt, char *key, char*nick);
65char *abac_context_expand_key(abac_context_t *ctxt, char *s );
66char *abac_context_expand_nickname(abac_context_t *ctxt, char *s );
67abac_keyid_map_t *abac_context_get_keyid_map(abac_context_t *ctxt);
68
69/*
70 * Operations on credentials
71 */
72abac_role_t *abac_credential_head(abac_credential_t *cred);
73abac_role_t *abac_credential_tail(abac_credential_t *cred);
74abac_chunk_t abac_credential_attribute_cert(abac_credential_t *cred);
75abac_chunk_t abac_credential_issuer_cert(abac_credential_t *cred);
76
77abac_credential_t *abac_credential_dup(abac_credential_t *cred);
78void abac_credential_free(abac_credential_t *cred);
79char *abac_id_cert_keyid(abac_id_cert_t *);
80
81/*
82 * Operations on roles.
83 */
84abac_role_t *abac_role_principal_new(char *principal);
85abac_role_t *abac_role_role_new(char *principal, char *abac_role_name);
86abac_role_t *abac_role_linking_new(char *principal, char *linked_role, char *abac_role_name);
87
88void abac_role_free(abac_role_t *role);
89
90abac_role_t *abac_role_from_string(char *string);
91abac_role_t *abac_role_dup(abac_role_t *role);
92
93int abac_role_is_principal(abac_role_t *role);
94int abac_role_is_role(abac_role_t *role);
95int abac_role_is_linking(abac_role_t *role);
96int abac_role_is_intersection(abac_role_t *role);
97
98char *abac_role_string(abac_role_t *role);
99char *abac_role_short_string(abac_role_t *role, abac_context_t *ctxt);
100char *abac_role_linked_role(abac_role_t *role);
101char *abac_role_linking_role(abac_role_t *role);
102char *abac_role_role_name(abac_role_t *role);
103char *abac_role_principal(abac_role_t *role);
104
105char *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
111abac_id_t *abac_id_from_file(char *);
112
113// create an ID from a X.509 certificate PEM chunk
114abac_id_t *abac_id_from_chunk(abac_chunk_t chunk);
115
116// load an X.509 private key from a file
117int abac_id_privkey_from_file(abac_id_t *id, char *filename);
118
119// load an X.509 private key from a chunk
120int 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)
124int 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)
128int 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
131char *abac_id_keyid(abac_id_t *id);
132
133// get the CN of keyid, pointer is valid for the lifetime of the object
134char *abac_id_cn(abac_id_t *id);
135
136// get the name of the issuer
137// caller must free the returned string
138char *abac_id_issuer(abac_id_t *id);
139
140// get the DN of the subject
141// caller must free the returned string
142char *abac_id_subject(abac_id_t *id);
143
144// check if the cert is still valid
145int abac_id_still_valid(abac_id_t *id);
146
147// check if the principal cert's keyid is specified
148int abac_id_has_keyid(abac_id_t *id, char *);
149
150// check if the cert is has a private key
151int abac_id_has_privkey(abac_id_t *id);
152
153// get the validity period of the cert
154int 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
158char *abac_id_cert_filename(abac_id_t *id);
159
160// write the cert fo an open file pointer
161int 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
165char *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
170int 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
174abac_chunk_t abac_id_cert_chunk(abac_id_t *id);
175
176// get a chunk representing the private key of the id
177abac_chunk_t abac_id_privkey_chunk(abac_id_t *id);
178
179// dup an ID (increases its refcount)
180abac_id_t *abac_id_dup(abac_id_t *id);
181
182// destroy the id
183// decreases refcount and destroys when it hits 0
184void 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)
201int abac_attribute_create(abac_attribute_t **attr, abac_id_t *issuer, char *role, long validity);
202
203// add a head string to the cert
204void abac_attribute_set_head(abac_attribute_t *attr, char *string);
205
206// return the head string of the attribute
207char *abac_attribute_get_head(abac_attribute_t *);
208
209// add a principal subject to the cert
210int abac_attribute_principal(abac_attribute_t *attr, char *keyid);
211
212// add a role subject
213int abac_attribute_role(abac_attribute_t *attr, char *keyid, char *role);
214
215// add a linking role subject
216int 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
220int abac_attribute_bake(abac_attribute_t *attr);
221int abac_attribute_bake_context(abac_attribute_t *attr, abac_context_t *ctxt);
222
223// returns true iff the cert's been baked
224int abac_attribute_baked(abac_attribute_t *attr);
225
226// write the cert to a file. returns 0 if the cert hasn't been baked
227int abac_attribute_write(abac_attribute_t *attr, FILE *out);
228int abac_attribute_write_file(abac_attribute_t *attr, const char *fname);
229
230/*
231 * Return the number of tail strings
232 */
233int abac_attribute_get_ntails(abac_attribute_t *a);
234
235/*
236 * Return the nth tail string or NULL if it is undefined
237 */
238char *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
242abac_chunk_t abac_attribute_cert_chunk(abac_attribute_t *);
243
244// destroy the cert
245void abac_attribute_free(abac_attribute_t *);
246
247// load a list of attr cert from file (aborts on fail)
248ABAC_LIST_T *abac_attribute_certs_from_file(ABAC_LIST_T *,char *);
249
250// load a list of attr cert from chunk (aborts on fail)
251ABAC_LIST_T *abac_attribute_certs_from_chunk(ABAC_LIST_T *,abac_chunk_t);
252
253        // get the attribute role string
254char *abac_attribute_role_string(abac_attribute_t *attr);
255
256// get the issuer id of an attribute
257abac_id_t *abac_attribute_issuer_id(abac_attribute_t *ptr);
258
259// get the attribute output format
260char *abac_attribute_get_output_format(abac_attribute_t *);
261
262// set the attribute output format
263// Valid formats GENIv1.0, GENIv1.1
264void abac_attribute_set_output_format(abac_attribute_t *, char *);
265
266// get the validity period of the attribute cert
267int abac_attribute_validity(abac_attribute_t *attr, struct tm *not_before, struct tm *not_after);
268abac_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
272char *abac_attribute_get_principal(abac_attribute_t *attr);
273
274// check if the attribute cert is still valid
275int abac_attribute_still_valid(abac_attribute_t *attr);
276
277/* abac name mappings.  These are used internally, mostly */
278abac_keyid_mapping_t *abac_keyid_mapping_new(char *k, char *v);
279void abac_keyid_mapping_free(abac_keyid_mapping_t *m);
280abac_keyid_map_t *abac_keyid_map_new();
281abac_keyid_map_t *abac_keyid_map_dup(abac_keyid_map_t *);
282abac_keyid_map_t *abac_keyid_map_clone(abac_keyid_map_t *);
283void abac_keyid_map_free(abac_keyid_map_t *m); 
284char *abac_keyid_map_key_to_nickname(abac_keyid_map_t *m, char *key);
285char *abac_keyid_map_nickname_to_key(abac_keyid_map_t *m, char *nick); 
286int abac_keyid_map_remove_keyid(abac_keyid_map_t *m, char *key); 
287int abac_keyid_map_add_nickname(abac_keyid_map_t *m, char *key, char *nick);
288void abac_keyid_map_merge(abac_keyid_map_t *d, abac_keyid_map_t *s, 
289        int overwrite);
290char *abac_keyid_map_expand_key(abac_keyid_map_t *m, char *s); 
291char *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__ */
Note: See TracBrowser for help on using the repository browser.