source: libabac/abac_m64.c @ 2e9455f

mei_rt2
Last change on this file since 2e9455f was 2e9455f, checked in by Mei <mei@…>, 11 years ago

1) added namespace
2) tweak ?This,
3) allowing linking role/oset as constraining conditions
4) adding access_tests regression testing that uses GENI's access policy
5) added couple multi contexts regression tests
6) add compression/uncompression calls to abac_encode_string/abac_decode_string
(libstrongwan only allows 512 char for attribute rule storage)
7) add attribute_now option to creddy that takes a whole char string for attribute
rule

  • Property mode set to 100644
File size: 2.2 KB
Line 
1/**
2**  abac_m64.c
3**/
4
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <assert.h>
9
10#include <credentials/certificates/certificate.h>
11
12#include "zlib.h"
13#define CHUNK 1024 /* 16384 */
14
15static int debug=0;
16static int zip=1; /* to choose to compress/decompress */
17
18/*
19using strongswan's m64 utility so we can stay within the
20buf len size that strongwan set in using its certifiate
21generation code (512)
22    chunk_t b64 = chunk_to_base64(chunk, NULL);
23    chunk_t chk = chunk_from_base64(base64, NULL);
24*/
25
26static int _compress(char *string, int ilen, char*ostring)
27{
28    int olen=CHUNK;
29    int rc=compress(ostring,&olen,string,ilen); 
30    if(rc==Z_OK) { 
31        if(debug) fprintf(stderr,"compress from %d to %d\n", ilen, olen);
32        return olen;
33    }
34    return 0;
35}
36
37/* all of these are deep copy of a string */
38char* abac_encode_string(char *string)
39{
40    assert(string);
41    char *ret=NULL;
42    char *ptr=string;;
43    int len=strlen(ptr);
44    char ostring[CHUNK];
45    if(zip) {
46        int olen=_compress(ptr, len,  ostring);
47        if(olen==0) { /* can not do the compress */
48           return NULL;
49        } else {
50           ptr=ostring;
51           len=olen;
52        }
53    } 
54    chunk_t orig = { ptr, len };
55    chunk_t b64 = chunk_to_base64(orig, NULL);
56    if(debug) fprintf(stderr,"abac_encode_string: (%s)\n",b64.ptr);
57    ret=strndup(b64.ptr,b64.len);
58    chunk_free(&b64);
59    return ret;
60}
61
62static int _uncompress(char *string, int ilen, char *ostring)
63{
64    int olen=CHUNK;
65    int rc=uncompress(ostring,&olen,string,ilen); 
66    if(rc==Z_OK) { 
67        if(debug) fprintf(stderr,"uncompress from %d to %d\n", ilen, olen);
68        return olen;
69    }
70    return 0;
71}
72
73char* abac_decode_string(char *string)
74{
75    assert(string);
76
77    char *ret=NULL;
78    int len=strlen(string);
79    chunk_t b64 = { string, len };
80    chunk_t chk= chunk_from_base64(b64, NULL);
81
82    if(debug) fprintf(stderr,"abac_decode_string: (%s)\n",chk.ptr);
83
84    if(zip) {
85        char ostring[CHUNK];
86        int olen=_uncompress(chk.ptr, chk.len, ostring);
87        if(olen==0) { /* unable to uncompress the string!! */
88            ret=NULL;
89        } else ret=strndup(ostring,olen);
90    } else {
91        ret=strndup(chk.ptr, chk.len);
92    }
93    chunk_free(&chk);
94    return ret;
95}
96
Note: See TracBrowser for help on using the repository browser.