source: zcrypt.c @ 0fa0745

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