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