source: libabac/abac.h @ 461541a

abac0-leakabac0-meimei-idmei-rt0-nmei_rt0tvf-new-xml
Last change on this file since 461541a was 461541a, checked in by Mei <mei@…>, 11 years ago

1) updated original rt0 to remove libstrongswan dependency

a) identity credential being made/accessed with openssl api calls

(X509/EVP_PKEY pem)

b) attribute credential being made/access via xmlsec1 (custom XML

structure)

2) refactored libcreddy into libabac and now one ABAC namespace for

libabac

3) added attribute_rule suboption to creddy's attribute as another way

to insert access rule

4) added some regression tests into example directory
5) updated some docs.

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