a478648f8a2d6e09e9babc0ab810237911e9795d
[profile/ivi/eet.git] / src / bin / eet_main.c
1 /*
2  * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
3  */
4
5 #ifdef HAVE_CONFIG_H
6 # include <config.h>
7 #endif
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <string.h>
13
14 #ifdef HAVE_EVIL
15 # include <Evil.h>
16 #endif
17
18 #include <Eet.h>
19
20 static void
21 do_eet_list(const char *file)
22 {
23    int i, num;
24    char **list;
25    Eet_File *ef;
26
27    ef = eet_open(file, EET_FILE_MODE_READ);
28    if (!ef)
29      {
30         printf("cannot open for reading: %s\n", file);
31         exit(-1);
32      }
33    list = eet_list(ef, "*", &num);
34    if (list)
35      {
36         for (i = 0; i < num; i++)
37           printf("%s\n",list[i]);
38         free(list);
39      }
40    eet_close(ef);
41 }
42
43 static void
44 do_eet_extract(const char *file, const char *key, const char *out, const char *crypto_key)
45 {
46    Eet_File *ef;
47    void *data;
48    int size = 0;
49    FILE *f;
50
51    ef = eet_open(file, EET_FILE_MODE_READ);
52    if (!ef)
53      {
54         printf("cannot open for reading: %s\n", file);
55         exit(-1);
56      }
57    data = eet_read_cipher(ef, key, &size, crypto_key);
58    if (!data)
59      {
60         printf("cannot read key %s\n", key);
61         exit(-1);
62      }
63    f = fopen(out, "w");
64    if (!f)
65      {
66         printf("cannot open %s\n", out);
67         exit(-1);
68      }
69    if (fwrite(data, size, 1, f) != 1)
70      {
71         printf("cannot write to %s\n", out);
72         exit(-1);
73      }
74    fclose(f);
75    free(data);
76    eet_close(ef);
77 }
78
79 static void
80 do_eet_decode_dump(void *data, const char *str)
81 {
82    fputs(str, (FILE *)data);
83 }
84
85 static void
86 do_eet_decode(const char *file, const char *key, const char *out, const char *crypto_key)
87 {
88    Eet_File *ef;
89    FILE *f;
90
91    ef = eet_open(file, EET_FILE_MODE_READ);
92    if (!ef)
93      {
94         printf("cannot open for reading: %s\n", file);
95         exit(-1);
96      }
97    f = fopen(out, "w");
98    if (!f)
99      {
100         printf("cannot open %s\n", out);
101         exit(-1);
102      }
103    if (!eet_data_dump_cipher(ef, key, crypto_key, do_eet_decode_dump, f))
104      {
105         printf("cannot write to %s\n", out);
106         exit(-1);
107      }
108    fclose(f);
109    eet_close(ef);
110 }
111
112 static void
113 do_eet_insert(const char *file, const char *key, const char *out, int compress, const char *crypto_key)
114 {
115    Eet_File *ef;
116    void *data;
117    int size = 0;
118    FILE *f;
119
120    ef = eet_open(file, EET_FILE_MODE_READ_WRITE);
121    if (!ef)
122      ef = eet_open(file, EET_FILE_MODE_WRITE);
123    if (!ef)
124      {
125         printf("cannot open for read+write: %s\n", file);
126         exit(-1);
127      }
128    f = fopen(out, "r");
129    if (!f)
130      {
131         printf("cannot open %s\n", out);
132         exit(-1);
133      }
134    fseek(f, 0, SEEK_END);
135    size = ftell(f);
136    rewind(f);
137    data = malloc(size);
138    if (!data)
139      {
140         printf("cannot allocate %i bytes\n", size);
141         exit(-1);
142      }
143    if (fread(data, size, 1, f) != 1)
144      {
145         printf("cannot read file %s\n", out);
146         exit(-1);
147      }
148    fclose(f);
149    eet_write_cipher(ef, key, data, size, compress, crypto_key);
150    free(data);
151    eet_close(ef);
152 }
153
154 static void
155 do_eet_encode(const char *file, const char *key, const char *out, int compress, const char *crypto_key)
156 {
157    Eet_File *ef;
158    char *text;
159    int textlen = 0;
160    int size = 0;
161    FILE *f;
162
163    ef = eet_open(file, EET_FILE_MODE_READ_WRITE);
164    if (!ef)
165      ef = eet_open(file, EET_FILE_MODE_WRITE);
166    if (!ef)
167      {
168         printf("cannot open for read+write: %s\n", file);
169         exit(-1);
170      }
171    f = fopen(out, "r");
172    if (!f)
173      {
174         printf("cannot open %s\n", out);
175         exit(-1);
176      }
177    fseek(f, 0, SEEK_END);
178    textlen = ftell(f);
179    rewind(f);
180    text = malloc(textlen);
181    if (!text)
182      {
183         printf("cannot allocate %i bytes\n", size);
184         exit(-1);
185      }
186    if (fread(text, textlen, 1, f) != 1)
187      {
188         printf("cannot read file %s\n", out);
189         exit(-1);
190      }
191    fclose(f);
192    if (!eet_data_undump_cipher(ef, key, crypto_key, text, textlen, compress))
193      {
194         printf("cannot parse %s\n", out);
195         exit(-1);
196      }
197    free(text);
198    eet_close(ef);
199 }
200
201 static void
202 do_eet_remove(const char *file, const char *key)
203 {
204    Eet_File *ef;
205
206    ef = eet_open(file, EET_FILE_MODE_READ_WRITE);
207    if (!ef)
208      {
209         printf("cannot open for read+write: %s\n", file);
210         exit(-1);
211      }
212    eet_delete(ef, key);
213    eet_close(ef);
214 }
215
216 static void
217 do_eet_check(const char *file)
218 {
219    Eet_File *ef;
220    const void *der;
221    int der_length;
222    int sign_length;
223
224    ef = eet_open(file, EET_FILE_MODE_READ);
225    if (!ef)
226      {
227         fprintf(stdout, "checking signature of `%s` failed\n", file);
228         exit(-1);
229      }
230
231    der = eet_identity_x509(ef, &der_length);
232
233    fprintf(stderr, "Certificate length %i.\n", der_length);
234    eet_identity_certificate_print(der, der_length, stdout);
235
236    eet_identity_signature(ef, &sign_length);
237    fprintf(stderr, "Signature length %i.\n", sign_length);
238
239    eet_close(ef);
240 }
241
242 static void
243 do_eet_sign(const char *file, const char *private_key, const char *public_key)
244 {
245    Eet_File *ef;
246    Eet_Key *key;
247
248    ef = eet_open(file, EET_FILE_MODE_READ_WRITE);
249    if (!ef)
250      {
251         fprintf(stdout, "cannot open for read+write: %s.\n", file);
252         exit(-1);
253      }
254
255    key = eet_identity_open(public_key, private_key, NULL);
256    if (!key)
257      {
258         fprintf(stdout, "cannot open key '%s:%s'.\n", public_key, private_key);
259         exit(-1);
260      }
261
262    fprintf(stdout, "Using the following key to sign `%s`.\n", file);
263    eet_identity_print(key, stdout);
264
265    eet_identity_set(ef, key);
266
267    eet_close(ef);
268 }
269
270 int
271 main(int argc, char **argv)
272 {
273    eet_init();
274    if (argc < 2)
275      {
276         help:
277         printf("Usage:\n"
278                "  eet -l FILE.EET                                    list all keys in FILE.EET\n"
279                "  eet -x FILE.EET KEY OUT-FILE [CRYPTO_KEY]          extract data stored in KEY in FILE.EET and write to OUT-FILE\n"
280                "  eet -d FILE.EET KEY OUT-FILE [CRYPTO_KEY]          extract and decode data stored in KEY in FILE.EET and write to OUT-FILE\n"
281                "  eet -i FILE.EET KEY IN-FILE COMPRESS [CRYPTO_KEY]  insert data to KEY in FILE.EET from IN-FILE and if COMPRESS is 1, compress it\n"
282                "  eet -e FILE.EET KEY IN-FILE COMPRESS [CRYPTO_KEY]  insert and encode to KEY in FILE.EET from IN-FILE and if COMPRESS is 1, compress it\n"
283                "  eet -r FILE.EET KEY                                remove KEY in FILE.EET\n"
284                "  eet -c FILE.EET                                    report and check the signature information of an eet file\n"
285                "  eet -s FILE.EET PRIVATE_KEY PUBLIC_KEY             sign FILE.EET with PRIVATE_KEY and attach PUBLIC_KEY as it's certificate\n"
286                );
287         eet_shutdown();
288         return -1;
289      }
290    if ((!strncmp(argv[1], "-h", 2)))
291      {
292         goto help;
293      }
294    else if ((!strcmp(argv[1], "-l")) && (argc > 2))
295      {
296         do_eet_list(argv[2]);
297      }
298    else if ((!strcmp(argv[1], "-x")) && (argc > 4))
299      {
300         if (argc > 5)
301           do_eet_extract(argv[2], argv[3], argv[4], argv[5]);
302         else
303           do_eet_extract(argv[2], argv[3], argv[4], NULL);
304      }
305    else if ((!strcmp(argv[1], "-d")) && (argc > 4))
306      {
307         if (argc > 5)
308          do_eet_decode(argv[2], argv[3], argv[4], argv[5]);
309         else
310          do_eet_decode(argv[2], argv[3], argv[4], NULL);
311      }
312    else if ((!strcmp(argv[1], "-i")) && (argc > 5))
313      {
314         if (argc > 6)
315           do_eet_insert(argv[2], argv[3], argv[4], atoi(argv[5]), argv[6]);
316         else
317           do_eet_insert(argv[2], argv[3], argv[4], atoi(argv[5]), NULL);
318      }
319    else if ((!strcmp(argv[1], "-e")) && (argc > 5))
320      {
321         if (argc > 6)
322           do_eet_encode(argv[2], argv[3], argv[4], atoi(argv[5]), argv[6]);
323         else
324           do_eet_encode(argv[2], argv[3], argv[4], atoi(argv[5]), NULL);
325      }
326    else if ((!strcmp(argv[1], "-r")) && (argc > 3))
327      {
328         do_eet_remove(argv[2], argv[3]);
329      }
330    else if ((!strcmp(argv[1], "-c")) && (argc > 2))
331      {
332         do_eet_check(argv[2]);
333      }
334    else if ((!strcmp(argv[1], "-s")) && (argc > 4))
335      {
336         do_eet_sign(argv[2], argv[3], argv[4]);
337      }
338    else
339      {
340         goto help;
341      }
342    eet_shutdown();
343    return 0;
344 }