Skip to content

Commit

Permalink
Part of otrv4/libotr-ng#163 - sort out prekey messages
Browse files Browse the repository at this point in the history
  • Loading branch information
olabini committed Oct 4, 2018
1 parent 4a8ec98 commit 9d39c52
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 67 deletions.
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pidgin_otrng_la_SOURCES = otrng-plugin.c \
prekey-plugin-shared.c \
prekey-discovery.c \
prekey-discovery-jabber.c \
prekeys.c \
plugin-all.c \
ui.c \
dialogs.c \
Expand Down
33 changes: 33 additions & 0 deletions persistance.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,36 @@ void persistance_read_prekey_profile(otrng_global_state_s *otrng_state) {
fclose(fp);
}
}

int persistance_write_prekey_messages(otrng_global_state_s *otrng_state) {
FILE *fp = NULL;
gchar *f = g_build_filename(purple_user_dir(), PREKEYS_FILE_NAME, NULL);
if (!f) {
return -1;
}

fp = open_file_write_mode(f);

if (!otrng_global_state_prekey_messages_write_to(otrng_state, fp)) {
fclose(fp);
return -1;
}

return 0;
}

void persistance_read_prekey_messages(otrng_global_state_s *otrng_state) {
gchar *f = g_build_filename(purple_user_dir(), PREKEYS_FILE_NAME, NULL);
if (!f) {
return;
}

FILE *fp = g_fopen(f, "rb");
g_free(f);

if (fp) {
otrng_global_state_prekeys_read_from(
otrng_state, fp, protocol_and_account_to_purple_conversation);
fclose(fp);
}
}
5 changes: 5 additions & 0 deletions persistance.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#define PRIVKEY_FILE_NAME_V4 "otr4.private_key"
#define CLIENT_PROFILE_FILE_NAME "otr4.client_profile"
#define PREKEY_PROFILE_FILE_NAME "otr4.prekey_profile"
#define PREKEYS_FILE_NAME "otr4.prekey_messages"

int persistance_write_privkey_v4_FILEp(otrng_global_state_s *otrng_state);

Expand All @@ -41,4 +42,8 @@ int persistance_write_prekey_profile_FILEp(otrng_global_state_s *otrng_state);

void persistance_read_prekey_profile(otrng_global_state_s *otrng_state);

int persistance_write_prekey_messages(otrng_global_state_s *otrng_state);

void persistance_read_prekey_messages(otrng_global_state_s *otrng_state);

#endif
26 changes: 4 additions & 22 deletions plugin-all.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
#include "persistance.h"
#include "plugin-all.h"
#include "prekey-plugin.h"
#include "prekeys.h"
#include "profiles.h"

#ifdef USING_GTK
Expand Down Expand Up @@ -163,14 +164,6 @@ static void otrng_plugin_read_expired_prekey_profile(FILE *profiles_filep) {
}
}

static void otrng_plugin_read_prekeys(FILE *prekeys_filep) {
if (otrng_failed(otrng_global_state_prekeys_read_from(
otrng_state, prekeys_filep,
protocol_and_account_to_purple_conversation))) {
return;
}
}

static void otrng_plugin_fingerprint_store_create() {
otrng_fingerprints_table = g_hash_table_new_full(
g_str_hash, g_str_equal, g_free, g_destroy_plugin_fingerprint);
Expand Down Expand Up @@ -2017,7 +2010,6 @@ static int otrng_plugin_init_userstate(void) {
gchar *instagfile = NULL;
gchar *exp_client_profile_filename = NULL;
gchar *exp_prekey_profile_filename = NULL;
gchar *prekeysfile = NULL;

forging_key_file =
g_build_filename(purple_user_dir(), FORGING_KEY_FILE_NAME, NULL);
Expand All @@ -2028,18 +2020,15 @@ static int otrng_plugin_init_userstate(void) {
g_build_filename(purple_user_dir(), EXP_CLIENT_PROFILE_FILE_NAME, NULL);
exp_prekey_profile_filename =
g_build_filename(purple_user_dir(), EXP_PREKEY_PROFILE_FILE_NAME, NULL);
prekeysfile = g_build_filename(purple_user_dir(), PREKEYS_FILE_NAME, NULL);

if (!forging_key_file || !privkeyfile3 || !storefile || !instagfile ||
!exp_client_profile_filename || !exp_prekey_profile_filename ||
!prekeysfile) {
!exp_client_profile_filename || !exp_prekey_profile_filename) {
g_free(forging_key_file);
g_free(privkeyfile3);
g_free(storefile);
g_free(instagfile);
g_free(exp_client_profile_filename);
g_free(exp_prekey_profile_filename);
g_free(prekeysfile);

return 1;
}
Expand All @@ -2050,18 +2039,18 @@ static int otrng_plugin_init_userstate(void) {
FILE *instagf = g_fopen(instagfile, "rb");
FILE *exp_client_profile_f = g_fopen(exp_client_profile_filename, "rb");
FILE *exp_prekey_profile_filep = g_fopen(exp_prekey_profile_filename, "rb");
FILE *prekeyf = g_fopen(prekeysfile, "rb");

g_free(forging_key_file);
g_free(privkeyfile3);
g_free(storefile);
g_free(instagfile);
g_free(exp_client_profile_filename);
g_free(exp_prekey_profile_filename);
g_free(prekeysfile);

otrng_client_callbacks_s *callbacks = otrng_plugin_client_callbacks_new();
long_term_keys_set_callbacks(callbacks);
profiles_set_callbacks(callbacks);
prekeys_set_callbacks(callbacks);

otrng_state = otrng_global_state_new(callbacks, otrng_false);

Expand All @@ -2085,9 +2074,6 @@ static int otrng_plugin_init_userstate(void) {
/* Read prekey profile */
otrng_plugin_read_expired_prekey_profile(exp_prekey_profile_filep);

/* Read prekey messages */
otrng_plugin_read_prekeys(prekeyf);

if (priv3f) {
fclose(priv3f);
}
Expand All @@ -2104,10 +2090,6 @@ static int otrng_plugin_init_userstate(void) {
fclose(instagf);
}

if (prekeyf) {
fclose(prekeyf);
}

return 0;
}

Expand Down
2 changes: 2 additions & 0 deletions po/POTFILES.in
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ persistance.c
persistance.h
plugin-all.c
plugin-all.h
prekeys.c
prekeys.h
prekey-plugin.c
prekey-plugin.h
prekey-plugin-account.c
Expand Down
5 changes: 4 additions & 1 deletion prekey-discovery-jabber.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ int otrng_plugin_jabber_lookup_prekey_servers_for(PurpleAccount *account,
const char *who,
PrekeyServerResult result_cb,
void *context) {
if (account == NULL || who == NULL) {
return 0;
}
PurplePlugin *prpl = purple_plugins_find_with_id("prpl-jabber");
if (prpl == NULL) {
return 0;
Expand Down Expand Up @@ -260,7 +263,7 @@ int otrng_plugin_jabber_lookup_prekey_servers_for(PurpleAccount *account,
send_iq(pc, server, NS_DISCO_ITEMS, iq_handle);

free(server);
free(nwho);
g_free(nwho);
return 1;
}

Expand Down
49 changes: 5 additions & 44 deletions prekey-plugin-account.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,6 @@ get_prekey_client_for_publishing(PurpleAccount *account, otrng_client_s *client,

void low_prekey_messages_in_storage_cb(otrng_client_s *client,
char *server_identity, void *ctx) {
otrng_debug_fprintf(stderr,
"[%s] Prekey Server: Publishing prekey messages.\n",
client->client_id.account);
// TODO: @ola
// Once ensure_state can handle prekey messages, it should be called here
// And then trigger a maybe_publish later
Expand All @@ -124,41 +121,14 @@ int build_prekey_publication_message_cb(
return 0;
}

FILE *prekeyf = NULL;
gchar *prekeysfile =
g_build_filename(purple_user_dir(), PREKEYS_FILE_NAME, NULL);
if (!prekeysfile) {
fprintf(stderr, _("Out of memory building filenames!\n"));
otrng_debug_exit("build_prekey_publication_message_cb");
return 0;
}

prekeyf = g_fopen(prekeysfile, "w+b");
g_free(prekeysfile);

#ifndef WIN32
mode_t mask = umask(0077);
umask(mask);
#endif /* WIN32 */

if (!prekeyf) {
fprintf(stderr, _("Could not write prekey messages file\n"));
otrng_debug_exit("build_prekey_publication_message_cb");
return 0;
}

otrng_client_ensure_correct_state(client);

// TODO: @ola continue here - we should not create prekey messages here
// instead, they should be done in the orchestration part

msg->num_prekey_messages = client->prekey_msgs_num_to_publish;
msg->prekey_messages = otrng_client_build_prekey_messages(
msg->num_prekey_messages, client, &msg->ecdh_keys, &msg->dh_keys);
otrng_prekey_client_add_prekey_messages_for_publication(client, msg);

if (msg->num_prekey_messages > 0 && !msg->prekey_messages) {
otrng_debug_exit("build_prekey_publication_message_cb");
return 0;
if (msg->num_prekey_messages > 0) {
otrng_debug_fprintf(stderr,
"[%s] Prekey Server: Publishing %d Prekey Messages\n",
client->client_id.account, msg->num_prekey_messages);
}

otrng_client_profile_s *client_profile =
Expand All @@ -183,17 +153,8 @@ int build_prekey_publication_message_cb(
client->client_id.account);
msg->prekey_profile = otrng_xmalloc_z(sizeof(otrng_prekey_profile_s));
otrng_prekey_profile_copy(msg->prekey_profile, prekey_profile);

// TODO: this shouldn't really be necessary now
*msg->prekey_profile_key = *prekey_profile->keys->priv;
}

if (!otrng_global_state_prekey_messages_write_to(otrng_state, prekeyf)) {
otrng_debug_exit("build_prekey_publication_message_cb");
return 0;
}

fclose(prekeyf);
otrng_debug_exit("build_prekey_publication_message_cb");
return 1;
}
Expand Down
42 changes: 42 additions & 0 deletions prekeys.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Off-the-Record Messaging plugin for pidgin
* Copyright (C) 2004-2018 Ian Goldberg, Rob Smits,
* Chris Alexander, Willy Lew,
* Nikita Borisov
* <[email protected]>
* The pidgin-otrng contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <libotr-ng/messaging.h>

#include "persistance.h"
#include "pidgin-helpers.h"
#include "prekeys.h"

extern otrng_global_state_s *otrng_state;

static void load_prekey_messages(otrng_client_s *client) {
persistance_read_prekey_messages(otrng_state);
}

static void store_prekey_messages(otrng_client_s *client) {
persistance_write_prekey_messages(otrng_state);
}

void prekeys_set_callbacks(otrng_client_callbacks_s *callbacks) {
callbacks->load_prekey_messages = load_prekey_messages;
callbacks->store_prekey_messages = store_prekey_messages;
}
33 changes: 33 additions & 0 deletions prekeys.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Off-the-Record Messaging plugin for pidgin
* Copyright (C) 2004-2018 Ian Goldberg, Rob Smits,
* Chris Alexander, Willy Lew,
* Nikita Borisov
* <[email protected]>
* The pidgin-otrng contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef __OTRNG_PREKEYS_H__
#define __OTRNG_PREKEYS_H__

#include <account.h>

#include <libotr-ng/client.h>
#include <libotr-ng/client_callbacks.h>

void prekeys_set_callbacks(otrng_client_callbacks_s *callbacks);

#endif

0 comments on commit 9d39c52

Please sign in to comment.