[186cb75] | 1 | |
---|
[461541a] | 2 | /* abac_verifier.c */ |
---|
| 3 | #include <assert.h> |
---|
[5450aeb] | 4 | |
---|
[461541a] | 5 | #include "libabac_common.h" |
---|
| 6 | #include "abac.h" |
---|
[5450aeb] | 7 | |
---|
[342e28f] | 8 | #include "abac_list.h" |
---|
[461541a] | 9 | #include "uthash.h" |
---|
[3c251d0] | 10 | #include "abac_util.h" |
---|
[5450aeb] | 11 | |
---|
[43e3b71] | 12 | typedef struct _abac_id_cert_t { |
---|
[461541a] | 13 | char *keyid; // using cert's keyid |
---|
| 14 | abac_id_t *cert; |
---|
[5450aeb] | 15 | |
---|
| 16 | UT_hash_handle hh; |
---|
[43e3b71] | 17 | } abac_id_cert_t; |
---|
[5450aeb] | 18 | |
---|
[401a054] | 19 | struct _abac_credential_t { |
---|
[1743825] | 20 | abac_role_t *head; |
---|
| 21 | abac_role_t *tail; |
---|
[461541a] | 22 | abac_id_t *issuer; /* Acme of Acme.customer <- Coyote */ |
---|
| 23 | abac_attribute_t *cert; |
---|
[fbb591e] | 24 | |
---|
| 25 | int refcount; |
---|
[5f99fbc] | 26 | }; |
---|
| 27 | |
---|
[461541a] | 28 | /* hash table of all the principal id credentials */ |
---|
[43e3b71] | 29 | abac_id_cert_t *id_certs = NULL; |
---|
[5450aeb] | 30 | |
---|
[461541a] | 31 | /***********************************************************************/ |
---|
[5450aeb] | 32 | // convert a chunk to a lowercase binary string |
---|
| 33 | // malloc's the string |
---|
[461541a] | 34 | static char *_chunk_to_string(abac_chunk_t chunk) { |
---|
[5450aeb] | 35 | int i; |
---|
| 36 | |
---|
[3c251d0] | 37 | char *ret = abac_xmalloc(chunk.len * 2 + 1); |
---|
[5450aeb] | 38 | |
---|
| 39 | for (i = 0; i < chunk.len; ++i) |
---|
| 40 | sprintf(ret + 2 * i, "%02x", chunk.ptr[i]); |
---|
| 41 | |
---|
| 42 | return ret; |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | // verify that cert was issued by issuer |
---|
| 46 | // cert and issuer can be the same, in which case the self-sig is validated |
---|
[461541a] | 47 | static int _verify_cert(abac_id_t *cert) { |
---|
| 48 | |
---|
| 49 | assert(cert); |
---|
| 50 | if(!abac_id_still_valid(cert)) |
---|
| 51 | return 0; |
---|
| 52 | return 1; |
---|
[5450aeb] | 53 | } |
---|
| 54 | |
---|
| 55 | /** |
---|
| 56 | * Init the verifier subsystem. |
---|
| 57 | */ |
---|
[43e3b71] | 58 | void abac_verifier_init(void) { |
---|
[461541a] | 59 | init_openssl(); |
---|
| 60 | init_xmlsec(); |
---|
[5450aeb] | 61 | } |
---|
| 62 | |
---|
[186cb75] | 63 | /** |
---|
| 64 | * Uninitialize the system, free up any allocated memory. |
---|
| 65 | */ |
---|
[43e3b71] | 66 | void abac_verifier_deinit(void) { |
---|
| 67 | abac_id_cert_t *id; |
---|
[186cb75] | 68 | |
---|
| 69 | while ((id = id_certs) != NULL) { |
---|
| 70 | HASH_DEL(id_certs, id); |
---|
| 71 | |
---|
| 72 | free(id->keyid); |
---|
[461541a] | 73 | abac_id_free(id->cert); |
---|
[186cb75] | 74 | free(id); |
---|
| 75 | } |
---|
[461541a] | 76 | deinit_xmlsec(); |
---|
| 77 | deinit_openssl(); |
---|
[186cb75] | 78 | } |
---|
| 79 | |
---|
[5450aeb] | 80 | /** |
---|
| 81 | * Load an ID certificate. |
---|
| 82 | */ |
---|
[461541a] | 83 | static int _load_id(abac_id_t *cert) { |
---|
[327e808] | 84 | abac_id_cert_t *id_cert = NULL; |
---|
[5450aeb] | 85 | char *keyid = NULL; |
---|
| 86 | int ret; |
---|
| 87 | |
---|
[461541a] | 88 | assert(cert); |
---|
[5450aeb] | 89 | |
---|
[2ef48fa] | 90 | // get the key ID |
---|
[461541a] | 91 | keyid = abac_id_keyid(cert); |
---|
[327e808] | 92 | |
---|
| 93 | // if we already have this cert 'error' with success |
---|
| 94 | HASH_FIND_STR(id_certs, keyid, id_cert); |
---|
| 95 | if (id_cert != NULL) { |
---|
| 96 | ret = ABAC_CERT_SUCCESS; |
---|
| 97 | goto error; |
---|
| 98 | } |
---|
| 99 | |
---|
[461541a] | 100 | ret = abac_id_still_valid(cert); |
---|
[0779c99] | 101 | if (!ret) { |
---|
[461541a] | 102 | ret = ABAC_CERT_INVALID; |
---|
[0779c99] | 103 | goto error; |
---|
| 104 | } |
---|
[5450aeb] | 105 | |
---|
| 106 | // success, add the key to the map of certificates |
---|
[327e808] | 107 | id_cert = abac_xmalloc(sizeof(abac_id_cert_t)); |
---|
[461541a] | 108 | id_cert->keyid = abac_xstrdup(keyid); |
---|
[5450aeb] | 109 | id_cert->cert = cert; |
---|
| 110 | HASH_ADD_KEYPTR(hh, id_certs, id_cert->keyid, strlen(id_cert->keyid), id_cert); |
---|
| 111 | |
---|
[0779c99] | 112 | return ABAC_CERT_SUCCESS; |
---|
[5450aeb] | 113 | |
---|
| 114 | error: |
---|
[327e808] | 115 | if (keyid != NULL) free(keyid); |
---|
[0779c99] | 116 | return ret; |
---|
[5450aeb] | 117 | } |
---|
| 118 | |
---|
[461541a] | 119 | abac_id_t *abac_verifier_lookup_id(char *keyid) |
---|
| 120 | { |
---|
| 121 | abac_id_cert_t *id_cert; |
---|
| 122 | HASH_FIND_STR(id_certs, keyid, id_cert); |
---|
| 123 | if(id_cert == NULL) return NULL; |
---|
| 124 | return id_cert->cert; |
---|
| 125 | } |
---|
| 126 | |
---|
[e898049] | 127 | /** |
---|
| 128 | * Load an ID cert from a file. |
---|
| 129 | */ |
---|
[43e3b71] | 130 | int abac_verifier_load_id_file(char *filename) { |
---|
[e898049] | 131 | // load the cert |
---|
[461541a] | 132 | abac_id_t *cert = abac_id_from_file(filename); |
---|
| 133 | |
---|
[e898049] | 134 | if (cert == NULL) |
---|
[0779c99] | 135 | return ABAC_CERT_INVALID; |
---|
[461541a] | 136 | |
---|
[e898049] | 137 | return _load_id(cert); |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | /** |
---|
| 141 | * Load an ID cert from a chunk. |
---|
| 142 | */ |
---|
[461541a] | 143 | int abac_verifier_load_id_chunk(abac_chunk_t chunk) { |
---|
| 144 | |
---|
[e898049] | 145 | // load the cert |
---|
[461541a] | 146 | abac_id_t *cert= abac_id_from_chunk(chunk); |
---|
| 147 | |
---|
[e898049] | 148 | if (cert == NULL) |
---|
[0779c99] | 149 | return ABAC_CERT_INVALID; |
---|
[e898049] | 150 | return _load_id(cert); |
---|
| 151 | } |
---|
| 152 | |
---|
[5450aeb] | 153 | /** |
---|
| 154 | * Load an attribute cert. |
---|
| 155 | * Returns true only if the certificate is valid and is issued by the proper |
---|
| 156 | * authority. |
---|
| 157 | */ |
---|
[461541a] | 158 | static int _load_attribute_cert(abac_attribute_t *cert, abac_credential_t **cred_ret) { |
---|
[1743825] | 159 | abac_role_t *head_role = NULL; |
---|
| 160 | abac_role_t *tail_role = NULL; |
---|
[461541a] | 161 | abac_id_cert_t *issuer=NULL; |
---|
| 162 | int ret; |
---|
[5450aeb] | 163 | |
---|
[461541a] | 164 | char *attr_string=abac_attribute_role_string(cert); |
---|
[5450aeb] | 165 | |
---|
[461541a] | 166 | if(attr_string == NULL) { |
---|
| 167 | ret = ABAC_CERT_INVALID; |
---|
| 168 | goto error; |
---|
[0779c99] | 169 | } |
---|
[5450aeb] | 170 | |
---|
| 171 | // split into head/tail parts |
---|
[342e28f] | 172 | char *head_tail[2]; |
---|
[461541a] | 173 | abac_split(attr_string, "<-", head_tail, &ret); |
---|
[0779c99] | 174 | if (ret != 2) { |
---|
| 175 | ret = ABAC_CERT_INVALID; |
---|
| 176 | goto error; |
---|
| 177 | } |
---|
[5450aeb] | 178 | |
---|
| 179 | // must be a role |
---|
[342e28f] | 180 | head_role = abac_role_from_string(head_tail[0]); |
---|
[5450aeb] | 181 | if (head_role == NULL) goto error; |
---|
[0779c99] | 182 | if (!abac_role_is_role(head_role)) { |
---|
| 183 | ret = ABAC_CERT_INVALID; |
---|
| 184 | goto error; |
---|
| 185 | } |
---|
[5450aeb] | 186 | |
---|
[9a411d7] | 187 | // make sure the tail's valid too |
---|
| 188 | tail_role = abac_role_from_string(head_tail[1]); |
---|
| 189 | if (tail_role == NULL) { |
---|
| 190 | ret = ABAC_CERT_INVALID; |
---|
| 191 | goto error; |
---|
[0779c99] | 192 | } |
---|
[5450aeb] | 193 | |
---|
[dcc1a8e] | 194 | char *principal = abac_role_principal(head_role); |
---|
[5450aeb] | 195 | HASH_FIND_STR(id_certs, principal, issuer); |
---|
[0779c99] | 196 | if (issuer == NULL) { |
---|
| 197 | ret = ABAC_CERT_MISSING_ISSUER; |
---|
| 198 | goto error; |
---|
| 199 | } |
---|
[5450aeb] | 200 | |
---|
[461541a] | 201 | // check if issuer is still valid |
---|
| 202 | if (!abac_id_still_valid(issuer->cert)) { |
---|
| 203 | ret = ABAC_CERT_INVALID_ISSUER; |
---|
[0779c99] | 204 | goto error; |
---|
| 205 | } |
---|
[5450aeb] | 206 | |
---|
[461541a] | 207 | // make sure principal match up with keyid |
---|
| 208 | if(!abac_id_has_keyid(issuer->cert,principal)) { |
---|
| 209 | ret = ABAC_CERT_BAD_PRINCIPAL; |
---|
| 210 | goto error; |
---|
| 211 | } |
---|
| 212 | |
---|
[5450aeb] | 213 | // at this point we know we have a good attribute cert |
---|
[401a054] | 214 | abac_credential_t *cred = abac_xmalloc(sizeof(abac_credential_t)); |
---|
| 215 | cred->head = head_role; |
---|
| 216 | cred->tail = tail_role; |
---|
| 217 | cred->cert = cert; |
---|
[461541a] | 218 | /* acme's cert */ |
---|
| 219 | cred->issuer = abac_id_dup(issuer->cert); |
---|
[fbb591e] | 220 | cred->refcount = 1; |
---|
[0779c99] | 221 | *cred_ret = cred; |
---|
[5450aeb] | 222 | |
---|
[0779c99] | 223 | return ABAC_CERT_SUCCESS; |
---|
[5450aeb] | 224 | |
---|
| 225 | error: |
---|
[dcc1a8e] | 226 | if (head_role != NULL) abac_role_free(head_role); |
---|
| 227 | if (tail_role != NULL) abac_role_free(tail_role); |
---|
[5450aeb] | 228 | |
---|
[0779c99] | 229 | return ret; |
---|
[5450aeb] | 230 | } |
---|
[5f99fbc] | 231 | |
---|
[461541a] | 232 | static int _load_attribute_certs(abac_list_t *attr_list, abac_list_t *cred_list) { |
---|
| 233 | |
---|
| 234 | int sz=abac_list_size(attr_list); |
---|
| 235 | abac_credential_t *cred_ret=NULL; |
---|
| 236 | abac_attribute_t *attr; |
---|
| 237 | if(sz) { |
---|
| 238 | abac_list_foreach(attr_list, attr, |
---|
| 239 | int ret=_load_attribute_cert(attr, &cred_ret); |
---|
| 240 | if(ret==ABAC_CERT_SUCCESS) { |
---|
| 241 | abac_list_add(cred_list, cred_ret); |
---|
| 242 | } |
---|
| 243 | ); |
---|
| 244 | } |
---|
| 245 | return sz; |
---|
| 246 | } |
---|
| 247 | |
---|
[e898049] | 248 | /** |
---|
| 249 | * Load an attribute cert from a file. |
---|
| 250 | */ |
---|
[461541a] | 251 | int abac_verifier_load_attribute_cert_file(char *filename, abac_list_t *cred_list) { |
---|
| 252 | |
---|
[e898049] | 253 | // load the cert |
---|
[461541a] | 254 | abac_list_t *attr_list = abac_attribute_certs_from_file(filename); |
---|
| 255 | |
---|
| 256 | if (abac_list_size(attr_list) == 0) |
---|
| 257 | return ABAC_CERT_INVALID; |
---|
| 258 | |
---|
| 259 | int ret=_load_attribute_certs(attr_list, cred_list); |
---|
| 260 | |
---|
| 261 | if(ret) return ABAC_CERT_SUCCESS; |
---|
[0779c99] | 262 | return ABAC_CERT_INVALID; |
---|
[e898049] | 263 | } |
---|
| 264 | |
---|
| 265 | /** |
---|
| 266 | * Load an attribute cert from a chunk. |
---|
| 267 | */ |
---|
[461541a] | 268 | int abac_verifier_load_attribute_cert_chunk(abac_chunk_t chunk, abac_list_t *cred_list) { |
---|
| 269 | |
---|
[e898049] | 270 | // load the cert |
---|
[461541a] | 271 | abac_list_t *attr_list = abac_attribute_certs_from_chunk(chunk); |
---|
| 272 | |
---|
| 273 | if (abac_list_size(attr_list) == 0) |
---|
[0779c99] | 274 | return ABAC_CERT_INVALID; |
---|
[461541a] | 275 | return _load_attribute_certs(attr_list,cred_list); |
---|
[e898049] | 276 | } |
---|
| 277 | |
---|
[5f99fbc] | 278 | /** |
---|
| 279 | * Return the head role. |
---|
| 280 | */ |
---|
[401a054] | 281 | abac_role_t *abac_credential_head(abac_credential_t *cred) { |
---|
| 282 | return cred->head; |
---|
[5f99fbc] | 283 | } |
---|
| 284 | |
---|
| 285 | /** |
---|
| 286 | * Return the tail role. |
---|
| 287 | */ |
---|
[401a054] | 288 | abac_role_t *abac_credential_tail(abac_credential_t *cred) { |
---|
| 289 | return cred->tail; |
---|
[5f99fbc] | 290 | } |
---|
[85cdf53] | 291 | |
---|
| 292 | /** |
---|
[461541a] | 293 | * Return the xml chunk of the attribute cert. |
---|
[85cdf53] | 294 | */ |
---|
[401a054] | 295 | abac_chunk_t abac_credential_attribute_cert(abac_credential_t *cred) { |
---|
[461541a] | 296 | assert(cred); assert(cred->cert); |
---|
| 297 | return abac_attribute_cert(cred->cert); |
---|
[85cdf53] | 298 | } |
---|
| 299 | |
---|
| 300 | /** |
---|
[461541a] | 301 | * Return the chunk of the issuer cert. |
---|
[85cdf53] | 302 | */ |
---|
[401a054] | 303 | abac_chunk_t abac_credential_issuer_cert(abac_credential_t *cred) { |
---|
[461541a] | 304 | assert(cred); assert(cred->issuer); |
---|
| 305 | abac_chunk_t ret= abac_id_in_PEM(cred->issuer); |
---|
[9efbfbf] | 306 | return ret; |
---|
[85cdf53] | 307 | } |
---|
[186cb75] | 308 | |
---|
| 309 | /** |
---|
[fbb591e] | 310 | * Increase the ref count of a credential. |
---|
[186cb75] | 311 | */ |
---|
[401a054] | 312 | abac_credential_t *abac_credential_dup(abac_credential_t *cred) { |
---|
| 313 | assert(cred != NULL); |
---|
[186cb75] | 314 | |
---|
[fbb591e] | 315 | ++cred->refcount; |
---|
| 316 | return cred; |
---|
[186cb75] | 317 | } |
---|
| 318 | |
---|
| 319 | /** |
---|
[fbb591e] | 320 | * Decrease the reference count of a credential, freeing it when it reaches 0. |
---|
[186cb75] | 321 | */ |
---|
[401a054] | 322 | void abac_credential_free(abac_credential_t *cred) { |
---|
| 323 | if (cred == NULL) |
---|
[186cb75] | 324 | return; |
---|
| 325 | |
---|
[fbb591e] | 326 | --cred->refcount; |
---|
| 327 | if (cred->refcount > 0) |
---|
| 328 | return; |
---|
| 329 | |
---|
[401a054] | 330 | abac_role_free(cred->head); |
---|
| 331 | abac_role_free(cred->tail); |
---|
[461541a] | 332 | abac_attribute_free(cred->cert); |
---|
| 333 | abac_id_free(cred->issuer); |
---|
[186cb75] | 334 | |
---|
[401a054] | 335 | free(cred); |
---|
[186cb75] | 336 | } |
---|