source: libabac/abac.h @ 13b087a

abac0-leakabac0-meimei-idtvf-new-xml
Last change on this file since 13b087a was 13b087a, checked in by Mei <mei@…>, 11 years ago

1) fix some compiling warnings

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