source: zcrypt.c @ a1074de

release-1.6release-1.7release-1.8release-1.9
Last change on this file since a1074de was 732cc15, checked in by Anders Kaseorg <andersk@mit.edu>, 11 years ago
owl_zcrypt_decrypt, owl_zcrypt_encrypt: Don’t leak the key filename. Signed-off-by: Anders Kaseorg <andersk@mit.edu> Reviewed-by: Alejandro R. Sedeño <asedeno@mit.edu>
  • Property mode set to 100644
File size: 7.6 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(const 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'.  Return must be freed by caller.
79 */
80char *owl_zcrypt_decrypt(const char *in, const char *class, const char *instance)
81{
82  char *fname, keystring[MAX_KEY];
83  FILE *fkey;
84 
85  fname=GetZephyrVarKeyFile("zcrypt", class, instance);
86  if (!fname) return NULL;
87  fkey=fopen(fname, "r");
88  owl_free(fname);
89  if (!fkey) return NULL;
90  if (!fgets(keystring, MAX_KEY-1, fkey)) {
91    fclose(fkey);
92    return NULL;
93  }
94  fclose(fkey);
95
96  return owl_zcrypt_decrypt_with_key(in, keystring);
97}
98
99char *owl_zcrypt_decrypt_with_key(const char *in, const char *keystring)
100{
101  const char *inptr, *endptr;
102  char *out;
103  des_key_schedule schedule;
104  unsigned char input[8], output[8];
105  int i, c1, c2;
106
107  /* We read in some number of full 16-byte blocks and write out the
108   * same number of 8-byte blocks, plus a null terminator.
109   */
110  out = owl_malloc((strlen(in) / 16) * 8 + 1);
111
112  strcpy(out, "");
113
114  output[0] = '\0';    /* In case no message at all                 */
115
116  owl_zcrypt_string_to_schedule(keystring, &schedule);
117
118  inptr=in;
119  endptr = in + strlen(in);
120  while (inptr + 16 <= endptr) {
121    for (i=0; i<8; i++) {
122      c1=(inptr[0])-BASE_CODE;
123      c2=(inptr[1])-BASE_CODE;
124      input[i]=c1 * 0x10 + c2;
125      inptr+=2;
126    }
127    des_ecb_encrypt(&input, &output, schedule, FALSE);
128    strncat(out, (const char *)output, 8);
129  }
130
131  return out;
132}
133
134char *owl_zcrypt_encrypt(const char *in, const char *class, const char *instance)
135{
136  char *fname, keystring[MAX_KEY];
137  FILE *fkey;
138
139  fname=GetZephyrVarKeyFile("zcrypt", class, instance);
140  if (!fname) return NULL;
141  fkey=fopen(fname, "r");
142  owl_free(fname);
143  if (!fkey) return NULL;
144  if (!fgets(keystring, MAX_KEY-1, fkey)) {
145    fclose(fkey);
146    return NULL;
147  }
148  fclose(fkey);
149
150  return owl_zcrypt_encrypt_with_key(in, keystring);
151}
152
153char *owl_zcrypt_encrypt_with_key(const char *in, const char *keystring)
154{
155  des_key_schedule schedule;
156  char *out;
157  unsigned char input[8], output[8];
158  int size, length, i;
159  const char *inbuff = NULL, *inptr;
160  int num_blocks=0, last_block_size=0;
161
162  owl_zcrypt_string_to_schedule(keystring, &schedule);
163
164  /* Allocate enough space for the crypted message. For each byte of
165   * the message, the encoded cyphertext will have two bytes. Block
166   * size is 8 bytes of input, or 16 bytes of output, so make sure we
167   * have at least one block worth of space allocated. If the message
168   * is empty, no blocks are sent, but we still allocate one
169   * block. The additional 16 bytes also provide space for the null
170   * terminator, as we will never use all of it for cyphertext.
171   */
172  out = owl_malloc((strlen(in) * 2) + 16);
173
174  inbuff=in;
175  length=strlen(inbuff);
176  num_blocks=(length+7)/8;
177  last_block_size=((length+7)%8)+1;
178
179  strcpy(out, "");
180 
181  inptr=inbuff;
182  while (TRUE) {
183    /* Get 8 bytes from buffer */
184    if (num_blocks > 1) {
185      size = 8;
186      memcpy(input, inptr, size);
187      inptr+=8;
188      num_blocks--;
189    } else if (num_blocks == 1) {
190      size=last_block_size;
191      memcpy(input, inptr, size);
192      num_blocks--;
193    } else {
194      size=0;
195    }
196
197    /* Check for EOF and pad the string to 8 chars, if needed */
198    if (size == 0) break;     /* END OF INPUT: BREAK FROM while LOOP! */
199     
200    if (size<8) memset(input + size, 0, 8 - size);
201
202    /* Encrypt and output the block */
203    des_ecb_encrypt(&input, &output, schedule, TRUE);
204
205    for (i = 0; i < 8; i++) {
206      sprintf(out + strlen(out), "%c", ((output[i] & 0xf0) >> 4) + BASE_CODE);
207      sprintf(out + strlen(out), "%c", (output[i] & 0x0f)        + BASE_CODE);
208    }
209
210    if (size < 8) break;
211  }
212  return out;
213}
214
215
216#define MAX_BUFF 258
217#define MAX_SEARCH 3
218/* Find the class/instance in the .crypt-table */
219char *GetZephyrVarKeyFile(const char *whoami, const char *class, const char *instance) {
220  char *keyfile = NULL;
221  char *varname[MAX_SEARCH];
222  int length[MAX_SEARCH], i;
223  char buffer[MAX_BUFF];
224  char *filename;
225  char result[MAX_SEARCH][MAX_BUFF];
226  int numsearch = 0;
227  FILE *fsearch;
228
229  memset(varname, 0, sizeof(varname));
230
231  /* Determine names to look for in .crypt-table */
232  if (instance) {
233    varname[numsearch++] = owl_sprintf("crypt-%s-%s:", (class?class:"message"), instance);
234  }
235  if (class) {
236    varname[numsearch++] = owl_sprintf("crypt-%s:", class);
237  }
238  varname[numsearch++] = owl_strdup("crypt-default:");
239
240  /* Setup the result array, and determine string lengths */
241  for (i = 0; i < numsearch; i++) {
242    result[i][0] = '\0';
243    length[i] = strlen(varname[i]);
244  }
245
246  /* Open~/.crypt-table */
247  filename = owl_sprintf("%s/.crypt-table", getenv("HOME"));
248  fsearch = fopen(filename, "r");
249  if (fsearch) {
250    /* Scan file for a match */
251    while (!feof(fsearch)) {
252      if (!fgets(buffer, MAX_BUFF - 3, fsearch)) break;
253      for (i = 0; i < numsearch; i++) {
254        if (strncasecmp(varname[i], buffer, length[i]) == 0) {
255          int j;
256          for (j = length[i]; buffer[j] == ' '; j++)
257            ;
258          strcpy(result[i], &buffer[j]);
259          if (*result[i]) {
260            if (result[i][strlen(result[i])-1] == '\n') {
261              result[i][strlen(result[i])-1] = '\0';
262            }
263          }
264        }
265      }
266    }
267
268    /* Pick the "best" match found */
269    keyfile = NULL;
270    for (i = 0; i < numsearch; i++) {
271      if (*result[i]) {
272        keyfile = result[i];
273        break;
274      }
275    }
276
277    if (keyfile == NULL) {
278      /* printf("Could not find key table entry.\n"); */
279    } else {
280      /* Prepare result to be returned */
281      keyfile = owl_strdup(keyfile);
282    }
283   
284    fclose(fsearch);
285  } else {
286    /* printf("Could not open key table file: %s\n", filename); */
287  }
288
289  for(i = 0; i < MAX_SEARCH; i++) {
290    owl_free(varname[i]);
291  }
292
293  owl_free(filename);
294
295  return(keyfile);
296}
297
298#endif
Note: See TracBrowser for help on using the repository browser.