summaryrefslogtreecommitdiff
path: root/src/bin/eet_main.c
blob: a433bc5eca11ee1fb300f7061bcf6044836aa4f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif /* ifdef HAVE_CONFIG_H */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif /* ifdef HAVE_UNISTD_H */

#ifdef HAVE_EVIL
# include <Evil.h>
#endif /* ifdef HAVE_EVIL */

#include <Eet.h>

static int _eet_main_log_dom = -1;

#ifdef EET_DEFAULT_LOG_COLOR
#undef EET_DEFAULT_LOG_COLOR
#endif /* ifdef EET_DEFAULT_LOG_COLOR */
#define EET_DEFAULT_LOG_COLOR EINA_COLOR_CYAN
#ifdef ERR
#undef ERR
#endif /* ifdef ERR */
#define ERR(...)  EINA_LOG_DOM_ERR(_eet_main_log_dom, __VA_ARGS__)
#ifdef DBG
#undef DBG
#endif /* ifdef DBG */
#define DBG(...)  EINA_LOG_DOM_DBG(_eet_main_log_dom, __VA_ARGS__)
#ifdef INF
#undef INF
#endif /* ifdef INF */
#define INF(...)  EINA_LOG_DOM_INFO(_eet_main_log_dom, __VA_ARGS__)
#ifdef WRN
#undef WRN
#endif /* ifdef WRN */
#define WRN(...)  EINA_LOG_DOM_WARN(_eet_main_log_dom, __VA_ARGS__)
#ifdef CRIT
#undef CRIT
#endif /* ifdef CRIT */
#define CRIT(...) EINA_LOG_DOM_CRIT(_eet_main_log_dom, __VA_ARGS__)

static void
do_eet_list(const char * file)
{
   int i, num;
   char ** list;
   Eet_File * ef;

   ef = eet_open(file, EET_FILE_MODE_READ);
   if (!ef)
     {
        ERR("cannot open for reading: %s", file);
        exit(-1);
     }

   list = eet_list(ef, "*", &num);
   if (list)
     {
        for (i = 0; i < num; i++)
           printf("%s\n",list[i]);
        free(list);
     }

   eet_close(ef);
} /* do_eet_list */

static void
do_eet_extract(const char * file,
               const char * key,
               const char * out,
               const char * crypto_key)
{
   Eet_File * ef;
   void * data;
   int size = 0;
   FILE * f;

   ef = eet_open(file, EET_FILE_MODE_READ);
   if (!ef)
     {
        ERR("cannot open for reading: %s", file);
        exit(-1);
     }

   data = eet_read_cipher(ef, key, &size, crypto_key);
   if (!data)
     {
        ERR("cannot read key %s", key);
        exit(-1);
     }

   f = fopen(out, "wb");
   if (!f)
     {
        ERR("cannot open %s", out);
        exit(-1);
     }

   if (fwrite(data, size, 1, f) != 1)
     {
        ERR("cannot write to %s", out);
        exit(-1);
     }

   fclose(f);
   free(data);
   eet_close(ef);
} /* do_eet_extract */

static void
do_eet_decode_dump(void * data, const char * str)
{
   fputs(str, (FILE *)data);
} /* do_eet_decode_dump */

static void
do_eet_decode(const char * file,
              const char * key,
              const char * out,
              const char * crypto_key)
{
   Eet_File * ef;
   FILE * f;

   ef = eet_open(file, EET_FILE_MODE_READ);
   if (!ef)
     {
        ERR("cannot open for reading: %s", file);
        exit(-1);
     }

   f = fopen(out, "wb");
   if (!f)
     {
        ERR("cannot open %s", out);
        exit(-1);
     }

   if (!eet_data_dump_cipher(ef, key, crypto_key, do_eet_decode_dump, f))
     {
        ERR("cannot write to %s", out);
        exit(-1);
     }

   fclose(f);
   eet_close(ef);
} /* do_eet_decode */

static void
do_eet_insert(const char * file,
              const char * key,
              const char * out,
              int          compress,
              const char * crypto_key)
{
   Eet_File * ef;
   void * data;
   int size = 0;
   FILE * f;

   ef = eet_open(file, EET_FILE_MODE_READ_WRITE);
   if (!ef)
      ef = eet_open(file, EET_FILE_MODE_WRITE);

   if (!ef)
     {
        ERR("cannot open for read+write: %s", file);
        exit(-1);
     }

   f = fopen(out, "rb");
   if (!f)
     {
        ERR("cannot open %s", out);
        exit(-1);
     }

   fseek(f, 0, SEEK_END);
   size = ftell(f);
   rewind(f);
   data = malloc(size);
   if (!data)
     {
        ERR("cannot allocate %i bytes", size);
        exit(-1);
     }

   if (fread(data, size, 1, f) != 1)
     {
        ERR("cannot read file %s", out);
        exit(-1);
     }

   fclose(f);
   eet_write_cipher(ef, key, data, size, compress, crypto_key);
   free(data);
   eet_close(ef);
} /* do_eet_insert */

static void
do_eet_encode(const char * file,
              const char * key,
              const char * out,
              int          compress,
              const char * crypto_key)
{
   Eet_File * ef;
   char * text;
   int textlen = 0;
   int size = 0;
   FILE * f;

   ef = eet_open(file, EET_FILE_MODE_READ_WRITE);
   if (!ef)
      ef = eet_open(file, EET_FILE_MODE_WRITE);

   if (!ef)
     {
        ERR("cannot open for read+write: %s", file);
        exit(-1);
     }

   f = fopen(out, "rb");
   if (!f)
     {
        ERR("cannot open %s", out);
        exit(-1);
     }

   fseek(f, 0, SEEK_END);
   textlen = ftell(f);
   rewind(f);
   text = malloc(textlen);
   if (!text)
     {
        ERR("cannot allocate %i bytes", size);
        exit(-1);
     }

   if (fread(text, textlen, 1, f) != 1)
     {
        ERR("cannot read file %s", out);
        exit(-1);
     }

   fclose(f);
   if (!eet_data_undump_cipher(ef, key, crypto_key, text, textlen, compress))
     {
        ERR("cannot parse %s", out);
        exit(-1);
     }

   free(text);
   eet_close(ef);
} /* do_eet_encode */

static void
do_eet_remove(const char * file, const char * key)
{
   Eet_File * ef;

   ef = eet_open(file, EET_FILE_MODE_READ_WRITE);
   if (!ef)
     {
        ERR("cannot open for read+write: %s", file);
        exit(-1);
     }

   eet_delete(ef, key);
   eet_close(ef);
} /* do_eet_remove */

static void
do_eet_check(const char * file)
{
   Eet_File * ef;
   const void * der;
   int der_length;
   int sign_length;

   ef = eet_open(file, EET_FILE_MODE_READ);
   if (!ef)
     {
        ERR("checking signature of `%s` failed", file);
        exit(-1);
     }

   der = eet_identity_x509(ef, &der_length);

   fprintf(stdout, "Certificate length %i.\n", der_length);
   eet_identity_certificate_print(der, der_length, stdout);

   eet_identity_signature(ef, &sign_length);
   fprintf(stdout, "Signature length %i.\n", sign_length);

   eet_close(ef);
} /* do_eet_check */

static void
do_eet_sign(const char * file, const char * private_key, const char * public_key)
{
   Eet_File * ef;
   Eet_Key * key;

   ef = eet_open(file, EET_FILE_MODE_READ_WRITE);
   if (!ef)
     {
        ERR("cannot open for read+write: %s.", file);
        exit(-1);
     }

   key = eet_identity_open(public_key, private_key, NULL);
   if (!key)
     {
        ERR("cannot open key '%s:%s'.", public_key, private_key);
        exit(-1);
     }

   fprintf(stdout, "Using the following key to sign `%s`.\n", file);
   eet_identity_print(key, stdout);

   eet_identity_set(ef, key);

   eet_close(ef);
} /* do_eet_sign */

int
main(int argc, char ** argv)
{
   if (!eet_init())
      return -1;

   _eet_main_log_dom = eina_log_domain_register("eet_main", EINA_COLOR_CYAN);
   if(_eet_main_log_dom < -1)
     {
        EINA_LOG_ERR("Impossible to create a log domain for eet_main.");
        eet_shutdown();
        return(-1);
     }

   if (argc < 2)
     {
help:
        printf(
           "Usage:\n"
           "  eet -l FILE.EET				     list all keys in FILE.EET\n"
           "  eet -x FILE.EET KEY OUT-FILE [CRYPTO_KEY]          extract data stored in KEY in FILE.EET and write to OUT-FILE\n"
           "  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"
           "  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"
           "  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"
           "  eet -r FILE.EET KEY                                remove KEY in FILE.EET\n"
           "  eet -c FILE.EET                                    report and check the signature information of an eet file\n"
           "  eet -s FILE.EET PRIVATE_KEY PUBLIC_KEY             sign FILE.EET with PRIVATE_KEY and attach PUBLIC_KEY as it's certificate\n"
           );
        eet_shutdown();
        return -1;
     }

   if ((!strncmp(argv[1], "-h", 2)))
      goto help;
   else if ((!strcmp(argv[1], "-l")) && (argc > 2))
      do_eet_list(argv[2]);
   else if ((!strcmp(argv[1], "-x")) && (argc > 4))
     {
        if (argc > 5)
           do_eet_extract(argv[2], argv[3], argv[4], argv[5]);
        else
           do_eet_extract(argv[2], argv[3], argv[4], NULL);
     }
   else if ((!strcmp(argv[1], "-d")) && (argc > 4))
     {
        if (argc > 5)
           do_eet_decode(argv[2], argv[3], argv[4], argv[5]);
        else
           do_eet_decode(argv[2], argv[3], argv[4], NULL);
     }
   else if ((!strcmp(argv[1], "-i")) && (argc > 5))
     {
        if (argc > 6)
           do_eet_insert(argv[2], argv[3], argv[4], atoi(argv[5]), argv[6]);
        else
           do_eet_insert(argv[2], argv[3], argv[4], atoi(argv[5]), NULL);
     }
   else if ((!strcmp(argv[1], "-e")) && (argc > 5))
     {
        if (argc > 6)
           do_eet_encode(argv[2], argv[3], argv[4], atoi(argv[5]), argv[6]);
        else
           do_eet_encode(argv[2], argv[3], argv[4], atoi(argv[5]), NULL);
     }
   else if ((!strcmp(argv[1], "-r")) && (argc > 3))
      do_eet_remove(argv[2], argv[3]);
   else if ((!strcmp(argv[1], "-c")) && (argc > 2))
      do_eet_check(argv[2]);
   else if ((!strcmp(argv[1], "-s")) && (argc > 4))
      do_eet_sign(argv[2], argv[3], argv[4]);
   else
      goto help;

   eina_log_domain_unregister(_eet_main_log_dom);
   eet_shutdown();
   return 0;
} /* main */