source: libabac/abac_attribute.c

Last change on this file was 99e5bfd, checked in by Ted Faber <faber@…>, 9 years ago

Defensive driving

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