1 | /* zcrypt.c -- Read in a data stream from stdin & dump a decrypted/encrypted * |
---|
2 | * datastream. Reads the string to make the key from from the first * |
---|
3 | * parameter. Encrypts or decrypts according to -d or -e flag. (-e is * |
---|
4 | * default.) Will invoke zwrite if the -c option is provided for * |
---|
5 | * encryption. If a zephyr class is specified & the keyfile name omitted * |
---|
6 | * the ~/.crypt-table will be checked for "crypt-classname" and then * |
---|
7 | * "crypt-default" for the keyfile name. */ |
---|
8 | |
---|
9 | #include <stdio.h> |
---|
10 | |
---|
11 | #include <unistd.h> |
---|
12 | #include <sys/types.h> |
---|
13 | #include <glib.h> |
---|
14 | #include <string.h> |
---|
15 | #include <stdlib.h> |
---|
16 | #include <sys/wait.h> |
---|
17 | #include <ctype.h> |
---|
18 | #include <limits.h> |
---|
19 | #include <getopt.h> |
---|
20 | |
---|
21 | #include <config.h> |
---|
22 | |
---|
23 | #include <openssl/des.h> |
---|
24 | |
---|
25 | #include "filterproc.h" |
---|
26 | |
---|
27 | extern const char *version; |
---|
28 | |
---|
29 | /* Annotate functions in which the caller owns the return value and is |
---|
30 | * responsible for ensuring it is freed. */ |
---|
31 | #define CALLER_OWN G_GNUC_WARN_UNUSED_RESULT |
---|
32 | |
---|
33 | #define MAX_KEY 128 |
---|
34 | #define MAX_LINE 128 |
---|
35 | #define MAX_RESULT 4096 |
---|
36 | |
---|
37 | #ifndef TRUE |
---|
38 | #define TRUE -1 |
---|
39 | #endif |
---|
40 | #ifndef FALSE |
---|
41 | #define FALSE 0 |
---|
42 | #endif |
---|
43 | |
---|
44 | #define ZWRITE_OPT_NOAUTH (1<<0) |
---|
45 | #define ZWRITE_OPT_SIGNATURE (1<<1) |
---|
46 | #define ZWRITE_OPT_IGNOREVARS (1<<2) |
---|
47 | #define ZWRITE_OPT_VERBOSE (1<<3) |
---|
48 | #define ZWRITE_OPT_QUIET (1<<4) |
---|
49 | #define ZCRYPT_OPT_MESSAGE (1<<5) |
---|
50 | #define ZCRYPT_OPT_IGNOREDOT (1<<6) |
---|
51 | |
---|
52 | typedef struct |
---|
53 | { |
---|
54 | int flags; |
---|
55 | const char *signature; |
---|
56 | char *message; |
---|
57 | } ZWRITEOPTIONS; |
---|
58 | |
---|
59 | CALLER_OWN char *GetZephyrVarKeyFile(const char *whoami, const char *class, const char *instance); |
---|
60 | int ParseCryptSpec(const char *spec, const char **keyfile); |
---|
61 | CALLER_OWN char *BuildArgString(char **argv, int start, int end); |
---|
62 | CALLER_OWN char *read_keystring(const char *keyfile); |
---|
63 | |
---|
64 | int do_encrypt(int zephyr, const char *class, const char *instance, |
---|
65 | ZWRITEOPTIONS *zoptions, const char* keyfile, int cipher); |
---|
66 | int do_encrypt_des(const char *keyfile, const char *in, int len, FILE *out); |
---|
67 | int do_encrypt_aes(const char *keyfile, const char *in, int len, FILE *out); |
---|
68 | |
---|
69 | int do_decrypt(const char *keyfile, int cipher); |
---|
70 | int do_decrypt_aes(const char *keyfile); |
---|
71 | int do_decrypt_des(const char *keyfile); |
---|
72 | |
---|
73 | |
---|
74 | #define M_NONE 0 |
---|
75 | #define M_ZEPHYR_ENCRYPT 1 |
---|
76 | #define M_DECRYPT 2 |
---|
77 | #define M_ENCRYPT 3 |
---|
78 | #define M_RANDOMIZE 4 |
---|
79 | #define M_SETKEY 5 |
---|
80 | |
---|
81 | enum cipher_algo { |
---|
82 | CIPHER_DES, |
---|
83 | CIPHER_AES, |
---|
84 | NCIPHER |
---|
85 | }; |
---|
86 | |
---|
87 | typedef struct { |
---|
88 | int (*encrypt)(const char *keyfile, const char *in, int len, FILE *out); |
---|
89 | int (*decrypt)(const char *keyfile); |
---|
90 | } cipher_pair; |
---|
91 | |
---|
92 | cipher_pair ciphers[NCIPHER] = { |
---|
93 | [CIPHER_DES] = { do_encrypt_des, do_decrypt_des}, |
---|
94 | [CIPHER_AES] = { do_encrypt_aes, do_decrypt_aes}, |
---|
95 | }; |
---|
96 | |
---|
97 | static void owl_zcrypt_string_to_schedule(char *keystring, DES_key_schedule *schedule) { |
---|
98 | DES_cblock key; |
---|
99 | |
---|
100 | DES_string_to_key(keystring, &key); |
---|
101 | DES_key_sched(&key, schedule); |
---|
102 | } |
---|
103 | |
---|
104 | void usage(FILE *file, const char *progname) |
---|
105 | { |
---|
106 | fprintf(file, "Usage: %s [-Z|-D|-E|-R|-S] [-F Keyfile] [-c class] [-i instance]\n", progname); |
---|
107 | fprintf(file, " [-advqtluon] [-s signature] [-f arg] [-m message]\n"); |
---|
108 | fprintf(file, " One or more of class, instance, and keyfile must be specified.\n"); |
---|
109 | } |
---|
110 | |
---|
111 | int main(int argc, char *argv[]) |
---|
112 | { |
---|
113 | char *cryptspec = NULL; |
---|
114 | const char *keyfile; |
---|
115 | int cipher; |
---|
116 | int error = FALSE; |
---|
117 | int zephyr = FALSE; |
---|
118 | const char *class = NULL, *instance = NULL; |
---|
119 | int mode = M_NONE; |
---|
120 | |
---|
121 | int c; |
---|
122 | |
---|
123 | int messageflag = FALSE; |
---|
124 | ZWRITEOPTIONS zoptions; |
---|
125 | zoptions.flags = 0; |
---|
126 | |
---|
127 | enum { |
---|
128 | OPT_VERSION = CHAR_MAX + 1, |
---|
129 | OPT_HELP, |
---|
130 | }; |
---|
131 | static const struct option options[] = { |
---|
132 | {"version", no_argument, NULL, OPT_VERSION}, |
---|
133 | {"help", no_argument, NULL, OPT_HELP}, |
---|
134 | {NULL, 0, NULL, 0} |
---|
135 | }; |
---|
136 | |
---|
137 | while ((c = getopt_long(argc, argv, "ZDERSF:c:i:advqtluons:f:m", options, NULL)) != -1) |
---|
138 | { |
---|
139 | switch(c) |
---|
140 | { |
---|
141 | case OPT_VERSION: |
---|
142 | /* Version */ |
---|
143 | printf("This is zcrypt version %s\n", version); |
---|
144 | exit(0); |
---|
145 | case OPT_HELP: |
---|
146 | /* Help */ |
---|
147 | usage(stdout, argv[0]); |
---|
148 | exit(0); |
---|
149 | case 'Z': |
---|
150 | /* Zephyr encrypt */ |
---|
151 | mode = M_ZEPHYR_ENCRYPT; |
---|
152 | break; |
---|
153 | case 'D': |
---|
154 | /* Decrypt */ |
---|
155 | mode = M_DECRYPT; |
---|
156 | break; |
---|
157 | case 'E': |
---|
158 | /* Encrypt */ |
---|
159 | mode = M_ENCRYPT; |
---|
160 | break; |
---|
161 | case 'R': |
---|
162 | /* Randomize the keyfile */ |
---|
163 | mode = M_RANDOMIZE; |
---|
164 | break; |
---|
165 | case 'S': |
---|
166 | /* Set a new key value from stdin */ |
---|
167 | mode = M_SETKEY; |
---|
168 | break; |
---|
169 | case 'F': |
---|
170 | /* Specify the keyfile explicitly */ |
---|
171 | if (cryptspec != NULL) error = TRUE; |
---|
172 | cryptspec = optarg; |
---|
173 | break; |
---|
174 | case 'c': |
---|
175 | /* Zwrite/zcrypt: class name */ |
---|
176 | if (class != NULL) error = TRUE; |
---|
177 | class = optarg; |
---|
178 | break; |
---|
179 | case 'i': |
---|
180 | /* Zwrite/zcrypt: instance name */ |
---|
181 | if (instance != NULL) error = TRUE; |
---|
182 | instance = optarg; |
---|
183 | break; |
---|
184 | case 'a': |
---|
185 | /* Zwrite: authenticate (default) */ |
---|
186 | zoptions.flags &= ~ZWRITE_OPT_NOAUTH; |
---|
187 | break; |
---|
188 | case 'd': |
---|
189 | /* Zwrite: do not authenticate */ |
---|
190 | zoptions.flags |= ZWRITE_OPT_NOAUTH; |
---|
191 | break; |
---|
192 | case 'v': |
---|
193 | /* Zwrite: verbose */ |
---|
194 | zoptions.flags |= ZWRITE_OPT_VERBOSE; |
---|
195 | break; |
---|
196 | case 'q': |
---|
197 | /* Zwrite: quiet */ |
---|
198 | zoptions.flags |= ZWRITE_OPT_QUIET; |
---|
199 | break; |
---|
200 | case 't': |
---|
201 | /* Zwrite: no expand tabs (ignored) */ |
---|
202 | break; |
---|
203 | case 'l': |
---|
204 | /* Zwrite: ignore '.' on a line by itself (ignored) */ |
---|
205 | zoptions.flags |= ZCRYPT_OPT_IGNOREDOT; |
---|
206 | break; |
---|
207 | case 'u': |
---|
208 | /* Zwrite: urgent message */ |
---|
209 | instance = "URGENT"; |
---|
210 | break; |
---|
211 | case 'o': |
---|
212 | /* Zwrite: ignore zephyr variables zwrite-class, zwrite-inst, */ |
---|
213 | /* zwrite-opcode */ |
---|
214 | zoptions.flags |= ZWRITE_OPT_IGNOREVARS; |
---|
215 | break; |
---|
216 | case 'n': |
---|
217 | /* Zwrite: prevent PING message (always used) */ |
---|
218 | break; |
---|
219 | case 's': |
---|
220 | /* Zwrite: signature */ |
---|
221 | zoptions.flags |= ZWRITE_OPT_SIGNATURE; |
---|
222 | zoptions.signature = optarg; |
---|
223 | break; |
---|
224 | case 'f': |
---|
225 | /* Zwrite: file system specification (ignored) */ |
---|
226 | break; |
---|
227 | case 'm': |
---|
228 | /* Message on rest of line*/ |
---|
229 | messageflag = TRUE; |
---|
230 | break; |
---|
231 | case '?': |
---|
232 | error = TRUE; |
---|
233 | break; |
---|
234 | } |
---|
235 | if (error || messageflag) |
---|
236 | break; |
---|
237 | } |
---|
238 | |
---|
239 | if (class != NULL || instance != NULL) |
---|
240 | zephyr = TRUE; |
---|
241 | |
---|
242 | if (messageflag) |
---|
243 | { |
---|
244 | zoptions.flags |= ZCRYPT_OPT_MESSAGE; |
---|
245 | zoptions.message = BuildArgString(argv, optind, argc); |
---|
246 | if (!zoptions.message) |
---|
247 | { |
---|
248 | fprintf(stderr, "Memory allocation error.\n"); |
---|
249 | error = TRUE; |
---|
250 | } |
---|
251 | } |
---|
252 | else if (optind < argc) |
---|
253 | { |
---|
254 | error = TRUE; |
---|
255 | } |
---|
256 | |
---|
257 | if (mode == M_NONE) |
---|
258 | mode = (zephyr?M_ZEPHYR_ENCRYPT:M_ENCRYPT); |
---|
259 | |
---|
260 | if (mode == M_ZEPHYR_ENCRYPT && !zephyr) |
---|
261 | error = TRUE; |
---|
262 | |
---|
263 | if (!error && cryptspec == NULL && (class != NULL || instance != NULL)) { |
---|
264 | cryptspec = GetZephyrVarKeyFile(argv[0], class, instance); |
---|
265 | if(!cryptspec) { |
---|
266 | fprintf(stderr, "Unable to find keyfile for "); |
---|
267 | if(class != NULL) { |
---|
268 | fprintf(stderr, "-c %s ", class); |
---|
269 | } |
---|
270 | if(instance != NULL) { |
---|
271 | fprintf(stderr, "-i %s ", instance); |
---|
272 | } |
---|
273 | fprintf(stderr, "\n"); |
---|
274 | exit(-1); |
---|
275 | } |
---|
276 | } |
---|
277 | |
---|
278 | if (error || !cryptspec) |
---|
279 | { |
---|
280 | usage(stderr, argv[0]); |
---|
281 | exit(1); |
---|
282 | } |
---|
283 | |
---|
284 | cipher = ParseCryptSpec(cryptspec, &keyfile); |
---|
285 | if(cipher < 0) { |
---|
286 | fprintf(stderr, "Invalid cipher specification: %s\n", cryptspec); |
---|
287 | exit(1); |
---|
288 | } |
---|
289 | |
---|
290 | |
---|
291 | if (mode == M_RANDOMIZE) |
---|
292 | { |
---|
293 | /* Choose a new, random key */ |
---|
294 | /* |
---|
295 | FILE *fkey = fopen(fname, "w"); |
---|
296 | if (!fkey) |
---|
297 | printf("Could not open key file for writing: %s\n", fname); |
---|
298 | else |
---|
299 | { |
---|
300 | char string[100]; |
---|
301 | fputs(fkey, string); |
---|
302 | fclose(fkey); |
---|
303 | } |
---|
304 | */ |
---|
305 | fprintf(stderr, "Feature not yet implemented.\n"); |
---|
306 | } |
---|
307 | else if (mode == M_SETKEY) |
---|
308 | { |
---|
309 | /* Set a new, user-entered key */ |
---|
310 | char newkey[MAX_KEY]; |
---|
311 | FILE *fkey; |
---|
312 | |
---|
313 | if (isatty(0)) |
---|
314 | { |
---|
315 | printf("Enter new key: "); |
---|
316 | /* Really should read without echo!!! */ |
---|
317 | } |
---|
318 | if(!fgets(newkey, MAX_KEY - 1, stdin)) { |
---|
319 | fprintf(stderr, "Error reading key.\n"); |
---|
320 | return 1; |
---|
321 | } |
---|
322 | |
---|
323 | fkey = fopen(keyfile, "w"); |
---|
324 | if (!fkey) |
---|
325 | fprintf(stderr, "Could not open key file for writing: %s\n", keyfile); |
---|
326 | else |
---|
327 | { |
---|
328 | if (fputs(newkey, fkey) != strlen(newkey) || putc('\n', fkey) != '\n') |
---|
329 | { |
---|
330 | fprintf(stderr, "Error writing to key file.\n"); |
---|
331 | fclose(fkey); |
---|
332 | exit(1); |
---|
333 | } |
---|
334 | else |
---|
335 | { |
---|
336 | fclose(fkey); |
---|
337 | fprintf(stderr, "Key update complete.\n"); |
---|
338 | } |
---|
339 | } |
---|
340 | } |
---|
341 | else |
---|
342 | { |
---|
343 | if (mode == M_ZEPHYR_ENCRYPT || mode == M_ENCRYPT) |
---|
344 | error = !do_encrypt((mode == M_ZEPHYR_ENCRYPT), class, instance, |
---|
345 | &zoptions, keyfile, cipher); |
---|
346 | else |
---|
347 | error = !do_decrypt(keyfile, cipher); |
---|
348 | } |
---|
349 | |
---|
350 | /* Always print the **END** message if -D is specified. */ |
---|
351 | if (mode == M_DECRYPT) |
---|
352 | printf("**END**\n"); |
---|
353 | |
---|
354 | return error; |
---|
355 | } |
---|
356 | |
---|
357 | int ParseCryptSpec(const char *spec, const char **keyfile) { |
---|
358 | int cipher = CIPHER_DES; |
---|
359 | char *cipher_name = strdup(spec); |
---|
360 | char *colon = strchr(cipher_name, ':'); |
---|
361 | |
---|
362 | *keyfile = spec; |
---|
363 | |
---|
364 | if (colon) { |
---|
365 | char *rest = strchr(spec, ':') + 1; |
---|
366 | while(isspace(*rest)) rest++; |
---|
367 | |
---|
368 | *colon-- = '\0'; |
---|
369 | while (colon >= cipher_name && isspace(*colon)) { |
---|
370 | *colon = '\0'; |
---|
371 | } |
---|
372 | |
---|
373 | if(strcmp(cipher_name, "AES") == 0) { |
---|
374 | cipher = CIPHER_AES; |
---|
375 | *keyfile = rest; |
---|
376 | } else if(strcmp(cipher_name, "DES") == 0) { |
---|
377 | cipher = CIPHER_DES; |
---|
378 | *keyfile = rest; |
---|
379 | } |
---|
380 | } |
---|
381 | |
---|
382 | free(cipher_name); |
---|
383 | |
---|
384 | return cipher; |
---|
385 | } |
---|
386 | |
---|
387 | /* Build a space-separated string from argv from elements between start * |
---|
388 | * and end - 1. malloc()'s the returned string. */ |
---|
389 | CALLER_OWN char *BuildArgString(char **argv, int start, int end) |
---|
390 | { |
---|
391 | int len = 1; |
---|
392 | int i; |
---|
393 | char *result; |
---|
394 | |
---|
395 | /* Compute the length of the string. (Plus 1 or 2) */ |
---|
396 | for (i = start; i < end; i++) |
---|
397 | len += strlen(argv[i]) + 1; |
---|
398 | |
---|
399 | /* Allocate memory */ |
---|
400 | result = (char *)malloc(len); |
---|
401 | if (result) |
---|
402 | { |
---|
403 | /* Build the string */ |
---|
404 | char *ptr = result; |
---|
405 | /* Start with an empty string, in case nothing is copied. */ |
---|
406 | *ptr = '\0'; |
---|
407 | /* Copy the arguments */ |
---|
408 | for (i = start; i < end; i++) |
---|
409 | { |
---|
410 | char *temp = argv[i]; |
---|
411 | /* Add a space, if not the first argument */ |
---|
412 | if (i != start) |
---|
413 | *ptr++ = ' '; |
---|
414 | /* Copy argv[i], leaving ptr pointing to the '\0' copied from temp */ |
---|
415 | while ((*ptr = *temp++)) |
---|
416 | ptr++; |
---|
417 | } |
---|
418 | } |
---|
419 | |
---|
420 | return result; |
---|
421 | } |
---|
422 | |
---|
423 | #define MAX_BUFF 258 |
---|
424 | #define MAX_SEARCH 3 |
---|
425 | /* Find the class/instance in the .crypt-table */ |
---|
426 | CALLER_OWN char *GetZephyrVarKeyFile(const char *whoami, const char *class, const char *instance) |
---|
427 | { |
---|
428 | char *keyfile = NULL; |
---|
429 | char *varname[MAX_SEARCH]; |
---|
430 | int length[MAX_SEARCH], i; |
---|
431 | char buffer[MAX_BUFF]; |
---|
432 | const char *home; |
---|
433 | char *filename; |
---|
434 | char result[MAX_SEARCH][MAX_BUFF]; |
---|
435 | int numsearch = 0; |
---|
436 | FILE *fsearch; |
---|
437 | |
---|
438 | memset(varname, 0, sizeof(varname)); |
---|
439 | |
---|
440 | /* Determine names to look for in .crypt-table */ |
---|
441 | if (instance) |
---|
442 | varname[numsearch++] = g_strdup_printf("crypt-%s-%s:", (class?class:"message"), instance); |
---|
443 | if (class) |
---|
444 | varname[numsearch++] = g_strdup_printf("crypt-%s:", class); |
---|
445 | varname[numsearch++] = g_strdup("crypt-default:"); |
---|
446 | |
---|
447 | /* Setup the result array, and determine string lengths */ |
---|
448 | for (i = 0; i < numsearch; i++) |
---|
449 | { |
---|
450 | result[i][0] = '\0'; |
---|
451 | length[i] = strlen(varname[i]); |
---|
452 | } |
---|
453 | |
---|
454 | /* Open~/.crypt-table */ |
---|
455 | home = getenv("HOME"); |
---|
456 | if (home == NULL) |
---|
457 | home = g_get_home_dir(); |
---|
458 | filename = g_build_filename(home, ".crypt-table", NULL); |
---|
459 | fsearch = fopen(filename, "r"); |
---|
460 | if (fsearch) |
---|
461 | { |
---|
462 | /* Scan file for a match */ |
---|
463 | while (!feof(fsearch)) |
---|
464 | { |
---|
465 | if (!fgets(buffer, MAX_BUFF - 3, fsearch)) break; |
---|
466 | for (i = 0; i < numsearch; i++) |
---|
467 | if (strncasecmp(varname[i], buffer, length[i]) == 0) |
---|
468 | { |
---|
469 | int j; |
---|
470 | for (j = length[i]; buffer[j] == ' '; j++) |
---|
471 | ; |
---|
472 | strcpy(result[i], &buffer[j]); |
---|
473 | if (*result[i]) |
---|
474 | if (result[i][strlen(result[i])-1] == '\n') |
---|
475 | result[i][strlen(result[i])-1] = '\0'; |
---|
476 | } |
---|
477 | } |
---|
478 | |
---|
479 | /* Pick the "best" match found */ |
---|
480 | keyfile = NULL; |
---|
481 | for (i = 0; i < numsearch; i++) |
---|
482 | if (*result[i]) |
---|
483 | { |
---|
484 | keyfile = result[i]; |
---|
485 | break; |
---|
486 | } |
---|
487 | |
---|
488 | if (keyfile != NULL) |
---|
489 | { |
---|
490 | /* Prepare result to be returned */ |
---|
491 | char *temp = keyfile; |
---|
492 | keyfile = (char *)malloc(strlen(temp) + 1); |
---|
493 | if (keyfile) |
---|
494 | strcpy(keyfile, temp); |
---|
495 | else |
---|
496 | fprintf(stderr, "Memory allocation error.\n"); |
---|
497 | } |
---|
498 | fclose(fsearch); |
---|
499 | } |
---|
500 | else |
---|
501 | fprintf(stderr, "Could not open key table file: %s\n", filename); |
---|
502 | |
---|
503 | for(i = 0; i < MAX_SEARCH; i++) { |
---|
504 | g_free(varname[i]); |
---|
505 | } |
---|
506 | |
---|
507 | g_free(filename); |
---|
508 | |
---|
509 | return keyfile; |
---|
510 | } |
---|
511 | |
---|
512 | static pid_t zephyrpipe_pid = 0; |
---|
513 | |
---|
514 | /* Open a pipe to zwrite */ |
---|
515 | FILE *GetZephyrPipe(const char *class, const char *instance, const ZWRITEOPTIONS *zoptions) |
---|
516 | { |
---|
517 | int fildes[2]; |
---|
518 | pid_t pid; |
---|
519 | FILE *result; |
---|
520 | const char *argv[20]; |
---|
521 | int argc = 0; |
---|
522 | |
---|
523 | if (pipe(fildes) < 0) |
---|
524 | return NULL; |
---|
525 | pid = fork(); |
---|
526 | |
---|
527 | if (pid < 0) |
---|
528 | { |
---|
529 | /* Error: clean up */ |
---|
530 | close(fildes[0]); |
---|
531 | close(fildes[1]); |
---|
532 | result = NULL; |
---|
533 | } |
---|
534 | else if (pid == 0) |
---|
535 | { |
---|
536 | /* Setup child process */ |
---|
537 | argv[argc++] = "zwrite"; |
---|
538 | argv[argc++] = "-n"; /* Always send without ping */ |
---|
539 | if (class) |
---|
540 | { |
---|
541 | argv[argc++] = "-c"; |
---|
542 | argv[argc++] = class; |
---|
543 | } |
---|
544 | if (instance) |
---|
545 | { |
---|
546 | argv[argc++] = "-i"; |
---|
547 | argv[argc++] = instance; |
---|
548 | } |
---|
549 | if (zoptions->flags & ZWRITE_OPT_NOAUTH) |
---|
550 | argv[argc++] = "-d"; |
---|
551 | if (zoptions->flags & ZWRITE_OPT_QUIET) |
---|
552 | argv[argc++] = "-q"; |
---|
553 | if (zoptions->flags & ZWRITE_OPT_VERBOSE) |
---|
554 | argv[argc++] = "-v"; |
---|
555 | if (zoptions->flags & ZWRITE_OPT_SIGNATURE) |
---|
556 | { |
---|
557 | argv[argc++] = "-s"; |
---|
558 | argv[argc++] = zoptions->signature; |
---|
559 | } |
---|
560 | argv[argc++] = "-O"; |
---|
561 | argv[argc++] = "crypt"; |
---|
562 | argv[argc] = NULL; |
---|
563 | close(fildes[1]); |
---|
564 | if (fildes[0] != STDIN_FILENO) |
---|
565 | { |
---|
566 | if (dup2(fildes[0], STDIN_FILENO) != STDIN_FILENO) |
---|
567 | exit(0); |
---|
568 | close(fildes[0]); |
---|
569 | } |
---|
570 | close(fildes[0]); |
---|
571 | execvp(argv[0], (char **)argv); |
---|
572 | fprintf(stderr, "Exec error: could not run zwrite\n"); |
---|
573 | exit(0); |
---|
574 | } |
---|
575 | else |
---|
576 | { |
---|
577 | close(fildes[0]); |
---|
578 | /* Create a FILE * for the zwrite pipe */ |
---|
579 | result = (FILE *)fdopen(fildes[1], "w"); |
---|
580 | zephyrpipe_pid = pid; |
---|
581 | } |
---|
582 | |
---|
583 | return result; |
---|
584 | } |
---|
585 | |
---|
586 | /* Close the pipe to zwrite */ |
---|
587 | void CloseZephyrPipe(FILE *pipe) |
---|
588 | { |
---|
589 | fclose(pipe); |
---|
590 | waitpid(zephyrpipe_pid, NULL, 0); |
---|
591 | zephyrpipe_pid = 0; |
---|
592 | } |
---|
593 | |
---|
594 | #define BASE_CODE 70 |
---|
595 | #define LAST_CODE (BASE_CODE + 15) |
---|
596 | #define OUTPUT_BLOCK_SIZE 16 |
---|
597 | |
---|
598 | void block_to_ascii(unsigned char *output, FILE *outfile) |
---|
599 | { |
---|
600 | int i; |
---|
601 | for (i = 0; i < 8; i++) |
---|
602 | { |
---|
603 | putc(((output[i] & 0xf0) >> 4) + BASE_CODE, outfile); |
---|
604 | putc( (output[i] & 0x0f) + BASE_CODE, outfile); |
---|
605 | } |
---|
606 | } |
---|
607 | |
---|
608 | CALLER_OWN char *slurp_stdin(int ignoredot, int *length) { |
---|
609 | char *buf; |
---|
610 | char *inptr; |
---|
611 | |
---|
612 | if ((inptr = buf = (char *)malloc(MAX_RESULT)) == NULL) |
---|
613 | { |
---|
614 | fprintf(stderr, "Memory allocation error\n"); |
---|
615 | return NULL; |
---|
616 | } |
---|
617 | while (inptr - buf < MAX_RESULT - MAX_LINE - 20) |
---|
618 | { |
---|
619 | if (fgets(inptr, MAX_LINE, stdin) == NULL) |
---|
620 | break; |
---|
621 | |
---|
622 | if (inptr[0]) |
---|
623 | { |
---|
624 | if (inptr[0] == '.' && inptr[1] == '\n' && !ignoredot) |
---|
625 | { |
---|
626 | inptr[0] = '\0'; |
---|
627 | break; |
---|
628 | } |
---|
629 | else |
---|
630 | inptr += strlen(inptr); |
---|
631 | } |
---|
632 | else |
---|
633 | break; |
---|
634 | } |
---|
635 | *length = inptr - buf; |
---|
636 | |
---|
637 | return buf; |
---|
638 | } |
---|
639 | |
---|
640 | CALLER_OWN char *GetInputBuffer(ZWRITEOPTIONS *zoptions, int *length) { |
---|
641 | char *buf; |
---|
642 | |
---|
643 | if (zoptions->flags & ZCRYPT_OPT_MESSAGE) |
---|
644 | { |
---|
645 | /* Use the -m message */ |
---|
646 | buf = strdup(zoptions->message); |
---|
647 | *length = strlen(buf); |
---|
648 | } |
---|
649 | else |
---|
650 | { |
---|
651 | if (isatty(0)) { |
---|
652 | /* tty input, so show the "Type your message now..." message */ |
---|
653 | if (zoptions->flags & ZCRYPT_OPT_IGNOREDOT) |
---|
654 | printf("Type your message now. End with the end-of-file character.\n"); |
---|
655 | else |
---|
656 | printf("Type your message now. End with control-D or a dot on a line by itself.\n"); |
---|
657 | } else { |
---|
658 | zoptions->flags |= ZCRYPT_OPT_IGNOREDOT; |
---|
659 | } |
---|
660 | |
---|
661 | buf = slurp_stdin(zoptions->flags & ZCRYPT_OPT_IGNOREDOT, length); |
---|
662 | } |
---|
663 | return buf; |
---|
664 | } |
---|
665 | |
---|
666 | CALLER_OWN char *read_keystring(const char *keyfile) { |
---|
667 | char *keystring; |
---|
668 | FILE *fkey = fopen(keyfile, "r"); |
---|
669 | if(!fkey) { |
---|
670 | fprintf(stderr, "Unable to open keyfile %s\n", keyfile); |
---|
671 | return NULL; |
---|
672 | } |
---|
673 | keystring = malloc(MAX_KEY); |
---|
674 | if(!fgets(keystring, MAX_KEY-1, fkey)) { |
---|
675 | fprintf(stderr, "Unable to read from keyfile: %s\n", keyfile); |
---|
676 | free(keystring); |
---|
677 | keystring = NULL; |
---|
678 | } |
---|
679 | fclose(fkey); |
---|
680 | return keystring; |
---|
681 | } |
---|
682 | |
---|
683 | /* Encrypt stdin, with prompt if isatty, and send to stdout, or to zwrite |
---|
684 | if zephyr is set. */ |
---|
685 | int do_encrypt(int zephyr, const char *class, const char *instance, |
---|
686 | ZWRITEOPTIONS *zoptions, const char *keyfile, int cipher) |
---|
687 | { |
---|
688 | FILE *outfile = stdout; |
---|
689 | char *inbuff = NULL; |
---|
690 | int buflen; |
---|
691 | int out = TRUE; |
---|
692 | |
---|
693 | inbuff = GetInputBuffer(zoptions, &buflen); |
---|
694 | |
---|
695 | if(!inbuff) { |
---|
696 | fprintf(stderr, "Error reading zcrypt input!\n"); |
---|
697 | return FALSE; |
---|
698 | } |
---|
699 | |
---|
700 | if (zephyr) { |
---|
701 | outfile = GetZephyrPipe(class, instance, zoptions); |
---|
702 | if (!outfile) |
---|
703 | { |
---|
704 | fprintf(stderr, "Could not run zwrite\n"); |
---|
705 | if (inbuff) |
---|
706 | free(inbuff); |
---|
707 | return FALSE; |
---|
708 | } |
---|
709 | } |
---|
710 | |
---|
711 | out = ciphers[cipher].encrypt(keyfile, inbuff, buflen, outfile); |
---|
712 | |
---|
713 | if (zephyr) |
---|
714 | CloseZephyrPipe(outfile); |
---|
715 | |
---|
716 | free(inbuff); |
---|
717 | return out; |
---|
718 | } |
---|
719 | |
---|
720 | int do_encrypt_des(const char *keyfile, const char *in, int length, FILE *outfile) |
---|
721 | { |
---|
722 | DES_key_schedule schedule; |
---|
723 | unsigned char input[8], output[8]; |
---|
724 | const char *inptr; |
---|
725 | int num_blocks, last_block_size; |
---|
726 | char *keystring; |
---|
727 | int size; |
---|
728 | |
---|
729 | keystring = read_keystring(keyfile); |
---|
730 | if(!keystring) { |
---|
731 | return FALSE; |
---|
732 | } |
---|
733 | |
---|
734 | owl_zcrypt_string_to_schedule(keystring, &schedule); |
---|
735 | free(keystring); |
---|
736 | |
---|
737 | inptr = in; |
---|
738 | num_blocks = (length + 7) / 8; |
---|
739 | last_block_size = ((length + 7) % 8) + 1; |
---|
740 | |
---|
741 | /* Encrypt the input (inbuff or stdin) and send it to outfile */ |
---|
742 | while (TRUE) |
---|
743 | { |
---|
744 | /* Get 8 bytes from buffer */ |
---|
745 | if (num_blocks > 1) |
---|
746 | { |
---|
747 | size = 8; |
---|
748 | memcpy(input, inptr, size); |
---|
749 | inptr += 8; |
---|
750 | num_blocks--; |
---|
751 | } |
---|
752 | else if (num_blocks == 1) |
---|
753 | { |
---|
754 | size = last_block_size; |
---|
755 | memcpy(input, inptr, size); |
---|
756 | num_blocks--; |
---|
757 | } |
---|
758 | else |
---|
759 | size = 0; |
---|
760 | |
---|
761 | /* Check for EOF and pad the string to 8 chars, if needed */ |
---|
762 | if (size == 0) |
---|
763 | break; |
---|
764 | if (size < 8) |
---|
765 | memset(input + size, 0, 8 - size); |
---|
766 | |
---|
767 | /* Encrypt and output the block */ |
---|
768 | DES_ecb_encrypt(&input, &output, &schedule, TRUE); |
---|
769 | block_to_ascii(output, outfile); |
---|
770 | |
---|
771 | if (size < 8) |
---|
772 | break; |
---|
773 | } |
---|
774 | |
---|
775 | putc('\n', outfile); |
---|
776 | |
---|
777 | return TRUE; |
---|
778 | } |
---|
779 | |
---|
780 | int do_encrypt_aes(const char *keyfile, const char *in, int length, FILE *outfile) |
---|
781 | { |
---|
782 | char *out; |
---|
783 | int err, status; |
---|
784 | int tried_gpg1 = FALSE; |
---|
785 | const char *argv[] = { |
---|
786 | "gpg1", |
---|
787 | "--symmetric", |
---|
788 | "--no-options", |
---|
789 | "--no-default-keyring", |
---|
790 | "--keyring", "/dev/null", |
---|
791 | "--secret-keyring", "/dev/null", |
---|
792 | "--batch", |
---|
793 | "--quiet", |
---|
794 | "--no-use-agent", |
---|
795 | "--armor", |
---|
796 | "--cipher-algo", "AES", |
---|
797 | "--passphrase-file", keyfile, |
---|
798 | NULL |
---|
799 | }; |
---|
800 | while ((err = call_filter(argv, in, &out, &status)) && !out && !tried_gpg1) { |
---|
801 | tried_gpg1 = TRUE; |
---|
802 | argv[0] = "gpg"; |
---|
803 | } |
---|
804 | if(err || status) { |
---|
805 | g_free(out); |
---|
806 | return FALSE; |
---|
807 | } |
---|
808 | fwrite(out, strlen(out), 1, outfile); |
---|
809 | g_free(out); |
---|
810 | return TRUE; |
---|
811 | } |
---|
812 | |
---|
813 | /* Read a half-byte from stdin, skipping invalid characters. Returns -1 |
---|
814 | if at EOF or file error */ |
---|
815 | int read_ascii_nybble(void) |
---|
816 | { |
---|
817 | char c; |
---|
818 | |
---|
819 | while (TRUE) |
---|
820 | { |
---|
821 | if (fread(&c, 1, 1, stdin) == 0) |
---|
822 | return -1; |
---|
823 | else if (c >= BASE_CODE && c <= LAST_CODE) |
---|
824 | return c - BASE_CODE; |
---|
825 | } |
---|
826 | } |
---|
827 | |
---|
828 | /* Read both halves of the byte and return the single byte. Returns -1 |
---|
829 | if at EOF or file error. */ |
---|
830 | int read_ascii_byte(void) |
---|
831 | { |
---|
832 | int c1, c2; |
---|
833 | c1 = read_ascii_nybble(); |
---|
834 | if (c1 >= 0) |
---|
835 | { |
---|
836 | c2 = read_ascii_nybble(); |
---|
837 | if (c2 >= 0) |
---|
838 | { |
---|
839 | return c1 * 0x10 + c2; |
---|
840 | } |
---|
841 | } |
---|
842 | return -1; |
---|
843 | } |
---|
844 | |
---|
845 | /* Read an 8-byte DES block from stdin */ |
---|
846 | int read_ascii_block(unsigned char *input) |
---|
847 | { |
---|
848 | int c; |
---|
849 | |
---|
850 | int i; |
---|
851 | for (i = 0; i < 8; i++) |
---|
852 | { |
---|
853 | c = read_ascii_byte(); |
---|
854 | if (c < 0) |
---|
855 | return FALSE; |
---|
856 | |
---|
857 | input[i] = c; |
---|
858 | } |
---|
859 | |
---|
860 | return TRUE; |
---|
861 | } |
---|
862 | |
---|
863 | /* Decrypt stdin */ |
---|
864 | int do_decrypt(const char *keyfile, int cipher) |
---|
865 | { |
---|
866 | return ciphers[cipher].decrypt(keyfile); |
---|
867 | } |
---|
868 | |
---|
869 | int do_decrypt_aes(const char *keyfile) { |
---|
870 | char *in, *out; |
---|
871 | int length; |
---|
872 | int tried_gpg1 = FALSE; |
---|
873 | const char *argv[] = { |
---|
874 | "gpg1", |
---|
875 | "--decrypt", |
---|
876 | "--no-options", |
---|
877 | "--no-default-keyring", |
---|
878 | "--keyring", "/dev/null", |
---|
879 | "--secret-keyring", "/dev/null", |
---|
880 | "--batch", |
---|
881 | "--no-use-agent", |
---|
882 | "--quiet", |
---|
883 | "--passphrase-file", keyfile, |
---|
884 | NULL |
---|
885 | }; |
---|
886 | int err, status; |
---|
887 | |
---|
888 | in = slurp_stdin(TRUE, &length); |
---|
889 | if(!in) return FALSE; |
---|
890 | |
---|
891 | while ((err = call_filter(argv, in, &out, &status)) && !out && !tried_gpg1) { |
---|
892 | tried_gpg1 = TRUE; |
---|
893 | argv[0] = "gpg"; |
---|
894 | } |
---|
895 | free(in); |
---|
896 | if(err || status) { |
---|
897 | g_free(out); |
---|
898 | return FALSE; |
---|
899 | } |
---|
900 | fwrite(out, strlen(out), 1, stdout); |
---|
901 | g_free(out); |
---|
902 | |
---|
903 | return TRUE; |
---|
904 | } |
---|
905 | |
---|
906 | int do_decrypt_des(const char *keyfile) { |
---|
907 | DES_key_schedule schedule; |
---|
908 | unsigned char input[8], output[8]; |
---|
909 | char tmp[9]; |
---|
910 | char *keystring; |
---|
911 | |
---|
912 | /* |
---|
913 | DES decrypts 8 bytes at a time. We copy those over into the 9-byte |
---|
914 | 'tmp', which has the final byte zeroed, to ensure that we always |
---|
915 | have a NULL-terminated string we can call printf/strlen on. |
---|
916 | |
---|
917 | We don't pass 'tmp' to DES_ecb_encrypt directly, because it's |
---|
918 | prototyped as taking 'unsigned char[8]', and this avoids a stupid |
---|
919 | cast. |
---|
920 | |
---|
921 | We zero 'tmp' entirely, not just the final byte, in case there are |
---|
922 | no input blocks. |
---|
923 | */ |
---|
924 | memset(tmp, 0, sizeof tmp); |
---|
925 | |
---|
926 | keystring = read_keystring(keyfile); |
---|
927 | if(!keystring) return FALSE; |
---|
928 | |
---|
929 | owl_zcrypt_string_to_schedule(keystring, &schedule); |
---|
930 | |
---|
931 | free(keystring); |
---|
932 | |
---|
933 | while (read_ascii_block(input)) |
---|
934 | { |
---|
935 | DES_ecb_encrypt(&input, &output, &schedule, FALSE); |
---|
936 | memcpy(tmp, output, 8); |
---|
937 | printf("%s", tmp); |
---|
938 | } |
---|
939 | |
---|
940 | if (!tmp[0] || tmp[strlen(tmp) - 1] != '\n') |
---|
941 | printf("\n"); |
---|
942 | return TRUE; |
---|
943 | } |
---|