diff --git a/encrypt.c b/encrypt.c index 3e5e7d9..97f80a9 100644 --- a/encrypt.c +++ b/encrypt.c @@ -7,14 +7,38 @@ #include #include +#include "libbaseencode/baseencode.h" + #include "purr.h" -int encrypt_FILE(FILE **filep, uint8_t **keyp, uint8_t **ivp, char **tempp) +#define MAX_FILES 32 +static char *files_to_delete[32] = { 0 }; + +static bool called_atexit = false; + +static void clean_up_files(void) { + for (int i = 0; i < MAX_FILES && files_to_delete[i]; i++) { + unlink(files_to_delete[i]); + free(files_to_delete[i]); + } +} + +/* + * This function takes a FILE pointer, and creates an encrypted file from it. + * The created file is passed to an atexit function so it can be deleted automatically. + * Args: + * filep: original FILE pointer, will be closed and changed for the new encrypted FILE + * keyp: will receive the newly generated random key + * ivp: will receive the newly generated random IV (if enabled in purr.h) + */ +int encrypt_FILE(FILE **filep, uint8_t **keyp, uint8_t **ivp) +{ + if (!called_atexit) { + atexit(clean_up_files); + } + FILE *input = *filep; - uint8_t *key = *keyp; - uint8_t *iv = *ivp; - char *temp = *tempp; if (input == stdin) { fputs("currently can't encrypt stdin!\n", stderr); @@ -31,8 +55,8 @@ int encrypt_FILE(FILE **filep, uint8_t **keyp, uint8_t **ivp, char **tempp) if (blocks * br_aes_big_BLOCK_SIZE < file_size) blocks++; file_size = blocks * br_aes_big_BLOCK_SIZE; - key = calloc(KEY_LEN, 1); - iv = calloc(IV_LEN, 1); + uint8_t *key = calloc(KEY_LEN, 1); + uint8_t *iv = calloc(IV_LEN, 1); if (key == NULL || iv == NULL) { perror("allocation failure"); return -1; @@ -52,11 +76,20 @@ int encrypt_FILE(FILE **filep, uint8_t **keyp, uint8_t **ivp, char **tempp) } #endif - temp = strdup("/tmp/purrito.XXXXXX"); + char temp[] = "/tmp/purrito.XXXXXX"; int tfd = mkstemp(temp); if (tfd < 0) { perror("couldn't create temp file"); return -1; + } else { + // add cleanup for file + int i = 0; + for (; i < MAX_FILES && files_to_delete[i]; i++); + if (i == MAX_FILES) { + fputs("couldn't add file to files_to_delete\n", stderr); + } else { + files_to_delete[i] = strdup(temp); + } } int errfa = posix_fallocate(tfd, 0, file_size); if (errfa) { @@ -85,13 +118,14 @@ int encrypt_FILE(FILE **filep, uint8_t **keyp, uint8_t **ivp, char **tempp) fclose(input); munmap(temp_map, file_size); - input = fopen(temp, "r"); + // pass pointers to caller + *filep = fopen(temp, "r"); if (input == NULL) { perror("couldn't read temp file"); return -1; } - fstat(fileno(input), &s); - fprintf(stderr, "output file size: %lu\n", s.st_size); + *keyp = key; + *ivp = iv; return 0; } diff --git a/purr.c b/purr.c index 6ff31f0..c3a70a8 100644 --- a/purr.c +++ b/purr.c @@ -221,10 +221,9 @@ int main (int argc, char **argv) uint8_t *key = NULL; uint8_t *iv = NULL; - char *temp = NULL; if (send && encrypt) { // requires error checking - encrypt_FILE(&input, &key, &iv, &temp); + encrypt_FILE(&input, &key, &iv); } int socket = host_connect(link, port, debug); @@ -245,6 +244,10 @@ int main (int argc, char **argv) rv = send_and_receive(&ci); + if (encrypt) { + print_hex(key, KEY_LEN, true); + } + //out: close(socket); free(link); @@ -253,7 +256,6 @@ int main (int argc, char **argv) free(request); free(key); free(iv); - free(temp); early_out: if (input != stdin) fclose(input); if (output != stdout) fclose(output); diff --git a/purr.h b/purr.h index b2f41e6..22b440c 100644 --- a/purr.h +++ b/purr.h @@ -62,6 +62,6 @@ int send_and_receive(struct connection_information *); char *print_hex(uint8_t *, int, bool); /* encrypt.c */ -int encrypt_FILE(FILE **, uint8_t **, uint8_t **, char **); +int encrypt_FILE(FILE **, uint8_t **, uint8_t **); #endif // __PURR_H_