source: zcrypt.c @ 5175cf8

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