- Timestamp:
- Mar 17, 2010, 11:30:30 PM (15 years ago)
- Branches:
- master, release-1.10, release-1.6, release-1.7, release-1.8, release-1.9
- Children:
- 6d7f2a8
- Parents:
- c836519
- git-author:
- Nelson Elhage <nelhage@ksplice.com> (03/17/10 21:37:45)
- git-committer:
- Nelson Elhage <nelhage@ksplice.com> (03/17/10 23:30:30)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
zcrypt.c
rc836519 r8bd190d 16 16 #include <stdlib.h> 17 17 #include <sys/wait.h> 18 #include <ctype.h> 18 19 19 20 #ifdef HAVE_KERBEROS_IV … … 23 24 #endif 24 25 25 #define MAX_KEY 128 26 #define MAX_LINE 128 26 #include "filterproc.h" 27 28 #define MAX_KEY 128 29 #define MAX_LINE 128 30 #define MAX_RESULT 4096 27 31 28 32 #ifndef TRUE … … 52 56 char *BuildArgString(char **argv, int start, int end); 53 57 char *read_keystring(char *keyfile); 58 54 59 int do_encrypt(int zephyr, char *class, char *instance, 55 60 ZWRITEOPTIONS *zoptions, char* keyfile, int cipher); 56 61 int do_encrypt_des(char *keyfile, char *in, int len, FILE *out); 57 int do_decrypt(char *keystring); 62 int do_encrypt_aes(char *keyfile, char *in, int len, FILE *out); 63 64 int do_decrypt(char *keyfile, int cipher); 65 int do_decrypt_aes(char *keyfile); 66 int do_decrypt_des(char *keyfile); 67 58 68 59 69 #define M_NONE 0 … … 298 308 { 299 309 if (mode == M_ZEPHYR_ENCRYPT || mode == M_ENCRYPT) 300 do_encrypt((mode == M_ZEPHYR_ENCRYPT), class, instance,301 &zoptions, keyfile, cipher);310 error = !do_encrypt((mode == M_ZEPHYR_ENCRYPT), class, instance, 311 &zoptions, keyfile, cipher); 302 312 else 303 do_decrypt(keyfile);313 error = !do_decrypt(keyfile, cipher); 304 314 } 305 315 … … 307 317 if (mode == M_DECRYPT) 308 318 printf("**END**\n"); 309 return 0; 319 320 return error; 310 321 } 311 322 312 323 int ParseCryptSpec(char *spec, char **keyfile) { 324 int cipher = CIPHER_DES; 325 char *cipher_name = strdup(spec); 326 char *colon = strchr(cipher_name, ':'); 327 313 328 *keyfile = spec; 314 return CIPHER_DES; 329 330 if (colon) { 331 char *rest = strchr(spec, ':') + 1; 332 while(isspace(*rest)) rest++; 333 334 *colon-- = '\0'; 335 while (colon >= cipher_name && isspace(*colon)) { 336 *colon = '\0'; 337 } 338 339 if(strcmp(cipher_name, "AES") == 0) { 340 cipher = CIPHER_AES; 341 *keyfile = rest; 342 } else if(strcmp(cipher_name, "DES") == 0) { 343 cipher = CIPHER_DES; 344 *keyfile = rest; 345 } 346 } 347 348 free(cipher_name); 349 350 return cipher; 315 351 } 316 352 … … 522 558 } 523 559 524 #define MAX_RESULT 2048525 526 560 #define BASE_CODE 70 527 561 #define LAST_CODE (BASE_CODE + 15) … … 538 572 } 539 573 574 char *slurp_stdin(int ignoredot, int *length) { 575 char *buf; 576 char *inptr; 577 578 if ((inptr = buf = (char *)malloc(MAX_RESULT)) == NULL) 579 { 580 fprintf(stderr, "Memory allocation error\n"); 581 return NULL; 582 } 583 while (inptr - buf < MAX_RESULT - MAX_LINE - 20) 584 { 585 if (fgets(inptr, MAX_LINE, stdin) == NULL) 586 break; 587 588 if (inptr[0]) 589 { 590 if (inptr[0] == '.' && inptr[1] == '\n' && !ignoredot) 591 { 592 inptr[0] = '\0'; 593 break; 594 } 595 else 596 inptr += strlen(inptr); 597 } 598 else 599 break; 600 } 601 *length = inptr - buf; 602 603 return buf; 604 } 605 540 606 char *GetInputBuffer(ZWRITEOPTIONS *zoptions, int *length) { 541 607 char *buf; 542 char *inptr;543 608 544 609 if (zoptions->flags & ZCRYPT_OPT_MESSAGE) … … 560 625 } 561 626 562 if ((inptr = buf = (char *)malloc(MAX_RESULT)) == NULL) 563 { 564 fprintf(stderr, "Memory allocation error\n"); 565 return NULL; 566 } 567 while (inptr - buf < MAX_RESULT - MAX_LINE - 20) 568 { 569 if (!fgets(inptr, MAX_LINE, stdin)) break; 570 if (inptr[0]) 571 { 572 if (inptr[0] == '.' && inptr[1] == '\n' && 573 !(zoptions->flags & ZCRYPT_OPT_IGNOREDOT)) 574 { 575 inptr[0] = '\0'; 576 break; 577 } 578 else 579 inptr += strlen(inptr); 580 } 581 else 582 break; 583 } 584 *length = inptr - buf; 627 buf = slurp_stdin(zoptions->flags & ZCRYPT_OPT_IGNOREDOT, length); 585 628 } 586 629 return buf; … … 637 680 break; 638 681 case CIPHER_AES: 639 out = FALSE;682 out = do_encrypt_aes(keyfile, inbuff, buflen, outfile); 640 683 break; 641 684 } … … 708 751 } 709 752 753 int do_encrypt_aes(char *keyfile, char *in, int length, FILE *outfile) 754 { 755 char *out; 756 int err, status; 757 const char *argv[] = { 758 "gpg", 759 "--symmetric", 760 "--batch", 761 "--quiet", 762 "--no-use-agent", 763 "--armor", 764 "--cipher-algo", "AES", 765 "--passphrase-file", keyfile, 766 NULL 767 }; 768 err = call_filter("gpg", argv, in, &out, &status); 769 if(err || status) { 770 if(out) g_free(out); 771 return FALSE; 772 } 773 fwrite(out, strlen(out), 1, outfile); 774 g_free(out); 775 return TRUE; 776 } 777 710 778 /* Read a half-byte from stdin, skipping invalid characters. Returns -1 711 779 if at EOF or file error */ … … 759 827 760 828 /* Decrypt stdin */ 761 int do_decrypt(char *keystring) 762 { 829 int do_decrypt(char *keyfile, int cipher) 830 { 831 switch(cipher) { 832 case CIPHER_DES: 833 return do_decrypt_des(keyfile); 834 case CIPHER_AES: 835 return do_decrypt_aes(keyfile); 836 default: 837 return FALSE; 838 } 839 } 840 841 int do_decrypt_aes(char *keyfile) { 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 869 int do_decrypt_des(char *keyfile) { 763 870 des_key_schedule schedule; 764 871 unsigned char input[8], output[8]; 872 char *keystring; 765 873 766 874 output[0] = '\0'; /* In case no message at all */ 767 875 876 keystring = read_keystring(keyfile); 877 if(!keystring) return FALSE; 878 768 879 owl_zcrypt_string_to_schedule(keystring, &schedule); 880 881 free(keystring); 769 882 770 883 while (read_ascii_block(input))
Note: See TracChangeset
for help on using the changeset viewer.