source: zcrypt.c @ 8fff19a

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