summaryrefslogtreecommitdiff
path: root/ratox.c
diff options
context:
space:
mode:
authorsin <sin@2f30.org>2014-09-22 11:27:30 +0100
committersin <sin@2f30.org>2014-09-22 11:27:30 +0100
commite6e924374d49aa14e94a28d9b64a9f63bae2426d (patch)
tree8ac09e4a34c12b3b8b670e40f8b6b26a264bd1fa /ratox.c
parent31c8d76ff28eee958fdbc3c84918a53040968860 (diff)
Add support for encrypted save files
Diffstat (limited to 'ratox.c')
-rw-r--r--ratox.c49
1 files changed, 43 insertions, 6 deletions
diff --git a/ratox.c b/ratox.c
index d3a53ba..9c68003 100644
--- a/ratox.c
+++ b/ratox.c
@@ -18,9 +18,11 @@
#include <unistd.h>
#include <tox/tox.h>
+#include <tox/toxencryptsave.h>
#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);
@@ -548,6 +553,25 @@ sendfriendtext(struct friend *f)
}
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)
{
FILE *fp;
@@ -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);