[53e540d] | 1 | |
---|
| 2 | // include the GNU extension of asprintf |
---|
| 3 | #define _GNU_SOURCE |
---|
| 4 | |
---|
[186cb75] | 5 | #include <assert.h> |
---|
[202a7f9] | 6 | #include <stdio.h> |
---|
[186cb75] | 7 | |
---|
[5450aeb] | 8 | #include <library.h> |
---|
| 9 | #include <credentials/certificates/certificate.h> |
---|
| 10 | #include <credentials/certificates/x509.h> |
---|
| 11 | #include <credentials/certificates/ac.h> |
---|
| 12 | |
---|
[43e3b71] | 13 | #include "abac_verifier.h" |
---|
[5450aeb] | 14 | |
---|
[342e28f] | 15 | #include "abac_list.h" |
---|
[3c251d0] | 16 | #include "abac_util.h" |
---|
[202a7f9] | 17 | #include "abac_rt.h" |
---|
[5450aeb] | 18 | |
---|
[b5a3da4] | 19 | extern abac_role_t *abac_yy_get_rule_head_role(); |
---|
| 20 | extern abac_role_t *abac_yy_get_rule_tail_role(); |
---|
| 21 | extern abac_oset_t *abac_yy_get_rule_head_oset(); |
---|
| 22 | extern abac_oset_t *abac_yy_get_rule_tail_oset(); |
---|
| 23 | extern int abac_yy_get_rule_is_oset(); |
---|
| 24 | extern abac_list_t *abac_yy_get_rule_clauses(); |
---|
[da5afdf] | 25 | extern char* abac_decode_string(char *); |
---|
[b5a3da4] | 26 | extern void abac_yy_free_rule_clauses(); |
---|
[0d0c3a9] | 27 | extern char *generate_pl_type_clause(char *, int); |
---|
| 28 | |
---|
[202a7f9] | 29 | |
---|
| 30 | /* keeping this a fixed enum is to have tighter control of |
---|
| 31 | what are acceptabletypes of keyid are we allowing */ |
---|
[0d0c3a9] | 32 | typedef enum _keyidtype_t { |
---|
| 33 | e_KEYID = 1, |
---|
| 34 | } keyidtype_t; |
---|
| 35 | |
---|
[202a7f9] | 36 | /* keyidname[keyidtype] */ |
---|
[da5afdf] | 37 | static char * const keyidname[] = |
---|
[202a7f9] | 38 | { |
---|
| 39 | "badidtype", "keyid", 0 |
---|
| 40 | }; |
---|
| 41 | |
---|
[0d0c3a9] | 42 | typedef enum _credtype_t { |
---|
[da5afdf] | 43 | e_CREDTYPE_ROLE =1, |
---|
[0d0c3a9] | 44 | e_CREDTYPE_OSET =2, |
---|
| 45 | } credtype_t; |
---|
[da5afdf] | 46 | |
---|
| 47 | static int keyidname_cnt=1; |
---|
[202a7f9] | 48 | |
---|
| 49 | struct _abac_id_cert_t { |
---|
[5450aeb] | 50 | char *keyid; |
---|
[53e540d] | 51 | char *cn; |
---|
[202a7f9] | 52 | int type; /* keyidtype */ |
---|
[5450aeb] | 53 | certificate_t *cert; |
---|
[202a7f9] | 54 | char *clause; |
---|
[5450aeb] | 55 | |
---|
| 56 | UT_hash_handle hh; |
---|
[202a7f9] | 57 | }; |
---|
[5450aeb] | 58 | |
---|
[da5afdf] | 59 | /* can store either role or oset */ |
---|
[401a054] | 60 | struct _abac_credential_t { |
---|
[da5afdf] | 61 | int type; |
---|
| 62 | void *head; |
---|
| 63 | void *tail; |
---|
[5f99fbc] | 64 | certificate_t *cert; |
---|
| 65 | certificate_t *issuer; |
---|
[202a7f9] | 66 | abac_list_t *clauses; |
---|
[fbb591e] | 67 | |
---|
| 68 | int refcount; |
---|
[5f99fbc] | 69 | }; |
---|
| 70 | |
---|
[53e540d] | 71 | typedef struct _abac_str_cred_t { |
---|
| 72 | char *strid; |
---|
| 73 | abac_credential_t *cred; |
---|
| 74 | |
---|
| 75 | UT_hash_handle hh; |
---|
| 76 | } abac_str_cred_t; |
---|
| 77 | |
---|
[43e3b71] | 78 | abac_id_cert_t *id_certs = NULL; |
---|
[53e540d] | 79 | abac_str_cred_t *str_creds = NULL; |
---|
[5450aeb] | 80 | |
---|
[202a7f9] | 81 | static int debug=0; |
---|
| 82 | static void check_id_cert(abac_id_cert_t *id_cert); |
---|
[da5afdf] | 83 | extern void abac_print_role_string_with_condition(abac_role_t *role, FILE*); |
---|
[202a7f9] | 84 | |
---|
| 85 | // |
---|
[da5afdf] | 86 | void abac_print_cred_info(abac_credential_t *cred, FILE *fp) |
---|
[202a7f9] | 87 | { |
---|
[da5afdf] | 88 | if(fp == NULL) |
---|
| 89 | fp=stdout; |
---|
| 90 | |
---|
[202a7f9] | 91 | if(debug) { |
---|
[da5afdf] | 92 | fprintf(fp,"---> printing out credential info cred(%d)..\n", (int) cred); |
---|
[202a7f9] | 93 | if(cred->head) { |
---|
[da5afdf] | 94 | fprintf(fp," head cred(%d):\n",(int)cred->head); |
---|
| 95 | if(cred->type == e_CREDTYPE_ROLE) |
---|
| 96 | abac_print_role_string_with_condition((abac_role_t*)cred->head,fp); |
---|
| 97 | else |
---|
| 98 | abac_print_oset_string_with_condition((abac_oset_t*)cred->head,fp); |
---|
[202a7f9] | 99 | } |
---|
| 100 | if(cred->tail) { |
---|
[da5afdf] | 101 | fprintf(fp,"\n tail cred(%d):\n",(int)cred->tail); |
---|
| 102 | if(cred->type == e_CREDTYPE_ROLE) |
---|
| 103 | abac_print_role_string_with_condition((abac_role_t*)cred->tail,fp); |
---|
| 104 | else |
---|
| 105 | abac_print_oset_string_with_condition((abac_oset_t*)cred->tail,fp); |
---|
[202a7f9] | 106 | } |
---|
| 107 | |
---|
| 108 | abac_list_t *clauses=cred->clauses; |
---|
| 109 | if (clauses != NULL) { |
---|
| 110 | char *cur=NULL; |
---|
| 111 | abac_list_foreach(clauses, cur, |
---|
[da5afdf] | 112 | fprintf(fp,"\n a clause(%d):\n",(int)cur); |
---|
[202a7f9] | 113 | if(cur) |
---|
[da5afdf] | 114 | fprintf(fp,"strlen(%d)loc(%d)(%s)\n", strlen(cur),(int)cur, cur); |
---|
[202a7f9] | 115 | ); |
---|
| 116 | } |
---|
| 117 | } else { |
---|
[da5afdf] | 118 | fprintf(fp," "); |
---|
| 119 | if(cred->type == e_CREDTYPE_ROLE) |
---|
| 120 | abac_print_role_string_with_condition((abac_role_t *)cred->head,fp); |
---|
| 121 | else abac_print_oset_string_with_condition((abac_oset_t *)cred->head,fp); |
---|
| 122 | fprintf(fp," <- "); |
---|
| 123 | if(cred->type == e_CREDTYPE_ROLE) |
---|
| 124 | abac_print_role_string_with_condition((abac_role_t *)cred->tail,fp); |
---|
| 125 | else abac_print_oset_string_with_condition((abac_oset_t *)cred->tail,fp); |
---|
| 126 | fprintf(fp,"\n"); |
---|
[202a7f9] | 127 | } |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | // |
---|
| 131 | int abac_verify_keyid_type(char *type) { |
---|
| 132 | int i; |
---|
| 133 | |
---|
| 134 | if (type == NULL) |
---|
| 135 | return 0; |
---|
| 136 | |
---|
| 137 | for (i = 1; i <= keyidname_cnt ; i++) |
---|
| 138 | if(strcmp(type,keyidname[i])==0) |
---|
| 139 | return i; |
---|
| 140 | return 0; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | char *abac_keyid_type(int i) |
---|
| 144 | { |
---|
| 145 | return keyidname[i]; |
---|
| 146 | } |
---|
| 147 | |
---|
[5450aeb] | 148 | // convert a chunk to a lowercase binary string |
---|
| 149 | // malloc's the string |
---|
| 150 | static char *_chunk_to_string(chunk_t chunk) { |
---|
| 151 | int i; |
---|
| 152 | |
---|
[3c251d0] | 153 | char *ret = abac_xmalloc(chunk.len * 2 + 1); |
---|
[5450aeb] | 154 | |
---|
| 155 | for (i = 0; i < chunk.len; ++i) |
---|
| 156 | sprintf(ret + 2 * i, "%02x", chunk.ptr[i]); |
---|
| 157 | |
---|
| 158 | return ret; |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | // verify that cert was issued by issuer |
---|
| 162 | // cert and issuer can be the same, in which case the self-sig is validated |
---|
[0779c99] | 163 | static int _verify_signature(certificate_t *issuer, certificate_t *cert) { |
---|
[5450aeb] | 164 | if (cert->issued_by(cert, issuer)) |
---|
| 165 | if (cert->get_validity(cert, NULL, NULL, NULL)) |
---|
| 166 | return 1; |
---|
| 167 | return 0; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | /** |
---|
| 171 | * Init the verifier subsystem. |
---|
| 172 | */ |
---|
[43e3b71] | 173 | void abac_verifier_init(void) { |
---|
[1324a63] | 174 | // silence all debugging |
---|
| 175 | |
---|
[5450aeb] | 176 | if (!library_init(NULL)) |
---|
| 177 | exit(SS_RC_LIBSTRONGSWAN_INTEGRITY); |
---|
| 178 | |
---|
| 179 | if (!lib->plugins->load(lib->plugins, NULL, |
---|
| 180 | lib->settings->get_str(lib->settings, "pki.load", PLUGINS))) |
---|
| 181 | exit(SS_RC_INITIALIZATION_FAILED); |
---|
| 182 | } |
---|
| 183 | |
---|
[186cb75] | 184 | /** |
---|
| 185 | * Uninitialize the system, free up any allocated memory. |
---|
| 186 | */ |
---|
[43e3b71] | 187 | void abac_verifier_deinit(void) { |
---|
| 188 | abac_id_cert_t *id; |
---|
[186cb75] | 189 | while ((id = id_certs) != NULL) { |
---|
| 190 | HASH_DEL(id_certs, id); |
---|
| 191 | |
---|
| 192 | free(id->keyid); |
---|
[53e540d] | 193 | free(id->cn); |
---|
[186cb75] | 194 | id->cert->destroy(id->cert); |
---|
| 195 | free(id); |
---|
| 196 | } |
---|
[55c272b] | 197 | |
---|
[53e540d] | 198 | abac_str_cred_t *id2; |
---|
| 199 | while ((id2 = str_creds) != NULL) { |
---|
| 200 | HASH_DEL(str_creds, id2); |
---|
| 201 | |
---|
| 202 | free(id2->strid); |
---|
| 203 | free(id2); |
---|
| 204 | } |
---|
| 205 | |
---|
[55c272b] | 206 | library_deinit(); |
---|
[186cb75] | 207 | } |
---|
| 208 | |
---|
[5450aeb] | 209 | /** |
---|
| 210 | * Load an ID certificate. |
---|
| 211 | */ |
---|
[202a7f9] | 212 | static int _load_id(certificate_t *cert, abac_id_cert_t **id_cert_ret) { |
---|
[327e808] | 213 | abac_id_cert_t *id_cert = NULL; |
---|
[5450aeb] | 214 | char *keyid = NULL; |
---|
| 215 | chunk_t id; |
---|
| 216 | int ret; |
---|
[2ef48fa] | 217 | x509_t *x509 = (x509_t *)cert; |
---|
[5450aeb] | 218 | |
---|
[e898049] | 219 | assert(cert != NULL); |
---|
[5450aeb] | 220 | |
---|
[2ef48fa] | 221 | // get the key ID |
---|
| 222 | id = x509->get_subjectKeyIdentifier(x509); |
---|
[327e808] | 223 | keyid = _chunk_to_string(id); |
---|
| 224 | |
---|
[0d0c3a9] | 225 | /* Mike said this is the way it is */ |
---|
[53e540d] | 226 | char *str; |
---|
| 227 | int rv = asprintf(&str, "%Y", cert->get_issuer(cert)); |
---|
| 228 | |
---|
[36b100a] | 229 | /* add p to cn name here */ |
---|
[202a7f9] | 230 | char *cn=(char *)abac_xmalloc(strlen(str)+2); |
---|
[53e540d] | 231 | cn[0]='p'; |
---|
| 232 | int n=sscanf(str,"CN=%s", &(cn[1])); |
---|
| 233 | if ( n!=1 ) { |
---|
| 234 | ret = ABAC_CERT_BAD_CN; |
---|
| 235 | goto error; |
---|
| 236 | } |
---|
| 237 | |
---|
[202a7f9] | 238 | if(debug) { |
---|
| 239 | printf ("DEBUG:keyid %s \n", keyid); |
---|
| 240 | printf( "DEBUG:issuer '%s' \n", str); |
---|
| 241 | printf ("DEBUG:cn %s \n", cn); |
---|
| 242 | } |
---|
[53e540d] | 243 | free(str); |
---|
| 244 | |
---|
[327e808] | 245 | // if we already have this cert 'error' with success |
---|
| 246 | HASH_FIND_STR(id_certs, keyid, id_cert); |
---|
| 247 | if (id_cert != NULL) { |
---|
| 248 | ret = ABAC_CERT_SUCCESS; |
---|
| 249 | goto error; |
---|
| 250 | } |
---|
| 251 | |
---|
[5450aeb] | 252 | // validate sig |
---|
[0779c99] | 253 | ret = _verify_signature(cert, cert); |
---|
| 254 | if (!ret) { |
---|
| 255 | ret = ABAC_CERT_BAD_SIG; |
---|
| 256 | goto error; |
---|
| 257 | } |
---|
[5450aeb] | 258 | |
---|
| 259 | // success, add the key to the map of certificates |
---|
[327e808] | 260 | id_cert = abac_xmalloc(sizeof(abac_id_cert_t)); |
---|
[5450aeb] | 261 | id_cert->keyid = keyid; |
---|
| 262 | id_cert->cert = cert; |
---|
[53e540d] | 263 | id_cert->cn = cn; |
---|
[da5afdf] | 264 | id_cert->type = e_KEYID; |
---|
[202a7f9] | 265 | id_cert->clause = generate_pl_type_clause(cn, id_cert->type); |
---|
| 266 | |
---|
[5450aeb] | 267 | HASH_ADD_KEYPTR(hh, id_certs, id_cert->keyid, strlen(id_cert->keyid), id_cert); |
---|
| 268 | |
---|
[202a7f9] | 269 | *id_cert_ret=id_cert; |
---|
| 270 | |
---|
[0779c99] | 271 | return ABAC_CERT_SUCCESS; |
---|
[5450aeb] | 272 | |
---|
| 273 | error: |
---|
[327e808] | 274 | if (keyid != NULL) free(keyid); |
---|
[53e540d] | 275 | if (cn != NULL) free(cn); |
---|
[5450aeb] | 276 | |
---|
[0779c99] | 277 | return ret; |
---|
[5450aeb] | 278 | } |
---|
| 279 | |
---|
[202a7f9] | 280 | static void check_id_cert(abac_id_cert_t *id_cert) |
---|
| 281 | { |
---|
| 282 | if(id_cert) { |
---|
| 283 | printf("checking on this id_cert %d\n", (int)id_cert); |
---|
| 284 | printf(" --> sha is (%s)\n", id_cert->keyid); |
---|
| 285 | printf(" --> cn is (%s)\n", id_cert->cn); |
---|
| 286 | } |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | /** |
---|
| 290 | * Load an attribute cert as string. |
---|
| 291 | * have minimum syntax & validity check |
---|
| 292 | */ |
---|
| 293 | static int _load_attribute_string(char* attr_string) { |
---|
| 294 | printf("NOT implemented yet!!!"); |
---|
| 295 | return ABAC_CERT_INVALID; |
---|
| 296 | } |
---|
| 297 | |
---|
[e898049] | 298 | /** |
---|
| 299 | * Load an ID cert from a file. |
---|
| 300 | */ |
---|
[202a7f9] | 301 | int abac_verifier_load_id_file(char *filename, abac_id_cert_t **id_cert_ret) { |
---|
[61278ec] | 302 | if (lib == NULL) |
---|
| 303 | errx(1, "looks like you didn't call libabac_init() (lib is NULL)"); |
---|
| 304 | |
---|
[e898049] | 305 | // load the cert |
---|
| 306 | certificate_t *cert = lib->creds->create( |
---|
| 307 | lib->creds, CRED_CERTIFICATE, CERT_X509, |
---|
| 308 | BUILD_FROM_FILE, filename, |
---|
| 309 | BUILD_X509_FLAG, X509_AA, // attribute authority, dumb |
---|
| 310 | BUILD_END |
---|
| 311 | ); |
---|
| 312 | if (cert == NULL) |
---|
[0779c99] | 313 | return ABAC_CERT_INVALID; |
---|
[202a7f9] | 314 | return _load_id(cert,id_cert_ret); |
---|
[e898049] | 315 | } |
---|
| 316 | |
---|
| 317 | /** |
---|
| 318 | * Load an ID cert from a chunk. |
---|
| 319 | */ |
---|
[202a7f9] | 320 | int abac_verifier_load_id_chunk(chunk_t chunk, abac_id_cert_t **id_cert_ret) { |
---|
[e898049] | 321 | // load the cert |
---|
| 322 | certificate_t *cert = lib->creds->create( |
---|
| 323 | lib->creds, CRED_CERTIFICATE, CERT_X509, |
---|
| 324 | BUILD_BLOB_ASN1_DER, chunk, |
---|
| 325 | BUILD_X509_FLAG, X509_AA, // attribute authority, dumb |
---|
| 326 | BUILD_END |
---|
| 327 | ); |
---|
| 328 | if (cert == NULL) |
---|
[0779c99] | 329 | return ABAC_CERT_INVALID; |
---|
[202a7f9] | 330 | return _load_id(cert,id_cert_ret); |
---|
[e898049] | 331 | } |
---|
| 332 | |
---|
[53e540d] | 333 | |
---|
[da5afdf] | 334 | void abac_print_clauses(abac_list_t *clauses, FILE *fp) |
---|
[202a7f9] | 335 | { |
---|
| 336 | if (clauses != NULL) { |
---|
| 337 | char *cur; |
---|
| 338 | abac_list_foreach(clauses, cur, |
---|
[da5afdf] | 339 | if(cur) { |
---|
| 340 | if(fp) |
---|
| 341 | fprintf (fp,"a clause, %d(%s)\n", (int)cur,cur); |
---|
| 342 | else printf ("a clause, %d(%s)\n", (int)cur,cur); |
---|
| 343 | } |
---|
[202a7f9] | 344 | ); |
---|
| 345 | } |
---|
| 346 | } |
---|
| 347 | |
---|
[da5afdf] | 348 | static int _verify_valid_role_credential(certificate_t *cert, |
---|
| 349 | abac_credential_t **cred_ret, char *encoded_attr_string) |
---|
| 350 | { |
---|
[53e540d] | 351 | abac_role_t *head_role = NULL; |
---|
| 352 | abac_role_t *tail_role = NULL; |
---|
[202a7f9] | 353 | abac_list_t *clauses=NULL; |
---|
[53e540d] | 354 | abac_id_cert_t *issuer; |
---|
| 355 | int ret, i; |
---|
| 356 | |
---|
| 357 | // get the attr |
---|
[b5a3da4] | 358 | head_role = abac_yy_get_rule_head_role(); |
---|
| 359 | tail_role = abac_yy_get_rule_tail_role(); |
---|
| 360 | clauses = abac_yy_get_rule_clauses(); |
---|
[53e540d] | 361 | |
---|
[da5afdf] | 362 | // get the issuer based on keyid |
---|
| 363 | char *principalname = abac_role_principal(head_role); |
---|
| 364 | HASH_FIND_STR(id_certs, principalname, issuer); |
---|
| 365 | if (issuer == NULL) { |
---|
| 366 | ret = ABAC_CERT_MISSING_ISSUER; |
---|
[53e540d] | 367 | goto error; |
---|
| 368 | } |
---|
| 369 | |
---|
[da5afdf] | 370 | // make sure the issuer's signed it |
---|
| 371 | ret = _verify_signature(issuer->cert, cert); |
---|
| 372 | if (!ret) { |
---|
[b5a3da4] | 373 | abac_yy_set_error_code(ABAC_RT_CERT_BAD_SIG); |
---|
[53e540d] | 374 | goto error; |
---|
| 375 | } |
---|
[da5afdf] | 376 | |
---|
| 377 | // at this point we know we have a good attribute cert |
---|
| 378 | abac_credential_t *cred = abac_xmalloc(sizeof(abac_credential_t)); |
---|
| 379 | cred->type= e_CREDTYPE_ROLE; |
---|
| 380 | cred->head = head_role; |
---|
| 381 | cred->tail = tail_role; |
---|
| 382 | cred->cert = cert; |
---|
| 383 | cred->issuer = issuer->cert->get_ref(issuer->cert); |
---|
| 384 | cred->clauses = clauses; |
---|
| 385 | cred->refcount = 1; |
---|
| 386 | *cred_ret = cred; |
---|
| 387 | |
---|
| 388 | // success, add the key to the map of certificates |
---|
| 389 | abac_str_cred_t *str_cred; |
---|
| 390 | str_cred=abac_xmalloc(sizeof(abac_str_cred_t)); |
---|
| 391 | str_cred->strid=strdup(encoded_attr_string); |
---|
| 392 | str_cred->cred=cred; |
---|
| 393 | HASH_ADD_KEYPTR(hh, str_creds, str_cred->strid, strlen(str_cred->strid), str_cred); |
---|
| 394 | |
---|
| 395 | return ABAC_CERT_SUCCESS; |
---|
| 396 | |
---|
| 397 | error: |
---|
| 398 | if (head_role) abac_role_free(head_role); |
---|
| 399 | if (tail_role) abac_role_free(tail_role); |
---|
[b5a3da4] | 400 | abac_yy_free_rule_clauses(); |
---|
[da5afdf] | 401 | |
---|
| 402 | return ret; |
---|
| 403 | } |
---|
| 404 | |
---|
| 405 | static int _verify_valid_oset_credential(certificate_t *cert, |
---|
| 406 | abac_credential_t **cred_ret, char *encoded_attr_string) |
---|
| 407 | { |
---|
| 408 | abac_oset_t *head_oset = NULL; |
---|
| 409 | abac_oset_t *tail_oset = NULL; |
---|
| 410 | abac_list_t *clauses=NULL; |
---|
| 411 | abac_id_cert_t *issuer; |
---|
| 412 | int ret, i; |
---|
| 413 | |
---|
| 414 | // get the attr |
---|
[b5a3da4] | 415 | head_oset = abac_yy_get_rule_head_oset(); |
---|
| 416 | tail_oset = abac_yy_get_rule_tail_oset(); |
---|
| 417 | clauses = abac_yy_get_rule_clauses(); |
---|
[53e540d] | 418 | |
---|
| 419 | // get the issuer based on keyid |
---|
[da5afdf] | 420 | char *principalname = abac_oset_principal(head_oset); |
---|
[202a7f9] | 421 | HASH_FIND_STR(id_certs, principalname, issuer); |
---|
[53e540d] | 422 | if (issuer == NULL) { |
---|
| 423 | ret = ABAC_CERT_MISSING_ISSUER; |
---|
| 424 | goto error; |
---|
| 425 | } |
---|
| 426 | |
---|
| 427 | // make sure the issuer's signed it |
---|
| 428 | ret = _verify_signature(issuer->cert, cert); |
---|
| 429 | if (!ret) { |
---|
[b5a3da4] | 430 | abac_yy_set_error_code(ABAC_RT_CERT_BAD_SIG); |
---|
[53e540d] | 431 | goto error; |
---|
| 432 | } |
---|
| 433 | |
---|
| 434 | // at this point we know we have a good attribute cert |
---|
| 435 | abac_credential_t *cred = abac_xmalloc(sizeof(abac_credential_t)); |
---|
[da5afdf] | 436 | cred->type= e_CREDTYPE_OSET; |
---|
| 437 | cred->head = head_oset; |
---|
| 438 | cred->tail = tail_oset; |
---|
[53e540d] | 439 | cred->cert = cert; |
---|
| 440 | cred->issuer = issuer->cert->get_ref(issuer->cert); |
---|
[202a7f9] | 441 | cred->clauses = clauses; |
---|
[53e540d] | 442 | cred->refcount = 1; |
---|
| 443 | *cred_ret = cred; |
---|
| 444 | |
---|
| 445 | // success, add the key to the map of certificates |
---|
| 446 | abac_str_cred_t *str_cred; |
---|
| 447 | str_cred=abac_xmalloc(sizeof(abac_str_cred_t)); |
---|
[da5afdf] | 448 | str_cred->strid=strdup(encoded_attr_string); |
---|
[53e540d] | 449 | str_cred->cred=cred; |
---|
| 450 | HASH_ADD_KEYPTR(hh, str_creds, str_cred->strid, strlen(str_cred->strid), str_cred); |
---|
| 451 | |
---|
[da5afdf] | 452 | return ABAC_CERT_SUCCESS; |
---|
| 453 | |
---|
| 454 | error: |
---|
| 455 | if (head_oset) abac_oset_free(head_oset); |
---|
| 456 | if (tail_oset) abac_oset_free(tail_oset); |
---|
[b5a3da4] | 457 | abac_yy_free_rule_clauses(); |
---|
[da5afdf] | 458 | |
---|
| 459 | return ret; |
---|
| 460 | } |
---|
| 461 | /** |
---|
| 462 | * Load an attribute cert. |
---|
| 463 | * Returns true only if the certificate is valid and is issued by the proper |
---|
| 464 | * authority. |
---|
| 465 | */ |
---|
| 466 | static int _load_attribute_cert(certificate_t *cert, abac_credential_t **cred_ret) { |
---|
| 467 | ietf_attributes_t *attr_cert = NULL; |
---|
| 468 | abac_id_cert_t *issuer; |
---|
| 469 | int ret, i; |
---|
| 470 | |
---|
| 471 | // get the attr |
---|
| 472 | ac_t *ac = (ac_t *)cert; |
---|
| 473 | attr_cert = ac->get_groups(ac); |
---|
| 474 | if (attr_cert == NULL) { |
---|
| 475 | ret = ABAC_CERT_INVALID; |
---|
| 476 | goto error; |
---|
| 477 | } |
---|
| 478 | |
---|
| 479 | char *encoded_attr_string=attr_cert->get_string(attr_cert); |
---|
| 480 | char *attr_string = abac_decode_string(encoded_attr_string); |
---|
| 481 | if(debug) |
---|
| 482 | printf("string to be yyparse..(%s)\n",attr_string); |
---|
| 483 | |
---|
| 484 | if (attr_string == NULL) { |
---|
| 485 | ret = ABAC_CERT_INVALID; |
---|
| 486 | goto error; |
---|
| 487 | } |
---|
| 488 | |
---|
| 489 | /* call into yacc parser */ |
---|
| 490 | abac_reset_yyfptr(attr_string); |
---|
[b5a3da4] | 491 | abac_yy_init(); |
---|
[da5afdf] | 492 | int rc=yyparse(); |
---|
| 493 | if (rc) { |
---|
| 494 | ret = ABAC_CERT_INVALID; |
---|
| 495 | goto error; |
---|
| 496 | } |
---|
| 497 | |
---|
[b5a3da4] | 498 | if(abac_yy_get_rule_is_oset()) { |
---|
[da5afdf] | 499 | ret=_verify_valid_oset_credential(cert,cred_ret,encoded_attr_string); |
---|
| 500 | } else { |
---|
| 501 | ret=_verify_valid_role_credential(cert,cred_ret,encoded_attr_string); |
---|
| 502 | } |
---|
| 503 | |
---|
[53e540d] | 504 | // free up some crap |
---|
| 505 | attr_cert->destroy(attr_cert); |
---|
| 506 | |
---|
| 507 | return ABAC_CERT_SUCCESS; |
---|
| 508 | |
---|
| 509 | error: |
---|
[202a7f9] | 510 | if (cert) cert->destroy(cert); |
---|
| 511 | if (attr_cert) attr_cert->destroy(attr_cert); |
---|
[0779c99] | 512 | return ret; |
---|
[5450aeb] | 513 | } |
---|
[5f99fbc] | 514 | |
---|
[e898049] | 515 | /** |
---|
| 516 | * Load an attribute cert from a file. |
---|
| 517 | */ |
---|
[0779c99] | 518 | int abac_verifier_load_attribute_cert_file(char *filename, abac_credential_t **cred) { |
---|
[e898049] | 519 | // load the cert |
---|
| 520 | certificate_t *cert = lib->creds->create( |
---|
| 521 | lib->creds, CRED_CERTIFICATE, CERT_X509_AC, |
---|
| 522 | BUILD_FROM_FILE, filename, |
---|
| 523 | BUILD_END |
---|
| 524 | ); |
---|
| 525 | if (cert == NULL) |
---|
[0779c99] | 526 | return ABAC_CERT_INVALID; |
---|
| 527 | return _load_attribute_cert(cert, cred); |
---|
[e898049] | 528 | } |
---|
| 529 | |
---|
[202a7f9] | 530 | /** |
---|
| 531 | * Load attribute cert rules from a file. (this should only be used to test) |
---|
| 532 | */ |
---|
| 533 | int abac_verifier_load_certs_file(char *filename) { |
---|
| 534 | size_t nbytes=0; |
---|
| 535 | char *input=NULL; |
---|
| 536 | FILE *fp; |
---|
| 537 | int ret; |
---|
| 538 | |
---|
| 539 | fp=fopen(filename, "r"); |
---|
| 540 | |
---|
| 541 | if(fp == NULL) |
---|
| 542 | return ABAC_CERT_INVALID; |
---|
| 543 | |
---|
| 544 | while( (ret=getline(&input, &nbytes, fp)) != -1 ) { |
---|
| 545 | ret=_load_attribute_string(input); |
---|
| 546 | free(input); |
---|
| 547 | if(ret != ABAC_CERT_SUCCESS ) |
---|
| 548 | fclose(fp); |
---|
| 549 | return ret; |
---|
| 550 | } |
---|
| 551 | fclose(fp); |
---|
| 552 | return ABAC_CERT_SUCCESS; |
---|
| 553 | } |
---|
| 554 | |
---|
| 555 | |
---|
[e898049] | 556 | /** |
---|
| 557 | * Load an attribute cert from a chunk. |
---|
| 558 | */ |
---|
[0779c99] | 559 | int abac_verifier_load_attribute_cert_chunk(chunk_t chunk, abac_credential_t **cred) { |
---|
[e898049] | 560 | // load the cert |
---|
| 561 | certificate_t *cert = lib->creds->create( |
---|
| 562 | lib->creds, CRED_CERTIFICATE, CERT_X509_AC, |
---|
| 563 | BUILD_BLOB_ASN1_DER, chunk, |
---|
| 564 | BUILD_END |
---|
| 565 | ); |
---|
| 566 | if (cert == NULL) |
---|
[0779c99] | 567 | return ABAC_CERT_INVALID; |
---|
| 568 | return _load_attribute_cert(cert, cred); |
---|
[e898049] | 569 | } |
---|
| 570 | |
---|
[85cdf53] | 571 | /** |
---|
| 572 | * Return the encoding of the attribute cert. |
---|
| 573 | */ |
---|
[401a054] | 574 | abac_chunk_t abac_credential_attribute_cert(abac_credential_t *cred) { |
---|
| 575 | chunk_t encoding = cred->cert->get_encoding(cred->cert); |
---|
[9efbfbf] | 576 | abac_chunk_t ret = { encoding.ptr, encoding.len }; |
---|
| 577 | return ret; |
---|
[85cdf53] | 578 | } |
---|
| 579 | |
---|
| 580 | /** |
---|
| 581 | * Return the encoding of the issuer cert. |
---|
| 582 | */ |
---|
[401a054] | 583 | abac_chunk_t abac_credential_issuer_cert(abac_credential_t *cred) { |
---|
| 584 | chunk_t encoding = cred->issuer->get_encoding(cred->issuer); |
---|
[9efbfbf] | 585 | abac_chunk_t ret = { encoding.ptr, encoding.len }; |
---|
| 586 | return ret; |
---|
[85cdf53] | 587 | } |
---|
[186cb75] | 588 | |
---|
[53e540d] | 589 | /** |
---|
| 590 | * Return the clause of the cert |
---|
| 591 | */ |
---|
[202a7f9] | 592 | abac_list_t *abac_credential_clauses(abac_credential_t *cred) { |
---|
| 593 | return cred->clauses; |
---|
[53e540d] | 594 | } |
---|
| 595 | |
---|
[202a7f9] | 596 | |
---|
[186cb75] | 597 | /** |
---|
[fbb591e] | 598 | * Increase the ref count of a credential. |
---|
[186cb75] | 599 | */ |
---|
[401a054] | 600 | abac_credential_t *abac_credential_dup(abac_credential_t *cred) { |
---|
| 601 | assert(cred != NULL); |
---|
[186cb75] | 602 | |
---|
[fbb591e] | 603 | ++cred->refcount; |
---|
| 604 | return cred; |
---|
[186cb75] | 605 | } |
---|
| 606 | |
---|
[53e540d] | 607 | /** |
---|
| 608 | * lookup for a credential. |
---|
| 609 | */ |
---|
[202a7f9] | 610 | abac_credential_t *abac_credential_lookup(char* cred_string) |
---|
| 611 | { |
---|
| 612 | if(debug) |
---|
| 613 | printf("DEBUG:doing abac_crendential_lookup ... (%s)\n", cred_string); |
---|
[53e540d] | 614 | abac_str_cred_t *str_cred; |
---|
| 615 | HASH_FIND_STR(str_creds, cred_string, str_cred); |
---|
| 616 | if (str_cred == NULL) { |
---|
[202a7f9] | 617 | if(debug) printf("DEBUG:NOT FOUND..\n"); |
---|
[53e540d] | 618 | return NULL; |
---|
| 619 | } |
---|
[202a7f9] | 620 | if(debug) printf("DEBUG:FOUND..(%d)\n", (int) str_cred->cred); |
---|
[53e540d] | 621 | return str_cred->cred; |
---|
| 622 | } |
---|
| 623 | |
---|
[186cb75] | 624 | /** |
---|
[fbb591e] | 625 | * Decrease the reference count of a credential, freeing it when it reaches 0. |
---|
[186cb75] | 626 | */ |
---|
[202a7f9] | 627 | void abac_credential_free(abac_credential_t *cred) |
---|
| 628 | { |
---|
| 629 | if(debug) |
---|
| 630 | printf("DEBUG:freeing cred(%d)clause(%d)\n", (int)cred, (int)cred->clauses); |
---|
| 631 | |
---|
[401a054] | 632 | if (cred == NULL) |
---|
[186cb75] | 633 | return; |
---|
| 634 | |
---|
[fbb591e] | 635 | --cred->refcount; |
---|
| 636 | if (cred->refcount > 0) |
---|
| 637 | return; |
---|
| 638 | |
---|
[da5afdf] | 639 | if(cred->type == e_CREDTYPE_ROLE) { |
---|
| 640 | abac_role_free((abac_role_t *)cred->head); |
---|
| 641 | abac_role_free((abac_role_t *)cred->tail); |
---|
| 642 | } else { |
---|
| 643 | abac_oset_free((abac_oset_t *)cred->head); |
---|
| 644 | abac_oset_free((abac_oset_t *)cred->tail); |
---|
| 645 | } |
---|
[401a054] | 646 | cred->cert->destroy(cred->cert); |
---|
| 647 | cred->issuer->destroy(cred->issuer); // reference counted |
---|
[b5a3da4] | 648 | /* no need to freeyap_clauses here */ |
---|
[401a054] | 649 | free(cred); |
---|
[186cb75] | 650 | } |
---|
[53e540d] | 651 | |
---|
[202a7f9] | 652 | char *abac_id_clause(abac_id_cert_t *id_cert) |
---|
| 653 | { |
---|
| 654 | if(id_cert) |
---|
| 655 | return id_cert->clause; |
---|
| 656 | return NULL; |
---|
| 657 | } |
---|
| 658 | |
---|
[53e540d] | 659 | /* retrieve the cn that is associated with this sha_string */ |
---|
| 660 | char *abac_cn_with_sha(char *sha_string) { |
---|
| 661 | |
---|
| 662 | // get the issuer based on keyid |
---|
| 663 | abac_id_cert_t *id_cert; |
---|
| 664 | HASH_FIND_STR(id_certs, sha_string, id_cert); |
---|
| 665 | if (id_cert == NULL) { |
---|
| 666 | return NULL; |
---|
| 667 | } |
---|
[202a7f9] | 668 | if(debug) |
---|
| 669 | check_id_cert(id_cert); |
---|
[53e540d] | 670 | return id_cert->cn; |
---|
| 671 | } |
---|
| 672 | |
---|
| 673 | /* retrieve the cn that is associated with the principal of this role */ |
---|
[202a7f9] | 674 | char *abac_cn_with_role(abac_role_t *role) { |
---|
[53e540d] | 675 | |
---|
[202a7f9] | 676 | char *principal = abac_role_principal(role); |
---|
[53e540d] | 677 | return abac_cn_with_sha(principal); |
---|
| 678 | } |
---|
| 679 | |
---|
[da5afdf] | 680 | /* retrieve the cn that is associated with the principal of this oset */ |
---|
| 681 | char *abac_cn_with_oset(abac_oset_t *oset) { |
---|
| 682 | char *principal = abac_oset_principal(oset); |
---|
| 683 | return abac_cn_with_sha(principal); |
---|
| 684 | } |
---|
| 685 | |
---|