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 ilen, char*ostring) |
---|
28 | { |
---|
29 | int olen=CHUNK; |
---|
30 | int rc=compress(ostring,&olen,string,ilen); |
---|
31 | if(rc==Z_OK) { |
---|
32 | if(debug) fprintf(stderr,"compress from %d to %d\n", ilen, olen); |
---|
33 | return olen; |
---|
34 | } |
---|
35 | return 0; |
---|
36 | } |
---|
37 | |
---|
38 | /* all of these are deep copy of a string */ |
---|
39 | char* abac_encode_string(char *string) |
---|
40 | { |
---|
41 | assert(string); |
---|
42 | char *ret=NULL; |
---|
43 | char *ptr=string;; |
---|
44 | int len=strlen(ptr); |
---|
45 | char ostring[CHUNK]; |
---|
46 | if(zip) { |
---|
47 | int olen=_compress(ptr, len, ostring); |
---|
48 | if(olen==0) { /* can not do the compress */ |
---|
49 | if(debug) fprintf(stderr,"ERROR, can not compress the string!!\n"); |
---|
50 | errx(1, "ERROR, compress failed!!"); |
---|
51 | } else { |
---|
52 | ptr=ostring; |
---|
53 | len=olen; |
---|
54 | } |
---|
55 | } |
---|
56 | chunk_t orig = { ptr, len }; |
---|
57 | chunk_t b64 = chunk_to_base64(orig, NULL); |
---|
58 | if(debug) fprintf(stderr,"abac_encode_string: (%s)\n",b64.ptr); |
---|
59 | ret=strndup(b64.ptr,b64.len); |
---|
60 | chunk_free(&b64); |
---|
61 | return ret; |
---|
62 | } |
---|
63 | |
---|
64 | static int _uncompress(char *string, int ilen, char *ostring) |
---|
65 | { |
---|
66 | int olen=CHUNK; |
---|
67 | int rc=uncompress(ostring,&olen,string,ilen); |
---|
68 | if(rc==Z_OK) { |
---|
69 | if(debug) fprintf(stderr,"uncompress from %d to %d\n", ilen, olen); |
---|
70 | return olen; |
---|
71 | } |
---|
72 | return 0; |
---|
73 | } |
---|
74 | |
---|
75 | char* abac_decode_string(char *string) |
---|
76 | { |
---|
77 | assert(string); |
---|
78 | |
---|
79 | char *ret=NULL; |
---|
80 | int len=strlen(string); |
---|
81 | chunk_t b64 = { string, len }; |
---|
82 | chunk_t chk= chunk_from_base64(b64, NULL); |
---|
83 | |
---|
84 | if(debug) fprintf(stderr,"abac_decode_string: (%s)\n",chk.ptr); |
---|
85 | |
---|
86 | if(zip) { |
---|
87 | char ostring[CHUNK]; |
---|
88 | int olen=_uncompress(chk.ptr, chk.len, ostring); |
---|
89 | if(olen==0) { /* unable to uncompress the string!! */ |
---|
90 | if(debug) fprintf(stderr,"ERROR, can not uncompress the string!!\n"); |
---|
91 | errx(1, "ERROR, uncompress failed!!"); |
---|
92 | } else ret=strndup(ostring,olen); |
---|
93 | } else { |
---|
94 | ret=strndup(chk.ptr, chk.len); |
---|
95 | } |
---|
96 | chunk_free(&chk); |
---|
97 | return ret; |
---|
98 | } |
---|
99 | |
---|