1 | /** |
---|
2 | ** abac_m64.c |
---|
3 | **/ |
---|
4 | |
---|
5 | #include <stdio.h> |
---|
6 | #include <stdlib.h> |
---|
7 | #include <string.h> |
---|
8 | #include <assert.h> |
---|
9 | #include <err.h> |
---|
10 | |
---|
11 | #include <credentials/certificates/certificate.h> |
---|
12 | |
---|
13 | #include "zlib.h" |
---|
14 | #define CHUNK 1024 /* 16384 */ |
---|
15 | |
---|
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); |
---|
25 | */ |
---|
26 | |
---|
27 | static int _compress(char *string, int slen, char*ostring) |
---|
28 | { |
---|
29 | unsigned long olen=CHUNK; |
---|
30 | unsigned long ilen=(unsigned long) slen; |
---|
31 | int rc=compress(ostring,&olen,string,ilen); |
---|
32 | if(rc==Z_OK) { |
---|
33 | if(debug) fprintf(stderr,"compress from %ld to %ld\n", ilen, olen); |
---|
34 | return (int)olen; |
---|
35 | } |
---|
36 | return 0; |
---|
37 | } |
---|
38 | |
---|
39 | /* all of these are deep copy of a string */ |
---|
40 | char* abac_encode_string(char *string) |
---|
41 | { |
---|
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 */ |
---|
50 | if(debug) fprintf(stderr,"ERROR, can not compress the string!!\n"); |
---|
51 | errx(1, "ERROR, compress failed!!"); |
---|
52 | } else { |
---|
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 | } |
---|
64 | |
---|
65 | static int _uncompress(char *string, int slen, char *ostring) |
---|
66 | { |
---|
67 | unsigned long olen=CHUNK; |
---|
68 | unsigned long ilen=(unsigned long) slen; |
---|
69 | int rc=uncompress(ostring,&olen,string,ilen); |
---|
70 | if(rc==Z_OK) { |
---|
71 | if(debug) fprintf(stderr,"uncompress from %ld to %ld\n", ilen, olen); |
---|
72 | return (int)olen; |
---|
73 | } |
---|
74 | return 0; |
---|
75 | } |
---|
76 | |
---|
77 | char* abac_decode_string(char *string) |
---|
78 | { |
---|
79 | assert(string); |
---|
80 | |
---|
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!! */ |
---|
92 | if(debug) fprintf(stderr,"ERROR, can not uncompress the string!!\n"); |
---|
93 | errx(1, "ERROR, uncompress failed!!"); |
---|
94 | } else ret=strndup(ostring,olen); |
---|
95 | } else { |
---|
96 | ret=strndup(chk.ptr, chk.len); |
---|
97 | } |
---|
98 | chunk_free(&chk); |
---|
99 | return ret; |
---|
100 | } |
---|
101 | |
---|