1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-23 03:03:02 +01:00

fix encryption API breakage

This commit is contained in:
Jfreegman 2015-03-31 21:56:11 -04:00
parent eaea68c33e
commit e998c8a866
No known key found for this signature in database
GPG Key ID: 3627F3144076AE63
3 changed files with 59 additions and 50 deletions

View File

@ -206,9 +206,7 @@ static void chat_onConnectionChange(ToxWindow *self, Tox *m, uint32_t num, TOX_C
char nick[TOX_MAX_NAME_LENGTH]; char nick[TOX_MAX_NAME_LENGTH];
get_nick_truncate(m, nick, num); get_nick_truncate(m, nick, num);
statusbar->connection = connection_status; if (connection_status != TOX_CONNECTION_NONE && statusbar->connection == TOX_CONNECTION_NONE) {
if (connection_status != TOX_CONNECTION_NONE) {
Friends.list[num].is_typing = user_settings->show_typing_other == SHOW_TYPING_ON Friends.list[num].is_typing = user_settings->show_typing_other == SHOW_TYPING_ON
? tox_friend_get_typing(m, num, NULL) : false; ? tox_friend_get_typing(m, num, NULL) : false;
chat_resume_file_transfers(m, num); chat_resume_file_transfers(m, num);
@ -216,7 +214,7 @@ static void chat_onConnectionChange(ToxWindow *self, Tox *m, uint32_t num, TOX_C
msg = "has come online"; msg = "has come online";
line_info_add(self, timefrmt, nick, NULL, CONNECTION, 0, GREEN, msg); line_info_add(self, timefrmt, nick, NULL, CONNECTION, 0, GREEN, msg);
write_to_log(msg, nick, ctx->log, true); write_to_log(msg, nick, ctx->log, true);
} else { } else if (connection_status == TOX_CONNECTION_NONE) {
Friends.list[num].is_typing = false; Friends.list[num].is_typing = false;
if (self->chatwin->self_is_typing) if (self->chatwin->self_is_typing)
@ -228,6 +226,8 @@ static void chat_onConnectionChange(ToxWindow *self, Tox *m, uint32_t num, TOX_C
line_info_add(self, timefrmt, nick, NULL, DISCONNECTION, 0, RED, msg); line_info_add(self, timefrmt, nick, NULL, DISCONNECTION, 0, RED, msg);
write_to_log(msg, nick, ctx->log, true); write_to_log(msg, nick, ctx->log, true);
} }
statusbar->connection = connection_status;
} }
static void chat_onTypingChange(ToxWindow *self, Tox *m, uint32_t num, bool is_typing) static void chat_onTypingChange(ToxWindow *self, Tox *m, uint32_t num, bool is_typing)

View File

@ -460,7 +460,7 @@ static void first_time_encrypt(const char *msg)
valid_password = true; valid_password = true;
} }
queue_init_message("Data file '%s' will be encrypted", DATA_FILE); queue_init_message("Data file '%s' is encrypted", DATA_FILE);
memset(passconfirm, 0, sizeof(passconfirm)); memset(passconfirm, 0, sizeof(passconfirm));
user_password.data_is_encrypted = true; user_password.data_is_encrypted = true;
} }
@ -468,44 +468,51 @@ static void first_time_encrypt(const char *msg)
system("clear"); system("clear");
} }
/* /* Store Tox data to given location
* Store Messenger to given location *
* Return 0 if stored successfully or ignoring data file. Return -1 on error * Return 0 if stored successfully or ignoring data file.
* Return -1 on error
*/ */
int store_data(Tox *m, const char *path) int store_data(Tox *m, const char *path)
{ {
if (path == NULL) if (path == NULL)
return -1; return -1;
size_t len = user_password.data_is_encrypted ? tox_encrypted_size(m) : tox_get_savedata_size(m);
char *buf = malloc(len);
if (buf == NULL)
exit_toxic_err("failed in store_data", FATALERR_MEMORY);
if (user_password.data_is_encrypted && !arg_opts.unencrypt_data) {
if (tox_encrypted_save(m, (uint8_t *) buf, (uint8_t *) user_password.pass, user_password.len) != 0) {
free(buf);
return -1;
}
} else {
tox_get_savedata(m, (uint8_t *) buf);
}
FILE *fp = fopen(path, "wb"); FILE *fp = fopen(path, "wb");
if (fp == NULL) { if (fp == NULL)
free(buf);
return -1; return -1;
}
if (fwrite(buf, len, 1, fp) != 1) { size_t data_len = tox_get_savedata_size(m);
free(buf); char data[data_len];
tox_get_savedata(m, (uint8_t *) data);
if (user_password.data_is_encrypted && !arg_opts.unencrypt_data) {
size_t enc_len = data_len + TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
char enc_data[enc_len];
TOX_ERR_ENCRYPTION err;
tox_pass_encrypt((uint8_t *) data, data_len, (uint8_t *) user_password.pass, user_password.len,
(uint8_t *) enc_data, &err);
if (err != TOX_ERR_ENCRYPTION_OK) {
fprintf(stderr, "tox_pass_encrypt() failed with error %d\n", err);
fclose(fp); fclose(fp);
return -1; return -1;
} }
free(buf); if (fwrite(enc_data, enc_len, 1, fp) != 1) {
fclose(fp);
return -1;
}
} else {
if (fwrite(data, data_len, 1, fp) != 1) {
fclose(fp);
return -1;
}
}
fclose(fp); fclose(fp);
return 0; return 0;
} }
@ -570,7 +577,7 @@ static Tox *load_tox(char *data_path, struct Tox_Options *tox_opts)
FILE *fp = fopen(data_path, "rb"); FILE *fp = fopen(data_path, "rb");
if (fp != NULL) { if (fp != NULL) { /* Data file exists */
off_t len = file_size(data_path); off_t len = file_size(data_path);
if (len == 0) { if (len == 0) {
@ -578,20 +585,14 @@ static Tox *load_tox(char *data_path, struct Tox_Options *tox_opts)
exit_toxic_err("failed in load_toxic", FATALERR_FILEOP); exit_toxic_err("failed in load_toxic", FATALERR_FILEOP);
} }
char *buf = malloc(len); char data[len];
if (buf == NULL) { if (fread(data, sizeof(data), 1, fp) != 1) {
fclose(fp);
exit_toxic_err("failed in load_toxic", FATALERR_MEMORY);
}
if (fread(buf, len, 1, fp) != 1) {
free(buf);
fclose(fp); fclose(fp);
exit_toxic_err("failed in load_toxic", FATALERR_FILEOP); exit_toxic_err("failed in load_toxic", FATALERR_FILEOP);
} }
bool is_encrypted = tox_is_data_encrypted((uint8_t *) buf); bool is_encrypted = tox_is_data_encrypted((uint8_t *) data);
/* attempt to encrypt an already encrypted data file */ /* attempt to encrypt an already encrypted data file */
if (arg_opts.encrypt_data && is_encrypted) if (arg_opts.encrypt_data && is_encrypted)
@ -610,6 +611,9 @@ static Tox *load_tox(char *data_path, struct Tox_Options *tox_opts)
system("clear"); // TODO: is this portable? system("clear"); // TODO: is this portable?
printf("Enter password (q to quit) "); printf("Enter password (q to quit) ");
size_t plain_len = len - TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
char plain[plain_len];
while (true) { while (true) {
pwlen = password_prompt(user_password.pass, sizeof(user_password.pass)); pwlen = password_prompt(user_password.pass, sizeof(user_password.pass));
user_password.len = pwlen; user_password.len = pwlen;
@ -624,31 +628,36 @@ static Tox *load_tox(char *data_path, struct Tox_Options *tox_opts)
continue; continue;
} }
TOX_ERR_ENCRYPTED_NEW enc_err; TOX_ERR_DECRYPTION pwerr;
m = tox_encrypted_new(tox_opts, (uint8_t *) buf, len, (uint8_t *) user_password.pass, pwlen, &enc_err); tox_pass_decrypt((uint8_t *) data, len, (uint8_t *) user_password.pass, pwlen,
(uint8_t *) plain, &pwerr);
if (pwerr == TOX_ERR_DECRYPTION_OK) {
TOX_ERR_NEW err;
m = tox_new(tox_opts, (uint8_t *) plain, plain_len, &err);
if (err != TOX_ERR_NEW_OK)
exit_toxic_err("tox_new() failed", err);
if (enc_err == TOX_ERR_ENCRYPTED_NEW_OK) {
break; break;
} else if (enc_err == TOX_ERR_ENCRYPTED_NEW_LOAD_DECRYPTION_FAILED) { } else if (pwerr == TOX_ERR_DECRYPTION_FAILED) {
system("clear"); system("clear");
sleep(1); sleep(1);
printf("Invalid password. Try again. "); printf("Invalid password. Try again. ");
} else { } else {
exit_toxic_err("tox_encrypted_new() failed", enc_err); exit_toxic_err("tox_pass_decrypt() failed", pwerr);
} }
} }
} else { } else { /* data is not encrypted */
TOX_ERR_NEW err; TOX_ERR_NEW err;
m = tox_new(tox_opts, (uint8_t *) buf, len, &err); m = tox_new(tox_opts, (uint8_t *) data, len, &err);
if (err != TOX_ERR_NEW_OK) if (err != TOX_ERR_NEW_OK)
exit_toxic_err("tox_new() failed", err); exit_toxic_err("tox_new() failed", err);
} }
free(buf);
fclose(fp); fclose(fp);
} else { } else { /* Data file does not/should not exist */
/* if file exists then open() failing is fatal */
if (file_exists(data_path)) if (file_exists(data_path))
exit_toxic_err("failed in load_toxic", FATALERR_FILEOP); exit_toxic_err("failed in load_toxic", FATALERR_FILEOP);