source: libabac/abac_attribute.c @ ab63ece

abac0-leakabac0-meitvf-new-xml
Last change on this file since ab63ece was ab63ece, checked in by Ted Faber <faber@…>, 11 years ago

Mofify attributes to keep a list of tails instead of one string.

  • Property mode set to 100644
File size: 11.9 KB
Line 
1
2/* abac_attribute.c */
3
4#define _GNU_SOURCE
5#include <stdio.h>
6#include <assert.h>
7#include <ctype.h>
8#include <string.h>
9#include <time.h>
10
11#include "libabac_common.h"
12#include "abac_list.h"
13#include "abac_util.h"
14#include "abac_xml.h"
15
16#define ROLE_SEPARATOR " <- "
17#define INTERSECTION_SEP " & "
18#define SHA1_LENGTH 40
19
20static int debug=0;
21
22// a GENI XML attribute chunk might contain multiple
23// attribute rules. It will be translate into multiple
24// abac_attribute structures but with cert ptr pointing
25// to the same xml chunk
26// issuer can be missing but then it won't be bakable
27// unless it is baked just for creddy's roles call
28struct _abac_attribute_t {
29    abac_id_t *issuer_id; 
30    char *role;
31    long validity;
32    int ntails;
33
34    char *head_string;
35    char **tail_strings;
36
37    abac_chunk_t cert; // the XML chunk
38};
39
40char *abac_attribute_role_string(abac_attribute_t *attr);
41extern abac_id_t *abac_verifier_lookup_id(abac_list_t*, char *keyid);
42static char *_validate_principal(char *keyid);
43
44/************************************************************/
45abac_chunk_t abac_attribute_cert(abac_attribute_t *ptr)
46{
47    assert(ptr);
48    return ptr->cert;
49}
50
51abac_id_t *abac_attribute_issuer_id(abac_attribute_t *ptr)
52{
53    assert(ptr);
54    return ptr->issuer_id;
55}
56
57// validity is measured in seconds (as of 0.2.0)
58// Acme.customer
59int abac_attribute_create(abac_attribute_t **ret, abac_id_t *issuer_id, char *role, long validity) {
60    libabac_init();
61    if (!abac_id_has_privkey(issuer_id))
62        return ABAC_ATTRIBUTE_ISSUER_NOKEY;
63    if (!abac_clean_name(role))
64        return ABAC_ATTRIBUTE_INVALID_ROLE;
65    if (validity < 0)
66        return ABAC_ATTRIBUTE_INVALID_VALIDITY;
67    if (!abac_id_still_valid(issuer_id))
68        return ABAC_ATTRIBUTE_INVALID_ISSUER;
69
70    if(validity == 0) validity = (long)(60*60*24*(365));
71
72    abac_attribute_t *attr = abac_xmalloc(sizeof(abac_attribute_t));
73    if(issuer_id) attr->issuer_id = abac_id_dup(issuer_id);
74        else attr->issuer_id = NULL;
75    attr->role = abac_xstrdup(role);
76    attr->validity = validity;
77
78    attr->head_string = NULL;
79    asprintf(&attr->head_string,"%s.%s",abac_id_keyid(issuer_id),role);
80    attr->ntails = 0;
81    attr->tail_strings = NULL;
82
83    // NULL until baked
84    attr->cert.ptr=NULL;
85    attr->cert.len=0;
86
87    *ret = attr;
88    return ABAC_SUCCESS;
89}
90
91/**
92 * Get the validity period.(xml module returns the diff from expire time - now()
93 */
94int abac_attribute_validity(abac_attribute_t *attr,struct tm *not_before,struct tm *not_after) {
95    assert(attr);
96    memset(not_before, 0, sizeof(struct tm));
97    memset(not_after, 0, sizeof(struct tm));
98
99    time_t now;
100    time(&now);
101    gmtime_r(&now, not_before);
102    char *xml=(char *)attr->cert.ptr;
103    long validity=get_validity_from_xml(xml);
104
105    time_t etime = now + validity;
106    gmtime_r(&etime, not_after);
107
108    if(debug) fprintf(stderr,"validity from the xml blob is %ld\n",validity);
109    if(validity == 0)
110        return ABAC_FAILURE;
111    return ABAC_SUCCESS;
112}
113
114int abac_attribute_still_valid(abac_attribute_t *attr)
115{
116    assert(attr);
117    assert(attr->cert.ptr);
118    long v=get_validity_from_xml((char *)attr->cert.ptr);
119    if (v > 0.0)
120        return 1;
121    else return 0;
122}
123
124int abac_attribute_add_tail(abac_attribute_t *attr, char *string) {
125    assert(attr);
126
127    char **old_tail = attr->tail_strings;
128    int newsize = (attr->ntails+1)*sizeof(char *);
129
130    if ( !(attr->tail_strings = realloc(attr->tail_strings, newsize))) {
131        attr->tail_strings = old_tail;
132        return 0;
133    }
134    attr->tail_strings[attr->ntails++] = abac_xstrdup(string);
135    return 1;
136}
137
138
139void abac_attribute_set_head(abac_attribute_t *attr, char *string)
140{
141    assert(attr);
142    attr->head_string=string;
143}
144
145char *abac_attribute_get_head(abac_attribute_t *attr)
146{
147    assert(attr);
148    return attr->head_string;
149}
150/*
151void abac_attribute_set_tail(abac_attribute_t *attr, char *string)
152{
153    assert(attr);
154    attr->tail_string=string;
155}
156
157char *abac_attribute_get_tail(abac_attribute_t *attr)
158{
159    assert(attr);
160    return attr->tail_string;
161}
162*/
163
164/* A.b->C, return copy of a A */
165char *abac_attribute_get_principal(abac_attribute_t *attr)
166{
167    char *role_string=abac_attribute_role_string(attr);
168    /* make an copy */
169    char *tmp=strdup(role_string);
170    char *head_tail[2];
171    int ret = 2;
172    abac_split(tmp, "<-", head_tail, &ret);
173    if (ret != 2) goto err;
174    abac_split(head_tail[0], ".", head_tail, &ret);
175    if (ret != 2) goto err;
176    char *prin=strdup(head_tail[0]);
177    free(tmp);
178    return prin;
179
180err:     
181     free(tmp);
182     return NULL;
183}
184
185int abac_attribute_principal(abac_attribute_t *attr, char *keyid) {
186    char *copy = _validate_principal(keyid);
187    if (copy == NULL)
188        return 0;
189
190    return abac_attribute_add_tail(attr,copy);
191}
192
193int abac_attribute_role(abac_attribute_t *attr, char *keyid, char *role) {
194    if (!abac_clean_name(role))
195        return 0;
196
197    char *copy = _validate_principal(keyid);
198    char *newcopy=NULL;
199    if (copy == NULL)
200        return 0;
201
202    asprintf(&newcopy,"%s.%s", copy,role);
203    free(copy);
204    return abac_attribute_add_tail(attr, newcopy);
205}
206
207int abac_attribute_linking_role(abac_attribute_t *attr, char *keyid, char *role, char *linked) {
208    if (!abac_clean_name(role) || !abac_clean_name(linked))
209        return 0;
210
211    char *copy = _validate_principal(keyid);
212    if (copy == NULL)
213        return 0;
214
215    char *newcopy=NULL;
216    asprintf(&newcopy,"%s.%s.%s", copy,role,linked);
217    free(copy);
218    return abac_attribute_add_tail(attr, newcopy);
219}
220
221
222
223// 0 for fail to bake, 1 is baked okay
224int abac_attribute_bake(abac_attribute_t *attr) {
225    assert(attr);
226    assert(attr->head_string);
227    assert(attr->tail_strings);
228
229    abac_chunk_t id_chunk = { NULL, 0 };
230    int ret=abac_id_PEM(attr->issuer_id, &id_chunk);
231    if(ret != ABAC_CERT_SUCCESS)
232        return 0; 
233
234    char *role_string = abac_attribute_role_string(attr);
235
236/* Make an new GENI abac credential with the rt0 rule that expires secs from
237 * now.  cert is the PEM encoded X.509 of the issuer's certificate as a string.
238 * certlen is the length of cert.  Returns the XML. Caller is responsible for
239 * freeing it. */
240    char *attr_cert=my_make_credential(role_string, attr->validity, (char *)id_chunk.ptr, id_chunk.len);
241
242    if (attr_cert == NULL)
243        return 0;
244
245    attr->cert.ptr = (unsigned char *)attr_cert;
246    attr->cert.len = strlen(attr_cert);
247
248    free(role_string);
249    return 1;
250}
251
252// make an explicit copy
253abac_chunk_t abac_attribute_cert_chunk(abac_attribute_t *attr) {
254    abac_chunk_t chunk= {NULL,0};
255
256    if (attr->cert.ptr == NULL)
257        return chunk;
258
259    /* return the xml chunk */
260    chunk.ptr= (unsigned char *)abac_xstrdup((char *)attr->cert.ptr);
261    chunk.len=attr->cert.len;
262    return chunk;
263}
264
265int abac_attribute_baked(abac_attribute_t *attr) {
266    return (attr->cert.ptr != NULL);
267}
268
269
270static abac_attribute_t *_load_attr(abac_list_t *id_certs,char *rstring, char *xml)
271{
272    /* make a copy of rle_string */
273    char *role_string=abac_xstrdup(rstring);
274
275if(debug) fprintf(stderr,"loading -> %s \n", role_string);
276
277    char *head_tail[2];
278    char *role_rest[2];
279    int ret = 2;
280    abac_split(role_string, "<-", head_tail, &ret);
281    if (ret != 2) return NULL; 
282
283    char *keyid=get_keyid_from_xml(xml);
284    abac_id_t *issuer_id=abac_verifier_lookup_id(id_certs,keyid);
285
286    long validity=get_validity_from_xml(xml);
287   
288    abac_attribute_t *attr = abac_xmalloc(sizeof(abac_attribute_t));
289    if(issuer_id)
290        attr->issuer_id = abac_id_dup(issuer_id);
291        else attr->issuer_id=NULL;
292    attr->validity = validity;
293
294    attr->head_string = abac_xstrdup(head_tail[0]);
295    do {
296        ret = 2;
297        abac_split(head_tail[1], " & ", role_rest, &ret);
298        abac_attribute_add_tail(attr, abac_xstrdup(role_rest[0]));
299        head_tail[1] =role_rest[1];
300    } while (ret == 2);
301
302    char *tmp=strstr(attr->head_string,".");
303    attr->role =abac_xstrdup(tmp+1);
304
305    attr->cert.ptr=(unsigned char *)abac_xstrdup(xml);
306    attr->cert.len=strlen(xml);
307
308    return attr;
309}
310
311abac_list_t *abac_attribute_certs_from_file(abac_list_t *id_certs,char *filename)
312{
313    libabac_init();
314    abac_list_t *alist=abac_list_new();
315    char *xml=NULL;
316    char *rt0=NULL;
317
318    char **rt0s=my_read_credential((void *)id_certs,filename, &xml);
319    if(rt0s == NULL) return alist;
320    if(xml == NULL || strlen(xml)==0) return alist;
321
322    abac_attribute_t *attr;
323
324    int i=0;
325    do {
326        rt0 = rt0s[i]; 
327        if(rt0 == NULL) break;
328        attr=_load_attr(id_certs,rt0, xml);
329        if(attr)
330            abac_list_add(alist, attr);
331        free(rt0);
332        i++;
333    } while (rt0s[i] !=NULL);
334
335    free(rt0s);
336    free(xml);
337
338    return alist;
339}
340
341abac_list_t *abac_attribute_certs_from_chunk(abac_list_t *id_certs,abac_chunk_t chunk) {
342    libabac_init();
343
344    abac_list_t *alist=abac_list_new();
345    char *xml=(char *)chunk.ptr;
346    if(chunk.len==0) return alist;
347
348    char **rt0s=get_rt0_from_xml((void *) id_certs,xml);
349    char *rt0=NULL;
350    if(rt0s==NULL) return alist;
351
352    abac_attribute_t *attr;
353    int i=0;
354    do {
355        rt0 = rt0s[i]; 
356        if(rt0 == NULL) break;
357        attr=_load_attr(id_certs,rt0, xml);
358        if(attr)
359            abac_list_add(alist, attr);
360        free(rt0);
361        i++;
362    } while (rt0s[i] !=NULL);
363
364    free(rt0s);
365    return alist;
366}
367
368// returns ABAC_FAILURE if the cert hasn't been baked
369int abac_attribute_write(abac_attribute_t *attr, FILE *out) {
370    assert(attr != NULL);
371
372    if (attr->cert.ptr == NULL)
373        return ABAC_FAILURE;
374
375    // write to file
376    fwrite(attr->cert.ptr, attr->cert.len, 1, out);
377
378    return ABAC_SUCCESS;
379}
380
381// returns ABAC_FAILURE if the cert hasn't been baked
382int abac_attribute_write_file(abac_attribute_t *attr, const char *fname) {
383    if (attr->cert.ptr == NULL)
384        return ABAC_FAILURE;
385
386    FILE *fp=fopen(fname,"w+");
387    if(fp) {
388         // write to file
389         fwrite(attr->cert.ptr, attr->cert.len, 1, fp);
390    } else return ABAC_FAILURE;
391    fclose(fp);
392
393    return ABAC_SUCCESS;
394}
395
396
397void abac_attribute_free(abac_attribute_t *attr) {
398
399if(debug) fprintf(stderr,"calling abac_attribute_free on %s\n", attr->head_string);
400    int i = 0;
401
402    if (attr == NULL)
403        return;
404
405    if(attr->issuer_id) abac_id_free(attr->issuer_id);
406
407    free(attr->role);
408    free(attr->head_string);
409    for (i=0; i < attr->ntails; i++) 
410        free(attr->tail_strings[i]);
411    free(attr->tail_strings);
412
413/* XXX this can not be free as if it is a char string
414    free(attr->cert.ptr);
415*/
416
417    free(attr);
418}
419
420//
421// Helper functions below
422//
423
424// validate a princpal's name
425// makes sure it's a valid SHA1 identifier
426// return values:
427//  success: malloc'd copy with all hex digits lowercase
428//  fail: NULL
429static char *_validate_principal(char *keyid) {
430    int i;
431    char *copy = NULL;
432
433    if (strlen(keyid) != SHA1_LENGTH)
434        return NULL;
435
436    copy = abac_xstrdup(keyid);
437    for (i = 0; i < SHA1_LENGTH; ++i) {
438        copy[i] = tolower(copy[i]);
439        if (!isxdigit(copy[i]))
440            goto error;
441    }
442
443    return copy;
444
445error:
446    free(copy);
447    return NULL;
448}
449
450static int abac_attribute_role_string_size(abac_attribute_t *attr) {
451    int sz = 3; /* Start with the end of string character and <-*/
452    int i;      /* Scratch */
453
454    if ( !attr) return sz;
455    if ( attr->head_string) 
456        sz += strlen(attr->head_string);
457    for (i = 0; i < attr->ntails; i++) 
458        if ( attr->tail_strings[i]) 
459            sz += strlen(attr->tail_strings[i]);
460    /* " & " between each pair of tails */
461    sz += 3 * (attr->ntails-1);
462    return sz;
463}
464
465// combine up the attribute's rule string, explicit copy
466char *abac_attribute_role_string(abac_attribute_t *attr) {
467    assert(attr);
468
469    int sz = abac_attribute_role_string_size(attr);
470    char *role_string=abac_xmalloc(sz);
471    int i;
472
473    if ( !role_string) return NULL;
474
475    sz -= snprintf(role_string, sz, "%s<-", attr->head_string);
476    for ( i = 0 ; i < attr->ntails; i++ ) {
477        if ( i > 0 ) {
478            strncat(role_string, " & ", 3);
479            sz -= 3;
480        }
481        strncat(role_string,attr->tail_strings[i], sz); 
482
483        sz -= strlen(attr->tail_strings[i]);
484        if (sz < 0 ) return NULL;
485    }
486    return role_string;
487}
Note: See TracBrowser for help on using the repository browser.