| 1 | |
|---|
| 2 | /** |
|---|
| 3 | ** abac_attribute.c |
|---|
| 4 | **/ |
|---|
| 5 | |
|---|
| 6 | #include <assert.h> |
|---|
| 7 | #include <ctype.h> |
|---|
| 8 | #include <string.h> |
|---|
| 9 | #include <time.h> |
|---|
| 10 | #include <err.h> |
|---|
| 11 | |
|---|
| 12 | #include "abac_internal.h" |
|---|
| 13 | #include "abac_util.h" |
|---|
| 14 | |
|---|
| 15 | #define SHA1_LENGTH 40 |
|---|
| 16 | |
|---|
| 17 | extern char* abac_encode_string(char*); |
|---|
| 18 | |
|---|
| 19 | static int debug=0; |
|---|
| 20 | |
|---|
| 21 | struct _abac_attribute_t { |
|---|
| 22 | abac_id_t *issuer_id; |
|---|
| 23 | abac_aspect_t *head; |
|---|
| 24 | abac_aspect_t *tail; |
|---|
| 25 | int validity; |
|---|
| 26 | certificate_t *cert; // NULL until baked |
|---|
| 27 | certificate_t *issuer_cert; |
|---|
| 28 | |
|---|
| 29 | int refcount; |
|---|
| 30 | }; |
|---|
| 31 | |
|---|
| 32 | /********************************************************************/ |
|---|
| 33 | static char *_get_cred_encoding(abac_attribute_t *ptr) |
|---|
| 34 | { |
|---|
| 35 | char *encoding=NULL; |
|---|
| 36 | |
|---|
| 37 | /* must have a head aspect and tail aspect */ |
|---|
| 38 | assert(ptr->head != NULL); |
|---|
| 39 | assert(ptr->tail != NULL); |
|---|
| 40 | |
|---|
| 41 | char *head_string=abac_aspect_typed_string_with_condition(ptr->head); |
|---|
| 42 | char *tail_string=abac_aspect_typed_string_with_condition(ptr->tail); |
|---|
| 43 | |
|---|
| 44 | asprintf(&encoding,"%s<-%s", head_string,tail_string); |
|---|
| 45 | char* base64_encoding=abac_encode_string(encoding); |
|---|
| 46 | |
|---|
| 47 | if(debug) printf("XXX abac_attribute_bake (%s)\n", encoding); |
|---|
| 48 | |
|---|
| 49 | free(encoding); |
|---|
| 50 | free(head_string); |
|---|
| 51 | free(tail_string); |
|---|
| 52 | return base64_encoding; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | /********************************************************************/ |
|---|
| 56 | abac_attribute_t *abac_attribute_new(abac_id_t *issuer, certificate_t *cert, |
|---|
| 57 | certificate_t *issuer_cert) |
|---|
| 58 | { |
|---|
| 59 | abac_attribute_t *ptr = abac_xmalloc(sizeof(abac_attribute_t)); |
|---|
| 60 | ptr->issuer_id = abac_id_dup(issuer); |
|---|
| 61 | ptr->head = NULL; |
|---|
| 62 | ptr->tail = NULL; |
|---|
| 63 | ptr->validity= 365 * 86400; // default |
|---|
| 64 | ptr->cert=cert; // already baked.. |
|---|
| 65 | ptr->issuer_cert=issuer_cert; |
|---|
| 66 | ptr->refcount=1; |
|---|
| 67 | |
|---|
| 68 | return ptr; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | // validity is measured in seconds (as of 0.2.0) |
|---|
| 72 | int abac_attribute_create(abac_attribute_t **ret, |
|---|
| 73 | abac_aspect_t *head, abac_aspect_t *tail, int validity) |
|---|
| 74 | { |
|---|
| 75 | abac_id_t *issuer_id=abac_aspect_get_issuer_id(head); |
|---|
| 76 | char *name=abac_aspect_aspect_name(head); |
|---|
| 77 | |
|---|
| 78 | if (abac_id_privkey(issuer_id) == NULL) |
|---|
| 79 | return ABAC_ATTRIBUTE_ISSUER_NOKEY; |
|---|
| 80 | if (!abac_validate_clean_aspect_name(name)) |
|---|
| 81 | return ABAC_ATTRIBUTE_INVALID_ROLE; |
|---|
| 82 | if (validity < 0) |
|---|
| 83 | return ABAC_ATTRIBUTE_INVALID_VALIDITY; |
|---|
| 84 | |
|---|
| 85 | abac_attribute_t *ptr = abac_xmalloc(sizeof(abac_attribute_t)); |
|---|
| 86 | ptr->issuer_id = abac_id_dup(issuer_id); |
|---|
| 87 | ptr->head = abac_aspect_dup(head); |
|---|
| 88 | ptr->tail = (tail!=NULL) ? abac_aspect_dup(tail):NULL; |
|---|
| 89 | ptr->validity=(validity==0?(365 * 86400):validity); |
|---|
| 90 | |
|---|
| 91 | // NULL until baked |
|---|
| 92 | ptr->cert=NULL; |
|---|
| 93 | ptr->issuer_cert=NULL; |
|---|
| 94 | ptr->refcount=1; |
|---|
| 95 | *ret = ptr; |
|---|
| 96 | return ABAC_ATTRIBUTE_SUCCESS; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | // validity is measured in seconds (as of 0.2.0) |
|---|
| 100 | int abac_attribute_create_creddy(abac_attribute_t **ret, abac_id_t *issuer, |
|---|
| 101 | abac_aspect_t *head_aspect, abac_aspect_t *tail_aspect, char *name, int validity) |
|---|
| 102 | { |
|---|
| 103 | if (abac_id_privkey(issuer) == NULL) |
|---|
| 104 | return ABAC_ATTRIBUTE_ISSUER_NOKEY; |
|---|
| 105 | if (!abac_validate_clean_aspect_name(name)) |
|---|
| 106 | return ABAC_ATTRIBUTE_INVALID_ROLE; |
|---|
| 107 | if (validity < 0) |
|---|
| 108 | return ABAC_ATTRIBUTE_INVALID_VALIDITY; |
|---|
| 109 | |
|---|
| 110 | abac_attribute_t *ptr = abac_xmalloc(sizeof(abac_attribute_t)); |
|---|
| 111 | ptr->issuer_id = abac_id_dup(issuer); |
|---|
| 112 | |
|---|
| 113 | ptr->head=head_aspect; |
|---|
| 114 | ptr->tail=tail_aspect; |
|---|
| 115 | ptr->validity = validity; |
|---|
| 116 | |
|---|
| 117 | // NULL until baked |
|---|
| 118 | ptr->cert = NULL; |
|---|
| 119 | ptr->issuer_cert = NULL; |
|---|
| 120 | *ret = ptr; |
|---|
| 121 | return ABAC_ATTRIBUTE_SUCCESS; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | int abac_attribute_bake(abac_attribute_t *ptr) { |
|---|
| 125 | assert(ptr); |
|---|
| 126 | |
|---|
| 127 | char *cred_encoding = _get_cred_encoding(ptr); |
|---|
| 128 | |
|---|
| 129 | // create attribute cert |
|---|
| 130 | time_t not_before = time(NULL); |
|---|
| 131 | time_t not_after = not_before + ptr->validity; |
|---|
| 132 | chunk_t serial = abac_generate_serial(); |
|---|
| 133 | |
|---|
| 134 | certificate_t *attr_cert = lib->creds->create(lib->creds, |
|---|
| 135 | CRED_CERTIFICATE, CERT_X509_AC, |
|---|
| 136 | BUILD_CERT, abac_id_cert(ptr->issuer_id), |
|---|
| 137 | BUILD_NOT_BEFORE_TIME, not_before, |
|---|
| 138 | BUILD_NOT_AFTER_TIME, not_after, |
|---|
| 139 | BUILD_SERIAL, serial, |
|---|
| 140 | BUILD_IETF_GROUP_ATTR, cred_encoding, |
|---|
| 141 | BUILD_SIGNING_CERT, abac_id_cert(ptr->issuer_id), |
|---|
| 142 | BUILD_SIGNING_KEY, abac_id_privkey(ptr->issuer_id), |
|---|
| 143 | BUILD_END |
|---|
| 144 | ); |
|---|
| 145 | |
|---|
| 146 | if (attr_cert == NULL) |
|---|
| 147 | return 0; |
|---|
| 148 | |
|---|
| 149 | ptr->cert = attr_cert; |
|---|
| 150 | ptr->issuer_cert= attr_cert->get_ref(attr_cert); |
|---|
| 151 | |
|---|
| 152 | free(cred_encoding); |
|---|
| 153 | free(serial.ptr); |
|---|
| 154 | return 1; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | abac_chunk_t abac_attribute_cert_chunk(abac_attribute_t *ptr) { |
|---|
| 158 | assert(ptr->cert); |
|---|
| 159 | chunk_t encoding = ptr->cert->get_encoding(ptr->cert); |
|---|
| 160 | abac_chunk_t ret = { encoding.ptr, encoding.len }; |
|---|
| 161 | return ret; |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | int abac_attribute_baked(abac_attribute_t *ptr) { |
|---|
| 165 | return ptr->cert != NULL; |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | // returns 0 if the cert hasn't been baked |
|---|
| 169 | int abac_attribute_write(abac_attribute_t *ptr, FILE *out) { |
|---|
| 170 | assert(ptr != NULL); |
|---|
| 171 | |
|---|
| 172 | if (ptr->cert == NULL) |
|---|
| 173 | return 0; |
|---|
| 174 | |
|---|
| 175 | // write to file |
|---|
| 176 | chunk_t encoding = ptr->cert->get_encoding(ptr->cert); |
|---|
| 177 | fwrite(encoding.ptr, encoding.len, 1, out); |
|---|
| 178 | free(encoding.ptr); |
|---|
| 179 | |
|---|
| 180 | return 1; |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | abac_attribute_t *abac_attribute_dup(abac_attribute_t *ptr) |
|---|
| 184 | { |
|---|
| 185 | assert(ptr); |
|---|
| 186 | ++ptr->refcount; |
|---|
| 187 | return ptr; |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | void abac_attribute_free(abac_attribute_t *ptr) { |
|---|
| 191 | int i; |
|---|
| 192 | |
|---|
| 193 | if (ptr == NULL) |
|---|
| 194 | return; |
|---|
| 195 | |
|---|
| 196 | --ptr->refcount; |
|---|
| 197 | if (ptr->refcount > 0) |
|---|
| 198 | return; |
|---|
| 199 | |
|---|
| 200 | if(ptr->issuer_id) abac_id_free(ptr->issuer_id); |
|---|
| 201 | if(ptr->head) abac_aspect_free(ptr->head); |
|---|
| 202 | if(ptr->tail) abac_aspect_free(ptr->tail); |
|---|
| 203 | |
|---|
| 204 | if(ptr->cert) DESTROY_IF(ptr->cert); |
|---|
| 205 | if(ptr->issuer_cert) DESTROY_IF(ptr->issuer_cert); |
|---|
| 206 | |
|---|
| 207 | free(ptr); |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | /********************************************************************/ |
|---|
| 211 | certificate_t *abac_attribute_issuer_cert(abac_attribute_t *ptr) |
|---|
| 212 | { |
|---|
| 213 | assert(ptr); |
|---|
| 214 | return ptr->issuer_cert; |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | abac_aspect_t *abac_attribute_head(abac_attribute_t *ptr) |
|---|
| 218 | { |
|---|
| 219 | assert(ptr); |
|---|
| 220 | return ptr->head; |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | abac_attribute_t *abac_attribute_set_head(abac_attribute_t *ptr, abac_aspect_t *head) |
|---|
| 224 | { |
|---|
| 225 | assert(ptr); |
|---|
| 226 | ptr->head=head; |
|---|
| 227 | return ptr; |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | abac_aspect_t *abac_attribute_tail(abac_attribute_t *ptr) |
|---|
| 231 | { |
|---|
| 232 | assert(ptr); |
|---|
| 233 | return ptr->tail; |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | abac_attribute_t *abac_attribute_add_tail(abac_attribute_t *ptr, abac_aspect_t *tail) |
|---|
| 237 | { |
|---|
| 238 | assert(ptr); |
|---|
| 239 | assert(tail); |
|---|
| 240 | |
|---|
| 241 | /* type of head and tail has to match */ |
|---|
| 242 | abac_aspect_t *head=abac_attribute_head(ptr); |
|---|
| 243 | if(abac_aspect_is_intersecting(tail)) { |
|---|
| 244 | if(debug) { |
|---|
| 245 | printf("tail is intersection \n"); |
|---|
| 246 | printf("tail is (%s)\n", abac_aspect_string_with_condition(tail)); |
|---|
| 247 | } |
|---|
| 248 | if(abac_aspect_intersecting_aspect_type(tail) != abac_aspect_aspect_type(head)) |
|---|
| 249 | errx(1, "head and tail's aspect type does not match"); |
|---|
| 250 | } else { |
|---|
| 251 | if(abac_aspect_aspect_type(head) != abac_aspect_aspect_type(tail)) { |
|---|
| 252 | if(debug) { |
|---|
| 253 | printf("head->(%s)\n",abac_aspect_type_string(head)); |
|---|
| 254 | printf("tail->(%s)\n",abac_aspect_type_string(tail)); |
|---|
| 255 | printf("tail is (%s)\n", abac_aspect_string_with_condition(tail)); |
|---|
| 256 | } |
|---|
| 257 | errx(1, "head and tail's aspect type does not match"); |
|---|
| 258 | } |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | /* special case.. if there is a tail there already and it is not an intersecting |
|---|
| 262 | tail, need to turn this into an intersecting tails */ |
|---|
| 263 | if(ptr->tail == NULL) { |
|---|
| 264 | ptr->tail=abac_aspect_dup(tail); |
|---|
| 265 | } else { |
|---|
| 266 | if(abac_aspect_is_intersecting(ptr->tail)) { |
|---|
| 267 | abac_aspect_add_intersecting_aspect(ptr->tail, tail); |
|---|
| 268 | } else { |
|---|
| 269 | /* special case.. if there is a tail there already and it is not an intersecting |
|---|
| 270 | tail, need to turn this into an intersecting tails */ |
|---|
| 271 | abac_aspect_t *nptr=abac_aspect_intersection_new(ptr->tail); |
|---|
| 272 | abac_aspect_add_intersecting_aspect(nptr, tail); |
|---|
| 273 | ptr->tail=nptr; |
|---|
| 274 | } |
|---|
| 275 | } |
|---|
| 276 | return ptr; |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | certificate_t *abac_attribute_cert(abac_attribute_t *ptr) |
|---|
| 280 | { |
|---|
| 281 | assert(ptr); |
|---|
| 282 | return ptr->cert; |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | int abac_attribute_lastone(abac_attribute_t *ptr) |
|---|
| 286 | { |
|---|
| 287 | assert(ptr); |
|---|
| 288 | if(ptr->refcount == 1) |
|---|
| 289 | return 1; |
|---|
| 290 | return 0; |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | abac_aspect_t **abac_attribute_tail_vectorized(abac_attribute_t *ptr) |
|---|
| 295 | { |
|---|
| 296 | abac_aspect_t **tails=NULL; |
|---|
| 297 | abac_aspect_t *tail=ptr->tail; |
|---|
| 298 | abac_list_t *list=NULL; |
|---|
| 299 | int cnt=0; |
|---|
| 300 | if(tail != NULL) { |
|---|
| 301 | if(!abac_aspect_is_intersecting(tail)) { |
|---|
| 302 | tails = abac_xmalloc(sizeof(abac_aspect_t *) * 2); |
|---|
| 303 | tails[0] = abac_aspect_dup(tail); |
|---|
| 304 | tails[1] = NULL; |
|---|
| 305 | if(debug) { |
|---|
| 306 | printf("abac_attribute_tail_vectorized, only 1 tail\n"); |
|---|
| 307 | } |
|---|
| 308 | return tails; |
|---|
| 309 | } else { |
|---|
| 310 | abac_list_t *list=abac_aspect_prereqs(tail); |
|---|
| 311 | cnt=abac_list_size(list); |
|---|
| 312 | } |
|---|
| 313 | } |
|---|
| 314 | // make the array (leave space to NULL terminate it) |
|---|
| 315 | // n.b., even if the list is empty, we still return an array that |
|---|
| 316 | // only contains the NULL terminator |
|---|
| 317 | tails = abac_xmalloc(sizeof(abac_aspect_t *) * (cnt + 1)); |
|---|
| 318 | abac_aspect_t *cur; |
|---|
| 319 | int i = 0; |
|---|
| 320 | if(i<cnt) { |
|---|
| 321 | abac_list_foreach(list, cur, |
|---|
| 322 | tails[i++] = abac_aspect_dup(cur); |
|---|
| 323 | ); |
|---|
| 324 | } |
|---|
| 325 | tails[i] = NULL; |
|---|
| 326 | if(debug) |
|---|
| 327 | printf("abac_attribute_tail_vectorized, %d tails\n", cnt); |
|---|
| 328 | return tails; |
|---|
| 329 | } |
|---|
| 330 | |
|---|
| 331 | void abac_aspects_free(abac_aspect_t **aspects) |
|---|
| 332 | { |
|---|
| 333 | /* always null terminating */ |
|---|
| 334 | assert(aspects); |
|---|
| 335 | int i; |
|---|
| 336 | for (i = 0; aspects[i] != NULL; ++i) { |
|---|
| 337 | abac_aspect_free(aspects[i]); |
|---|
| 338 | } |
|---|
| 339 | free(aspects); |
|---|
| 340 | } |
|---|