Skip to content

Commit

Permalink
Part of #163 - remove separate management of the shared prekey, inste…
Browse files Browse the repository at this point in the history
…ad make it part of the prekey profile structure
  • Loading branch information
olabini committed Oct 4, 2018
1 parent b0f7848 commit fed5ec7
Show file tree
Hide file tree
Showing 16 changed files with 65 additions and 319 deletions.
40 changes: 1 addition & 39 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ API void otrng_client_free(otrng_client_s *client) {
otrng_client_profile_free(client->exp_client_profile);
otrng_prekey_profile_free(client->prekey_profile);
otrng_prekey_profile_free(client->exp_prekey_profile);
otrng_shared_prekey_pair_free(client->shared_prekey_pair);
otrng_list_free(client->conversations, conversation_free);
otrng_prekey_client_free(client->prekey_client);

Expand Down Expand Up @@ -869,39 +868,6 @@ API otrng_result otrng_client_add_exp_client_profile(
return OTRNG_SUCCESS;
}

INTERNAL otrng_result otrng_client_add_shared_prekey_v4(
otrng_client_s *client, const uint8_t sym[ED448_PRIVATE_BYTES]) {
assert(client);

if (client->shared_prekey_pair) {
return OTRNG_ERROR;
}

/* @secret_information: the shared keypair lives for as long the client
decides */
client->shared_prekey_pair = otrng_shared_prekey_pair_new();
if (!client->shared_prekey_pair) {
return OTRNG_ERROR;
}

otrng_shared_prekey_pair_generate(client->shared_prekey_pair, sym);
return OTRNG_SUCCESS;
}

static const otrng_shared_prekey_pair_s *
get_shared_prekey_pair(otrng_client_s *client) {
assert(client);

if (client->shared_prekey_pair) {
return client->shared_prekey_pair;
}

client->global_state->callbacks->create_shared_prekey(client,
client->client_id);

return client->shared_prekey_pair;
}

API otrng_prekey_profile_s *
otrng_client_get_prekey_profile(otrng_client_s *client) {
assert(client);
Expand All @@ -920,17 +886,13 @@ API otrng_prekey_profile_s *
otrng_client_build_default_prekey_profile(otrng_client_s *client) {
assert(client);

/* @secret: the shared prekey should be deleted once the prekey profile
* expires */
return otrng_prekey_profile_build(otrng_client_get_instance_tag(client),
otrng_client_get_keypair_v4(client),
get_shared_prekey_pair(client));
otrng_client_get_keypair_v4(client));
}

API otrng_result otrng_client_add_prekey_profile(
otrng_client_s *client, const otrng_prekey_profile_s *profile) {
assert(client);

if (client->prekey_profile) {
return OTRNG_ERROR;
}
Expand Down
6 changes: 0 additions & 6 deletions src/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ typedef struct otrng_client_s {
otrng_prekey_profile_s *exp_prekey_profile;
list_element_s *our_prekeys; /* otrng_stored_prekeys_s */

/* @secret: this should be deleted once the prekey profile expires */
otrng_shared_prekey_pair_s *shared_prekey_pair;

unsigned int max_stored_msg_keys;
unsigned int max_published_prekey_msg;
unsigned int minimum_stored_prekey_msg;
Expand Down Expand Up @@ -217,9 +214,6 @@ otrng_client_get_exp_client_profile(otrng_client_s *client);
API otrng_result otrng_client_add_exp_client_profile(
otrng_client_s *client, const otrng_client_profile_s *exp_profile);

INTERNAL otrng_result otrng_client_add_shared_prekey_v4(
otrng_client_s *client, const uint8_t sym[ED448_PRIVATE_BYTES]);

API otrng_prekey_profile_s *
otrng_client_get_prekey_profile(otrng_client_s *client);

Expand Down
1 change: 0 additions & 1 deletion src/client_callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ otrng_client_callbacks_ensure_needed_exist(const otrng_client_callbacks_s *cb) {
cb->write_expired_client_profile != NULL &&
cb->create_prekey_profile != NULL &&
cb->write_expired_prekey_profile != NULL &&
cb->create_shared_prekey != NULL &&
cb->get_shared_session_state != NULL && cb->load_privkey_v4 != NULL &&
cb->load_client_profile != NULL && cb->load_prekey_profile != NULL &&
cb->store_client_profile != NULL && cb->store_prekey_profile != NULL);
Expand Down
4 changes: 0 additions & 4 deletions src/client_callbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,6 @@ typedef struct otrng_client_callbacks_s {
struct otrng_client_s *client,
const struct otrng_client_id_s client_opdata);

/* REQUIRED */
void (*create_shared_prekey)(struct otrng_client_s *client,
const struct otrng_client_id_s client_opdata);

/* OPTIONAL */
void (*gone_secure)(const struct otrng_s *);

Expand Down
65 changes: 0 additions & 65 deletions src/messaging.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,26 +176,6 @@ API otrng_result otrng_global_state_generate_forging_key(
return r;
}

API otrng_result otrng_global_state_generate_shared_prekey(
otrng_global_state_s *gs, const otrng_client_id_s client_id) {
otrng_result r;
uint8_t *sym = otrng_secure_alloc(ED448_PRIVATE_BYTES);
otrng_client_s *client;

gcry_randomize(sym, ED448_PRIVATE_BYTES, GCRY_VERY_STRONG_RANDOM);

client = get_client(gs, client_id);
if (!client) {
return OTRNG_ERROR;
}

r = otrng_client_add_shared_prekey_v4(client, sym);

otrng_secure_wipe(sym, ED448_PRIVATE_BYTES);
free(sym);
return r;
}

API otrng_result otrng_global_state_generate_client_profile(
otrng_global_state_s *gs, const otrng_client_id_s client_id) {
otrng_client_profile_s *profile;
Expand Down Expand Up @@ -276,25 +256,6 @@ API otrng_result otrng_global_state_forging_key_write_to(
return OTRNG_SUCCESS;
}

tstatic void add_shared_prekey_to(list_element_s *node, void *context) {
otrng_client_s *client = node->data;
// TODO: check the return value
if (!otrng_client_shared_prekey_write_to(client, context)) {
return;
}
}

API otrng_result otrng_global_state_shared_prekey_write_to(
const otrng_global_state_s *gs, FILE *shared_prekey_f) {
if (!shared_prekey_f) {
return OTRNG_ERROR;
}

otrng_list_foreach(gs->clients, add_shared_prekey_to, shared_prekey_f);

return OTRNG_SUCCESS;
}

tstatic void add_client_profile_to(list_element_s *node, void *context) {
otrng_client_client_profile_write_to(node->data, context);
}
Expand Down Expand Up @@ -409,32 +370,6 @@ API otrng_result otrng_global_state_forging_key_read_from(
return OTRNG_SUCCESS;
}

API otrng_result otrng_global_state_shared_prekey_read_from(
otrng_global_state_s *gs, FILE *shared_prekeyf,
otrng_client_id_s (*read_client_id_for_key)(FILE *filep)) {
if (!shared_prekeyf) {
return OTRNG_ERROR;
}

// Scan the whole file for a private key for this client
while (!feof(shared_prekeyf)) {
otrng_client_s *client;
const otrng_client_id_s client_id = read_client_id_for_key(shared_prekeyf);
if (!client_id.protocol || !client_id.account) {
continue;
}

client = get_client(gs, client_id);
if (otrng_client_shared_prekey_read_from(client, shared_prekeyf) !=
OTRNG_SUCCESS) {
return OTRNG_ERROR; /* We decide to abort, since this means the file is
malformed */
}
}

return OTRNG_SUCCESS;
}

API otrng_result otrng_global_state_client_profile_read_from(
otrng_global_state_s *gs, FILE *profile_filep,
otrng_client_id_s (*read_client_id_for_key)(FILE *filep)) {
Expand Down
10 changes: 0 additions & 10 deletions src/messaging.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ API otrng_result otrng_global_state_generate_private_key(
API otrng_result otrng_global_state_generate_forging_key(
otrng_global_state_s *gs, const otrng_client_id_s client_id);

API otrng_result otrng_global_state_generate_shared_prekey(
otrng_global_state_s *gs, const otrng_client_id_s client_id);

API otrng_result otrng_global_state_generate_client_profile(
otrng_global_state_s *gs, const otrng_client_id_s client_id);

Expand All @@ -87,9 +84,6 @@ API otrng_result otrng_global_state_private_key_v4_write_to(
API otrng_result otrng_global_state_forging_key_write_to(
const otrng_global_state_s *gs, FILE *f);

API otrng_result otrng_global_state_shared_prekey_write_to(
const otrng_global_state_s *gs, FILE *shared_prekey_f);

API otrng_result otrng_global_state_client_profile_write_to(
const otrng_global_state_s *gs, FILE *privf);

Expand All @@ -113,10 +107,6 @@ API otrng_result otrng_global_state_forging_key_read_from(
otrng_global_state_s *gs, FILE *f,
otrng_client_id_s (*read_client_id_for_key)(FILE *f));

API otrng_result otrng_global_state_shared_prekey_read_from(
otrng_global_state_s *gs, FILE *shared_prekeyf,
otrng_client_id_s (*read_client_id_for_key)(FILE *filep));

API otrng_result otrng_global_state_client_profile_read_from(
otrng_global_state_s *gs, FILE *profile_filep,
otrng_client_id_s (*read_client_id_for_key)(FILE *filep));
Expand Down
2 changes: 1 addition & 1 deletion src/otrng.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ tstatic void allowed_versions(string_p destination, const otrng_s *otr) {

tstatic const otrng_shared_prekey_pair_s *
our_shared_prekey(const otrng_s *otr) {
return otr->client->shared_prekey_pair;
return otr->client->prekey_profile->keys;
}

INTERNAL otrng_s *otrng_new(otrng_client_s *client, otrng_policy_s policy) {
Expand Down
93 changes: 0 additions & 93 deletions src/persistence.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,99 +265,6 @@ INTERNAL otrng_result otrng_client_forging_key_read_from(otrng_client_s *client,
return otrng_client_add_forging_key(client, key);
}

INTERNAL otrng_result otrng_client_shared_prekey_write_to(
const otrng_client_s *client, FILE *shared_prekey_f) {
char *storage_id;
int err;
char *buff = NULL;
size_t s = 0;

if (!shared_prekey_f) {
return OTRNG_ERROR;
}

if (!client->shared_prekey_pair) {
return OTRNG_ERROR;
}

storage_id = otrng_client_get_storage_id(client);
if (!storage_id) {
return OTRNG_ERROR;
}

err = fputs(storage_id, shared_prekey_f);
free(storage_id);

if (EOF == err) {
return OTRNG_ERROR;
}

if (EOF == fputs("\n", shared_prekey_f)) {
return OTRNG_ERROR;
}

if (!otrng_symmetric_key_serialize(&buff, &s,
client->shared_prekey_pair->sym)) {
return OTRNG_ERROR;
}

err = fwrite(buff, s, 1, shared_prekey_f);
free(buff);

if (err != 1) {
return OTRNG_ERROR;
}

if (EOF == fputs("\n", shared_prekey_f)) {
return OTRNG_ERROR;
}

return OTRNG_SUCCESS;
}

INTERNAL otrng_result otrng_client_shared_prekey_read_from(
otrng_client_s *client, FILE *shared_prekeyf) {
char *line = NULL;
int len = 0;
otrng_shared_prekey_pair_s *shared_prekey_pair;

if (!shared_prekeyf) {
return OTRNG_ERROR;
}

if (feof(shared_prekeyf)) {
return OTRNG_ERROR;
}

/* Free current keypair if any */
otrng_shared_prekey_pair_free(client->shared_prekey_pair);
client->shared_prekey_pair = NULL;

shared_prekey_pair = otrng_shared_prekey_pair_new();
if (!shared_prekey_pair) {
return OTRNG_ERROR;
}

len = get_limited_line(&line, shared_prekeyf);
if (len < 0) {
return OTRNG_ERROR;
}

/* line has the /n */
if (!otrng_symmetric_shared_prekey_deserialize(shared_prekey_pair, line,
len)) {
free(line);
otrng_shared_prekey_pair_free(client->shared_prekey_pair);
return OTRNG_ERROR;
}

free(line);

client->shared_prekey_pair = shared_prekey_pair;

return OTRNG_SUCCESS;
}

INTERNAL otrng_result otrng_client_instance_tag_write_to(otrng_client_s *client,
FILE *instagf) {
// TODO: We could use a "get storage key" callback and use it as
Expand Down
6 changes: 0 additions & 6 deletions src/persistence.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ otrng_client_private_key_v4_write_to(const otrng_client_s *client, FILE *privf);
INTERNAL otrng_result
otrng_client_forging_key_write_to(const otrng_client_s *client, FILE *f);

INTERNAL otrng_result otrng_client_shared_prekey_write_to(
const otrng_client_s *client, FILE *shared_prekey_f);

INTERNAL otrng_result otrng_client_shared_prekey_read_from(
otrng_client_s *client, FILE *shared_prekeyf);

INTERNAL otrng_result
otrng_client_instance_tag_read_from(otrng_client_s *client, FILE *instag);

Expand Down
Loading

0 comments on commit fed5ec7

Please sign in to comment.