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