| 1 |
#include "config.h" |
| 2 |
#include <sys/types.h> |
| 3 |
#include <fcntl.h> |
| 4 |
#include <unistd.h> |
| 5 |
#include <stdlib.h> |
| 6 |
#include <stdio.h> |
| 7 |
#include <syslog.h> |
| 8 |
#include <string.h> |
| 9 |
#include <strings.h> |
| 10 |
#include <errno.h> |
| 11 |
#include <openssl/evp.h> |
| 12 |
|
| 13 |
char *cipher (char *, int, int); |
| 14 |
int decrypt(void); |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
char * |
| 28 |
cipher(char *buf, int buflen, int do_encrypt) |
| 29 |
{ |
| 30 |
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); |
| 31 |
static unsigned char *out = NULL; |
| 32 |
static int outlen = 0; |
| 33 |
static int init = 0, which, blocksize; |
| 34 |
int n; |
| 35 |
|
| 36 |
if (!init) { |
| 37 |
static const EVP_CIPHER *cipher; |
| 38 |
unsigned char key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH]; |
| 39 |
|
| 40 |
const char *keyfile = ".ermt.key"; |
| 41 |
unsigned char buf[128]; |
| 42 |
FILE *fp; |
| 43 |
int i; |
| 44 |
|
| 45 |
openlog("ermt", LOG_PID, LOG_DAEMON); |
| 46 |
|
| 47 |
|
| 48 |
cipher = EVP_bf_cbc(); |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
if ((fp = fopen(keyfile, "r")) == NULL) { |
| 55 |
syslog(LOG_ERR, "Can't open key file %s: %m", keyfile); |
| 56 |
return NULL; |
| 57 |
} |
| 58 |
buf[0] = '\0'; |
| 59 |
if (!fgets((char*)buf, sizeof buf, fp)) { |
| 60 |
syslog(LOG_ERR, "Error reading key file %s: %m", keyfile); |
| 61 |
return NULL; |
| 62 |
} |
| 63 |
fclose(fp); |
| 64 |
i = strlen((char*)buf); |
| 65 |
if ((i > 0) && |
| 66 |
((buf[i-1] == '\n') || (buf[i-1] == '\r'))) |
| 67 |
buf[--i]='\0'; |
| 68 |
if ((i > 0) && |
| 69 |
((buf[i-1] == '\n') || (buf[i-1] == '\r'))) |
| 70 |
buf[--i]='\0'; |
| 71 |
if (i < 1) { |
| 72 |
syslog(LOG_ERR, "zero length key"); |
| 73 |
errno = EINVAL; |
| 74 |
return NULL; |
| 75 |
} |
| 76 |
EVP_BytesToKey(cipher, EVP_md5(), NULL, |
| 77 |
buf, strlen((char*)buf), 1, key, iv); |
| 78 |
EVP_CIPHER_CTX_init(ctx); |
| 79 |
EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, do_encrypt); |
| 80 |
EVP_CIPHER_CTX_set_padding(ctx, 0); |
| 81 |
OPENSSL_cleanse(buf, sizeof buf); |
| 82 |
OPENSSL_cleanse(key, sizeof key); |
| 83 |
OPENSSL_cleanse(iv, sizeof iv); |
| 84 |
blocksize = EVP_CIPHER_CTX_block_size(ctx); |
| 85 |
which = do_encrypt; |
| 86 |
init = 1; |
| 87 |
} |
| 88 |
if (which != do_encrypt) { |
| 89 |
syslog(LOG_ERR, "Cannot switch modes"); |
| 90 |
errno = EINVAL; |
| 91 |
return NULL; |
| 92 |
} |
| 93 |
if ((buflen % blocksize) != 0) { |
| 94 |
syslog(LOG_ERR, "Buffer size is not a multiple of cipher block size"); |
| 95 |
errno = EINVAL; |
| 96 |
return NULL; |
| 97 |
} |
| 98 |
if (outlen < buflen+blocksize) { |
| 99 |
outlen = (buflen+blocksize) * 2; |
| 100 |
out = realloc(out, outlen); |
| 101 |
} |
| 102 |
if (!EVP_CipherUpdate(ctx, out, &n, (unsigned char*)buf, buflen)) { |
| 103 |
syslog(LOG_ERR, "EVP_CipherUpdate failed"); |
| 104 |
errno = EINVAL; |
| 105 |
return NULL; |
| 106 |
} |
| 107 |
if (n != buflen) { |
| 108 |
syslog(LOG_ERR, "EVP_CipherUpdate: %d != %d", n, buflen); |
| 109 |
errno = EINVAL; |
| 110 |
return NULL; |
| 111 |
} |
| 112 |
|
| 113 |
EVP_CIPHER_CTX_free(ctx); |
| 114 |
return (char*)out; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
int |
| 119 |
decrypt(void) |
| 120 |
{ |
| 121 |
char buf[8*1024], *cp; |
| 122 |
int n; |
| 123 |
|
| 124 |
while ((n = fread(buf, 1, sizeof buf, stdin)) > 0) { |
| 125 |
if ((cp = cipher(buf, n, 0)) == NULL) { |
| 126 |
fprintf(stderr, "ermt: Error decoding input; see daemon.err syslog\n"); |
| 127 |
exit(1); |
| 128 |
} |
| 129 |
fwrite(cp, 1, n, stdout); |
| 130 |
} |
| 131 |
if (ferror(stdin)) { |
| 132 |
perror("ermt: stdin: read"); |
| 133 |
exit(1); |
| 134 |
} |
| 135 |
if (fflush(stdout)) { |
| 136 |
perror("ermt: stdout: write"); |
| 137 |
exit(1); |
| 138 |
} |
| 139 |
exit(0); |
| 140 |
} |