Changeset 8bd190d


Ignore:
Timestamp:
Mar 17, 2010, 11:30:30 PM (14 years ago)
Author:
Nelson Elhage <nelhage@ksplice.com>
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)
Message:
zcrypt: Implement AES encryption support using GPG.
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • Makefile

    r4942723 r8bd190d  
    11CFLAGS=$(shell pkg-config glib-2.0 --cflags)
    22LDFLAGS=$(shell pkg-config glib-2.0 --libs)
     3
     4zcrypt: zcrypt.o filterproc.o
     5
  • zcrypt.c

    rc836519 r8bd190d  
    1616#include <stdlib.h>
    1717#include <sys/wait.h>
     18#include <ctype.h>
    1819
    1920#ifdef HAVE_KERBEROS_IV
     
    2324#endif
    2425
    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
    2731
    2832#ifndef TRUE
     
    5256char *BuildArgString(char **argv, int start, int end);
    5357char *read_keystring(char *keyfile);
     58
    5459int do_encrypt(int zephyr, char *class, char *instance,
    5560               ZWRITEOPTIONS *zoptions, char* keyfile, int cipher);
    5661int do_encrypt_des(char *keyfile, char *in, int len, FILE *out);
    57 int do_decrypt(char *keystring);
     62int do_encrypt_aes(char *keyfile, char *in, int len, FILE *out);
     63
     64int do_decrypt(char *keyfile, int cipher);
     65int do_decrypt_aes(char *keyfile);
     66int do_decrypt_des(char *keyfile);
     67
    5868
    5969#define M_NONE            0
     
    298308  {
    299309    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);
    302312    else
    303       do_decrypt(keyfile);
     313      error = !do_decrypt(keyfile, cipher);
    304314  }
    305315
     
    307317  if (mode == M_DECRYPT)
    308318    printf("**END**\n");
    309   return 0;
     319
     320  return error;
    310321}
    311322
    312323int ParseCryptSpec(char *spec, char **keyfile) {
     324  int cipher = CIPHER_DES;
     325  char *cipher_name = strdup(spec);
     326  char *colon = strchr(cipher_name, ':');
     327
    313328  *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;
    315351}
    316352
     
    522558}
    523559
    524 #define MAX_RESULT 2048
    525 
    526560#define BASE_CODE 70
    527561#define LAST_CODE (BASE_CODE + 15)
     
    538572}
    539573
     574char *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
    540606char *GetInputBuffer(ZWRITEOPTIONS *zoptions, int *length) {
    541607  char *buf;
    542   char *inptr;
    543608
    544609  if (zoptions->flags & ZCRYPT_OPT_MESSAGE)
     
    560625    }
    561626
    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);
    585628  }
    586629  return buf;
     
    637680    break;
    638681  case CIPHER_AES:
    639     out = FALSE;
     682    out = do_encrypt_aes(keyfile, inbuff, buflen, outfile);
    640683    break;
    641684  }
     
    708751}
    709752
     753int 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
    710778/* Read a half-byte from stdin, skipping invalid characters.  Returns -1
    711779   if at EOF or file error */
     
    759827
    760828/* Decrypt stdin */
    761 int do_decrypt(char *keystring)
    762 {
     829int 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
     841int 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
     869int do_decrypt_des(char *keyfile) {
    763870  des_key_schedule schedule;
    764871  unsigned char input[8], output[8];
     872  char *keystring;
    765873
    766874  output[0] = '\0';    /* In case no message at all                 */
    767875
     876  keystring = read_keystring(keyfile);
     877  if(!keystring) return FALSE;
     878
    768879  owl_zcrypt_string_to_schedule(keystring, &schedule);
     880
     881  free(keystring);
    769882
    770883  while (read_ascii_block(input))
Note: See TracChangeset for help on using the changeset viewer.