[04f5da1] | 1 | #ifndef __LIBCREDDY_H__ |
---|
| 2 | #define __LIBCREDDY_H__ |
---|
| 3 | |
---|
| 4 | #include <stdio.h> |
---|
[405bba3] | 5 | #include <time.h> // for time_t |
---|
[04f5da1] | 6 | |
---|
[11e3eb7] | 7 | #include <abac_common.h> |
---|
| 8 | |
---|
[04f5da1] | 9 | #define CREDDY_SUCCESS 0 |
---|
| 10 | #define CREDDY_GENERATE_INVALID_CN -1 |
---|
| 11 | #define CREDDY_GENERATE_INVALID_VALIDITY -2 |
---|
| 12 | #define CREDDY_ATTRIBUTE_ISSUER_NOKEY -3 |
---|
| 13 | #define CREDDY_ATTRIBUTE_INVALID_ROLE -4 |
---|
| 14 | #define CREDDY_ATTRIBUTE_INVALID_VALIDITY -5 |
---|
| 15 | |
---|
| 16 | /** |
---|
| 17 | * Creddy identifiers. |
---|
| 18 | */ |
---|
| 19 | |
---|
| 20 | typedef struct _creddy_id_t creddy_id_t; |
---|
| 21 | |
---|
| 22 | // create an ID from an X.509 certificate |
---|
| 23 | creddy_id_t *creddy_id_from_file(char *filename); |
---|
| 24 | |
---|
[956e1c6] | 25 | // create an ID from a DER-encoded X.509 certificate chunk |
---|
| 26 | creddy_id_t *creddy_id_from_chunk(abac_chunk_t chunk); |
---|
| 27 | |
---|
[04f5da1] | 28 | // load an X.509 private key for an from a file |
---|
| 29 | // handles keys with a password |
---|
| 30 | int creddy_id_load_privkey(creddy_id_t *id, char *filename); |
---|
| 31 | |
---|
| 32 | // generate an ID |
---|
| 33 | // returns one of CREDDY_SUCCESS or CREDDY_GENERATE_* (see top) |
---|
| 34 | int creddy_id_generate(creddy_id_t **ret, char *cn, int validity); |
---|
| 35 | |
---|
| 36 | // get the SHA1 keyid, pointer is valid for the lifetime of the object |
---|
| 37 | char *creddy_id_keyid(creddy_id_t *id); |
---|
| 38 | |
---|
[adc86c3] | 39 | // get the name of the issuer |
---|
| 40 | // caller must free the returned string |
---|
| 41 | char *creddy_id_issuer(creddy_id_t *id); |
---|
| 42 | |
---|
[b19d1f0] | 43 | // get the DN of the subject |
---|
| 44 | // caller must free the returned string |
---|
| 45 | char *creddy_id_subject(creddy_id_t *id); |
---|
| 46 | |
---|
[405bba3] | 47 | // get the validity period of the cert |
---|
| 48 | void creddy_id_validity(creddy_id_t *id, time_t *not_before, time_t *not_after); |
---|
| 49 | |
---|
[04f5da1] | 50 | // default filename for the cert: ${CN}_ID.pem |
---|
| 51 | // caller must free the returned string |
---|
| 52 | char *creddy_id_cert_filename(creddy_id_t *id); |
---|
| 53 | |
---|
| 54 | // write the cert fo an open file pointer |
---|
| 55 | void creddy_id_write_cert(creddy_id_t *id, FILE *out); |
---|
| 56 | |
---|
| 57 | // default filename for the private key: ${CN}_key.pem |
---|
| 58 | // caller must free the return value |
---|
| 59 | char *creddy_id_privkey_filename(creddy_id_t *id); |
---|
| 60 | |
---|
| 61 | // write the private key to a file |
---|
| 62 | // it is recommended that you open this file mode 0600 |
---|
[d56e51b] | 63 | // returns false if there's no private key loaded |
---|
| 64 | int creddy_id_write_privkey(creddy_id_t *id, FILE *out); |
---|
[04f5da1] | 65 | |
---|
[11e3eb7] | 66 | // get a chunk representing the cert |
---|
| 67 | // you must free the ptr of the chunk when done |
---|
| 68 | abac_chunk_t creddy_id_cert_chunk(creddy_id_t *id); |
---|
| 69 | |
---|
[2a20fa0] | 70 | // dup an ID (increases its refcount) |
---|
| 71 | creddy_id_t *creddy_id_dup(creddy_id_t *id); |
---|
| 72 | |
---|
[04f5da1] | 73 | // destroy the id |
---|
[2a20fa0] | 74 | // decreases refcount and destroys when it hits 0 |
---|
[04f5da1] | 75 | void creddy_id_free(creddy_id_t *id); |
---|
| 76 | |
---|
| 77 | /** |
---|
| 78 | * Attribute cert |
---|
| 79 | */ |
---|
| 80 | typedef struct _creddy_attribute_t creddy_attribute_t; |
---|
| 81 | |
---|
| 82 | // |
---|
| 83 | // Here's the skinny: |
---|
| 84 | // Attribute cert objects don't contain an actual cert until they're baked. |
---|
| 85 | // First you construct the object using creddy_attribute_create, then you add |
---|
| 86 | // subjects to it using creddy_attribute_{principal,role,linking_role}. |
---|
| 87 | // Finally you bake it. Once you've done that, you can access the DER encoding |
---|
| 88 | // or write it to a file. |
---|
| 89 | // |
---|
| 90 | |
---|
| 91 | // create an attribute cert |
---|
| 92 | // validity is in days |
---|
| 93 | // returns one of CREDDY_SUCCESS or CREDDY_ATTRIBUTE_* (see top) |
---|
| 94 | int creddy_attribute_create(creddy_attribute_t **attr, creddy_id_t *issuer, char *role, int validity); |
---|
| 95 | |
---|
| 96 | // add a principal subject to the cert |
---|
| 97 | int creddy_attribute_principal(creddy_attribute_t *attr, char *keyid); |
---|
| 98 | |
---|
| 99 | // add a role subject |
---|
| 100 | int creddy_attribute_role(creddy_attribute_t *attr, char *keyid, char *role); |
---|
| 101 | |
---|
| 102 | // add a linking role subject |
---|
| 103 | int creddy_attribute_linking_role(creddy_attribute_t *attr, char *keyid, char *role, char *linked); |
---|
| 104 | |
---|
| 105 | // create the attribute cert once all the subjects have been added |
---|
| 106 | // can return 0 if there are no subjects or there's a problem building the cert |
---|
| 107 | int creddy_attribute_bake(creddy_attribute_t *attr); |
---|
| 108 | |
---|
[e02c742] | 109 | // returns true iff the cert's been baked |
---|
| 110 | int creddy_attribute_baked(creddy_attribute_t *attr); |
---|
| 111 | |
---|
[78358ab] | 112 | // write the cert to a file. returns 0 if the cert hasn't been baked |
---|
| 113 | int creddy_attribute_write(creddy_attribute_t *attr, FILE *out); |
---|
| 114 | |
---|
[11e3eb7] | 115 | // get the DER-encoded cert |
---|
| 116 | // returns 0 if the cert isn't baked |
---|
| 117 | int creddy_attribute_cert_chunk(creddy_attribute_t *attr, abac_chunk_t *chunk); |
---|
| 118 | |
---|
[04f5da1] | 119 | // destroy the cert |
---|
| 120 | void creddy_attribute_free(creddy_attribute_t *attr); |
---|
| 121 | |
---|
| 122 | #endif /* __LIBCREDDY_H__ */ |
---|