[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 | */ |
---|
[94605f2] | 87 | static int _load_id(abac_list_t *id_certs, abac_id_t *cert, |
---|
| 88 | abac_keyid_map_t *km) { |
---|
[327e808] | 89 | abac_id_cert_t *id_cert = NULL; |
---|
[5450aeb] | 90 | char *keyid = NULL; |
---|
[94605f2] | 91 | char *nick = NULL; |
---|
[5450aeb] | 92 | int ret; |
---|
| 93 | |
---|
[461541a] | 94 | assert(cert); |
---|
[5450aeb] | 95 | |
---|
[2ef48fa] | 96 | // get the key ID |
---|
[461541a] | 97 | keyid = abac_id_keyid(cert); |
---|
[327e808] | 98 | |
---|
| 99 | // if we already have this cert 'error' with success |
---|
[bec30b5] | 100 | id_cert=_lookup_cert(id_certs, keyid); |
---|
| 101 | |
---|
[327e808] | 102 | if (id_cert != NULL) { |
---|
[ec550f7] | 103 | /* special case, if |
---|
| 104 | existing id does not have private key and |
---|
| 105 | incoming does, then need to bring that bit of |
---|
| 106 | information in */ |
---|
| 107 | if(abac_id_has_privkey(cert) && |
---|
| 108 | !abac_id_has_privkey(id_cert->cert)) { |
---|
| 109 | abac_id_pass_privkey_from_id(id_cert->cert, cert); |
---|
| 110 | } |
---|
[327e808] | 111 | ret = ABAC_CERT_SUCCESS; |
---|
| 112 | goto error; |
---|
| 113 | } |
---|
| 114 | |
---|
[461541a] | 115 | ret = abac_id_still_valid(cert); |
---|
[0779c99] | 116 | if (!ret) { |
---|
[461541a] | 117 | ret = ABAC_CERT_INVALID; |
---|
[0779c99] | 118 | goto error; |
---|
| 119 | } |
---|
[5450aeb] | 120 | |
---|
| 121 | // success, add the key to the map of certificates |
---|
[327e808] | 122 | id_cert = abac_xmalloc(sizeof(abac_id_cert_t)); |
---|
[461541a] | 123 | id_cert->keyid = abac_xstrdup(keyid); |
---|
[5450aeb] | 124 | id_cert->cert = cert; |
---|
[bec30b5] | 125 | abac_list_add(id_certs, id_cert); |
---|
[94605f2] | 126 | /* Add the new id and issuer to the keyid <-> name map */ |
---|
| 127 | if ( km && keyid && cert ) { |
---|
| 128 | if ( (nick= abac_id_issuer(cert)) ) { |
---|
| 129 | /* If the issuer starts with /CN=, as many do, |
---|
| 130 | * trim the /CN= off */ |
---|
| 131 | if (!strncmp(nick, "/CN=", 4) && strlen(nick) > 4) |
---|
| 132 | abac_keyid_map_add_nickname(km, keyid, nick+4); |
---|
| 133 | else |
---|
| 134 | abac_keyid_map_add_nickname(km, keyid, nick); |
---|
| 135 | free(nick); |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | |
---|
[5450aeb] | 139 | |
---|
[0779c99] | 140 | return ABAC_CERT_SUCCESS; |
---|
[5450aeb] | 141 | |
---|
| 142 | error: |
---|
[327e808] | 143 | if (keyid != NULL) free(keyid); |
---|
[0779c99] | 144 | return ret; |
---|
[5450aeb] | 145 | } |
---|
| 146 | |
---|
[bec30b5] | 147 | abac_id_t *abac_verifier_lookup_id(abac_list_t *id_certs, char *keyid) |
---|
[461541a] | 148 | { |
---|
[bec30b5] | 149 | abac_id_cert_t *id_cert=_lookup_cert(id_certs,keyid); |
---|
[461541a] | 150 | if(id_cert == NULL) return NULL; |
---|
| 151 | return id_cert->cert; |
---|
| 152 | } |
---|
| 153 | |
---|
[e898049] | 154 | /** |
---|
| 155 | * Load an ID cert from a file. |
---|
| 156 | */ |
---|
[94605f2] | 157 | int abac_verifier_load_id_file(abac_list_t *id_certs, char *filename, |
---|
| 158 | abac_keyid_map_t *km) { |
---|
[e898049] | 159 | // load the cert |
---|
[461541a] | 160 | abac_id_t *cert = abac_id_from_file(filename); |
---|
| 161 | |
---|
[e898049] | 162 | if (cert == NULL) |
---|
[0779c99] | 163 | return ABAC_CERT_INVALID; |
---|
[461541a] | 164 | |
---|
[94605f2] | 165 | return _load_id(id_certs,cert, km); |
---|
[e898049] | 166 | } |
---|
| 167 | |
---|
| 168 | /** |
---|
| 169 | * Load an ID cert from a chunk. |
---|
| 170 | */ |
---|
[94605f2] | 171 | int abac_verifier_load_id_chunk(abac_list_t *id_certs,abac_chunk_t chunk, |
---|
| 172 | abac_keyid_map_t *km) { |
---|
[461541a] | 173 | |
---|
[e898049] | 174 | // load the cert |
---|
[461541a] | 175 | abac_id_t *cert= abac_id_from_chunk(chunk); |
---|
| 176 | |
---|
[e898049] | 177 | if (cert == NULL) |
---|
[0779c99] | 178 | return ABAC_CERT_INVALID; |
---|
[94605f2] | 179 | return _load_id(id_certs,cert, km); |
---|
[e898049] | 180 | } |
---|
| 181 | |
---|
[ec550f7] | 182 | /** |
---|
| 183 | * Load an ID cert from a char ptr of a X509 pem data |
---|
| 184 | * this is called from parse_privilege(..) |
---|
| 185 | */ |
---|
[94605f2] | 186 | int abac_verifier_load_id_chars(void *id_certs,char *naked_pem, |
---|
| 187 | abac_keyid_map_t *km) { |
---|
[bec30b5] | 188 | /* if id_certs is NULL, don't even try to load it */ |
---|
| 189 | if(id_certs == NULL) return ABAC_CERT_SUCCESS; |
---|
[ec550f7] | 190 | /* make a well formed pem from this */ |
---|
| 191 | char *new_naked_pem=abac_xstrdup(naked_pem); |
---|
| 192 | char *pem=make_pem_from_naked_pem(new_naked_pem); |
---|
| 193 | int len=strlen(pem); |
---|
| 194 | abac_chunk_t chunk = { pem, len }; |
---|
| 195 | free(new_naked_pem); |
---|
[94605f2] | 196 | return abac_verifier_load_id_chunk((abac_list_t *)id_certs,chunk, km); |
---|
[ec550f7] | 197 | } |
---|
[5450aeb] | 198 | /** |
---|
| 199 | * Load an attribute cert. |
---|
| 200 | * Returns true only if the certificate is valid and is issued by the proper |
---|
| 201 | * authority. |
---|
| 202 | */ |
---|
[d2b198c] | 203 | static int _load_attribute_cert(abac_list_t *id_certs,abac_attribute_t *cert, |
---|
| 204 | abac_credential_t **cred_ret, abac_keyid_map_t *km) { |
---|
[1743825] | 205 | abac_role_t *head_role = NULL; |
---|
| 206 | abac_role_t *tail_role = NULL; |
---|
[461541a] | 207 | abac_id_cert_t *issuer=NULL; |
---|
[d2b198c] | 208 | abac_keyid_map_t *local_names = NULL; |
---|
[461541a] | 209 | int ret; |
---|
[5450aeb] | 210 | |
---|
[461541a] | 211 | char *attr_string=abac_attribute_role_string(cert); |
---|
[5450aeb] | 212 | |
---|
[461541a] | 213 | if(attr_string == NULL) { |
---|
| 214 | ret = ABAC_CERT_INVALID; |
---|
| 215 | goto error; |
---|
[0779c99] | 216 | } |
---|
[5450aeb] | 217 | |
---|
| 218 | // split into head/tail parts |
---|
[342e28f] | 219 | char *head_tail[2]; |
---|
[9f78e4c] | 220 | ret = 2; |
---|
[461541a] | 221 | abac_split(attr_string, "<-", head_tail, &ret); |
---|
[0779c99] | 222 | if (ret != 2) { |
---|
| 223 | ret = ABAC_CERT_INVALID; |
---|
| 224 | goto error; |
---|
| 225 | } |
---|
[5450aeb] | 226 | |
---|
| 227 | // must be a role |
---|
[342e28f] | 228 | head_role = abac_role_from_string(head_tail[0]); |
---|
[5450aeb] | 229 | if (head_role == NULL) goto error; |
---|
[0779c99] | 230 | if (!abac_role_is_role(head_role)) { |
---|
| 231 | ret = ABAC_CERT_INVALID; |
---|
| 232 | goto error; |
---|
| 233 | } |
---|
[5450aeb] | 234 | |
---|
[9a411d7] | 235 | // make sure the tail's valid too |
---|
| 236 | tail_role = abac_role_from_string(head_tail[1]); |
---|
| 237 | if (tail_role == NULL) { |
---|
| 238 | ret = ABAC_CERT_INVALID; |
---|
| 239 | goto error; |
---|
[0779c99] | 240 | } |
---|
[5450aeb] | 241 | |
---|
[dcc1a8e] | 242 | char *principal = abac_role_principal(head_role); |
---|
[bec30b5] | 243 | issuer=_lookup_cert(id_certs,principal); |
---|
[0779c99] | 244 | if (issuer == NULL) { |
---|
| 245 | ret = ABAC_CERT_MISSING_ISSUER; |
---|
| 246 | goto error; |
---|
| 247 | } |
---|
[5450aeb] | 248 | |
---|
[461541a] | 249 | // check if issuer is still valid |
---|
| 250 | if (!abac_id_still_valid(issuer->cert)) { |
---|
| 251 | ret = ABAC_CERT_INVALID_ISSUER; |
---|
[0779c99] | 252 | goto error; |
---|
| 253 | } |
---|
[5450aeb] | 254 | |
---|
[461541a] | 255 | // make sure principal match up with keyid |
---|
| 256 | if(!abac_id_has_keyid(issuer->cert,principal)) { |
---|
| 257 | ret = ABAC_CERT_BAD_PRINCIPAL; |
---|
| 258 | goto error; |
---|
| 259 | } |
---|
[d2b198c] | 260 | |
---|
| 261 | if ( (local_names = abac_attribute_get_keyid_map(cert)) && km ) |
---|
| 262 | abac_keyid_map_merge(km, local_names, 1); |
---|
[461541a] | 263 | |
---|
[5450aeb] | 264 | // at this point we know we have a good attribute cert |
---|
[401a054] | 265 | abac_credential_t *cred = abac_xmalloc(sizeof(abac_credential_t)); |
---|
| 266 | cred->head = head_role; |
---|
| 267 | cred->tail = tail_role; |
---|
| 268 | cred->cert = cert; |
---|
[461541a] | 269 | /* acme's cert */ |
---|
| 270 | cred->issuer = abac_id_dup(issuer->cert); |
---|
[fbb591e] | 271 | cred->refcount = 1; |
---|
[0779c99] | 272 | *cred_ret = cred; |
---|
[5450aeb] | 273 | |
---|
[0779c99] | 274 | return ABAC_CERT_SUCCESS; |
---|
[5450aeb] | 275 | |
---|
| 276 | error: |
---|
[dcc1a8e] | 277 | if (head_role != NULL) abac_role_free(head_role); |
---|
| 278 | if (tail_role != NULL) abac_role_free(tail_role); |
---|
[5450aeb] | 279 | |
---|
[0779c99] | 280 | return ret; |
---|
[5450aeb] | 281 | } |
---|
[5f99fbc] | 282 | |
---|
[d2b198c] | 283 | static int _load_attribute_certs(abac_list_t *id_certs,abac_list_t *attr_list, |
---|
| 284 | abac_list_t *cred_list, abac_keyid_map_t *km) { |
---|
[461541a] | 285 | |
---|
| 286 | int sz=abac_list_size(attr_list); |
---|
| 287 | abac_credential_t *cred_ret=NULL; |
---|
| 288 | abac_attribute_t *attr; |
---|
| 289 | if(sz) { |
---|
| 290 | abac_list_foreach(attr_list, attr, |
---|
[d2b198c] | 291 | int ret=_load_attribute_cert(id_certs, attr, &cred_ret, km); |
---|
[461541a] | 292 | if(ret==ABAC_CERT_SUCCESS) { |
---|
| 293 | abac_list_add(cred_list, cred_ret); |
---|
| 294 | } |
---|
| 295 | ); |
---|
| 296 | } |
---|
| 297 | return sz; |
---|
| 298 | } |
---|
| 299 | |
---|
[e898049] | 300 | /** |
---|
| 301 | * Load an attribute cert from a file. |
---|
| 302 | */ |
---|
[d2b198c] | 303 | int abac_verifier_load_attribute_cert_file(abac_list_t *id_certs,char *filename, abac_list_t *cred_list, abac_keyid_map_t *km) { |
---|
[461541a] | 304 | |
---|
[e898049] | 305 | // load the cert |
---|
[bec30b5] | 306 | abac_list_t *attr_list = abac_attribute_certs_from_file(id_certs,filename); |
---|
[461541a] | 307 | |
---|
| 308 | if (abac_list_size(attr_list) == 0) |
---|
| 309 | return ABAC_CERT_INVALID; |
---|
| 310 | |
---|
[d2b198c] | 311 | int ret=_load_attribute_certs(id_certs,attr_list, cred_list, km); |
---|
[461541a] | 312 | |
---|
| 313 | if(ret) return ABAC_CERT_SUCCESS; |
---|
[0779c99] | 314 | return ABAC_CERT_INVALID; |
---|
[e898049] | 315 | } |
---|
| 316 | |
---|
| 317 | /** |
---|
| 318 | * Load an attribute cert from a chunk. |
---|
| 319 | */ |
---|
[d2b198c] | 320 | int abac_verifier_load_attribute_cert_chunk(abac_list_t *id_certs,abac_chunk_t chunk, abac_list_t *cred_list, abac_keyid_map_t *km) { |
---|
[461541a] | 321 | |
---|
[e898049] | 322 | // load the cert |
---|
[bec30b5] | 323 | abac_list_t *attr_list = abac_attribute_certs_from_chunk(id_certs,chunk); |
---|
[461541a] | 324 | |
---|
| 325 | if (abac_list_size(attr_list) == 0) |
---|
[0779c99] | 326 | return ABAC_CERT_INVALID; |
---|
[4721618] | 327 | |
---|
[d2b198c] | 328 | int ret=_load_attribute_certs(id_certs,attr_list,cred_list, km); |
---|
[4721618] | 329 | |
---|
| 330 | if(ret) return ABAC_CERT_SUCCESS; |
---|
| 331 | return ABAC_CERT_INVALID; |
---|
[e898049] | 332 | } |
---|
| 333 | |
---|
[5f99fbc] | 334 | /** |
---|
| 335 | * Return the head role. |
---|
| 336 | */ |
---|
[401a054] | 337 | abac_role_t *abac_credential_head(abac_credential_t *cred) { |
---|
| 338 | return cred->head; |
---|
[5f99fbc] | 339 | } |
---|
| 340 | |
---|
| 341 | /** |
---|
| 342 | * Return the tail role. |
---|
| 343 | */ |
---|
[401a054] | 344 | abac_role_t *abac_credential_tail(abac_credential_t *cred) { |
---|
| 345 | return cred->tail; |
---|
[5f99fbc] | 346 | } |
---|
[85cdf53] | 347 | |
---|
| 348 | /** |
---|
[461541a] | 349 | * Return the xml chunk of the attribute cert. |
---|
[85cdf53] | 350 | */ |
---|
[401a054] | 351 | abac_chunk_t abac_credential_attribute_cert(abac_credential_t *cred) { |
---|
[461541a] | 352 | assert(cred); assert(cred->cert); |
---|
| 353 | return abac_attribute_cert(cred->cert); |
---|
[85cdf53] | 354 | } |
---|
| 355 | |
---|
| 356 | /** |
---|
[461541a] | 357 | * Return the chunk of the issuer cert. |
---|
[85cdf53] | 358 | */ |
---|
[401a054] | 359 | abac_chunk_t abac_credential_issuer_cert(abac_credential_t *cred) { |
---|
[461541a] | 360 | assert(cred); assert(cred->issuer); |
---|
| 361 | abac_chunk_t ret= abac_id_in_PEM(cred->issuer); |
---|
[9efbfbf] | 362 | return ret; |
---|
[85cdf53] | 363 | } |
---|
[186cb75] | 364 | |
---|
| 365 | /** |
---|
[fbb591e] | 366 | * Increase the ref count of a credential. |
---|
[186cb75] | 367 | */ |
---|
[401a054] | 368 | abac_credential_t *abac_credential_dup(abac_credential_t *cred) { |
---|
| 369 | assert(cred != NULL); |
---|
[186cb75] | 370 | |
---|
[fbb591e] | 371 | ++cred->refcount; |
---|
| 372 | return cred; |
---|
[186cb75] | 373 | } |
---|
| 374 | |
---|
[bec30b5] | 375 | abac_id_cert_t *abac_id_cert_dup(abac_id_cert_t *id_cert) { |
---|
| 376 | assert(id_cert != NULL && id_cert->keyid!=NULL); |
---|
| 377 | |
---|
| 378 | abac_id_cert_t *new_cert = abac_xmalloc(sizeof(abac_id_cert_t)); |
---|
| 379 | new_cert->keyid=abac_xstrdup(id_cert->keyid); |
---|
| 380 | new_cert->cert=abac_id_dup(id_cert->cert); |
---|
| 381 | |
---|
| 382 | return new_cert; |
---|
| 383 | } |
---|
| 384 | |
---|
| 385 | |
---|
[186cb75] | 386 | /** |
---|
[fbb591e] | 387 | * Decrease the reference count of a credential, freeing it when it reaches 0. |
---|
[186cb75] | 388 | */ |
---|
[401a054] | 389 | void abac_credential_free(abac_credential_t *cred) { |
---|
| 390 | if (cred == NULL) |
---|
[186cb75] | 391 | return; |
---|
| 392 | |
---|
[fbb591e] | 393 | --cred->refcount; |
---|
| 394 | if (cred->refcount > 0) |
---|
| 395 | return; |
---|
| 396 | |
---|
[401a054] | 397 | abac_role_free(cred->head); |
---|
| 398 | abac_role_free(cred->tail); |
---|
[461541a] | 399 | abac_attribute_free(cred->cert); |
---|
| 400 | abac_id_free(cred->issuer); |
---|
[186cb75] | 401 | |
---|
[401a054] | 402 | free(cred); |
---|
[186cb75] | 403 | } |
---|
[bec30b5] | 404 | |
---|
| 405 | /** |
---|
| 406 | * remove an id cert, (not reference counted, so abac_id_certs are |
---|
| 407 | * deep-copied) |
---|
| 408 | */ |
---|
| 409 | void abac_id_cert_free(abac_id_cert_t *id_cert) { |
---|
| 410 | if (id_cert==NULL) |
---|
| 411 | return; |
---|
| 412 | |
---|
| 413 | abac_id_free(id_cert->cert); |
---|
| 414 | if(id_cert->keyid) free(id_cert->keyid); |
---|
| 415 | free(id_cert); |
---|
| 416 | } |
---|
| 417 | |
---|
| 418 | char *abac_id_cert_keyid(abac_id_cert_t *id_cert) |
---|
| 419 | { |
---|
| 420 | assert(id_cert); |
---|
| 421 | return id_cert->keyid; |
---|
| 422 | } |
---|
| 423 | |
---|
| 424 | |
---|
| 425 | |
---|