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