source: libabac/abac_attribute.c @ ca402ad

abac0-leak
Last change on this file since ca402ad was 8164e70, checked in by Mei <mei@…>, 11 years ago

1) tweak with changes for handling abac_chunk_t

  • Property mode set to 100644
File size: 13.7 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
20#define DEFAULT_OUTPUT_FORMAT "GENIv1.1"
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    char *output_format;
37    abac_keyid_map_t *keymap;
38
39    abac_chunk_t cert; // the XML chunk
40};
41
42char *abac_attribute_role_string(abac_attribute_t *attr);
43extern abac_id_t *abac_verifier_lookup_id(abac_list_t*, char *keyid);
44static char *_validate_principal(char *keyid);
45
46/************************************************************/
47abac_chunk_t abac_attribute_cert(abac_attribute_t *ptr)
48{
49    assert(ptr);
50    return ptr->cert;
51}
52
53abac_id_t *abac_attribute_issuer_id(abac_attribute_t *ptr)
54{
55    assert(ptr);
56    return ptr->issuer_id;
57}
58
59/* Get the format for this attribute to be output in.  This is NULL if the
60 * attribute has been read from a file. */
61char *abac_attribute_get_output_format(abac_attribute_t *a) {
62    return a->output_format;
63}
64
65/* Set the format for this attribute to be output in.  Valid formats are:
66 * GENIv1.0
67 * GENIv1.1
68 */
69void abac_attribute_set_output_format(abac_attribute_t *a, char *fmt) {
70    if (a->output_format) 
71        free(a->output_format);
72    a->output_format = abac_xstrdup(fmt);
73}
74
75
76// validity is measured in seconds (as of 0.2.0)
77// Acme.customer
78int abac_attribute_create(abac_attribute_t **ret, abac_id_t *issuer_id, char *role, long validity) {
79    libabac_init();
80    if (!abac_id_has_privkey(issuer_id))
81        return ABAC_ATTRIBUTE_ISSUER_NOKEY;
82    if (!abac_clean_name(role))
83        return ABAC_ATTRIBUTE_INVALID_ROLE;
84    if (validity < 0)
85        return ABAC_ATTRIBUTE_INVALID_VALIDITY;
86    if (!abac_id_still_valid(issuer_id))
87        return ABAC_ATTRIBUTE_INVALID_ISSUER;
88
89    if(validity == 0) validity = (long)(60*60*24*(365));
90
91    abac_attribute_t *attr = abac_xmalloc(sizeof(abac_attribute_t));
92    if(issuer_id) attr->issuer_id = abac_id_dup(issuer_id);
93        else attr->issuer_id = NULL;
94    attr->role = abac_xstrdup(role);
95    attr->validity = validity;
96
97    attr->head_string = NULL;
98    int rc=asprintf(&attr->head_string,"%s.%s",abac_id_keyid(issuer_id),role);
99    attr->ntails = 0;
100    attr->tail_strings = NULL;
101    attr->keymap = NULL;
102
103    attr->output_format = abac_xstrdup(DEFAULT_OUTPUT_FORMAT);
104
105    // NULL until baked
106    attr->cert=abac_chunk_new(NULL,0);
107
108    *ret = attr;
109    return ABAC_SUCCESS;
110}
111
112/**
113 * Get the validity period.(xml module returns the diff from expire time - now()
114 */
115int abac_attribute_validity(abac_attribute_t *attr,struct tm *not_before,struct tm *not_after) {
116    assert(attr);
117    memset(not_before, 0, sizeof(struct tm));
118    memset(not_after, 0, sizeof(struct tm));
119
120    time_t now;
121    time(&now);
122    gmtime_r(&now, not_before);
123    char *xml=(char *)attr->cert.ptr;
124    long validity=get_validity_from_xml(xml);
125
126    time_t etime = now + validity;
127    gmtime_r(&etime, not_after);
128
129    if(validity == 0)
130        return ABAC_FAILURE;
131    return ABAC_SUCCESS;
132}
133
134int abac_attribute_still_valid(abac_attribute_t *attr)
135{
136    assert(attr);
137    assert(attr->cert.ptr);
138    long v=get_validity_from_xml((char *)attr->cert.ptr);
139    if (v > 0.0)
140        return 1;
141    else return 0;
142}
143
144/* string is a malloc copy */
145int abac_attribute_add_tail(abac_attribute_t *attr, char *string) {
146    assert(attr);
147
148    char **old_tail = attr->tail_strings;
149    int newsize = (attr->ntails+1)*sizeof(char *);
150
151    if ( !(attr->tail_strings = realloc(attr->tail_strings, newsize))) {
152        attr->tail_strings = old_tail;
153        return 0;
154    }
155    attr->tail_strings[attr->ntails++] = string;
156    return 1;
157}
158
159
160void abac_attribute_set_head(abac_attribute_t *attr, char *string)
161{
162    assert(attr);
163    attr->head_string=string;
164}
165
166char *abac_attribute_get_head(abac_attribute_t *attr)
167{
168    assert(attr);
169    return attr->head_string;
170}
171
172/*
173 * Return the number of tail strings
174 */
175int abac_attribute_get_ntails(abac_attribute_t *attr) {
176    assert(attr);
177    return attr->ntails;
178}
179
180/*
181 * Return the nth tail string or NULL if it is undefined
182 */
183
184char *abac_attribute_get_tail_n(abac_attribute_t *attr, int n) {
185    assert(attr);
186    if ( n < 0 || n > attr->ntails) return NULL;
187    return attr->tail_strings[n];
188}
189
190
191/* A.b->C, return copy of a A */
192char *abac_attribute_get_principal(abac_attribute_t *attr)
193{
194    /* already a copy */
195    char *tmp=abac_attribute_role_string(attr);
196    char *head_tail[2];
197    int ret = 2;
198    abac_split(tmp, "<-", head_tail, &ret);
199    if (ret != 2) goto err;
200    abac_split(head_tail[0], ".", head_tail, &ret);
201    if (ret != 2) goto err;
202    char *prin=strdup(head_tail[0]);
203    free(tmp);
204    return prin;
205
206err:     
207     free(tmp);
208     return NULL;
209}
210
211int abac_attribute_principal(abac_attribute_t *attr, char *keyid) {
212    char *copy = _validate_principal(keyid);
213    if (copy == NULL)
214        return 0;
215
216    return abac_attribute_add_tail(attr,copy);
217}
218
219int abac_attribute_role(abac_attribute_t *attr, char *keyid, char *role) {
220    if (!abac_clean_name(role))
221        return 0;
222
223    char *copy = _validate_principal(keyid);
224    char *newcopy=NULL;
225    if (copy == NULL)
226        return 0;
227
228    int rc=asprintf(&newcopy,"%s.%s", copy,role);
229    free(copy);
230    return abac_attribute_add_tail(attr, newcopy);
231}
232
233int abac_attribute_linking_role(abac_attribute_t *attr, char *keyid, char *role, char *linked) {
234    if (!abac_clean_name(role) || !abac_clean_name(linked))
235        return 0;
236
237    char *copy = _validate_principal(keyid);
238    if (copy == NULL)
239        return 0;
240
241    char *newcopy=NULL;
242    int rc=asprintf(&newcopy,"%s.%s.%s", copy,role,linked);
243    free(copy);
244    return abac_attribute_add_tail(attr, newcopy);
245}
246
247
248
249// 0 for fail to bake, 1 is baked okay
250int abac_attribute_bake_context(abac_attribute_t *attr, abac_context_t *ctxt) {
251    assert(attr);
252    assert(attr->head_string);
253    assert(attr->tail_strings);
254    abac_keyid_map_t *km = NULL;
255
256    abac_chunk_t id_chunk = { NULL, 0 };
257    int ret=abac_id_PEM(attr->issuer_id, &id_chunk);
258    if(ret != ABAC_CERT_SUCCESS)
259        return 0; 
260
261    if ( ctxt && (km = abac_context_get_keyid_map(ctxt))) {
262        if (attr->keymap) abac_keyid_map_free(attr->keymap);
263        attr->keymap = abac_keyid_map_dup(km);
264    }
265
266    /* Make an new GENI abac credential with the rt0 rule that expires secs
267     * from now.  cert is the PEM encoded X.509 of the issuer's certificate as
268     * a string.  certlen is the length of cert.  Returns the XML. Caller is
269     * responsible for freeing it. */
270    char *attr_cert=make_credential(attr, attr->validity, 
271            (char *)id_chunk.ptr, id_chunk.len);
272
273    /*MEI, free id_chunk */
274    abac_chunk_free(id_chunk);
275
276    if (attr_cert == NULL)
277        return 0;
278
279    attr->cert=abac_chunk_new(attr_cert,strlen(attr_cert));
280
281    return 1;
282}
283// 0 for fail to bake, 1 is baked okay
284int abac_attribute_bake(abac_attribute_t *attr) {
285    return abac_attribute_bake_context(attr, NULL);
286}
287
288/*
289 * caller is responsible to free up the chunk after use
290*/
291abac_chunk_t abac_attribute_cert_chunk(abac_attribute_t *attr) {
292    abac_chunk_t chunk= {NULL,0};
293
294    if (abac_chunk_null(attr->cert))
295        return chunk;
296
297    /* return the xml chunk */
298    chunk= abac_chunk_dup(attr->cert.ptr, attr->cert.len);
299    return chunk;
300}
301
302int abac_attribute_baked(abac_attribute_t *attr) {
303    return (attr->cert.ptr != NULL);
304}
305
306
307static abac_attribute_t *_load_attr(abac_list_t *id_certs,char *rstring, char *xml, abac_keyid_map_t *km)
308{
309    /* make a copy of rle_string */
310    char *role_string=abac_xstrdup(rstring);
311
312    char *head_tail[2];
313    char *role_rest[2];
314    int ret = 2;
315    abac_split(role_string, "<-", head_tail, &ret);
316    if (ret != 2) return NULL; 
317
318    char *keyid=get_keyid_from_xml(xml);
319    abac_id_t *issuer_id=abac_verifier_lookup_id(id_certs,keyid);
320
321    long validity=get_validity_from_xml(xml);
322   
323    abac_attribute_t *attr = abac_xmalloc(sizeof(abac_attribute_t));
324    if(issuer_id)
325        attr->issuer_id = abac_id_dup(issuer_id);
326    else attr->issuer_id=NULL;
327    attr->validity = validity;
328    attr->ntails = 0;
329    attr->tail_strings = NULL;
330
331    /* If there is a keymap, make a reference to it. */
332    if ( km ) attr->keymap = abac_keyid_map_dup(km);
333    else attr->keymap = NULL;
334
335
336    attr->head_string = abac_xstrdup(head_tail[0]);
337    do {
338        ret = 2;
339        abac_split(head_tail[1], " & ", role_rest, &ret);
340        abac_attribute_add_tail(attr, abac_xstrdup(role_rest[0]));
341        head_tail[1] =role_rest[1];
342    } while (ret == 2);
343
344    char *tmp=strstr(attr->head_string,".");
345    attr->role =abac_xstrdup(tmp+1);
346
347    attr->cert=abac_chunk_dup(xml,strlen(xml));
348
349    attr->output_format = NULL;
350
351    free(keyid);
352    free(role_string);
353    return attr;
354}
355
356abac_list_t *abac_attribute_certs_from_file(abac_list_t *id_certs,char *filename)
357{
358    libabac_init();
359    abac_list_t *alist=abac_list_new();
360    char *xml=NULL;
361    char *rt0=NULL;
362    abac_keyid_map_t *km = abac_keyid_map_new();
363
364    char **rt0s=read_credential((void *)id_certs,filename, &xml, km);
365    if(rt0s == NULL) { 
366        abac_keyid_map_free(km);
367        return alist;
368    }
369    if(xml == NULL || strlen(xml)==0) { 
370        abac_keyid_map_free(km);
371        return alist;
372    }
373
374    abac_attribute_t *attr;
375
376    int i=0;
377    do {
378        rt0 = rt0s[i]; 
379        if(rt0 == NULL) break;
380        attr=_load_attr(id_certs,rt0, xml, km);
381        if(attr)
382            abac_list_add(alist, attr);
383        free(rt0);
384        i++;
385    } while (rt0s[i] !=NULL);
386    abac_keyid_map_free(km);
387
388    free(rt0s);
389    free(xml);
390
391    return alist;
392}
393
394abac_list_t *abac_attribute_certs_from_chunk(abac_list_t *id_certs,abac_chunk_t chunk) {
395    libabac_init();
396
397    abac_list_t *alist=abac_list_new();
398    char *xml=(char *)chunk.ptr;
399    abac_keyid_map_t *km = abac_keyid_map_new();
400
401    if(chunk.len==0) return alist;
402
403    char **rt0s=get_rt0_from_xml((void *) id_certs, xml, km);
404    char *rt0=NULL;
405    if(rt0s==NULL) {
406        abac_keyid_map_free(km);
407        return alist;
408    }
409
410    abac_attribute_t *attr;
411    int i=0;
412    do {
413        rt0 = rt0s[i]; 
414        if(rt0 == NULL) break;
415        attr=_load_attr(id_certs,rt0, xml, km);
416        if(attr)
417            abac_list_add(alist, attr);
418        free(rt0);
419        i++;
420    } while (rt0s[i] !=NULL);
421    abac_keyid_map_free(km);
422
423    free(rt0s);
424    return alist;
425}
426
427// returns ABAC_FAILURE if the cert hasn't been baked
428int abac_attribute_write(abac_attribute_t *attr, FILE *out) {
429    assert(attr != NULL);
430
431    if (abac_chunk_null(attr->cert))
432        return ABAC_FAILURE;
433
434    // write to file
435    fwrite(attr->cert.ptr, attr->cert.len, 1, out);
436
437    return ABAC_SUCCESS;
438}
439
440// returns ABAC_FAILURE if the cert hasn't been baked
441int abac_attribute_write_file(abac_attribute_t *attr, const char *fname) {
442    if (abac_chunk_null(attr->cert))
443        return ABAC_FAILURE;
444
445    FILE *fp=fopen(fname,"w+");
446    if(fp) {
447         // write to file
448         fwrite(attr->cert.ptr, attr->cert.len, 1, fp);
449    } else return ABAC_FAILURE;
450    fclose(fp);
451
452    return ABAC_SUCCESS;
453}
454
455/* return a copy of the local name mappings, if any.  The returned value is not
456 * reference counted, so callers will need to call abac_keyid_map_dup on it if
457 * they need to keep a copy of the pointer.
458 */
459abac_keyid_map_t *abac_attribute_get_keyid_map(abac_attribute_t *attr) {
460    return attr->keymap;
461}
462
463
464void abac_attribute_free(abac_attribute_t *attr) {
465
466    int i = 0;
467
468    if (attr == NULL)
469        return;
470
471    if(attr->issuer_id) abac_id_free(attr->issuer_id);
472
473    free(attr->role);
474    free(attr->head_string);
475    for (i=0; i < attr->ntails; i++) 
476        free(attr->tail_strings[i]);
477    free(attr->tail_strings);
478    if ( attr->output_format ) 
479        free(attr->output_format);
480
481    if ( attr->keymap ) abac_keyid_map_free(attr->keymap);
482
483    abac_chunk_free(attr->cert);
484
485    free(attr);
486}
487
488//
489// Helper functions below
490//
491
492// validate a princpal's name
493// makes sure it's a valid SHA1 identifier
494// return values:
495//  success: malloc'd copy with all hex digits lowercase
496//  fail: NULL
497static char *_validate_principal(char *keyid) {
498    int i;
499    char *copy = NULL;
500
501    if (strlen(keyid) != SHA1_LENGTH)
502        return NULL;
503
504    copy = abac_xstrdup(keyid);
505    for (i = 0; i < SHA1_LENGTH; ++i) {
506        copy[i] = tolower(copy[i]);
507        if (!isxdigit(copy[i]))
508            goto error;
509    }
510
511    return copy;
512
513error:
514    free(copy);
515    return NULL;
516}
517
518static int abac_attribute_role_string_size(abac_attribute_t *attr) {
519    int sz = 3; /* Start with the end of string character and <-*/
520    int i;      /* Scratch */
521
522    if ( !attr) return sz;
523    if ( attr->head_string) 
524        sz += strlen(attr->head_string);
525    for (i = 0; i < attr->ntails; i++) 
526        if ( attr->tail_strings[i]) 
527            sz += strlen(attr->tail_strings[i]);
528    /* " & " between each pair of tails */
529    sz += 3 * (attr->ntails-1);
530    return sz;
531}
532
533// combine up the attribute's rule string, explicit copy
534char *abac_attribute_role_string(abac_attribute_t *attr) {
535    assert(attr);
536
537    int sz = abac_attribute_role_string_size(attr);
538    char *role_string=abac_xmalloc(sz);
539    int i;
540
541    if ( !role_string) return NULL;
542
543    sz -= snprintf(role_string, sz, "%s<-", attr->head_string);
544    for ( i = 0 ; i < attr->ntails; i++ ) {
545        if ( i > 0 ) {
546            strncat(role_string, " & ", 3);
547            sz -= 3;
548        }
549        strncat(role_string,attr->tail_strings[i], sz); 
550
551        sz -= strlen(attr->tail_strings[i]);
552        if (sz < 0 ) return NULL;
553    }
554    return role_string;
555}
Note: See TracBrowser for help on using the repository browser.