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