From d566f45c6ad25526adfe3a4820dc1a52c87fd4f3 Mon Sep 17 00:00:00 2001 From: Tony Olagbaiye Date: Sun, 25 Jul 2021 19:28:34 +0100 Subject: [PATCH] signal-ish --- connection.c | 33 ++++++++-------------- omemo.c | 78 ++++++++++++++++++++++++++++++++++------------------ omemo.h | 20 ++++++-------- 3 files changed, 70 insertions(+), 61 deletions(-) diff --git a/connection.c b/connection.c index e3a6c39..4125951 100644 --- a/connection.c +++ b/connection.c @@ -1118,48 +1118,37 @@ void connection__handler(xmpp_conn_t *conn, xmpp_conn_event_t status, WEECHAT_HASHTABLE_STRING, NULL, NULL); weechat_hashtable_set(variables, "account", account->name); - char *dev_str = weechat_string_eval_expression( + char *device_r = weechat_string_eval_expression( "${sec.data.xmpp_device_${account}}", NULL, variables, NULL); - char *b64_id = weechat_string_eval_expression( + char *device_w = device_r; + char *account_r = weechat_string_eval_expression( "${sec.data.xmpp_identity_${account}}", NULL, variables, NULL); + char *account_w = account_r; weechat_hashtable_free(variables); - uint32_t dev_id = dev_str[0] ? atoi(dev_str) : 0; - uint8_t identity[128] = {0}; - if (b64_id && *b64_id) - weechat_string_base_decode(64, b64_id, (char*)identity); - struct t_omemo_identity id_key = { - .key = identity, - .length = 4, - }; - - omemo__init(account->buffer, &account->omemo, dev_id, b64_id && *b64_id ? &id_key : NULL); - - char account_id[64] = {0}; - snprintf(account_id, sizeof(account_id), "%d", account->omemo->device_id); - if (weechat_strcasecmp(dev_str, account_id) != 0) + + omemo__init(account->buffer, &account->omemo, &device_w, &account_w); + + if (weechat_strcasecmp(device_w, device_r) != 0) { char **command = weechat_string_dyn_alloc(256); weechat_string_dyn_concat(command, "/secure set ", -1); weechat_string_dyn_concat(command, "xmpp_device_", -1); weechat_string_dyn_concat(command, account->name, -1); weechat_string_dyn_concat(command, " ", -1); - weechat_string_dyn_concat(command, account_id, -1); + weechat_string_dyn_concat(command, device_w, -1); weechat_command(account->buffer, *command); weechat_string_dyn_free(command, 1); } - char account_key[64] = {0}; - weechat_string_base_encode(64, (char*)account->omemo->identity->key, - account->omemo->identity->length, account_key); - if (weechat_strcasecmp(b64_id, account_key) != 0) + if (weechat_strcasecmp(account_w, account_r) != 0) { char **command = weechat_string_dyn_alloc(256); weechat_string_dyn_concat(command, "/secure set ", -1); weechat_string_dyn_concat(command, "xmpp_identity_", -1); weechat_string_dyn_concat(command, account->name, -1); weechat_string_dyn_concat(command, " ", -1); - weechat_string_dyn_concat(command, account_key, -1); + weechat_string_dyn_concat(command, account_w, -1); weechat_command(account->buffer, *command); weechat_string_dyn_free(command, 1); } diff --git a/omemo.c b/omemo.c index 0c10673..17983b7 100644 --- a/omemo.c +++ b/omemo.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -441,20 +442,15 @@ int omemo__signal_init(struct t_gui_buffer *buffer, struct t_omemo *omemo) signal_context_set_crypto_provider(global_context, &provider); signal_context_set_locking_functions(global_context, &lock_function, &unlock_function); - ratchet_identity_key_pair *identity_key_pair; - uint32_t registration_id; signal_protocol_key_helper_pre_key_list_node *pre_keys_head; session_signed_pre_key *signed_pre_key; int start_id = 0; time_t timestamp = time(NULL); - signal_protocol_key_helper_generate_identity_key_pair(&identity_key_pair, global_context); - signal_protocol_key_helper_generate_registration_id(®istration_id, 0, global_context); + signal_protocol_key_helper_generate_identity_key_pair(&omemo->identity, global_context); + signal_protocol_key_helper_generate_registration_id(&omemo->device_id, 0, global_context); signal_protocol_key_helper_generate_pre_keys(&pre_keys_head, start_id, 100, global_context); - signal_protocol_key_helper_generate_signed_pre_key(&signed_pre_key, identity_key_pair, 5, timestamp, global_context); - - /* Store identity_key_pair somewhere durable and safe. */ - /* Store registration_id somewhere durable and safe. */ + signal_protocol_key_helper_generate_signed_pre_key(&signed_pre_key, omemo->identity, 5, timestamp, global_context); /* Store pre keys in the pre key store. */ /* Store signed pre key in the signed pre key store. */ @@ -465,36 +461,65 @@ int omemo__signal_init(struct t_gui_buffer *buffer, struct t_omemo *omemo) } void omemo__init(struct t_gui_buffer *buffer, struct t_omemo **omemo, - uint32_t device, struct t_omemo_identity *const identity) + char **device, char **identity) { struct t_omemo *new_omemo; new_omemo = calloc(1, sizeof(**omemo)); + omemo__deserialize(new_omemo, *device, *identity, strlen(*identity)); + omemo__signal_init(buffer, new_omemo); - new_omemo->identity = malloc(sizeof(*identity)); - if (identity) + omemo__serialize(new_omemo, device, identity, NULL); + + *omemo = new_omemo; +} + +void omemo__serialize(struct t_omemo *omemo, char **device, + char **identity, size_t *identity_len) +{ + if (device) { - new_omemo->identity->length = identity->length; - new_omemo->identity->key = calloc(identity->length, sizeof(*identity->key)); - memcpy(new_omemo->identity->key, identity->key, - identity->length * sizeof(*identity->key)); + size_t id_slen = log10(omemo->device_id) * 2; + char *id = malloc(sizeof(char) * id_slen); + snprintf(id, id_slen, "%d", omemo->device_id); + + *device = id; } - else + if (identity) { - new_omemo->identity->length = 4; - new_omemo->identity->key = calloc(new_omemo->identity->length, sizeof(*new_omemo->identity->key)); + signal_buffer *buffer; + ratchet_identity_key_pair_serialize(&buffer, omemo->identity); - new_omemo->identity->key[0] = random(); - new_omemo->identity->key[1] = random(); - new_omemo->identity->key[2] = random(); - new_omemo->identity->key[3] = random(); + size_t key_slen = signal_buffer_len(buffer) * 2; + char *key = malloc(sizeof(char) * key_slen); + size_t length = weechat_string_base_encode(64, (char*)signal_buffer_data(buffer), + signal_buffer_len(buffer), key); + + *identity = key; + if (identity_len) + *identity_len = length; } +} - new_omemo->device_id = device ? device : random(); +void omemo__deserialize(struct t_omemo *omemo, const char *device, + const char *identity, size_t identity_len) +{ + if (device) + { + uint32_t id = device[0] ? atoi(device) : 0; - *omemo = new_omemo; + omemo->device_id = id; + } + if (identity) + { + uint8_t *key = malloc(sizeof(uint8_t) * identity_len); + size_t length = weechat_string_base_decode(64, identity, (char*)key); + + ratchet_identity_key_pair_deserialize(&omemo->identity, + key, length, omemo->context); + } } void omemo__free(struct t_omemo *omemo) @@ -503,10 +528,9 @@ void omemo__free(struct t_omemo *omemo) { if (omemo->context) signal_context_destroy(omemo->context); - if (omemo->identity->key) - free(omemo->identity->key); if (omemo->identity) - free(omemo->identity); + ratchet_identity_key_pair_destroy( + (signal_type_base *)omemo->identity); free(omemo); } } diff --git a/omemo.h b/omemo.h index 96d7885..db6d4b1 100644 --- a/omemo.h +++ b/omemo.h @@ -7,27 +7,23 @@ extern const char *OMEMO_ADVICE; -struct t_omemo_identity -{ - uint8_t *key; - size_t length; -}; - struct t_omemo { struct signal_context *context; - //omemo_crypto_provider provider; - //axc_context *context; - //axc_bundle *a_bundle; - //omemo_bundle *o_bundle; - struct t_omemo_identity *identity; + struct ratchet_identity_key_pair *identity; uint32_t device_id; }; void omemo__init(struct t_gui_buffer *buffer, struct t_omemo **omemo, - uint32_t device, struct t_omemo_identity *identity); + char **device, char **identity); + +void omemo__serialize(struct t_omemo *omemo, char **device, + char **identity, size_t *identity_len); + +void omemo__deserialize(struct t_omemo *omemo, const char *device, + const char *identity, size_t identity_len); void omemo__free(struct t_omemo *omemo);