From e6e924374d49aa14e94a28d9b64a9f63bae2426d Mon Sep 17 00:00:00 2001 From: sin Date: Mon, 22 Sep 2014 11:27:30 +0100 Subject: Add support for encrypted save files --- ratox.c | 49 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 6 deletions(-) (limited to 'ratox.c') diff --git a/ratox.c b/ratox.c index d3a53ba..9c68003 100644 --- a/ratox.c +++ b/ratox.c @@ -18,9 +18,11 @@ #include #include +#include #include "arg.h" #include "queue.h" +#include "readpassphrase.h" #define LEN(x) (sizeof (x) / sizeof *(x)) #define DATAFILE ".ratox.data" @@ -161,6 +163,8 @@ static TAILQ_HEAD(reqhead, request) reqhead = TAILQ_HEAD_INITIALIZER(reqhead); static Tox *tox; static Tox_Options toxopt; +static uint8_t *passphrase; +static uint32_t pplen; static int running = 1; static int ipv6; @@ -176,6 +180,7 @@ static void cbstatusmessage(Tox *, int32_t, const uint8_t *, uint16_t, void *); static void cbuserstatus(Tox *, int32_t, uint8_t, void *); static void cbfilecontrol(Tox *, int32_t, uint8_t, uint8_t, uint8_t, const uint8_t *, uint16_t, void *); static void sendfriendfile(struct friend *); +static void readpass(void); static void dataload(void); static void datasave(void); static int localinit(void); @@ -547,6 +552,25 @@ sendfriendtext(struct friend *f) tox_send_message(tox, f->fid, buf, n); } +static void +readpass(void) +{ + char pass[BUFSIZ], *p; + + p = readpassphrase("Password: ", pass, sizeof(pass), RPP_ECHO_OFF); + if (!p) { + perror("readpassphrase"); + exit(EXIT_FAILURE); + } + passphrase = malloc(strlen(p)); /* not null-terminated */ + if (!passphrase) { + perror("malloc"); + exit(EXIT_FAILURE); + } + memcpy(passphrase, p, strlen(p)); + pplen = strlen(p); +} + static void dataload(void) { @@ -555,6 +579,9 @@ dataload(void) uint8_t *data; int r; + if (encryptsave == 1) + readpass(); + fp = fopen(DATAFILE, "r"); if (!fp) return; @@ -573,13 +600,20 @@ dataload(void) fprintf(stderr, "failed to read %s\n", DATAFILE); exit(EXIT_FAILURE); } - r = tox_load(tox, data, sz); + + if (encryptsave == 1) + r = tox_encrypted_load(tox, data, sz, passphrase, pplen); + else + r = tox_load(tox, data, sz); if (r < 0) { - fprintf(stderr, "tox_load() failed\n"); + fprintf(stderr, "%s failed\n", + encryptsave == 1 ? "tox_encrypted_load" : "tox_load"); + exit(EXIT_FAILURE); + } else if (r == 1) { + fprintf(stderr, "Found encrypted %s but encryption is disabled\n", + DATAFILE); exit(EXIT_FAILURE); } - if (r == 1) - printf("Found encrypted data in %s\n", DATAFILE); free(data); fclose(fp); @@ -598,14 +632,17 @@ datasave(void) exit(EXIT_FAILURE); } - sz = tox_size(tox); + sz = encryptsave == 1 ? tox_encrypted_size(tox) : tox_size(tox); data = malloc(sz); if (!data) { perror("malloc"); exit(EXIT_FAILURE); } - tox_save(tox, data); + if (encryptsave == 1) + tox_encrypted_save(tox, data, passphrase, pplen); + else + tox_save(tox, data); if (fwrite(data, 1, sz, fp) != sz || ferror(fp)) { fprintf(stderr, "failed to write %s\n", DATAFILE); exit(EXIT_FAILURE); -- cgit v1.2.3