source: zcrypt.c @ 946058b

release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 946058b was a6ac9fe, checked in by Anders Kaseorg <andersk@mit.edu>, 12 years ago
owl_zcrypt_encrypt: Remove unused variable use_buffer. Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Property mode set to 100644
File size: 6.7 KB
Line 
1/* This file is stolen and slightly modified code */
2
3/* zcrypt.c -- Read in a data stream from stdin & dump a decrypted/encrypted *
4 *   datastream.  Reads the string to make the key from from the first       *
5 *   parameter.  Encrypts or decrypts according to -d or -e flag.  (-e is    *
6 *   default.)  Will invoke zwrite if the -c option is provided for          *
7 *   encryption.  If a zephyr class is specified & the keyfile name omitted  *
8 *   the ~/.crypt-table will be checked for "crypt-classname" and then       *
9 *   "crypt-default" for the keyfile name.                                   */
10
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <sys/types.h>
15#include <sys/wait.h>
16#include "owl.h"
17
18#ifdef OWL_ENABLE_ZCRYPT
19
20#define BASE_CODE 70
21#define LAST_CODE (BASE_CODE + 15)
22#define OUTPUT_BLOCK_SIZE 16
23#include <unistd.h>
24#include <sys/types.h>
25
26#ifdef HAVE_KERBEROS_IV
27#include <kerberosIV/des.h>
28#else
29#include <openssl/des.h>
30#endif
31
32#define MAX_KEY 128
33
34#ifndef TRUE
35#define TRUE -1
36#endif
37#ifndef FALSE
38#define FALSE 0
39#endif
40
41#define ZWRITE_OPT_NOAUTH     (1<<0)
42#define ZWRITE_OPT_SIGNATURE  (1<<1)
43#define ZWRITE_OPT_IGNOREVARS (1<<2)
44#define ZWRITE_OPT_VERBOSE    (1<<3)
45#define ZWRITE_OPT_QUIET      (1<<4)
46#define ZCRYPT_OPT_MESSAGE    (1<<5)
47#define ZCRYPT_OPT_IGNOREDOT  (1<<6)
48
49typedef struct
50{
51  int flags;
52  char *signature;
53  char *message;
54} ZWRITEOPTIONS;
55
56char *GetZephyrVarKeyFile(const char *whoami, const char *class, const char *instance);
57
58#define M_NONE            0
59#define M_ZEPHYR_ENCRYPT  1
60#define M_DECRYPT         2
61#define M_ENCRYPT         3
62#define M_RANDOMIZE       4
63#define M_SETKEY          5
64
65static void owl_zcrypt_string_to_schedule(char *keystring, des_key_schedule schedule) {
66#ifdef HAVE_KERBEROS_IV
67  des_cblock key;
68#else
69  des_cblock _key, *key = &_key;
70#endif
71
72  des_string_to_key(keystring, key);
73  des_key_sched(key, schedule);
74}
75
76/* The 'owl_zcrypt_decrypt' function was written by kretch for Owl.
77 * Decrypt the message in 'in' on class 'class' and instance
78 * 'instance' and leave the result in 'out'.  Out must be a buffer
79 * allocated by the caller.
80 *
81 * return 0 on success, otherwise -1
82 */
83int owl_zcrypt_decrypt(char *out, const char *in, const char *class, const char *instance) {
84  const char *inptr, *endptr;
85  char *fname, keystring[MAX_KEY];
86  FILE *fkey;
87  des_key_schedule schedule;
88  unsigned char input[8], output[8];
89  int i, c1, c2;
90 
91  fname=GetZephyrVarKeyFile("zcrypt", class, instance);
92  if (!fname) return(-1);
93  fkey=fopen(fname, "r");
94  if (!fkey) return(-1);
95  if (!fgets(keystring, MAX_KEY-1, fkey)) {
96    fclose(fkey);
97    return -1;
98  }
99  fclose(fkey);
100
101  strcpy(out, "");
102
103  output[0] = '\0';    /* In case no message at all                 */
104
105  owl_zcrypt_string_to_schedule(keystring, schedule);
106
107  inptr=in;
108  endptr=in+strlen(in)-1;
109  while (inptr<endptr) {
110    for (i=0; i<8; i++) {
111      c1=(inptr[0])-BASE_CODE;
112      c2=(inptr[1])-BASE_CODE;
113      input[i]=c1 * 0x10 + c2;
114      inptr+=2;
115    }
116    des_ecb_encrypt(&input, &output, schedule, FALSE);
117    strncat(out, (const char *)output, 8);
118  }
119
120  if (out[0] && out[strlen(out) - 1] != '\n')
121    strcat(out, "\n");
122  return(0);
123}
124
125int owl_zcrypt_encrypt(char *out, const char *in, const char *class, const char *instance) {
126  char *fname, keystring[MAX_KEY];
127  FILE *fkey;
128  des_key_schedule schedule;
129  unsigned char input[8], output[8];
130  int size, length, i;
131  const char *inbuff = NULL, *inptr;
132  int num_blocks=0, last_block_size=0;
133
134  fname=GetZephyrVarKeyFile("zcrypt", class, instance);
135  if (!fname) return(-1);
136  fkey=fopen(fname, "r");
137  if (!fkey) return(-1);
138  if (!fgets(keystring, MAX_KEY-1, fkey)) {
139    fclose(fkey);
140    return -1;
141  }
142  fclose(fkey);
143
144  owl_zcrypt_string_to_schedule(keystring, schedule);
145
146  inbuff=in;
147  length=strlen(inbuff);
148  num_blocks=(length+7)/8;
149  last_block_size=((length+7)%8)+1;
150
151  strcpy(out, "");
152 
153  inptr=inbuff;
154  while (TRUE) {
155    /* Get 8 bytes from buffer */
156    if (num_blocks > 1) {
157      size = 8;
158      memcpy(input, inptr, size);
159      inptr+=8;
160      num_blocks--;
161    } else if (num_blocks == 1) {
162      size=last_block_size;
163      memcpy(input, inptr, size);
164      num_blocks--;
165    } else {
166      size=0;
167    }
168
169    /* Check for EOF and pad the string to 8 chars, if needed */
170    if (size == 0) break;     /* END OF INPUT: BREAK FROM while LOOP! */
171     
172    if (size<8) memset(input + size, 0, 8 - size);
173
174    /* Encrypt and output the block */
175    des_ecb_encrypt(&input, &output, schedule, TRUE);
176
177    for (i = 0; i < 8; i++) {
178      sprintf(out + strlen(out), "%c", ((output[i] & 0xf0) >> 4) + BASE_CODE);
179      sprintf(out + strlen(out), "%c", (output[i] & 0x0f)        + BASE_CODE);
180    }
181
182    if (size < 8) break;
183  }
184  return(0);
185}
186
187
188#define MAX_BUFF 258
189#define MAX_SEARCH 3
190/* Find the class/instance in the .crypt-table */
191char *GetZephyrVarKeyFile(const char *whoami, const char *class, const char *instance) {
192  char *keyfile = NULL;
193  char *varname[MAX_SEARCH];
194  int length[MAX_SEARCH], i;
195  char buffer[MAX_BUFF];
196  char *filename;
197  char result[MAX_SEARCH][MAX_BUFF];
198  int numsearch = 0;
199  FILE *fsearch;
200
201  memset(varname, 0, sizeof(varname));
202
203  /* Determine names to look for in .crypt-table */
204  if (instance) {
205    varname[numsearch++] = owl_sprintf("crypt-%s-%s:", (class?class:"message"), instance);
206  }
207  if (class) {
208    varname[numsearch++] = owl_sprintf("crypt-%s:", class);
209  }
210  varname[numsearch++] = owl_strdup("crypt-default:");
211
212  /* Setup the result array, and determine string lengths */
213  for (i = 0; i < numsearch; i++) {
214    result[i][0] = '\0';
215    length[i] = strlen(varname[i]);
216  }
217
218  /* Open~/.crypt-table */
219  filename = owl_sprintf("%s/.crypt-table", getenv("HOME"));
220  fsearch = fopen(filename, "r");
221  if (fsearch) {
222    /* Scan file for a match */
223    while (!feof(fsearch)) {
224      if (!fgets(buffer, MAX_BUFF - 3, fsearch)) break;
225      for (i = 0; i < numsearch; i++) {
226        if (strncasecmp(varname[i], buffer, length[i]) == 0) {
227          int j;
228          for (j = length[i]; buffer[j] == ' '; j++)
229            ;
230          strcpy(result[i], &buffer[j]);
231          if (*result[i]) {
232            if (result[i][strlen(result[i])-1] == '\n') {
233              result[i][strlen(result[i])-1] = '\0';
234            }
235          }
236        }
237      }
238    }
239
240    /* Pick the "best" match found */
241    keyfile = NULL;
242    for (i = 0; i < numsearch; i++) {
243      if (*result[i]) {
244        keyfile = result[i];
245        break;
246      }
247    }
248
249    if (keyfile == NULL) {
250      /* printf("Could not find key table entry.\n"); */
251    } else {
252      /* Prepare result to be returned */
253      keyfile = owl_strdup(keyfile);
254    }
255   
256    fclose(fsearch);
257  } else {
258    /* printf("Could not open key table file: %s\n", filename); */
259  }
260
261  for(i = 0; i < MAX_SEARCH; i++) {
262    owl_free(varname[i]);
263  }
264
265  owl_free(filename);
266
267  return(keyfile);
268}
269
270#endif
Note: See TracBrowser for help on using the repository browser.