[90d20f0] | 1 | #ifndef __ABAC_H__ |
---|
| 2 | #define __ABAC_H__ |
---|
| 3 | |
---|
[461541a] | 4 | #include <time.h> // for struct tm |
---|
| 5 | #include <stdio.h> // for FILE |
---|
[11e3eb7] | 6 | |
---|
[390f749] | 7 | typedef struct _abac_context_t abac_context_t; |
---|
[401a054] | 8 | typedef struct _abac_credential_t abac_credential_t; |
---|
[bec30b5] | 9 | typedef struct _abac_id_cert_t abac_id_cert_t; |
---|
[1743825] | 10 | typedef struct _abac_role_t abac_role_t; |
---|
[90d20f0] | 11 | |
---|
[461541a] | 12 | typedef struct _abac_id_t abac_id_t; |
---|
| 13 | typedef struct _abac_attribute_t abac_attribute_t; |
---|
| 14 | |
---|
[4721618] | 15 | typedef struct _abac_list_t ABAC_LIST_T; |
---|
| 16 | |
---|
[461541a] | 17 | typedef struct _abac_chunk_t { |
---|
| 18 | unsigned char *ptr; |
---|
| 19 | int len; |
---|
| 20 | } abac_chunk_t; |
---|
| 21 | |
---|
[94605f2] | 22 | |
---|
| 23 | typedef struct abac_keyid_mapping_t abac_keyid_mapping_t; |
---|
| 24 | typedef struct abac_keyid_map_t abac_keyid_map_t; |
---|
| 25 | |
---|
[0bf0e67] | 26 | /* |
---|
| 27 | * ABAC functions, operating on an ABAC context. |
---|
| 28 | */ |
---|
[390f749] | 29 | abac_context_t *abac_context_new(void); |
---|
| 30 | abac_context_t *abac_context_dup(abac_context_t *ctx); |
---|
| 31 | void abac_context_free(abac_context_t *ctx); |
---|
[90d20f0] | 32 | |
---|
[0779c99] | 33 | /* see the bottom of the file for possible return codes */ |
---|
[390f749] | 34 | int abac_context_load_id_file(abac_context_t *ctx, char *filename); |
---|
| 35 | int abac_context_load_id_chunk(abac_context_t *ctx, abac_chunk_t cert); |
---|
| 36 | int abac_context_load_attribute_file(abac_context_t *ctx, char *filename); |
---|
| 37 | int abac_context_load_attribute_chunk(abac_context_t *ctx, abac_chunk_t cert); |
---|
[90d20f0] | 38 | |
---|
[03b3293] | 39 | /* load an entire directory full of certs */ |
---|
[390f749] | 40 | void abac_context_load_directory(abac_context_t *ctx, char *path); |
---|
[03b3293] | 41 | |
---|
[401a054] | 42 | /* abac query, returns a NULL-terminated array of credentials on success, NULL on fail */ |
---|
[4e426c9] | 43 | abac_credential_t **abac_context_query(abac_context_t *ctx, char *role, char *principal, int *success); |
---|
[3c4fd68] | 44 | |
---|
| 45 | /* get all the credentials from the context, returns a NULL-terminated array of credentials */ |
---|
| 46 | abac_credential_t **abac_context_credentials(abac_context_t *ctx); |
---|
| 47 | |
---|
[13b087a] | 48 | /* get all the principals from the context, returns a NULL-terminated array of credentials */ |
---|
| 49 | abac_id_cert_t **abac_context_principals(abac_context_t *ctx); |
---|
| 50 | |
---|
[3c4fd68] | 51 | /* use this to free the results of either of the previous two functions */ |
---|
| 52 | void abac_context_credentials_free(abac_credential_t **credentials); |
---|
[94605f2] | 53 | /* Used to pretty print */ |
---|
[34565bf] | 54 | int abac_context_set_nickname(abac_context_t *ctxt, char *key, char*nick); |
---|
[94605f2] | 55 | char *abac_context_expand_key(abac_context_t *ctxt, char *s ); |
---|
| 56 | char *abac_context_expand_nickname(abac_context_t *ctxt, char *s ); |
---|
[afcafea] | 57 | abac_keyid_map_t *abac_context_get_keyid_map(abac_context_t *ctxt); |
---|
[90d20f0] | 58 | |
---|
[0bf0e67] | 59 | /* |
---|
[401a054] | 60 | * Operations on credentials |
---|
[0bf0e67] | 61 | */ |
---|
[401a054] | 62 | abac_role_t *abac_credential_head(abac_credential_t *cred); |
---|
| 63 | abac_role_t *abac_credential_tail(abac_credential_t *cred); |
---|
| 64 | abac_chunk_t abac_credential_attribute_cert(abac_credential_t *cred); |
---|
| 65 | abac_chunk_t abac_credential_issuer_cert(abac_credential_t *cred); |
---|
[0bf0e67] | 66 | |
---|
[401a054] | 67 | abac_credential_t *abac_credential_dup(abac_credential_t *cred); |
---|
| 68 | void abac_credential_free(abac_credential_t *cred); |
---|
[13b087a] | 69 | char *abac_id_cert_keyid(abac_id_cert_t *); |
---|
[0bf0e67] | 70 | |
---|
| 71 | /* |
---|
| 72 | * Operations on roles. |
---|
| 73 | */ |
---|
[dcc1a8e] | 74 | abac_role_t *abac_role_principal_new(char *principal); |
---|
| 75 | abac_role_t *abac_role_role_new(char *principal, char *abac_role_name); |
---|
| 76 | abac_role_t *abac_role_linking_new(char *principal, char *linked_role, char *abac_role_name); |
---|
[0bf0e67] | 77 | |
---|
[dcc1a8e] | 78 | void abac_role_free(abac_role_t *role); |
---|
[0bf0e67] | 79 | |
---|
[dcc1a8e] | 80 | abac_role_t *abac_role_from_string(char *string); |
---|
| 81 | abac_role_t *abac_role_dup(abac_role_t *role); |
---|
[0bf0e67] | 82 | |
---|
[dcc1a8e] | 83 | int abac_role_is_principal(abac_role_t *role); |
---|
| 84 | int abac_role_is_role(abac_role_t *role); |
---|
| 85 | int abac_role_is_linking(abac_role_t *role); |
---|
[9a411d7] | 86 | int abac_role_is_intersection(abac_role_t *role); |
---|
[0bf0e67] | 87 | |
---|
[dcc1a8e] | 88 | char *abac_role_string(abac_role_t *role); |
---|
[94605f2] | 89 | char *abac_role_short_string(abac_role_t *role, abac_context_t *ctxt); |
---|
[dcc1a8e] | 90 | char *abac_role_linked_role(abac_role_t *role); |
---|
[e50f807] | 91 | char *abac_role_linking_role(abac_role_t *role); |
---|
[dcc1a8e] | 92 | char *abac_role_role_name(abac_role_t *role); |
---|
| 93 | char *abac_role_principal(abac_role_t *role); |
---|
[0bf0e67] | 94 | |
---|
[dcc1a8e] | 95 | char *abac_role_attr_key(abac_role_t *head_role, abac_role_t *tail_role); |
---|
[0bf0e67] | 96 | |
---|
[461541a] | 97 | /* |
---|
| 98 | * Operations on ID |
---|
| 99 | */ |
---|
| 100 | // create an ID from an X.509 certificate |
---|
| 101 | abac_id_t *abac_id_from_file(char *); |
---|
| 102 | |
---|
| 103 | // create an ID from a X.509 certificate PEM chunk |
---|
| 104 | abac_id_t *abac_id_from_chunk(abac_chunk_t chunk); |
---|
| 105 | |
---|
| 106 | // load an X.509 private key from a file |
---|
| 107 | int abac_id_privkey_from_file(abac_id_t *id, char *filename); |
---|
| 108 | |
---|
[a02c849] | 109 | // load an X.509 private key from a chunk |
---|
| 110 | int abac_id_privkey_from_chunk(abac_id_t *id, abac_chunk_t chunk); |
---|
| 111 | |
---|
[461541a] | 112 | // generate an ID |
---|
| 113 | // returns one of ABAC_SUCCESS or ABAC_GENERATE_* (see top) |
---|
| 114 | int abac_id_generate(abac_id_t **ret, char *cn, long validity); |
---|
| 115 | |
---|
[9e063cb] | 116 | // generate an ID using supplied private key |
---|
| 117 | // returns one of ABAC_SUCCESS or ABAC_GENERATE_* (see top) |
---|
| 118 | int abac_id_generate_with_key(abac_id_t **ret, char *cn, long validity, char *keyfile); |
---|
| 119 | |
---|
[461541a] | 120 | // get the SHA1 keyid, pointer is valid for the lifetime of the object |
---|
| 121 | char *abac_id_keyid(abac_id_t *id); |
---|
| 122 | |
---|
[4f79997] | 123 | // get the CN of keyid, pointer is valid for the lifetime of the object |
---|
| 124 | char *abac_id_cn(abac_id_t *id); |
---|
| 125 | |
---|
[461541a] | 126 | // get the name of the issuer |
---|
| 127 | // caller must free the returned string |
---|
| 128 | char *abac_id_issuer(abac_id_t *id); |
---|
| 129 | |
---|
| 130 | // get the DN of the subject |
---|
| 131 | // caller must free the returned string |
---|
| 132 | char *abac_id_subject(abac_id_t *id); |
---|
| 133 | |
---|
| 134 | // check if the cert is still valid |
---|
| 135 | int abac_id_still_valid(abac_id_t *id); |
---|
| 136 | |
---|
| 137 | // check if the principal cert's keyid is specified |
---|
| 138 | int abac_id_has_keyid(abac_id_t *id, char *); |
---|
| 139 | |
---|
| 140 | // check if the cert is has a private key |
---|
| 141 | int abac_id_has_privkey(abac_id_t *id); |
---|
| 142 | |
---|
| 143 | // get the validity period of the cert |
---|
| 144 | int 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 |
---|
| 148 | char *abac_id_cert_filename(abac_id_t *id); |
---|
| 149 | |
---|
| 150 | // write the cert fo an open file pointer |
---|
| 151 | int 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 |
---|
| 155 | char *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 |
---|
| 160 | int 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 |
---|
| 164 | abac_chunk_t abac_id_cert_chunk(abac_id_t *id); |
---|
| 165 | |
---|
[92661b4] | 166 | // get a chunk representing the private key of the id |
---|
| 167 | abac_chunk_t abac_id_privkey_chunk(abac_id_t *id); |
---|
| 168 | |
---|
[461541a] | 169 | // dup an ID (increases its refcount) |
---|
| 170 | abac_id_t *abac_id_dup(abac_id_t *id); |
---|
| 171 | |
---|
| 172 | // destroy the id |
---|
| 173 | // decreases refcount and destroys when it hits 0 |
---|
| 174 | void 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) |
---|
| 191 | int abac_attribute_create(abac_attribute_t **attr, abac_id_t *issuer, char *role, long validity); |
---|
| 192 | |
---|
| 193 | // add a head string to the cert |
---|
| 194 | void abac_attribute_set_head(abac_attribute_t *attr, char *string); |
---|
| 195 | |
---|
| 196 | // return the head string of the attribute |
---|
| 197 | char *abac_attribute_get_head(abac_attribute_t *); |
---|
| 198 | |
---|
| 199 | // add a principal subject to the cert |
---|
| 200 | int abac_attribute_principal(abac_attribute_t *attr, char *keyid); |
---|
| 201 | |
---|
| 202 | // add a role subject |
---|
| 203 | int abac_attribute_role(abac_attribute_t *attr, char *keyid, char *role); |
---|
| 204 | |
---|
| 205 | // add a linking role subject |
---|
| 206 | int 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 |
---|
| 210 | int abac_attribute_bake(abac_attribute_t *attr); |
---|
[afcafea] | 211 | int abac_attribute_bake_context(abac_attribute_t *attr, abac_context_t *ctxt); |
---|
[461541a] | 212 | |
---|
| 213 | // returns true iff the cert's been baked |
---|
| 214 | int abac_attribute_baked(abac_attribute_t *attr); |
---|
| 215 | |
---|
| 216 | // write the cert to a file. returns 0 if the cert hasn't been baked |
---|
| 217 | int abac_attribute_write(abac_attribute_t *attr, FILE *out); |
---|
[4721618] | 218 | int abac_attribute_write_file(abac_attribute_t *attr, const char *fname); |
---|
[461541a] | 219 | |
---|
[e50f807] | 220 | /* |
---|
| 221 | * Return the number of tail strings |
---|
| 222 | */ |
---|
| 223 | int abac_attribute_get_ntails(abac_attribute_t *a); |
---|
| 224 | |
---|
| 225 | /* |
---|
| 226 | * Return the nth tail string or NULL if it is undefined |
---|
| 227 | */ |
---|
| 228 | char *abac_attribute_get_tail_n(abac_attribute_t *a, int n); |
---|
| 229 | |
---|
[461541a] | 230 | // get chunked cert |
---|
| 231 | // returns 0 if the cert isn't baked |
---|
[4721618] | 232 | abac_chunk_t abac_attribute_cert_chunk(abac_attribute_t *); |
---|
[461541a] | 233 | |
---|
| 234 | // destroy the cert |
---|
| 235 | void abac_attribute_free(abac_attribute_t *); |
---|
| 236 | |
---|
| 237 | // load a list of attr cert from file (aborts on fail) |
---|
[bec30b5] | 238 | ABAC_LIST_T *abac_attribute_certs_from_file(ABAC_LIST_T *,char *); |
---|
[461541a] | 239 | |
---|
| 240 | // load a list of attr cert from chunk (aborts on fail) |
---|
[bec30b5] | 241 | ABAC_LIST_T *abac_attribute_certs_from_chunk(ABAC_LIST_T *,abac_chunk_t); |
---|
[461541a] | 242 | |
---|
[9e063cb] | 243 | // get the attribute role string |
---|
[461541a] | 244 | char *abac_attribute_role_string(abac_attribute_t *attr); |
---|
| 245 | |
---|
| 246 | // get the issuer id of an attribute |
---|
| 247 | abac_id_t *abac_attribute_issuer_id(abac_attribute_t *ptr); |
---|
| 248 | |
---|
[34565bf] | 249 | // get the attribute output format |
---|
[bc12f3d] | 250 | char *abac_attribute_get_output_format(abac_attribute_t *); |
---|
| 251 | |
---|
| 252 | // set the attribute output format |
---|
| 253 | // Valid formats GENIv1.0, GENIv1.1 |
---|
| 254 | void abac_attribute_set_output_format(abac_attribute_t *, char *); |
---|
| 255 | |
---|
[461541a] | 256 | // get the validity period of the attribute cert |
---|
| 257 | int abac_attribute_validity(abac_attribute_t *attr, struct tm *not_before, struct tm *not_after); |
---|
[d2b198c] | 258 | abac_keyid_map_t *abac_attribute_get_keyid_map(abac_attribute_t *); |
---|
[461541a] | 259 | |
---|
| 260 | // return the principal from an attribute's role string |
---|
| 261 | // callee must free the space |
---|
| 262 | char *abac_attribute_get_principal(abac_attribute_t *attr); |
---|
| 263 | |
---|
| 264 | // check if the attribute cert is still valid |
---|
| 265 | int abac_attribute_still_valid(abac_attribute_t *attr); |
---|
| 266 | |
---|
[94605f2] | 267 | /* abac name mappings. These are used internally, mostly */ |
---|
| 268 | abac_keyid_mapping_t *abac_keyid_mapping_new(char *k, char *v); |
---|
| 269 | void abac_keyid_mapping_free(abac_keyid_mapping_t *m); |
---|
| 270 | abac_keyid_map_t *abac_keyid_map_new(); |
---|
| 271 | abac_keyid_map_t *abac_keyid_map_dup(abac_keyid_map_t *); |
---|
[d2b198c] | 272 | abac_keyid_map_t *abac_keyid_map_clone(abac_keyid_map_t *); |
---|
[94605f2] | 273 | void abac_keyid_map_free(abac_keyid_map_t *m); |
---|
| 274 | char *abac_keyid_map_key_to_nickname(abac_keyid_map_t *m, char *key); |
---|
| 275 | char *abac_keyid_map_nickname_to_key(abac_keyid_map_t *m, char *nick); |
---|
| 276 | int abac_keyid_map_remove_keyid(abac_keyid_map_t *m, char *key); |
---|
| 277 | int abac_keyid_map_add_nickname(abac_keyid_map_t *m, char *key, char *nick); |
---|
[d2b198c] | 278 | void abac_keyid_map_merge(abac_keyid_map_t *d, abac_keyid_map_t *s, |
---|
| 279 | int overwrite); |
---|
[94605f2] | 280 | char *abac_keyid_map_expand_key(abac_keyid_map_t *m, char *s); |
---|
| 281 | char *abac_keyid_map_expand_nickname(abac_keyid_map_t *m, char *s); |
---|
[9e063cb] | 282 | /* |
---|
| 283 | * Return code for libabac |
---|
| 284 | */ |
---|
| 285 | #define ABAC_RC_SUCCESS 0 |
---|
| 286 | #define ABAC_RC_FAILURE 1 |
---|
[461541a] | 287 | |
---|
[0779c99] | 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 |
---|
[461541a] | 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 | |
---|
[0779c99] | 312 | |
---|
[90d20f0] | 313 | #endif /* __ABAC_H__ */ |
---|