diff options
Diffstat (limited to '')
-rw-r--r-- | libraries/eet/src/bin/eet_main.c | 413 |
1 files changed, 413 insertions, 0 deletions
diff --git a/libraries/eet/src/bin/eet_main.c b/libraries/eet/src/bin/eet_main.c new file mode 100644 index 0000000..7ab5555 --- /dev/null +++ b/libraries/eet/src/bin/eet_main.c | |||
@@ -0,0 +1,413 @@ | |||
1 | #ifdef HAVE_CONFIG_H | ||
2 | # include <config.h> | ||
3 | #endif /* ifdef HAVE_CONFIG_H */ | ||
4 | |||
5 | #include <stdio.h> | ||
6 | #include <stdlib.h> | ||
7 | #include <string.h> | ||
8 | |||
9 | #ifdef HAVE_UNISTD_H | ||
10 | # include <unistd.h> | ||
11 | #endif /* ifdef HAVE_UNISTD_H */ | ||
12 | |||
13 | #ifdef HAVE_EVIL | ||
14 | # include <Evil.h> | ||
15 | #endif /* ifdef HAVE_EVIL */ | ||
16 | |||
17 | #include <Eet.h> | ||
18 | |||
19 | static int _eet_main_log_dom = -1; | ||
20 | |||
21 | #ifdef EET_DEFAULT_LOG_COLOR | ||
22 | #undef EET_DEFAULT_LOG_COLOR | ||
23 | #endif /* ifdef EET_DEFAULT_LOG_COLOR */ | ||
24 | #define EET_DEFAULT_LOG_COLOR EINA_COLOR_CYAN | ||
25 | #ifdef ERR | ||
26 | #undef ERR | ||
27 | #endif /* ifdef ERR */ | ||
28 | #define ERR(...) EINA_LOG_DOM_ERR(_eet_main_log_dom, __VA_ARGS__) | ||
29 | #ifdef DBG | ||
30 | #undef DBG | ||
31 | #endif /* ifdef DBG */ | ||
32 | #define DBG(...) EINA_LOG_DOM_DBG(_eet_main_log_dom, __VA_ARGS__) | ||
33 | #ifdef INF | ||
34 | #undef INF | ||
35 | #endif /* ifdef INF */ | ||
36 | #define INF(...) EINA_LOG_DOM_INFO(_eet_main_log_dom, __VA_ARGS__) | ||
37 | #ifdef WRN | ||
38 | #undef WRN | ||
39 | #endif /* ifdef WRN */ | ||
40 | #define WRN(...) EINA_LOG_DOM_WARN(_eet_main_log_dom, __VA_ARGS__) | ||
41 | #ifdef CRIT | ||
42 | #undef CRIT | ||
43 | #endif /* ifdef CRIT */ | ||
44 | #define CRIT(...) EINA_LOG_DOM_CRIT(_eet_main_log_dom, __VA_ARGS__) | ||
45 | |||
46 | static void | ||
47 | do_eet_list(const char *file) | ||
48 | { | ||
49 | int i, num; | ||
50 | char **list; | ||
51 | Eet_File *ef; | ||
52 | |||
53 | ef = eet_open(file, EET_FILE_MODE_READ); | ||
54 | if (!ef) | ||
55 | { | ||
56 | ERR("cannot open for reading: %s", file); | ||
57 | exit(-1); | ||
58 | } | ||
59 | |||
60 | list = eet_list(ef, "*", &num); | ||
61 | if (list) | ||
62 | { | ||
63 | for (i = 0; i < num; i++) | ||
64 | printf("%s\n", list[i]); | ||
65 | free(list); | ||
66 | } | ||
67 | |||
68 | eet_close(ef); | ||
69 | } /* do_eet_list */ | ||
70 | |||
71 | static void | ||
72 | do_eet_extract(const char *file, | ||
73 | const char *key, | ||
74 | const char *out, | ||
75 | const char *crypto_key) | ||
76 | { | ||
77 | Eet_File *ef; | ||
78 | void *data; | ||
79 | int size = 0; | ||
80 | FILE *f; | ||
81 | |||
82 | ef = eet_open(file, EET_FILE_MODE_READ); | ||
83 | if (!ef) | ||
84 | { | ||
85 | ERR("cannot open for reading: %s", file); | ||
86 | exit(-1); | ||
87 | } | ||
88 | |||
89 | data = eet_read_cipher(ef, key, &size, crypto_key); | ||
90 | if (!data) | ||
91 | { | ||
92 | ERR("cannot read key %s", key); | ||
93 | exit(-1); | ||
94 | } | ||
95 | |||
96 | f = fopen(out, "wb"); | ||
97 | if (!f) | ||
98 | { | ||
99 | ERR("cannot open %s", out); | ||
100 | exit(-1); | ||
101 | } | ||
102 | |||
103 | if (fwrite(data, size, 1, f) != 1) | ||
104 | { | ||
105 | ERR("cannot write to %s", out); | ||
106 | exit(-1); | ||
107 | } | ||
108 | |||
109 | fclose(f); | ||
110 | free(data); | ||
111 | eet_close(ef); | ||
112 | } /* do_eet_extract */ | ||
113 | |||
114 | static void | ||
115 | do_eet_decode_dump(void *data, | ||
116 | const char *str) | ||
117 | { | ||
118 | fputs(str, (FILE *)data); | ||
119 | } /* do_eet_decode_dump */ | ||
120 | |||
121 | static void | ||
122 | do_eet_decode(const char *file, | ||
123 | const char *key, | ||
124 | const char *out, | ||
125 | const char *crypto_key) | ||
126 | { | ||
127 | Eet_File *ef; | ||
128 | FILE *f; | ||
129 | |||
130 | ef = eet_open(file, EET_FILE_MODE_READ); | ||
131 | if (!ef) | ||
132 | { | ||
133 | ERR("cannot open for reading: %s", file); | ||
134 | exit(-1); | ||
135 | } | ||
136 | |||
137 | f = fopen(out, "wb"); | ||
138 | if (!f) | ||
139 | { | ||
140 | ERR("cannot open %s", out); | ||
141 | exit(-1); | ||
142 | } | ||
143 | |||
144 | if (!eet_data_dump_cipher(ef, key, crypto_key, do_eet_decode_dump, f)) | ||
145 | { | ||
146 | ERR("cannot write to %s", out); | ||
147 | exit(-1); | ||
148 | } | ||
149 | |||
150 | fclose(f); | ||
151 | eet_close(ef); | ||
152 | } /* do_eet_decode */ | ||
153 | |||
154 | static void | ||
155 | do_eet_insert(const char *file, | ||
156 | const char *key, | ||
157 | const char *out, | ||
158 | int compress, | ||
159 | const char *crypto_key) | ||
160 | { | ||
161 | Eet_File *ef; | ||
162 | void *data; | ||
163 | int size = 0; | ||
164 | FILE *f; | ||
165 | |||
166 | ef = eet_open(file, EET_FILE_MODE_READ_WRITE); | ||
167 | if (!ef) | ||
168 | ef = eet_open(file, EET_FILE_MODE_WRITE); | ||
169 | |||
170 | if (!ef) | ||
171 | { | ||
172 | ERR("cannot open for read+write: %s", file); | ||
173 | exit(-1); | ||
174 | } | ||
175 | |||
176 | f = fopen(out, "rb"); | ||
177 | if (!f) | ||
178 | { | ||
179 | ERR("cannot open %s", out); | ||
180 | exit(-1); | ||
181 | } | ||
182 | |||
183 | fseek(f, 0, SEEK_END); | ||
184 | size = ftell(f); | ||
185 | rewind(f); | ||
186 | data = malloc(size); | ||
187 | if (!data) | ||
188 | { | ||
189 | ERR("cannot allocate %i bytes", size); | ||
190 | exit(-1); | ||
191 | } | ||
192 | |||
193 | if (fread(data, size, 1, f) != 1) | ||
194 | { | ||
195 | ERR("cannot read file %s", out); | ||
196 | exit(-1); | ||
197 | } | ||
198 | |||
199 | fclose(f); | ||
200 | eet_write_cipher(ef, key, data, size, compress, crypto_key); | ||
201 | free(data); | ||
202 | eet_close(ef); | ||
203 | } /* do_eet_insert */ | ||
204 | |||
205 | static void | ||
206 | do_eet_encode(const char *file, | ||
207 | const char *key, | ||
208 | const char *out, | ||
209 | int compress, | ||
210 | const char *crypto_key) | ||
211 | { | ||
212 | Eet_File *ef; | ||
213 | char *text; | ||
214 | int textlen = 0; | ||
215 | int size = 0; | ||
216 | FILE *f; | ||
217 | |||
218 | ef = eet_open(file, EET_FILE_MODE_READ_WRITE); | ||
219 | if (!ef) | ||
220 | ef = eet_open(file, EET_FILE_MODE_WRITE); | ||
221 | |||
222 | if (!ef) | ||
223 | { | ||
224 | ERR("cannot open for read+write: %s", file); | ||
225 | exit(-1); | ||
226 | } | ||
227 | |||
228 | f = fopen(out, "rb"); | ||
229 | if (!f) | ||
230 | { | ||
231 | ERR("cannot open %s", out); | ||
232 | exit(-1); | ||
233 | } | ||
234 | |||
235 | fseek(f, 0, SEEK_END); | ||
236 | textlen = ftell(f); | ||
237 | rewind(f); | ||
238 | text = malloc(textlen); | ||
239 | if (!text) | ||
240 | { | ||
241 | ERR("cannot allocate %i bytes", size); | ||
242 | exit(-1); | ||
243 | } | ||
244 | |||
245 | if (fread(text, textlen, 1, f) != 1) | ||
246 | { | ||
247 | ERR("cannot read file %s", out); | ||
248 | exit(-1); | ||
249 | } | ||
250 | |||
251 | fclose(f); | ||
252 | if (!eet_data_undump_cipher(ef, key, crypto_key, text, textlen, compress)) | ||
253 | { | ||
254 | ERR("cannot parse %s", out); | ||
255 | exit(-1); | ||
256 | } | ||
257 | |||
258 | free(text); | ||
259 | eet_close(ef); | ||
260 | } /* do_eet_encode */ | ||
261 | |||
262 | static void | ||
263 | do_eet_remove(const char *file, | ||
264 | const char *key) | ||
265 | { | ||
266 | Eet_File *ef; | ||
267 | |||
268 | ef = eet_open(file, EET_FILE_MODE_READ_WRITE); | ||
269 | if (!ef) | ||
270 | { | ||
271 | ERR("cannot open for read+write: %s", file); | ||
272 | exit(-1); | ||
273 | } | ||
274 | |||
275 | eet_delete(ef, key); | ||
276 | eet_close(ef); | ||
277 | } /* do_eet_remove */ | ||
278 | |||
279 | static void | ||
280 | do_eet_check(const char *file) | ||
281 | { | ||
282 | Eet_File *ef; | ||
283 | const void *der; | ||
284 | int der_length; | ||
285 | int sign_length; | ||
286 | |||
287 | ef = eet_open(file, EET_FILE_MODE_READ); | ||
288 | if (!ef) | ||
289 | { | ||
290 | ERR("checking signature of `%s` failed", file); | ||
291 | exit(-1); | ||
292 | } | ||
293 | |||
294 | der = eet_identity_x509(ef, &der_length); | ||
295 | |||
296 | fprintf(stdout, "Certificate length %i.\n", der_length); | ||
297 | eet_identity_certificate_print(der, der_length, stdout); | ||
298 | |||
299 | eet_identity_signature(ef, &sign_length); | ||
300 | fprintf(stdout, "Signature length %i.\n", sign_length); | ||
301 | |||
302 | eet_close(ef); | ||
303 | } /* do_eet_check */ | ||
304 | |||
305 | static void | ||
306 | do_eet_sign(const char *file, | ||
307 | const char *private_key, | ||
308 | const char *public_key) | ||
309 | { | ||
310 | Eet_File *ef; | ||
311 | Eet_Key *key; | ||
312 | |||
313 | ef = eet_open(file, EET_FILE_MODE_READ_WRITE); | ||
314 | if (!ef) | ||
315 | { | ||
316 | ERR("cannot open for read+write: %s.", file); | ||
317 | exit(-1); | ||
318 | } | ||
319 | |||
320 | key = eet_identity_open(public_key, private_key, NULL); | ||
321 | if (!key) | ||
322 | { | ||
323 | ERR("cannot open key '%s:%s'.", public_key, private_key); | ||
324 | exit(-1); | ||
325 | } | ||
326 | |||
327 | fprintf(stdout, "Using the following key to sign `%s`.\n", file); | ||
328 | eet_identity_print(key, stdout); | ||
329 | |||
330 | eet_identity_set(ef, key); | ||
331 | |||
332 | eet_close(ef); | ||
333 | } /* do_eet_sign */ | ||
334 | |||
335 | int | ||
336 | main(int argc, | ||
337 | char **argv) | ||
338 | { | ||
339 | if (!eet_init()) | ||
340 | return -1; | ||
341 | |||
342 | _eet_main_log_dom = eina_log_domain_register("eet_main", EINA_COLOR_CYAN); | ||
343 | if(_eet_main_log_dom < -1) | ||
344 | { | ||
345 | EINA_LOG_ERR("Impossible to create a log domain for eet_main."); | ||
346 | eet_shutdown(); | ||
347 | return -1; | ||
348 | } | ||
349 | |||
350 | if (argc < 2) | ||
351 | { | ||
352 | help: | ||
353 | printf( | ||
354 | "Usage:\n" | ||
355 | " eet -l FILE.EET list all keys in FILE.EET\n" | ||
356 | " eet -x FILE.EET KEY OUT-FILE [CRYPTO_KEY] extract data stored in KEY in FILE.EET and write to OUT-FILE\n" | ||
357 | " 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" | ||
358 | " 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" | ||
359 | " 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" | ||
360 | " eet -r FILE.EET KEY remove KEY in FILE.EET\n" | ||
361 | " eet -c FILE.EET report and check the signature information of an eet file\n" | ||
362 | " eet -s FILE.EET PRIVATE_KEY PUBLIC_KEY sign FILE.EET with PRIVATE_KEY and attach PUBLIC_KEY as it's certificate\n" | ||
363 | ); | ||
364 | eet_shutdown(); | ||
365 | return -1; | ||
366 | } | ||
367 | |||
368 | if ((!strncmp(argv[1], "-h", 2))) | ||
369 | goto help; | ||
370 | else if ((!strcmp(argv[1], "-l")) && (argc > 2)) | ||
371 | do_eet_list(argv[2]); | ||
372 | else if ((!strcmp(argv[1], "-x")) && (argc > 4)) | ||
373 | { | ||
374 | if (argc > 5) | ||
375 | do_eet_extract(argv[2], argv[3], argv[4], argv[5]); | ||
376 | else | ||
377 | do_eet_extract(argv[2], argv[3], argv[4], NULL); | ||
378 | } | ||
379 | else if ((!strcmp(argv[1], "-d")) && (argc > 4)) | ||
380 | { | ||
381 | if (argc > 5) | ||
382 | do_eet_decode(argv[2], argv[3], argv[4], argv[5]); | ||
383 | else | ||
384 | do_eet_decode(argv[2], argv[3], argv[4], NULL); | ||
385 | } | ||
386 | else if ((!strcmp(argv[1], "-i")) && (argc > 5)) | ||
387 | { | ||
388 | if (argc > 6) | ||
389 | do_eet_insert(argv[2], argv[3], argv[4], atoi(argv[5]), argv[6]); | ||
390 | else | ||
391 | do_eet_insert(argv[2], argv[3], argv[4], atoi(argv[5]), NULL); | ||
392 | } | ||
393 | else if ((!strcmp(argv[1], "-e")) && (argc > 5)) | ||
394 | { | ||
395 | if (argc > 6) | ||
396 | do_eet_encode(argv[2], argv[3], argv[4], atoi(argv[5]), argv[6]); | ||
397 | else | ||
398 | do_eet_encode(argv[2], argv[3], argv[4], atoi(argv[5]), NULL); | ||
399 | } | ||
400 | else if ((!strcmp(argv[1], "-r")) && (argc > 3)) | ||
401 | do_eet_remove(argv[2], argv[3]); | ||
402 | else if ((!strcmp(argv[1], "-c")) && (argc > 2)) | ||
403 | do_eet_check(argv[2]); | ||
404 | else if ((!strcmp(argv[1], "-s")) && (argc > 4)) | ||
405 | do_eet_sign(argv[2], argv[3], argv[4]); | ||
406 | else | ||
407 | goto help; | ||
408 | |||
409 | eina_log_domain_unregister(_eet_main_log_dom); | ||
410 | eet_shutdown(); | ||
411 | return 0; | ||
412 | } /* main */ | ||
413 | |||