signal-ish

v1
Tony Olagbaiye 3 years ago
parent b2f38c4901
commit d566f45c6a
No known key found for this signature in database
GPG Key ID: 9E2FF3BDEBDFC910

@ -1118,48 +1118,37 @@ void connection__handler(xmpp_conn_t *conn, xmpp_conn_event_t status,
WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_STRING,
NULL, NULL); NULL, NULL);
weechat_hashtable_set(variables, "account", account->name); 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}}", "${sec.data.xmpp_device_${account}}",
NULL, variables, NULL); 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}}", "${sec.data.xmpp_identity_${account}}",
NULL, variables, NULL); NULL, variables, NULL);
char *account_w = account_r;
weechat_hashtable_free(variables); 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); omemo__init(account->buffer, &account->omemo, &device_w, &account_w);
char account_id[64] = {0}; if (weechat_strcasecmp(device_w, device_r) != 0)
snprintf(account_id, sizeof(account_id), "%d", account->omemo->device_id);
if (weechat_strcasecmp(dev_str, account_id) != 0)
{ {
char **command = weechat_string_dyn_alloc(256); char **command = weechat_string_dyn_alloc(256);
weechat_string_dyn_concat(command, "/secure set ", -1); weechat_string_dyn_concat(command, "/secure set ", -1);
weechat_string_dyn_concat(command, "xmpp_device_", -1); weechat_string_dyn_concat(command, "xmpp_device_", -1);
weechat_string_dyn_concat(command, account->name, -1); weechat_string_dyn_concat(command, account->name, -1);
weechat_string_dyn_concat(command, " ", -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_command(account->buffer, *command);
weechat_string_dyn_free(command, 1); weechat_string_dyn_free(command, 1);
} }
char account_key[64] = {0}; if (weechat_strcasecmp(account_w, account_r) != 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)
{ {
char **command = weechat_string_dyn_alloc(256); char **command = weechat_string_dyn_alloc(256);
weechat_string_dyn_concat(command, "/secure set ", -1); weechat_string_dyn_concat(command, "/secure set ", -1);
weechat_string_dyn_concat(command, "xmpp_identity_", -1); weechat_string_dyn_concat(command, "xmpp_identity_", -1);
weechat_string_dyn_concat(command, account->name, -1); weechat_string_dyn_concat(command, account->name, -1);
weechat_string_dyn_concat(command, " ", -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_command(account->buffer, *command);
weechat_string_dyn_free(command, 1); weechat_string_dyn_free(command, 1);
} }

@ -5,6 +5,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <time.h> #include <time.h>
#include <math.h>
#include <gcrypt.h> #include <gcrypt.h>
#include <signal_protocol.h> #include <signal_protocol.h>
#include <key_helper.h> #include <key_helper.h>
@ -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_crypto_provider(global_context, &provider);
signal_context_set_locking_functions(global_context, &lock_function, &unlock_function); 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; signal_protocol_key_helper_pre_key_list_node *pre_keys_head;
session_signed_pre_key *signed_pre_key; session_signed_pre_key *signed_pre_key;
int start_id = 0; int start_id = 0;
time_t timestamp = time(NULL); time_t timestamp = time(NULL);
signal_protocol_key_helper_generate_identity_key_pair(&identity_key_pair, global_context); signal_protocol_key_helper_generate_identity_key_pair(&omemo->identity, global_context);
signal_protocol_key_helper_generate_registration_id(&registration_id, 0, 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_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); signal_protocol_key_helper_generate_signed_pre_key(&signed_pre_key, omemo->identity, 5, timestamp, global_context);
/* Store identity_key_pair somewhere durable and safe. */
/* Store registration_id somewhere durable and safe. */
/* Store pre keys in the pre key store. */ /* Store pre keys in the pre key store. */
/* Store signed pre key in the signed 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, 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; struct t_omemo *new_omemo;
new_omemo = calloc(1, sizeof(**omemo)); new_omemo = calloc(1, sizeof(**omemo));
omemo__deserialize(new_omemo, *device, *identity, strlen(*identity));
omemo__signal_init(buffer, new_omemo); omemo__signal_init(buffer, new_omemo);
new_omemo->identity = malloc(sizeof(*identity)); omemo__serialize(new_omemo, device, identity, NULL);
if (identity)
*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; size_t id_slen = log10(omemo->device_id) * 2;
new_omemo->identity->key = calloc(identity->length, sizeof(*identity->key)); char *id = malloc(sizeof(char) * id_slen);
memcpy(new_omemo->identity->key, identity->key, snprintf(id, id_slen, "%d", omemo->device_id);
identity->length * sizeof(*identity->key));
*device = id;
} }
else if (identity)
{ {
new_omemo->identity->length = 4; signal_buffer *buffer;
new_omemo->identity->key = calloc(new_omemo->identity->length, sizeof(*new_omemo->identity->key)); ratchet_identity_key_pair_serialize(&buffer, omemo->identity);
new_omemo->identity->key[0] = random(); size_t key_slen = signal_buffer_len(buffer) * 2;
new_omemo->identity->key[1] = random(); char *key = malloc(sizeof(char) * key_slen);
new_omemo->identity->key[2] = random(); size_t length = weechat_string_base_encode(64, (char*)signal_buffer_data(buffer),
new_omemo->identity->key[3] = random(); 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) void omemo__free(struct t_omemo *omemo)
@ -503,10 +528,9 @@ void omemo__free(struct t_omemo *omemo)
{ {
if (omemo->context) if (omemo->context)
signal_context_destroy(omemo->context); signal_context_destroy(omemo->context);
if (omemo->identity->key)
free(omemo->identity->key);
if (omemo->identity) if (omemo->identity)
free(omemo->identity); ratchet_identity_key_pair_destroy(
(signal_type_base *)omemo->identity);
free(omemo); free(omemo);
} }
} }

@ -7,27 +7,23 @@
extern const char *OMEMO_ADVICE; extern const char *OMEMO_ADVICE;
struct t_omemo_identity
{
uint8_t *key;
size_t length;
};
struct t_omemo struct t_omemo
{ {
struct signal_context *context; 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; uint32_t device_id;
}; };
void omemo__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 *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); void omemo__free(struct t_omemo *omemo);

Loading…
Cancel
Save