[8bd77b5] | 1 | /** |
---|
| 2 | ** abac_verifier.c |
---|
| 3 | **/ |
---|
[53e540d] | 4 | |
---|
| 5 | // include the GNU extension of asprintf |
---|
| 6 | #define _GNU_SOURCE |
---|
| 7 | |
---|
[5110d42] | 8 | #include <stdio.h> |
---|
| 9 | #include <regex.h> |
---|
| 10 | #include <sys/mman.h> |
---|
| 11 | #include <fcntl.h> |
---|
[d037f54] | 12 | #include <sys/types.h> |
---|
| 13 | #include <sys/stat.h> |
---|
| 14 | #include <unistd.h> |
---|
| 15 | |
---|
[5110d42] | 16 | |
---|
[186cb75] | 17 | #include <assert.h> |
---|
[202a7f9] | 18 | #include <stdio.h> |
---|
[186cb75] | 19 | |
---|
[5450aeb] | 20 | #include <library.h> |
---|
| 21 | |
---|
[8bd77b5] | 22 | #include "abac_internal.h" |
---|
[43e3b71] | 23 | #include "abac_verifier.h" |
---|
[5450aeb] | 24 | |
---|
[3c251d0] | 25 | #include "abac_util.h" |
---|
[202a7f9] | 26 | #include "abac_rt.h" |
---|
[5450aeb] | 27 | |
---|
[8bd77b5] | 28 | static int debug=0; |
---|
[ba6027a] | 29 | |
---|
[5d06689] | 30 | /* from abac_attribute.c */ |
---|
| 31 | extern char *get_cred_encoding(abac_attribute_t *ptr); |
---|
[8bd77b5] | 32 | |
---|
| 33 | extern abac_aspect_t *abac_yy_get_rule_head_aspect(); |
---|
| 34 | extern abac_aspect_t *abac_yy_get_rule_tail_aspect(); |
---|
[b5a3da4] | 35 | extern abac_list_t *abac_yy_get_rule_clauses(); |
---|
[da5afdf] | 36 | extern char* abac_decode_string(char *); |
---|
[b5a3da4] | 37 | extern void abac_yy_free_rule_clauses(); |
---|
[0d0c3a9] | 38 | extern char *generate_pl_type_clause(char *, int); |
---|
[5d06689] | 39 | extern abac_list_t *generate_pl_clauses(abac_aspect_t *, abac_aspect_t *); |
---|
[9b43fc3] | 40 | extern void generate_pl_set_abac_yyfptr_encoded(char *string); |
---|
[0d0c3a9] | 41 | |
---|
[8bd77b5] | 42 | extern void abac_print_aspect_string_with_condition(abac_aspect_t *role, FILE*); |
---|
[202a7f9] | 43 | |
---|
[ba6027a] | 44 | extern int abac_id_load_privkey_chunk(abac_id_t *ptr, chunk_t keychunk); |
---|
| 45 | |
---|
[8bd77b5] | 46 | struct _abac_id_credential_t { |
---|
| 47 | char *hashkeyid; |
---|
| 48 | abac_id_t *id; |
---|
| 49 | char *pl_clause; |
---|
[5450aeb] | 50 | |
---|
| 51 | UT_hash_handle hh; |
---|
[202a7f9] | 52 | }; |
---|
[8bd77b5] | 53 | /* hash table base on sha with a p */ |
---|
| 54 | abac_id_credential_t *id_creds = NULL; |
---|
[5110d42] | 55 | /* linked list of all the ids's hashkeyid */ |
---|
| 56 | abac_list_t *id_hashkeyid_list = NULL; |
---|
[5450aeb] | 57 | |
---|
[da5afdf] | 58 | /* can store either role or oset */ |
---|
[401a054] | 59 | struct _abac_credential_t { |
---|
[8bd77b5] | 60 | char *hashkeyid; |
---|
| 61 | abac_attribute_t *attr; |
---|
| 62 | abac_list_t *pl_clauses; |
---|
[53e540d] | 63 | |
---|
| 64 | UT_hash_handle hh; |
---|
[8bd77b5] | 65 | }; |
---|
| 66 | /* hash table base on encoded attr rule */ |
---|
| 67 | abac_credential_t *attr_creds = NULL; |
---|
| 68 | /* linked list of all the attr's hashkeyid */ |
---|
| 69 | abac_list_t *attr_hashkeyid_list = NULL; |
---|
[202a7f9] | 70 | |
---|
[8bd77b5] | 71 | /*****************************************************************************/ |
---|
[da5afdf] | 72 | void abac_print_cred_info(abac_credential_t *cred, FILE *fp) |
---|
[202a7f9] | 73 | { |
---|
[da5afdf] | 74 | if(fp == NULL) |
---|
| 75 | fp=stdout; |
---|
| 76 | |
---|
[8bd77b5] | 77 | abac_attribute_t *ptr=cred->attr; |
---|
| 78 | abac_aspect_t *head=abac_attribute_head(ptr); |
---|
| 79 | abac_aspect_t *tail=abac_attribute_tail(ptr); |
---|
| 80 | |
---|
[202a7f9] | 81 | if(debug) { |
---|
[da5afdf] | 82 | fprintf(fp,"---> printing out credential info cred(%d)..\n", (int) cred); |
---|
[8bd77b5] | 83 | if(head) { |
---|
| 84 | abac_print_aspect_string_with_condition(head,fp); |
---|
[202a7f9] | 85 | } |
---|
[8bd77b5] | 86 | if(tail) { |
---|
| 87 | abac_print_aspect_string_with_condition(tail,fp); |
---|
[202a7f9] | 88 | } |
---|
| 89 | |
---|
[8bd77b5] | 90 | abac_list_t *clauses=cred->pl_clauses; |
---|
[202a7f9] | 91 | if (clauses != NULL) { |
---|
| 92 | char *cur=NULL; |
---|
| 93 | abac_list_foreach(clauses, cur, |
---|
[da5afdf] | 94 | fprintf(fp,"\n a clause(%d):\n",(int)cur); |
---|
[202a7f9] | 95 | if(cur) |
---|
[da5afdf] | 96 | fprintf(fp,"strlen(%d)loc(%d)(%s)\n", strlen(cur),(int)cur, cur); |
---|
[202a7f9] | 97 | ); |
---|
| 98 | } |
---|
| 99 | } else { |
---|
[da5afdf] | 100 | fprintf(fp," "); |
---|
[8bd77b5] | 101 | abac_print_aspect_string_with_condition(head,fp); |
---|
[da5afdf] | 102 | fprintf(fp," <- "); |
---|
[8bd77b5] | 103 | abac_print_aspect_string_with_condition(tail,fp); |
---|
[da5afdf] | 104 | fprintf(fp,"\n"); |
---|
[202a7f9] | 105 | } |
---|
| 106 | } |
---|
| 107 | |
---|
[5110d42] | 108 | void abac_print_prin_info(abac_id_credential_t *prin, FILE *fp) |
---|
| 109 | { |
---|
| 110 | if(fp == NULL) |
---|
| 111 | return; |
---|
| 112 | |
---|
| 113 | abac_id_t *ptr=abac_id_credential_id(prin); |
---|
| 114 | |
---|
| 115 | if(debug) fprintf(fp,"---> printing out principal info ..\n"); |
---|
| 116 | if(abac_id_has_privkey(ptr)) |
---|
| 117 | fprintf(fp," (%s,%s,y)\n",abac_id_name(ptr),abac_id_idtype_string(ptr)); |
---|
| 118 | else |
---|
| 119 | fprintf(fp," (%s,%s,n)\n",abac_id_name(ptr),abac_id_idtype_string(ptr)); |
---|
| 120 | } |
---|
| 121 | |
---|
[202a7f9] | 122 | // |
---|
[8bd77b5] | 123 | int abac_verify_idtype_type(char *type) { |
---|
[202a7f9] | 124 | int i; |
---|
| 125 | |
---|
| 126 | if (type == NULL) |
---|
| 127 | return 0; |
---|
| 128 | |
---|
[8bd77b5] | 129 | for (i = 1; i <= _idtypename_cnt ; i++) |
---|
| 130 | if(strcmp(type,_idtypename[i])==0) |
---|
[202a7f9] | 131 | return i; |
---|
| 132 | return 0; |
---|
| 133 | } |
---|
| 134 | |
---|
[8bd77b5] | 135 | char *abac_idtype_string(int i) |
---|
[202a7f9] | 136 | { |
---|
[440ba20] | 137 | if(i > _idtypename_cnt) { |
---|
| 138 | printf("bad idtypename idx %d\n", i); |
---|
[8bd77b5] | 139 | panic("abac_idtype_string: went out of range on idtypename"); |
---|
[440ba20] | 140 | } |
---|
| 141 | return (char*) _idtypename[i]; |
---|
[202a7f9] | 142 | } |
---|
| 143 | |
---|
[5450aeb] | 144 | // convert a chunk to a lowercase binary string |
---|
| 145 | // malloc's the string |
---|
| 146 | static char *_chunk_to_string(chunk_t chunk) { |
---|
| 147 | int i; |
---|
| 148 | |
---|
[3c251d0] | 149 | char *ret = abac_xmalloc(chunk.len * 2 + 1); |
---|
[5450aeb] | 150 | |
---|
| 151 | for (i = 0; i < chunk.len; ++i) |
---|
| 152 | sprintf(ret + 2 * i, "%02x", chunk.ptr[i]); |
---|
| 153 | |
---|
| 154 | return ret; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | // verify that cert was issued by issuer |
---|
| 158 | // cert and issuer can be the same, in which case the self-sig is validated |
---|
[8bd77b5] | 159 | static int _verify_signature(certificate_t *issuer_cert, certificate_t *cert) { |
---|
| 160 | if (cert->issued_by(cert, issuer_cert)) |
---|
[5450aeb] | 161 | if (cert->get_validity(cert, NULL, NULL, NULL)) |
---|
| 162 | return 1; |
---|
| 163 | return 0; |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | /** |
---|
| 167 | * Init the verifier subsystem. |
---|
| 168 | */ |
---|
[43e3b71] | 169 | void abac_verifier_init(void) { |
---|
[1324a63] | 170 | // silence all debugging |
---|
| 171 | |
---|
[5450aeb] | 172 | if (!library_init(NULL)) |
---|
| 173 | exit(SS_RC_LIBSTRONGSWAN_INTEGRITY); |
---|
| 174 | |
---|
| 175 | if (!lib->plugins->load(lib->plugins, NULL, |
---|
| 176 | lib->settings->get_str(lib->settings, "pki.load", PLUGINS))) |
---|
| 177 | exit(SS_RC_INITIALIZATION_FAILED); |
---|
[8bd77b5] | 178 | |
---|
| 179 | attr_hashkeyid_list = abac_list_new(); |
---|
[5110d42] | 180 | id_hashkeyid_list = abac_list_new(); |
---|
[5450aeb] | 181 | } |
---|
| 182 | |
---|
[186cb75] | 183 | /** |
---|
| 184 | * Uninitialize the system, free up any allocated memory. |
---|
| 185 | */ |
---|
[43e3b71] | 186 | void abac_verifier_deinit(void) { |
---|
[137b55f] | 187 | if(debug)printf("abac_verifier_deinit... being called..\n"); |
---|
| 188 | |
---|
[8bd77b5] | 189 | abac_id_credential_t *ptr; |
---|
| 190 | while ((ptr = id_creds) != NULL) { |
---|
[5110d42] | 191 | if(debug) printf("abac_verifier_deinit: deleting id (%s)\n",abac_id_name(ptr->id)); |
---|
[8bd77b5] | 192 | HASH_DEL(id_creds, ptr); |
---|
| 193 | |
---|
[137b55f] | 194 | free(ptr->hashkeyid); |
---|
[8bd77b5] | 195 | free(ptr->pl_clause); |
---|
[137b55f] | 196 | abac_id_free(ptr->id); |
---|
[8bd77b5] | 197 | free(ptr); |
---|
[186cb75] | 198 | } |
---|
[55c272b] | 199 | |
---|
[8bd77b5] | 200 | abac_credential_t *ptr2; |
---|
| 201 | while ((ptr2 = attr_creds) != NULL) { |
---|
| 202 | HASH_DEL(attr_creds, ptr2); |
---|
| 203 | if(debug) { |
---|
[5110d42] | 204 | printf("abac_verifier_deinit: deleting attr hashkey(%s)\n",ptr2->hashkeyid); |
---|
[8bd77b5] | 205 | } |
---|
| 206 | free(ptr2->hashkeyid); |
---|
| 207 | abac_attribute_free(ptr2->attr); |
---|
| 208 | char *cur=NULL; |
---|
| 209 | abac_list_foreach(ptr2->pl_clauses, cur, |
---|
| 210 | free(cur); |
---|
| 211 | ); |
---|
| 212 | free(ptr2->pl_clauses); |
---|
| 213 | free(ptr2); |
---|
[53e540d] | 214 | } |
---|
| 215 | |
---|
[8bd77b5] | 216 | if(attr_hashkeyid_list) { |
---|
| 217 | char *cur; |
---|
| 218 | if(attr_hashkeyid_list) { |
---|
| 219 | abac_list_foreach(attr_hashkeyid_list, cur, |
---|
| 220 | if(cur) free(cur); |
---|
| 221 | ); |
---|
| 222 | } |
---|
| 223 | abac_list_free(attr_hashkeyid_list); |
---|
| 224 | } |
---|
[137b55f] | 225 | |
---|
[5110d42] | 226 | if(id_hashkeyid_list) { |
---|
[137b55f] | 227 | if(debug) printf("looking to free id_hashkeyid_lst..\n"); |
---|
[5110d42] | 228 | char *cur; |
---|
| 229 | if(id_hashkeyid_list) { |
---|
| 230 | abac_list_foreach(id_hashkeyid_list, cur, |
---|
[137b55f] | 231 | if(cur) { |
---|
[2efdff5] | 232 | if(debug) printf("hum.. id hashkey being freed.. %s\n", cur); |
---|
[137b55f] | 233 | free(cur); |
---|
| 234 | } |
---|
[5110d42] | 235 | ); |
---|
| 236 | } |
---|
| 237 | abac_list_free(id_hashkeyid_list); |
---|
| 238 | } |
---|
| 239 | |
---|
[55c272b] | 240 | library_deinit(); |
---|
[186cb75] | 241 | } |
---|
| 242 | |
---|
[8bd77b5] | 243 | abac_id_credential_t *abac_verifier_add_id_credential(abac_id_t *a_id) |
---|
| 244 | { |
---|
| 245 | abac_id_credential_t *id_cred; |
---|
| 246 | char *cn=abac_id_cn(a_id); |
---|
[d037f54] | 247 | char *keyid=abac_id_keyid(a_id); |
---|
[8bd77b5] | 248 | |
---|
[440ba20] | 249 | if(debug) { |
---|
| 250 | printf("abac_verifier_add_id_credential, cn(%s),keyid(%s)\n", |
---|
| 251 | cn, keyid); |
---|
| 252 | } |
---|
| 253 | |
---|
[8bd77b5] | 254 | // add the abac_id to the map of id credentials |
---|
| 255 | id_cred = abac_xmalloc(sizeof(abac_id_credential_t)); |
---|
| 256 | id_cred->hashkeyid = abac_xstrdup(keyid); |
---|
| 257 | id_cred->id=abac_id_dup(a_id); |
---|
| 258 | |
---|
| 259 | /* special handling here */ |
---|
| 260 | if(USE("ABAC_CN")) { |
---|
| 261 | id_cred->pl_clause = generate_pl_type_clause(cn, abac_id_idtype(a_id)); |
---|
| 262 | } else { |
---|
[d037f54] | 263 | id_cred->pl_clause=generate_pl_type_clause(prologIt(keyid), abac_id_idtype(a_id)); |
---|
[8bd77b5] | 264 | } |
---|
| 265 | HASH_ADD_KEYPTR(hh, id_creds, id_cred->hashkeyid, strlen(id_cred->hashkeyid), id_cred); |
---|
| 266 | if(debug) |
---|
[ba6027a] | 267 | printf("-->adding into id_creds, (%s)..cnt(%d)(%d)\n", |
---|
| 268 | id_cred->hashkeyid, HASH_COUNT(id_creds),(int)id_cred); |
---|
[8bd77b5] | 269 | |
---|
[5110d42] | 270 | assert(id_hashkeyid_list); |
---|
| 271 | abac_list_add(id_hashkeyid_list, abac_xstrdup(id_cred->hashkeyid)); |
---|
| 272 | return id_cred; |
---|
[8bd77b5] | 273 | } |
---|
| 274 | |
---|
[5450aeb] | 275 | /** |
---|
[440ba20] | 276 | * Load an ID |
---|
[5450aeb] | 277 | */ |
---|
[8bd77b5] | 278 | static int _load_id(abac_id_t **a_id,certificate_t *cert, abac_id_credential_t **id_cred_ret) { |
---|
| 279 | abac_id_credential_t *id_cred = NULL; |
---|
[d037f54] | 280 | char *keyid = NULL; |
---|
[5450aeb] | 281 | chunk_t id; |
---|
| 282 | int ret; |
---|
[2ef48fa] | 283 | x509_t *x509 = (x509_t *)cert; |
---|
[5450aeb] | 284 | |
---|
[e898049] | 285 | assert(cert != NULL); |
---|
[5450aeb] | 286 | |
---|
[7727f26] | 287 | // get the key ID, add p to keyid SHA here */ |
---|
[2ef48fa] | 288 | id = x509->get_subjectKeyIdentifier(x509); |
---|
[d037f54] | 289 | keyid = _chunk_to_string(id); |
---|
[327e808] | 290 | |
---|
[0d0c3a9] | 291 | /* Mike said this is the way it is */ |
---|
[53e540d] | 292 | char *str; |
---|
| 293 | int rv = asprintf(&str, "%Y", cert->get_issuer(cert)); |
---|
| 294 | |
---|
[36b100a] | 295 | /* add p to cn name here */ |
---|
[202a7f9] | 296 | char *cn=(char *)abac_xmalloc(strlen(str)+2); |
---|
[53e540d] | 297 | cn[0]='p'; |
---|
| 298 | int n=sscanf(str,"CN=%s", &(cn[1])); |
---|
| 299 | if ( n!=1 ) { |
---|
| 300 | ret = ABAC_CERT_BAD_CN; |
---|
| 301 | goto error; |
---|
| 302 | } |
---|
| 303 | |
---|
[202a7f9] | 304 | if(debug) { |
---|
| 305 | printf ("DEBUG:keyid %s \n", keyid); |
---|
| 306 | printf( "DEBUG:issuer '%s' \n", str); |
---|
| 307 | printf ("DEBUG:cn %s \n", cn); |
---|
| 308 | } |
---|
[53e540d] | 309 | free(str); |
---|
| 310 | |
---|
[327e808] | 311 | // if we already have this cert 'error' with success |
---|
[8bd77b5] | 312 | HASH_FIND_STR(id_creds, keyid, id_cred); |
---|
| 313 | if (id_cred != NULL) { |
---|
[440ba20] | 314 | if(debug) printf("existing cert \n"); |
---|
| 315 | ret = ABAC_CERT_EXISTS; |
---|
[327e808] | 316 | goto error; |
---|
| 317 | } |
---|
| 318 | |
---|
[5450aeb] | 319 | // validate sig |
---|
[0779c99] | 320 | ret = _verify_signature(cert, cert); |
---|
| 321 | if (!ret) { |
---|
| 322 | ret = ABAC_CERT_BAD_SIG; |
---|
| 323 | goto error; |
---|
| 324 | } |
---|
[5450aeb] | 325 | |
---|
[8bd77b5] | 326 | // success, add a new abac_id |
---|
[440ba20] | 327 | if(*a_id==NULL) { |
---|
| 328 | *a_id=abac_id_keyid_new(keyid,cn,cert); |
---|
| 329 | } |
---|
[5450aeb] | 330 | |
---|
[8bd77b5] | 331 | abac_id_credential_t *n_id_cred=abac_verifier_add_id_credential(*a_id); |
---|
| 332 | *id_cred_ret=n_id_cred; |
---|
[202a7f9] | 333 | |
---|
[0779c99] | 334 | return ABAC_CERT_SUCCESS; |
---|
[5450aeb] | 335 | |
---|
| 336 | error: |
---|
[327e808] | 337 | if (keyid != NULL) free(keyid); |
---|
[53e540d] | 338 | if (cn != NULL) free(cn); |
---|
[5450aeb] | 339 | |
---|
[0779c99] | 340 | return ret; |
---|
[5450aeb] | 341 | } |
---|
| 342 | |
---|
[8bd77b5] | 343 | /* collect all the creds stored so far */ |
---|
| 344 | abac_stack_t *abac_verifier_dump_creds() |
---|
[202a7f9] | 345 | { |
---|
[8bd77b5] | 346 | abac_stack_t *cred_list = abac_stack_new(); |
---|
| 347 | |
---|
| 348 | int cnt=0; |
---|
| 349 | if(attr_hashkeyid_list) { |
---|
| 350 | char *keyid; |
---|
| 351 | abac_credential_t *cred; |
---|
| 352 | abac_list_foreach(attr_hashkeyid_list, keyid, |
---|
| 353 | cred=abac_credential_lookup(keyid); |
---|
| 354 | abac_stack_push(cred_list, cred); |
---|
| 355 | cnt++; |
---|
| 356 | ); |
---|
| 357 | } |
---|
[5110d42] | 358 | if(debug) printf("abac_verifier_dump_cred: %d\n",cnt); |
---|
[8bd77b5] | 359 | return cred_list; |
---|
| 360 | } |
---|
| 361 | |
---|
[5110d42] | 362 | /* collect all the id stored so far */ |
---|
| 363 | abac_stack_t *abac_verifier_dump_principals() |
---|
| 364 | { |
---|
| 365 | abac_stack_t *id_list = abac_stack_new(); |
---|
| 366 | |
---|
| 367 | int cnt=0; |
---|
| 368 | if(id_hashkeyid_list) { |
---|
| 369 | char *keyid; |
---|
| 370 | abac_id_credential_t *id; |
---|
| 371 | abac_list_foreach(id_hashkeyid_list, keyid, |
---|
| 372 | id=abac_id_credential_lookup(keyid); |
---|
| 373 | abac_stack_push(id_list, id); |
---|
| 374 | cnt++; |
---|
| 375 | ); |
---|
| 376 | } |
---|
| 377 | if(debug) printf("abac_verifier_dump_principals: %d\n",cnt); |
---|
| 378 | return id_list; |
---|
| 379 | } |
---|
| 380 | |
---|
[8bd77b5] | 381 | |
---|
| 382 | static void check_id_cred(abac_id_credential_t *id_cred) |
---|
| 383 | { |
---|
| 384 | if(id_cred) { |
---|
| 385 | printf("checking on this id_cred location %d\n", (int)id_cred); |
---|
| 386 | printf(" --> sha is (%s)\n", abac_id_keyid(id_cred->id)); |
---|
| 387 | printf(" --> cn is (%s)\n", abac_id_cn(id_cred->id)); |
---|
[202a7f9] | 388 | } |
---|
| 389 | } |
---|
| 390 | |
---|
| 391 | /** |
---|
| 392 | * Load an attribute cert as string. |
---|
| 393 | * have minimum syntax & validity check |
---|
| 394 | */ |
---|
| 395 | static int _load_attribute_string(char* attr_string) { |
---|
| 396 | printf("NOT implemented yet!!!"); |
---|
| 397 | return ABAC_CERT_INVALID; |
---|
| 398 | } |
---|
| 399 | |
---|
[e898049] | 400 | /** |
---|
| 401 | * Load an ID cert from a file. |
---|
| 402 | */ |
---|
[ba6027a] | 403 | int abac_verifier_load_id_file_key_file(char *filename, char *keyfilename, |
---|
| 404 | abac_id_credential_t **id_cred_ret) |
---|
| 405 | { |
---|
[8bd77b5] | 406 | abac_id_t *id=NULL; |
---|
[61278ec] | 407 | if (lib == NULL) |
---|
| 408 | errx(1, "looks like you didn't call libabac_init() (lib is NULL)"); |
---|
| 409 | |
---|
[d037f54] | 410 | if(debug) printf("loading id file... %s\n", filename); |
---|
| 411 | |
---|
[137b55f] | 412 | // load the cert, with public key |
---|
[e898049] | 413 | certificate_t *cert = lib->creds->create( |
---|
| 414 | lib->creds, CRED_CERTIFICATE, CERT_X509, |
---|
| 415 | BUILD_FROM_FILE, filename, |
---|
| 416 | BUILD_X509_FLAG, X509_AA, // attribute authority, dumb |
---|
| 417 | BUILD_END |
---|
| 418 | ); |
---|
[d037f54] | 419 | |
---|
[e898049] | 420 | if (cert == NULL) |
---|
[0779c99] | 421 | return ABAC_CERT_INVALID; |
---|
[d037f54] | 422 | |
---|
| 423 | int rc=_load_id(&id,cert,id_cred_ret); |
---|
[440ba20] | 424 | if(rc==ABAC_CERT_EXISTS) { |
---|
| 425 | if(debug) printf("abac_verifier_load_id_files: id already exists\n"); |
---|
| 426 | return ABAC_CERT_SUCCESS; |
---|
| 427 | } |
---|
| 428 | |
---|
[d037f54] | 429 | /* try to load the private key if it is there */ |
---|
[2efdff5] | 430 | if((rc==ABAC_CERT_SUCCESS) && keyfilename!=NULL && file_exist(keyfilename)) { |
---|
[d037f54] | 431 | if(debug) printf("loading... %s\n", keyfilename); |
---|
[5110d42] | 432 | int keyrc=abac_id_load_privkey_file(id, keyfilename); |
---|
[d037f54] | 433 | if(debug) { |
---|
| 434 | if(keyrc == 1) printf("..load_id_file: load(%s) with a private key\n",filename); |
---|
| 435 | else printf("..load_id_file: load(%s) without a private key\n",filename); |
---|
| 436 | } |
---|
| 437 | } |
---|
[2efdff5] | 438 | return ABAC_CERT_SUCCESS; |
---|
[8bd77b5] | 439 | } |
---|
| 440 | |
---|
[ba6027a] | 441 | int abac_verifier_load_enc_id_file_key_file(char *filename, char *keyfilename, |
---|
| 442 | char *pfile, abac_id_credential_t **id_cred_ret) |
---|
| 443 | { |
---|
| 444 | abac_id_t *id=NULL; |
---|
| 445 | if (lib == NULL) |
---|
| 446 | errx(1, "looks like you didn't call libabac_init() (lib is NULL)"); |
---|
| 447 | |
---|
| 448 | if(debug) printf("loading id file... %s\n", filename); |
---|
| 449 | |
---|
| 450 | // load the cert, with public key |
---|
| 451 | certificate_t *cert = lib->creds->create( |
---|
| 452 | lib->creds, CRED_CERTIFICATE, CERT_X509, |
---|
| 453 | BUILD_FROM_FILE, filename, |
---|
| 454 | BUILD_X509_FLAG, X509_AA, // attribute authority, dumb |
---|
| 455 | BUILD_END |
---|
| 456 | ); |
---|
| 457 | |
---|
| 458 | if (cert == NULL) |
---|
| 459 | return ABAC_CERT_INVALID; |
---|
| 460 | |
---|
| 461 | int rc=_load_id(&id,cert,id_cred_ret); |
---|
| 462 | if(rc==ABAC_CERT_EXISTS) { |
---|
| 463 | if(debug) printf("abac_verifier_load_id_files: id already exists\n"); |
---|
| 464 | return ABAC_CERT_SUCCESS; |
---|
| 465 | } |
---|
| 466 | |
---|
| 467 | /* try to load the private key if it is there */ |
---|
| 468 | if((rc==ABAC_CERT_SUCCESS) && keyfilename!=NULL && file_exist(keyfilename)) { |
---|
| 469 | if(debug) printf("loading... %s\n", keyfilename); |
---|
| 470 | int keyrc=abac_id_load_enc_privkey_file(id, keyfilename, pfile); |
---|
| 471 | if(debug) { |
---|
| 472 | if(keyrc == 1) printf("..load_id_file: load(%s) with a private key\n",filename); |
---|
| 473 | else printf("..load_id_file: load(%s) without a private key\n",filename); |
---|
| 474 | } |
---|
| 475 | } |
---|
| 476 | return ABAC_CERT_SUCCESS; |
---|
| 477 | } |
---|
| 478 | |
---|
[8bd77b5] | 479 | /** |
---|
| 480 | * Load an ID cert from a abac_id_t |
---|
| 481 | */ |
---|
| 482 | int abac_verifier_load_id_id(abac_id_t *id, abac_id_credential_t **id_cred_ret) { |
---|
| 483 | certificate_t *cert = abac_id_cert(id); |
---|
| 484 | return _load_id(&id,cert,id_cred_ret); |
---|
[e898049] | 485 | } |
---|
| 486 | |
---|
[5110d42] | 487 | /** |
---|
| 488 | * Load an ID cert from a chunk. |
---|
| 489 | */ |
---|
| 490 | int abac_verifier_load_id_pem_chunk(chunk_t chunk, abac_id_credential_t **id_cred_ret) { |
---|
| 491 | abac_id_t *id=NULL; |
---|
| 492 | // load the cert |
---|
| 493 | certificate_t *cert = lib->creds->create( |
---|
| 494 | lib->creds, CRED_CERTIFICATE, CERT_X509, |
---|
| 495 | BUILD_BLOB_PEM, chunk, |
---|
| 496 | BUILD_X509_FLAG, X509_AA, // attribute authority, dumb |
---|
| 497 | BUILD_END |
---|
| 498 | ); |
---|
| 499 | if (cert == NULL) |
---|
| 500 | return ABAC_CERT_INVALID; |
---|
| 501 | return _load_id(&id, cert,id_cred_ret); |
---|
| 502 | } |
---|
[e898049] | 503 | /** |
---|
[9b43fc3] | 504 | * Load an ID cert from a chunk, Note, this only loads the id cert part |
---|
| 505 | * and so it is not suitable for creating an attribute credential with. |
---|
| 506 | * Only a complete id cert + privkey could create attribute credential. |
---|
[e898049] | 507 | */ |
---|
[8bd77b5] | 508 | int abac_verifier_load_id_chunk(chunk_t chunk, abac_id_credential_t **id_cred_ret) { |
---|
| 509 | abac_id_t *id=NULL; |
---|
[e898049] | 510 | // load the cert |
---|
| 511 | certificate_t *cert = lib->creds->create( |
---|
| 512 | lib->creds, CRED_CERTIFICATE, CERT_X509, |
---|
| 513 | BUILD_BLOB_ASN1_DER, chunk, |
---|
| 514 | BUILD_X509_FLAG, X509_AA, // attribute authority, dumb |
---|
| 515 | BUILD_END |
---|
| 516 | ); |
---|
| 517 | if (cert == NULL) |
---|
[0779c99] | 518 | return ABAC_CERT_INVALID; |
---|
[8bd77b5] | 519 | return _load_id(&id, cert,id_cred_ret); |
---|
[e898049] | 520 | } |
---|
| 521 | |
---|
[9b43fc3] | 522 | /** |
---|
| 523 | * Load an ID cert from a cert chunk and a privkey chunk, |
---|
| 524 | * this complete id cert + privkey could create attribute credential. |
---|
| 525 | */ |
---|
| 526 | int abac_verifier_load_id_privkey_chunk(chunk_t chunk, chunk_t privkey_chunk, |
---|
| 527 | abac_id_credential_t **id_cred_ret) { |
---|
| 528 | |
---|
| 529 | abac_id_t *id=NULL; |
---|
| 530 | // load the cert |
---|
| 531 | certificate_t *cert = lib->creds->create( |
---|
| 532 | lib->creds, CRED_CERTIFICATE, CERT_X509, |
---|
| 533 | BUILD_BLOB_ASN1_DER, chunk, |
---|
| 534 | BUILD_X509_FLAG, X509_AA, // attribute authority, dumb |
---|
| 535 | BUILD_END |
---|
| 536 | ); |
---|
| 537 | if (cert == NULL) |
---|
| 538 | return ABAC_CERT_INVALID; |
---|
| 539 | |
---|
| 540 | int rc=_load_id(&id, cert,id_cred_ret); |
---|
| 541 | if(rc == ABAC_CERT_SUCCESS) { |
---|
| 542 | rc=abac_id_load_privkey_chunk(id, privkey_chunk); |
---|
| 543 | } |
---|
| 544 | |
---|
| 545 | return rc; |
---|
| 546 | } |
---|
| 547 | |
---|
[ba6027a] | 548 | int abac_verifier_load_id_enc_privkey_chunk(chunk_t chunk, chunk_t privkey_chunk, |
---|
| 549 | char* pfile, abac_id_credential_t **id_cred_ret) { |
---|
| 550 | |
---|
| 551 | abac_id_t *id=NULL; |
---|
| 552 | // load the cert |
---|
| 553 | certificate_t *cert = lib->creds->create( |
---|
| 554 | lib->creds, CRED_CERTIFICATE, CERT_X509, |
---|
| 555 | BUILD_BLOB_ASN1_DER, chunk, |
---|
| 556 | BUILD_X509_FLAG, X509_AA, // attribute authority, dumb |
---|
| 557 | BUILD_END |
---|
| 558 | ); |
---|
| 559 | if (cert == NULL) |
---|
| 560 | return ABAC_CERT_INVALID; |
---|
| 561 | |
---|
| 562 | int rc=_load_id(&id, cert,id_cred_ret); |
---|
| 563 | if(rc == ABAC_CERT_SUCCESS) { |
---|
| 564 | rc=abac_id_load_enc_privkey_chunk(id, privkey_chunk, pfile); |
---|
| 565 | } |
---|
| 566 | |
---|
| 567 | return rc; |
---|
| 568 | } |
---|
| 569 | |
---|
| 570 | |
---|
[9b43fc3] | 571 | |
---|
[2efdff5] | 572 | abac_id_t *abac_verifier_load_issuer(char *filename, char *keyfilename) |
---|
| 573 | { |
---|
| 574 | abac_id_credential_t *id_cred=NULL; |
---|
| 575 | int ret=abac_verifier_load_id_file_key_file(filename,keyfilename,&id_cred); |
---|
| 576 | if(ret !=ABAC_CERT_SUCCESS) |
---|
| 577 | return NULL; |
---|
| 578 | abac_id_t *issuer=abac_id_credential_id(id_cred); |
---|
| 579 | return issuer; |
---|
| 580 | } |
---|
| 581 | |
---|
[53e540d] | 582 | |
---|
[5110d42] | 583 | static int matchex(const char *subject, const char *pattern, int *so, int *eo) |
---|
| 584 | { |
---|
| 585 | int rc; // Returned code |
---|
| 586 | regmatch_t pmatch; |
---|
| 587 | |
---|
| 588 | regex_t re; // Compiled regexp pattern |
---|
| 589 | |
---|
| 590 | if (regcomp(&re, pattern, 0) != 0) return 0; |
---|
| 591 | |
---|
| 592 | rc = regexec(&re, subject, 1 , &pmatch, 0); |
---|
| 593 | regfree(&re); |
---|
| 594 | |
---|
| 595 | if (rc == REG_NOMATCH ) return 0; |
---|
| 596 | |
---|
| 597 | *so=pmatch.rm_so; |
---|
| 598 | *eo=pmatch.rm_eo; |
---|
| 599 | |
---|
| 600 | return 1; |
---|
| 601 | } |
---|
| 602 | |
---|
| 603 | |
---|
| 604 | |
---|
| 605 | static chunk_t _extract_chunk(char *data, char* startline, char* endline) |
---|
| 606 | { |
---|
| 607 | int found=0; |
---|
| 608 | int startos=0; |
---|
| 609 | int endos=0; |
---|
| 610 | regex_t re; |
---|
| 611 | regmatch_t pm; |
---|
| 612 | int rc; |
---|
| 613 | |
---|
| 614 | int st; |
---|
| 615 | int et; |
---|
| 616 | |
---|
| 617 | rc=matchex(data, startline, &st, &et); |
---|
| 618 | if(rc==0) return chunk_empty; |
---|
| 619 | startos=st; |
---|
| 620 | |
---|
| 621 | rc=matchex(data, endline, &st, &et); |
---|
| 622 | if(rc==0) return chunk_empty; |
---|
| 623 | endos=et; |
---|
| 624 | |
---|
| 625 | int len=endos - startos; |
---|
| 626 | |
---|
| 627 | if(debug) printf("making a chunk of size (%d) from %d and %d\n",len, startos,endos); |
---|
| 628 | char *ptr=strndup(data+startos,len); |
---|
| 629 | |
---|
| 630 | chunk_t ret = { ptr, len }; |
---|
| 631 | return ret; |
---|
| 632 | } |
---|
| 633 | |
---|
| 634 | |
---|
| 635 | int abac_verifier_load_idkey_file(char *filename, abac_id_credential_t **id_cred_ret) |
---|
| 636 | { |
---|
| 637 | struct stat sb; |
---|
| 638 | int fd; |
---|
| 639 | int rc; |
---|
| 640 | |
---|
| 641 | fd = open(filename, O_RDONLY); |
---|
| 642 | if (fd == -1) { return 1; } |
---|
| 643 | if(stat(filename, &sb) == -1) { |
---|
| 644 | close(fd); |
---|
| 645 | return 1; |
---|
| 646 | } |
---|
| 647 | char* data = (char *)mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
---|
| 648 | if(data == MAP_FAILED) { |
---|
| 649 | close(fd); |
---|
| 650 | return 1; |
---|
| 651 | } |
---|
| 652 | close(fd); |
---|
| 653 | |
---|
| 654 | chunk_t key_pem_chunk=_extract_chunk(data, |
---|
| 655 | "-----BEGIN RSA PRIVATE KEY-----","-----END RSA PRIVATE KEY-----"); |
---|
| 656 | chunk_t cert_pem_chunk=_extract_chunk(data, |
---|
| 657 | "-----BEGIN CERTIFICATE-----","-----END CERTIFICATE-----"); |
---|
| 658 | |
---|
| 659 | /* key=0, cert=0 */ |
---|
| 660 | if(key_pem_chunk.len==0 && cert_pem_chunk.len==0) { |
---|
| 661 | if(debug) printf("abac_verifier_load_idkey_file: no key or id in idkey_file\n"); |
---|
| 662 | goto error; |
---|
| 663 | } |
---|
| 664 | |
---|
| 665 | /* key=0, cert=1 */ |
---|
| 666 | if(key_pem_chunk.len==0) { |
---|
| 667 | if(debug) printf("abac_verifier_load_idkey_file: no key in idkey_file\n"); |
---|
| 668 | munmap(data, sb.st_size); |
---|
| 669 | chunk_free(&key_pem_chunk); |
---|
| 670 | return abac_verifier_load_id_file_key_file(filename,NULL,id_cred_ret); |
---|
| 671 | } |
---|
| 672 | |
---|
| 673 | /* key=1, cert=1 */ |
---|
| 674 | if(cert_pem_chunk.len!=0) { |
---|
| 675 | if(debug) { |
---|
| 676 | printf("loading cert pem chunk %d\n", cert_pem_chunk.len); |
---|
| 677 | printf("(%s)\n",cert_pem_chunk.ptr); |
---|
| 678 | } |
---|
| 679 | rc=abac_verifier_load_id_pem_chunk(cert_pem_chunk, id_cred_ret); |
---|
| 680 | if(rc != 0) { |
---|
| 681 | if(debug) printf("abac_verifier_load_idkey_file: fail to load the id\n"); |
---|
| 682 | goto error; |
---|
| 683 | } |
---|
| 684 | /* key=1, cert=0 */ |
---|
| 685 | } else { |
---|
| 686 | if(debug) printf("abac_verifier_load_idkey_file: there is no id info in there\n"); |
---|
| 687 | goto error; |
---|
| 688 | } |
---|
| 689 | |
---|
| 690 | /* handle the key_pem_chunk */ |
---|
| 691 | if(debug) { |
---|
| 692 | printf("loading key pem chunk %d\n", key_pem_chunk.len); |
---|
| 693 | printf("(%s)\n",key_pem_chunk.ptr); |
---|
| 694 | } |
---|
| 695 | abac_id_t *id=abac_id_credential_id(*id_cred_ret); |
---|
| 696 | rc=abac_id_load_privkey_chunk(id, key_pem_chunk); |
---|
| 697 | if(rc==0) { |
---|
| 698 | printf("abac_verifier_load_idkey_file:failed to load priv key!!!\n"); |
---|
| 699 | goto error; |
---|
| 700 | } |
---|
| 701 | |
---|
| 702 | munmap(data, sb.st_size); |
---|
| 703 | return 0; |
---|
| 704 | |
---|
| 705 | error: |
---|
| 706 | munmap(data, sb.st_size); |
---|
| 707 | return 1; |
---|
| 708 | } |
---|
| 709 | |
---|
| 710 | |
---|
[ba6027a] | 711 | int abac_verifier_load_enc_idkey_file(char *filename, char *pfile, |
---|
| 712 | abac_id_credential_t **id_cred_ret) |
---|
| 713 | { |
---|
| 714 | struct stat sb; |
---|
| 715 | int fd; |
---|
| 716 | int rc; |
---|
| 717 | |
---|
| 718 | fd = open(filename, O_RDONLY); |
---|
| 719 | if (fd == -1) { return 1; } |
---|
| 720 | if(stat(filename, &sb) == -1) { |
---|
| 721 | close(fd); |
---|
| 722 | return 1; |
---|
| 723 | } |
---|
| 724 | char* data = (char *)mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
---|
| 725 | if(data == MAP_FAILED) { |
---|
| 726 | close(fd); |
---|
| 727 | return 1; |
---|
| 728 | } |
---|
| 729 | close(fd); |
---|
| 730 | |
---|
| 731 | chunk_t key_pem_chunk=_extract_chunk(data, |
---|
| 732 | "-----BEGIN RSA PRIVATE KEY-----","-----END RSA PRIVATE KEY-----"); |
---|
| 733 | chunk_t cert_pem_chunk=_extract_chunk(data, |
---|
| 734 | "-----BEGIN CERTIFICATE-----","-----END CERTIFICATE-----"); |
---|
| 735 | |
---|
| 736 | /* key=0, cert=0 */ |
---|
| 737 | if(key_pem_chunk.len==0 && cert_pem_chunk.len==0) { |
---|
| 738 | if(debug) printf("abac_verifier_load_idkey_file: no key or id in idkey_file\n"); |
---|
| 739 | goto error; |
---|
| 740 | } |
---|
| 741 | |
---|
| 742 | /* key=0, cert=1 */ |
---|
| 743 | if(key_pem_chunk.len==0) { |
---|
| 744 | if(debug) printf("abac_verifier_load_idkey_file: no key in idkey_file\n"); |
---|
| 745 | munmap(data, sb.st_size); |
---|
| 746 | chunk_free(&key_pem_chunk); |
---|
| 747 | return abac_verifier_load_id_file_key_file(filename,NULL,id_cred_ret); |
---|
| 748 | } |
---|
| 749 | |
---|
| 750 | /* key=1, cert=1 */ |
---|
| 751 | if(cert_pem_chunk.len!=0) { |
---|
| 752 | if(debug) { |
---|
| 753 | printf("loading cert pem chunk %d\n", cert_pem_chunk.len); |
---|
| 754 | printf("(%s)\n",cert_pem_chunk.ptr); |
---|
| 755 | } |
---|
| 756 | rc=abac_verifier_load_id_pem_chunk(cert_pem_chunk, id_cred_ret); |
---|
| 757 | if(rc != 0) { |
---|
| 758 | if(debug) printf("abac_verifier_load_idkey_file: fail to load the id\n"); |
---|
| 759 | goto error; |
---|
| 760 | } |
---|
| 761 | /* key=1, cert=0 */ |
---|
| 762 | } else { |
---|
| 763 | if(debug) printf("abac_verifier_load_idkey_file: there is no id info in there\n"); |
---|
| 764 | goto error; |
---|
| 765 | } |
---|
| 766 | |
---|
| 767 | /* handle the key_pem_chunk */ |
---|
| 768 | if(debug) { |
---|
| 769 | printf("loading key pem chunk %d\n", key_pem_chunk.len); |
---|
| 770 | printf("(%s)\n",key_pem_chunk.ptr); |
---|
| 771 | printf("(%d)\n",strlen(key_pem_chunk.ptr)); |
---|
| 772 | } |
---|
| 773 | abac_id_t *id=abac_id_credential_id(*id_cred_ret); |
---|
| 774 | rc=abac_id_load_enc_privkey_chunk(id, key_pem_chunk, pfile); |
---|
| 775 | if(rc==0) { |
---|
| 776 | printf("abac_verifier_load_idkey_file:failed to load priv key!!!\n"); |
---|
| 777 | goto error; |
---|
| 778 | } |
---|
| 779 | |
---|
| 780 | munmap(data, sb.st_size); |
---|
| 781 | return 0; |
---|
| 782 | |
---|
| 783 | error: |
---|
| 784 | munmap(data, sb.st_size); |
---|
| 785 | return 1; |
---|
| 786 | } |
---|
[5110d42] | 787 | |
---|
[da5afdf] | 788 | void abac_print_clauses(abac_list_t *clauses, FILE *fp) |
---|
[202a7f9] | 789 | { |
---|
| 790 | if (clauses != NULL) { |
---|
| 791 | char *cur; |
---|
[7b548fa] | 792 | printf("total-- %d clauses\n", abac_list_size(clauses)); |
---|
[202a7f9] | 793 | abac_list_foreach(clauses, cur, |
---|
[da5afdf] | 794 | if(cur) { |
---|
| 795 | if(fp) |
---|
| 796 | fprintf (fp,"a clause, %d(%s)\n", (int)cur,cur); |
---|
| 797 | else printf ("a clause, %d(%s)\n", (int)cur,cur); |
---|
| 798 | } |
---|
[202a7f9] | 799 | ); |
---|
| 800 | } |
---|
| 801 | } |
---|
| 802 | |
---|
[8bd77b5] | 803 | abac_id_credential_t *abac_id_credential_dup(abac_id_credential_t *ptr) { |
---|
| 804 | assert(ptr != NULL); |
---|
[ba6027a] | 805 | if(debug) |
---|
| 806 | printf("calling abac_id_credential_dup, increment id ref count\n"); |
---|
[8bd77b5] | 807 | abac_id_dup(ptr->id); |
---|
| 808 | return ptr; |
---|
| 809 | } |
---|
| 810 | |
---|
| 811 | abac_id_t *abac_id_credential_id(abac_id_credential_t *ptr) |
---|
[da5afdf] | 812 | { |
---|
[8bd77b5] | 813 | assert(ptr); |
---|
| 814 | return ptr->id; |
---|
| 815 | } |
---|
[53e540d] | 816 | |
---|
| 817 | |
---|
[8bd77b5] | 818 | abac_id_credential_t *abac_id_credential_lookup(char *pname) |
---|
| 819 | { |
---|
| 820 | abac_id_credential_t *id_cred=NULL; |
---|
| 821 | HASH_FIND_STR(id_creds, pname, id_cred); |
---|
[5110d42] | 822 | if(id_cred != NULL) |
---|
| 823 | return abac_id_credential_dup(id_cred); |
---|
| 824 | else return NULL; |
---|
[8bd77b5] | 825 | } |
---|
[7727f26] | 826 | |
---|
[8bd77b5] | 827 | void abac_id_credential_free(abac_id_credential_t *ptr) |
---|
| 828 | { |
---|
| 829 | if (ptr == NULL) |
---|
| 830 | return; |
---|
[53e540d] | 831 | |
---|
[5110d42] | 832 | if(debug) |
---|
| 833 | printf("abac_id_credential_free:freeing (%s)\n", abac_id_name(ptr->id)); |
---|
| 834 | |
---|
[8bd77b5] | 835 | // this is very hacky... |
---|
| 836 | int last=abac_id_lastone(ptr->id); |
---|
| 837 | if(!last) { |
---|
[5110d42] | 838 | if(debug) printf("abac_id_credential_free:not last one\n"); |
---|
[8bd77b5] | 839 | abac_id_free(ptr->id); |
---|
| 840 | } else { |
---|
[5110d42] | 841 | if(debug) printf("abac_id_credential_free: deleting id (%s)\n",abac_id_name(ptr->id)); |
---|
[8bd77b5] | 842 | free(ptr->hashkeyid); |
---|
| 843 | if(ptr->pl_clause) |
---|
| 844 | free(ptr->pl_clause); |
---|
| 845 | abac_id_free(ptr->id); |
---|
| 846 | HASH_DEL(id_creds, ptr); |
---|
| 847 | free(ptr); |
---|
[53e540d] | 848 | } |
---|
[da5afdf] | 849 | } |
---|
| 850 | |
---|
[8bd77b5] | 851 | /****************************************************************************/ |
---|
| 852 | |
---|
[5d06689] | 853 | static int _verify_valid_credential_string(certificate_t *cert, |
---|
[da5afdf] | 854 | abac_credential_t **cred_ret, char *encoded_attr_string) |
---|
| 855 | { |
---|
[8bd77b5] | 856 | abac_aspect_t *head_aspect = NULL; |
---|
| 857 | abac_aspect_t *tail_aspect = NULL; |
---|
[da5afdf] | 858 | abac_list_t *clauses=NULL; |
---|
[8bd77b5] | 859 | abac_id_credential_t *id_cred; |
---|
| 860 | abac_id_t *issuer_id; |
---|
[da5afdf] | 861 | int ret, i; |
---|
| 862 | |
---|
| 863 | // get the attr |
---|
[8bd77b5] | 864 | head_aspect = abac_yy_get_rule_head_aspect(); |
---|
| 865 | tail_aspect = abac_yy_get_rule_tail_aspect(); |
---|
[b5a3da4] | 866 | clauses = abac_yy_get_rule_clauses(); |
---|
[53e540d] | 867 | |
---|
| 868 | // get the issuer based on keyid |
---|
[8bd77b5] | 869 | char *principalname = abac_aspect_principal_principalname(head_aspect); |
---|
| 870 | if(debug) printf("LOOKING for %s\n", principalname); |
---|
| 871 | |
---|
| 872 | HASH_FIND_STR(id_creds, principalname, id_cred); |
---|
[d037f54] | 873 | if(id_cred == NULL) { |
---|
| 874 | ret = ABAC_CERT_MISSING_ISSUER; |
---|
| 875 | if(debug) |
---|
| 876 | printf("can not find %s in id_creds\n", principalname); |
---|
| 877 | goto error; |
---|
| 878 | } |
---|
[8bd77b5] | 879 | issuer_id=id_cred->id; |
---|
| 880 | if (issuer_id == NULL) { |
---|
[53e540d] | 881 | ret = ABAC_CERT_MISSING_ISSUER; |
---|
[8bd77b5] | 882 | if(debug) |
---|
| 883 | printf("can not find %s in id_creds\n", principalname); |
---|
[53e540d] | 884 | goto error; |
---|
| 885 | } |
---|
| 886 | |
---|
[8bd77b5] | 887 | |
---|
[53e540d] | 888 | // make sure the issuer's signed it |
---|
[8bd77b5] | 889 | ret = _verify_signature(abac_id_cert(issuer_id), cert); |
---|
[53e540d] | 890 | if (!ret) { |
---|
[b5a3da4] | 891 | abac_yy_set_error_code(ABAC_RT_CERT_BAD_SIG); |
---|
[5110d42] | 892 | ret=ABAC_CERT_BAD_SIG; |
---|
[53e540d] | 893 | goto error; |
---|
| 894 | } |
---|
| 895 | |
---|
[8bd77b5] | 896 | // at this point we know we have a good attribute cert baked it in |
---|
| 897 | abac_attribute_t *attr=abac_attribute_new(issuer_id, cert, cert->get_ref(cert)); |
---|
| 898 | abac_attribute_set_head(attr, head_aspect); |
---|
| 899 | abac_attribute_add_tail(attr, tail_aspect); |
---|
| 900 | |
---|
[53e540d] | 901 | abac_credential_t *cred = abac_xmalloc(sizeof(abac_credential_t)); |
---|
[8bd77b5] | 902 | cred->hashkeyid=abac_xstrdup(encoded_attr_string); |
---|
| 903 | cred->attr=attr; |
---|
| 904 | cred->pl_clauses = clauses; |
---|
[53e540d] | 905 | *cred_ret = cred; |
---|
| 906 | |
---|
| 907 | // success, add the key to the map of certificates |
---|
[8bd77b5] | 908 | HASH_ADD_KEYPTR(hh, attr_creds, cred->hashkeyid, strlen(cred->hashkeyid), cred); |
---|
[53e540d] | 909 | |
---|
[8bd77b5] | 910 | assert(attr_hashkeyid_list); |
---|
| 911 | abac_list_add(attr_hashkeyid_list, abac_xstrdup(cred->hashkeyid)); |
---|
[9b43fc3] | 912 | |
---|
| 913 | if(debug) |
---|
| 914 | printf("-->adding into attr_creds 2, (%s)..cnt(%d)\n", |
---|
| 915 | cred->hashkeyid, HASH_COUNT(attr_creds)); |
---|
| 916 | |
---|
[da5afdf] | 917 | return ABAC_CERT_SUCCESS; |
---|
| 918 | |
---|
| 919 | error: |
---|
[8bd77b5] | 920 | if (head_aspect) abac_aspect_free(head_aspect); |
---|
| 921 | if (tail_aspect) abac_aspect_free(tail_aspect); |
---|
[b5a3da4] | 922 | abac_yy_free_rule_clauses(); |
---|
[da5afdf] | 923 | |
---|
| 924 | return ret; |
---|
| 925 | } |
---|
[8bd77b5] | 926 | |
---|
[da5afdf] | 927 | /** |
---|
| 928 | * Load an attribute cert. |
---|
| 929 | * Returns true only if the certificate is valid and is issued by the proper |
---|
| 930 | * authority. |
---|
[5d06689] | 931 | * attribute string is parsed via yyparse call |
---|
[da5afdf] | 932 | */ |
---|
| 933 | static int _load_attribute_cert(certificate_t *cert, abac_credential_t **cred_ret) { |
---|
| 934 | ietf_attributes_t *attr_cert = NULL; |
---|
[8bd77b5] | 935 | abac_id_credential_t *issuer; |
---|
[da5afdf] | 936 | int ret, i; |
---|
| 937 | |
---|
| 938 | // get the attr |
---|
| 939 | ac_t *ac = (ac_t *)cert; |
---|
| 940 | attr_cert = ac->get_groups(ac); |
---|
| 941 | if (attr_cert == NULL) { |
---|
| 942 | ret = ABAC_CERT_INVALID; |
---|
| 943 | goto error; |
---|
| 944 | } |
---|
| 945 | |
---|
| 946 | char *encoded_attr_string=attr_cert->get_string(attr_cert); |
---|
| 947 | char *attr_string = abac_decode_string(encoded_attr_string); |
---|
| 948 | if(debug) |
---|
[669b481] | 949 | printf("string to be yyparse..(%d)(%s)\n",strlen(attr_string),attr_string); |
---|
[da5afdf] | 950 | |
---|
| 951 | if (attr_string == NULL) { |
---|
| 952 | ret = ABAC_CERT_INVALID; |
---|
| 953 | goto error; |
---|
| 954 | } |
---|
| 955 | |
---|
| 956 | /* call into yacc parser */ |
---|
| 957 | abac_reset_yyfptr(attr_string); |
---|
[b5a3da4] | 958 | abac_yy_init(); |
---|
[da5afdf] | 959 | int rc=yyparse(); |
---|
| 960 | if (rc) { |
---|
| 961 | ret = ABAC_CERT_INVALID; |
---|
| 962 | goto error; |
---|
| 963 | } |
---|
| 964 | |
---|
[5d06689] | 965 | ret=_verify_valid_credential_string(cert,cred_ret,encoded_attr_string); |
---|
[da5afdf] | 966 | |
---|
[5110d42] | 967 | if(ret != ABAC_CERT_SUCCESS) { |
---|
| 968 | panic("_load_attribute_cert: fail to process (%s)",encoded_attr_string); |
---|
| 969 | } |
---|
| 970 | |
---|
[53e540d] | 971 | // free up some crap |
---|
| 972 | attr_cert->destroy(attr_cert); |
---|
| 973 | |
---|
[5110d42] | 974 | return ret; |
---|
[53e540d] | 975 | |
---|
| 976 | error: |
---|
[202a7f9] | 977 | if (cert) cert->destroy(cert); |
---|
| 978 | if (attr_cert) attr_cert->destroy(attr_cert); |
---|
[0779c99] | 979 | return ret; |
---|
[5450aeb] | 980 | } |
---|
[5f99fbc] | 981 | |
---|
[8bd77b5] | 982 | /** |
---|
| 983 | * Load an attribute cert from a abac_attribute_t. |
---|
[5d06689] | 984 | * attr should be all checked out before arriving here |
---|
[8bd77b5] | 985 | */ |
---|
[5d06689] | 986 | int abac_verifier_load_attribute_cert_attribute(abac_attribute_t *ptr, abac_credential_t **cred_ret) { |
---|
| 987 | // get the attr |
---|
| 988 | abac_aspect_t *head=abac_attribute_head(ptr); |
---|
| 989 | abac_aspect_t *tail=abac_attribute_tail(ptr); |
---|
| 990 | |
---|
| 991 | // preprocess for constraint part |
---|
| 992 | preprocess_pl_head(head); |
---|
| 993 | preprocess_pl_tail(tail); |
---|
| 994 | |
---|
[9b43fc3] | 995 | /* setup the encoded attribute string */ |
---|
| 996 | char *encoded_attr_string=get_cred_encoding(ptr); |
---|
| 997 | generate_pl_set_abac_yyfptr_encoded(encoded_attr_string); |
---|
| 998 | /* collect up type clauses, constraint clauses and |
---|
[5d06689] | 999 | generate rule clauses */ |
---|
| 1000 | abac_list_t *clauses=generate_pl_clauses(head,tail); |
---|
[9b43fc3] | 1001 | |
---|
| 1002 | abac_print_clauses(clauses,NULL); |
---|
[5d06689] | 1003 | |
---|
| 1004 | abac_credential_t *cred = abac_xmalloc(sizeof(abac_credential_t)); |
---|
| 1005 | cred->hashkeyid=abac_xstrdup(encoded_attr_string); |
---|
| 1006 | cred->attr=abac_attribute_dup(ptr); |
---|
| 1007 | cred->pl_clauses = clauses; |
---|
| 1008 | *cred_ret = cred; |
---|
| 1009 | |
---|
| 1010 | // success, add the key to the map of certificates |
---|
| 1011 | HASH_ADD_KEYPTR(hh, attr_creds, cred->hashkeyid, strlen(cred->hashkeyid), cred); |
---|
| 1012 | |
---|
| 1013 | assert(attr_hashkeyid_list); |
---|
| 1014 | abac_list_add(attr_hashkeyid_list, abac_xstrdup(cred->hashkeyid)); |
---|
[9b43fc3] | 1015 | |
---|
| 1016 | if(debug) |
---|
| 1017 | printf("-->adding into attr_creds, (%s)..cnt(%d)\n", |
---|
| 1018 | cred->hashkeyid, HASH_COUNT(attr_creds)); |
---|
[5d06689] | 1019 | return ABAC_CERT_SUCCESS; |
---|
[8bd77b5] | 1020 | } |
---|
| 1021 | |
---|
[e898049] | 1022 | /** |
---|
| 1023 | * Load an attribute cert from a file. |
---|
| 1024 | */ |
---|
[0779c99] | 1025 | int abac_verifier_load_attribute_cert_file(char *filename, abac_credential_t **cred) { |
---|
[e898049] | 1026 | // load the cert |
---|
[d037f54] | 1027 | if(debug) printf("..loading attr file %s\n", filename); |
---|
[e898049] | 1028 | certificate_t *cert = lib->creds->create( |
---|
| 1029 | lib->creds, CRED_CERTIFICATE, CERT_X509_AC, |
---|
| 1030 | BUILD_FROM_FILE, filename, |
---|
| 1031 | BUILD_END |
---|
| 1032 | ); |
---|
| 1033 | if (cert == NULL) |
---|
[0779c99] | 1034 | return ABAC_CERT_INVALID; |
---|
| 1035 | return _load_attribute_cert(cert, cred); |
---|
[e898049] | 1036 | } |
---|
| 1037 | |
---|
| 1038 | /** |
---|
| 1039 | * Load an attribute cert from a chunk. |
---|
| 1040 | */ |
---|
[0779c99] | 1041 | int abac_verifier_load_attribute_cert_chunk(chunk_t chunk, abac_credential_t **cred) { |
---|
[e898049] | 1042 | // load the cert |
---|
| 1043 | certificate_t *cert = lib->creds->create( |
---|
| 1044 | lib->creds, CRED_CERTIFICATE, CERT_X509_AC, |
---|
| 1045 | BUILD_BLOB_ASN1_DER, chunk, |
---|
| 1046 | BUILD_END |
---|
| 1047 | ); |
---|
| 1048 | if (cert == NULL) |
---|
[0779c99] | 1049 | return ABAC_CERT_INVALID; |
---|
[9b43fc3] | 1050 | |
---|
[0779c99] | 1051 | return _load_attribute_cert(cert, cred); |
---|
[e898049] | 1052 | } |
---|
| 1053 | |
---|
[85cdf53] | 1054 | /** |
---|
| 1055 | * Return the encoding of the attribute cert. |
---|
| 1056 | */ |
---|
[401a054] | 1057 | abac_chunk_t abac_credential_attribute_cert(abac_credential_t *cred) { |
---|
[8bd77b5] | 1058 | abac_attribute_t *ptr=cred->attr; |
---|
| 1059 | certificate_t *cert=abac_attribute_cert(ptr); |
---|
[137b55f] | 1060 | chunk_t encoding = chunk_empty; |
---|
| 1061 | int rc=cert->get_encoding(cert,CERT_ASN1_DER,&encoding); |
---|
[9efbfbf] | 1062 | abac_chunk_t ret = { encoding.ptr, encoding.len }; |
---|
| 1063 | return ret; |
---|
[85cdf53] | 1064 | } |
---|
| 1065 | |
---|
| 1066 | /** |
---|
| 1067 | * Return the encoding of the issuer cert. |
---|
| 1068 | */ |
---|
[401a054] | 1069 | abac_chunk_t abac_credential_issuer_cert(abac_credential_t *cred) { |
---|
[8bd77b5] | 1070 | certificate_t *issuer_cert=abac_attribute_issuer_cert(cred->attr); |
---|
| 1071 | assert(issuer_cert); |
---|
[137b55f] | 1072 | chunk_t encoding = chunk_empty; |
---|
| 1073 | // XXX MEI, not sure ?? |
---|
| 1074 | int rc=issuer_cert->get_encoding(issuer_cert,CERT_ASN1_DER,&encoding); |
---|
[9efbfbf] | 1075 | abac_chunk_t ret = { encoding.ptr, encoding.len }; |
---|
| 1076 | return ret; |
---|
[85cdf53] | 1077 | } |
---|
[186cb75] | 1078 | |
---|
[53e540d] | 1079 | /** |
---|
| 1080 | * Return the clause of the cert |
---|
| 1081 | */ |
---|
[202a7f9] | 1082 | abac_list_t *abac_credential_clauses(abac_credential_t *cred) { |
---|
[8bd77b5] | 1083 | return cred->pl_clauses; |
---|
[53e540d] | 1084 | } |
---|
| 1085 | |
---|
[8bd77b5] | 1086 | abac_attribute_t *abac_credential_attribute(abac_credential_t *cred) { |
---|
| 1087 | assert(cred); |
---|
| 1088 | return cred->attr; |
---|
| 1089 | } |
---|
[202a7f9] | 1090 | |
---|
[186cb75] | 1091 | /** |
---|
[fbb591e] | 1092 | * Increase the ref count of a credential. |
---|
[186cb75] | 1093 | */ |
---|
[401a054] | 1094 | abac_credential_t *abac_credential_dup(abac_credential_t *cred) { |
---|
| 1095 | assert(cred != NULL); |
---|
[186cb75] | 1096 | |
---|
[8bd77b5] | 1097 | abac_attribute_dup(cred->attr); |
---|
[fbb591e] | 1098 | return cred; |
---|
[186cb75] | 1099 | } |
---|
| 1100 | |
---|
[53e540d] | 1101 | /** |
---|
| 1102 | * lookup for a credential. |
---|
| 1103 | */ |
---|
[202a7f9] | 1104 | abac_credential_t *abac_credential_lookup(char* cred_string) |
---|
| 1105 | { |
---|
| 1106 | if(debug) |
---|
[8bd77b5] | 1107 | printf("abac_credential_lookup: looking for (%s)\n", cred_string); |
---|
| 1108 | abac_credential_t *attr_cred; |
---|
[9b43fc3] | 1109 | |
---|
[8bd77b5] | 1110 | HASH_FIND_STR(attr_creds, cred_string, attr_cred); |
---|
| 1111 | if (attr_cred == NULL) { |
---|
[202a7f9] | 1112 | if(debug) printf("DEBUG:NOT FOUND..\n"); |
---|
[53e540d] | 1113 | return NULL; |
---|
| 1114 | } |
---|
[8bd77b5] | 1115 | abac_credential_t *rt=abac_credential_dup(attr_cred); |
---|
| 1116 | if(debug) printf("DEBUG:FOUND.. (%d) returning, (%d)\n", (int)attr_cred, (int) rt); |
---|
| 1117 | return rt; |
---|
[53e540d] | 1118 | } |
---|
| 1119 | |
---|
[186cb75] | 1120 | /** |
---|
[fbb591e] | 1121 | * Decrease the reference count of a credential, freeing it when it reaches 0. |
---|
[186cb75] | 1122 | */ |
---|
[202a7f9] | 1123 | void abac_credential_free(abac_credential_t *cred) |
---|
| 1124 | { |
---|
| 1125 | if(debug) |
---|
[8bd77b5] | 1126 | printf("abac_credential_free:freeing cred(%d)clause(%d)\n", (int)cred, (int)cred->pl_clauses); |
---|
[202a7f9] | 1127 | |
---|
[401a054] | 1128 | if (cred == NULL) |
---|
[186cb75] | 1129 | return; |
---|
| 1130 | |
---|
[8bd77b5] | 1131 | // this is very hacky... |
---|
| 1132 | int last=abac_attribute_lastone(cred->attr); |
---|
| 1133 | if(!last) { |
---|
| 1134 | abac_attribute_free(cred->attr); |
---|
[da5afdf] | 1135 | } else { |
---|
[8bd77b5] | 1136 | free(cred->hashkeyid); |
---|
| 1137 | char *cur=NULL; |
---|
| 1138 | abac_list_foreach(cred->pl_clauses, cur, |
---|
| 1139 | free(cur); |
---|
| 1140 | ); |
---|
| 1141 | abac_attribute_free(cred->attr); |
---|
| 1142 | HASH_DEL(attr_creds, cred); |
---|
| 1143 | free(cred); |
---|
[da5afdf] | 1144 | } |
---|
[186cb75] | 1145 | } |
---|
[53e540d] | 1146 | |
---|
[8bd77b5] | 1147 | char *abac_id_clause(abac_id_credential_t *id_cred) |
---|
[202a7f9] | 1148 | { |
---|
[8bd77b5] | 1149 | if(id_cred) |
---|
| 1150 | return id_cred->pl_clause; |
---|
[202a7f9] | 1151 | return NULL; |
---|
| 1152 | } |
---|
| 1153 | |
---|
[53e540d] | 1154 | /* retrieve the cn that is associated with this sha_string */ |
---|
[5110d42] | 1155 | char *abac_cn_with_sha(char *sha_string) |
---|
| 1156 | { |
---|
[53e540d] | 1157 | // get the issuer based on keyid |
---|
[8bd77b5] | 1158 | abac_id_credential_t *id_cred; |
---|
| 1159 | HASH_FIND_STR(id_creds, sha_string, id_cred); |
---|
| 1160 | if (id_cred == NULL) { |
---|
[53e540d] | 1161 | return NULL; |
---|
| 1162 | } |
---|
[202a7f9] | 1163 | if(debug) |
---|
[8bd77b5] | 1164 | check_id_cred(id_cred); |
---|
| 1165 | return abac_id_cn(id_cred->id); |
---|
[53e540d] | 1166 | } |
---|
| 1167 | |
---|
[8bd77b5] | 1168 | char *abac_idtype_with_sha(char* sha_string) |
---|
| 1169 | { |
---|
| 1170 | // get the issuer based on keyid |
---|
| 1171 | abac_id_credential_t *id_cred; |
---|
| 1172 | HASH_FIND_STR(id_creds, sha_string, id_cred); |
---|
| 1173 | if (id_cred == NULL) { |
---|
| 1174 | return NULL; |
---|
| 1175 | } |
---|
| 1176 | int idtype=abac_id_idtype(id_cred->id); |
---|
| 1177 | |
---|
| 1178 | return abac_idtype_string(idtype); |
---|
[923b4dd] | 1179 | } |
---|
| 1180 | |
---|
[8bd77b5] | 1181 | abac_aspect_t *abac_credential_head(abac_credential_t *cred) { |
---|
| 1182 | return abac_attribute_head(cred->attr); |
---|
[923b4dd] | 1183 | } |
---|
| 1184 | |
---|
[8bd77b5] | 1185 | abac_aspect_t *abac_credential_tail(abac_credential_t *cred) { |
---|
| 1186 | return abac_attribute_tail(cred->attr); |
---|
[923b4dd] | 1187 | } |
---|
| 1188 | |
---|
| 1189 | |
---|