2019-11-13 06:11:05 -05:00
|
|
|
/*
|
|
|
|
* omemo.c
|
|
|
|
* vim: expandtab:ts=4:sts=4:sw=4
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 Paul Fariello <paul@fariello.eu>
|
|
|
|
*
|
|
|
|
* This file is part of Profanity.
|
|
|
|
*
|
|
|
|
* Profanity is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Profanity 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 Profanity. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give permission to
|
|
|
|
* link the code of portions of this program with the OpenSSL library under
|
|
|
|
* certain conditions as described in each individual source file, and
|
|
|
|
* distribute linked combinations including the two.
|
|
|
|
*
|
|
|
|
* You must obey the GNU General Public License in all respects for all of the
|
|
|
|
* code used other than OpenSSL. If you modify file(s) with this exception, you
|
|
|
|
* may extend this exception to your version of the file(s), but you are not
|
|
|
|
* obligated to do so. If you do not wish to do so, delete this exception
|
|
|
|
* statement from your version. If you delete this exception statement from all
|
|
|
|
* source files in the program, then also delete it here.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2021-03-26 14:51:46 -04:00
|
|
|
#include "config.h"
|
|
|
|
|
2019-02-22 13:17:26 -05:00
|
|
|
#include <glib.h>
|
|
|
|
|
2019-03-28 18:30:28 -04:00
|
|
|
#include "log.h"
|
2019-02-21 00:04:47 -05:00
|
|
|
#include "xmpp/connection.h"
|
2019-03-28 18:30:28 -04:00
|
|
|
#include "xmpp/form.h"
|
2019-02-21 00:04:47 -05:00
|
|
|
#include "xmpp/iq.h"
|
2019-03-28 18:30:28 -04:00
|
|
|
#include "xmpp/message.h"
|
2019-02-21 00:04:47 -05:00
|
|
|
#include "xmpp/stanza.h"
|
|
|
|
|
2019-02-21 13:04:01 -05:00
|
|
|
#include "omemo/omemo.h"
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
static int _omemo_receive_devicelist(xmpp_stanza_t* const stanza, void* const userdata);
|
|
|
|
static int _omemo_bundle_publish_result(xmpp_stanza_t* const stanza, void* const userdata);
|
|
|
|
static int _omemo_bundle_publish_configure(xmpp_stanza_t* const stanza, void* const userdata);
|
|
|
|
static int _omemo_bundle_publish_configure_result(xmpp_stanza_t* const stanza, void* const userdata);
|
2019-02-22 13:17:26 -05:00
|
|
|
|
2021-06-01 00:39:32 -04:00
|
|
|
static int _omemo_device_list_publish_result(xmpp_stanza_t* const stanza, void* const userdata);
|
|
|
|
|
2019-02-21 00:04:47 -05:00
|
|
|
void
|
2019-02-21 13:04:01 -05:00
|
|
|
omemo_devicelist_subscribe(void)
|
2019-02-21 00:04:47 -05:00
|
|
|
{
|
2019-02-22 13:17:26 -05:00
|
|
|
message_pubsub_event_handler_add(STANZA_NS_OMEMO_DEVICELIST, _omemo_receive_devicelist, NULL, NULL);
|
2019-02-21 00:04:47 -05:00
|
|
|
|
2019-02-22 13:17:26 -05:00
|
|
|
caps_add_feature(XMPP_FEATURE_OMEMO_DEVICELIST_NOTIFY);
|
2019-02-21 00:04:47 -05:00
|
|
|
}
|
2019-02-21 13:04:01 -05:00
|
|
|
|
|
|
|
void
|
2020-07-07 08:18:57 -04:00
|
|
|
omemo_devicelist_publish(GList* device_list)
|
2019-02-21 13:04:01 -05:00
|
|
|
{
|
2020-07-07 08:18:57 -04:00
|
|
|
xmpp_ctx_t* const ctx = connection_get_ctx();
|
|
|
|
xmpp_stanza_t* iq = stanza_create_omemo_devicelist_publish(ctx, device_list);
|
2019-03-19 13:00:24 -04:00
|
|
|
|
2021-05-28 11:45:15 -04:00
|
|
|
log_debug("[OMEMO] publish device list");
|
2021-03-09 11:47:43 -05:00
|
|
|
|
2019-04-08 13:09:25 -04:00
|
|
|
if (connection_supports(XMPP_FEATURE_PUBSUB_PUBLISH_OPTIONS)) {
|
|
|
|
stanza_attach_publish_options(ctx, iq, "pubsub#access_model", "open");
|
|
|
|
}
|
2019-03-19 13:00:24 -04:00
|
|
|
|
2021-06-01 00:39:32 -04:00
|
|
|
iq_id_handler_add(xmpp_stanza_get_id(iq), _omemo_device_list_publish_result, NULL, NULL);
|
|
|
|
|
2019-02-21 13:04:01 -05:00
|
|
|
iq_send_stanza(iq);
|
|
|
|
xmpp_stanza_release(iq);
|
|
|
|
}
|
|
|
|
|
2019-02-22 13:17:26 -05:00
|
|
|
void
|
2020-07-07 08:18:57 -04:00
|
|
|
omemo_devicelist_request(const char* const jid)
|
2019-02-22 13:17:26 -05:00
|
|
|
{
|
2020-07-07 08:18:57 -04:00
|
|
|
xmpp_ctx_t* const ctx = connection_get_ctx();
|
|
|
|
char* id = connection_create_stanza_id();
|
2019-02-24 23:59:41 -05:00
|
|
|
|
2021-05-28 11:45:15 -04:00
|
|
|
log_debug("[OMEMO] request device list for jid: %s", jid);
|
2021-03-09 11:48:29 -05:00
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
xmpp_stanza_t* iq = stanza_create_omemo_devicelist_request(ctx, id, jid);
|
2019-02-24 23:59:41 -05:00
|
|
|
iq_id_handler_add(id, _omemo_receive_devicelist, NULL, NULL);
|
|
|
|
|
|
|
|
iq_send_stanza(iq);
|
|
|
|
|
|
|
|
free(id);
|
|
|
|
xmpp_stanza_release(iq);
|
2019-02-22 13:17:26 -05:00
|
|
|
}
|
|
|
|
|
2019-02-21 13:04:01 -05:00
|
|
|
void
|
2019-03-28 18:30:28 -04:00
|
|
|
omemo_bundle_publish(gboolean first)
|
2019-02-21 13:04:01 -05:00
|
|
|
{
|
2021-05-28 11:45:15 -04:00
|
|
|
log_debug("[OMEMO] publish own OMEMO bundle");
|
2020-07-07 08:18:57 -04:00
|
|
|
xmpp_ctx_t* const ctx = connection_get_ctx();
|
|
|
|
unsigned char* identity_key = NULL;
|
2019-02-21 13:04:01 -05:00
|
|
|
size_t identity_key_length;
|
2020-07-07 08:18:57 -04:00
|
|
|
unsigned char* signed_prekey = NULL;
|
2019-02-21 13:04:01 -05:00
|
|
|
size_t signed_prekey_length;
|
2020-07-07 08:18:57 -04:00
|
|
|
unsigned char* signed_prekey_signature = NULL;
|
2019-02-21 13:04:01 -05:00
|
|
|
size_t signed_prekey_signature_length;
|
|
|
|
GList *prekeys = NULL, *ids = NULL, *lengths = NULL;
|
|
|
|
|
|
|
|
omemo_identity_key(&identity_key, &identity_key_length);
|
|
|
|
omemo_signed_prekey(&signed_prekey, &signed_prekey_length);
|
|
|
|
omemo_signed_prekey_signature(&signed_prekey_signature, &signed_prekey_signature_length);
|
|
|
|
omemo_prekeys(&prekeys, &ids, &lengths);
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
char* id = connection_create_stanza_id();
|
|
|
|
xmpp_stanza_t* iq = stanza_create_omemo_bundle_publish(ctx, id,
|
|
|
|
omemo_device_id(), identity_key, identity_key_length, signed_prekey,
|
|
|
|
signed_prekey_length, signed_prekey_signature,
|
|
|
|
signed_prekey_signature_length, prekeys, ids, lengths);
|
2019-03-19 13:00:24 -04:00
|
|
|
|
2019-03-21 19:03:16 -04:00
|
|
|
g_list_free_full(prekeys, free);
|
|
|
|
g_list_free(lengths);
|
|
|
|
g_list_free(ids);
|
|
|
|
|
2019-04-08 13:09:25 -04:00
|
|
|
if (connection_supports(XMPP_FEATURE_PUBSUB_PUBLISH_OPTIONS)) {
|
2021-03-09 11:48:29 -05:00
|
|
|
stanza_attach_publish_options_va(ctx, iq,
|
|
|
|
4, // 2 * number of key-value pairs
|
|
|
|
"pubsub#persist_items", "true",
|
|
|
|
"pubsub#access_model", "open");
|
2019-04-08 13:09:25 -04:00
|
|
|
}
|
2019-03-19 13:00:24 -04:00
|
|
|
|
2019-03-28 18:30:28 -04:00
|
|
|
iq_id_handler_add(id, _omemo_bundle_publish_result, NULL, GINT_TO_POINTER(first));
|
|
|
|
|
2019-02-21 13:04:01 -05:00
|
|
|
iq_send_stanza(iq);
|
|
|
|
|
2019-03-28 18:30:28 -04:00
|
|
|
xmpp_stanza_release(iq);
|
2019-02-21 13:04:01 -05:00
|
|
|
free(identity_key);
|
|
|
|
free(signed_prekey);
|
|
|
|
free(signed_prekey_signature);
|
2019-03-28 18:30:28 -04:00
|
|
|
free(id);
|
2019-02-21 13:04:01 -05:00
|
|
|
}
|
2019-02-22 13:17:26 -05:00
|
|
|
|
|
|
|
void
|
2020-07-07 08:18:57 -04:00
|
|
|
omemo_bundle_request(const char* const jid, uint32_t device_id, ProfIqCallback func, ProfIqFreeCallback free_func, void* userdata)
|
2019-02-22 13:17:26 -05:00
|
|
|
{
|
2020-07-07 08:18:57 -04:00
|
|
|
xmpp_ctx_t* const ctx = connection_get_ctx();
|
|
|
|
char* id = connection_create_stanza_id();
|
2019-02-25 11:27:11 -05:00
|
|
|
|
2021-05-28 11:45:15 -04:00
|
|
|
log_debug("[OMEMO] request omemo bundle (jid: %s, device: %d)", jid, device_id);
|
2021-03-09 11:48:29 -05:00
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
xmpp_stanza_t* iq = stanza_create_omemo_bundle_request(ctx, id, jid, device_id);
|
2019-02-25 11:27:11 -05:00
|
|
|
iq_id_handler_add(id, func, free_func, userdata);
|
|
|
|
|
|
|
|
iq_send_stanza(iq);
|
|
|
|
|
|
|
|
free(id);
|
|
|
|
xmpp_stanza_release(iq);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2020-07-07 08:18:57 -04:00
|
|
|
omemo_start_device_session_handle_bundle(xmpp_stanza_t* const stanza, void* const userdata)
|
2019-02-25 11:27:11 -05:00
|
|
|
{
|
2020-07-07 08:18:57 -04:00
|
|
|
GList* prekeys_list = NULL;
|
|
|
|
unsigned char* signed_prekey_raw = NULL;
|
|
|
|
unsigned char* signed_prekey_signature_raw = NULL;
|
|
|
|
char* from = NULL;
|
2019-07-04 05:21:16 -04:00
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
const char* from_attr = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
|
2021-05-28 11:45:15 -04:00
|
|
|
log_debug("[OMEMO] omemo_start_device_session_handle_bundle: %s", from_attr);
|
|
|
|
|
|
|
|
const char* type = xmpp_stanza_get_type(stanza);
|
|
|
|
if ( g_strcmp0( type, "error") == 0 ) {
|
|
|
|
log_error("[OMEMO] Error to get key for a device from : %s", from_attr);
|
|
|
|
}
|
2019-07-04 05:21:16 -04:00
|
|
|
|
2019-03-29 08:54:21 -04:00
|
|
|
if (!from_attr) {
|
2020-07-07 08:18:57 -04:00
|
|
|
Jid* jid = jid_create(connection_get_fulljid());
|
2019-03-29 08:54:21 -04:00
|
|
|
from = strdup(jid->barejid);
|
|
|
|
jid_destroy(jid);
|
|
|
|
} else {
|
|
|
|
from = strdup(from_attr);
|
2019-02-25 11:27:11 -05:00
|
|
|
}
|
|
|
|
|
2019-02-26 13:53:06 -05:00
|
|
|
if (g_strcmp0(from, userdata) != 0) {
|
2019-07-04 05:21:16 -04:00
|
|
|
goto out;
|
2019-02-25 11:27:11 -05:00
|
|
|
}
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
xmpp_stanza_t* pubsub = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_PUBSUB);
|
2019-02-25 11:27:11 -05:00
|
|
|
if (!pubsub) {
|
2019-07-04 05:21:16 -04:00
|
|
|
goto out;
|
2019-02-25 11:27:11 -05:00
|
|
|
}
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
xmpp_stanza_t* items = xmpp_stanza_get_child_by_name(pubsub, "items");
|
2019-02-25 11:27:11 -05:00
|
|
|
if (!items) {
|
2019-07-04 05:21:16 -04:00
|
|
|
goto out;
|
2019-02-25 11:27:11 -05:00
|
|
|
}
|
2020-07-07 08:18:57 -04:00
|
|
|
const char* node = xmpp_stanza_get_attribute(items, "node");
|
|
|
|
char* device_id_str = strstr(node, ":");
|
2019-02-25 11:27:11 -05:00
|
|
|
if (!device_id_str) {
|
2019-07-04 05:21:16 -04:00
|
|
|
goto out;
|
2019-02-25 11:27:11 -05:00
|
|
|
}
|
|
|
|
|
2019-02-26 13:53:06 -05:00
|
|
|
uint32_t device_id = strtoul(++device_id_str, NULL, 10);
|
2021-05-28 11:45:15 -04:00
|
|
|
log_debug("[OMEMO] omemo_start_device_session_handle_bundle: %d", device_id);
|
2019-02-25 11:27:11 -05:00
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
xmpp_stanza_t* item = xmpp_stanza_get_child_by_name(items, "item");
|
2019-02-25 11:27:11 -05:00
|
|
|
if (!item) {
|
2019-07-04 05:21:16 -04:00
|
|
|
goto out;
|
2019-02-25 11:27:11 -05:00
|
|
|
}
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
xmpp_stanza_t* bundle = xmpp_stanza_get_child_by_ns(item, STANZA_NS_OMEMO);
|
2019-02-25 11:27:11 -05:00
|
|
|
if (!bundle) {
|
2019-07-04 05:21:16 -04:00
|
|
|
goto out;
|
2019-02-25 11:27:11 -05:00
|
|
|
}
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
xmpp_stanza_t* prekeys = xmpp_stanza_get_child_by_name(bundle, "prekeys");
|
2019-02-25 11:27:11 -05:00
|
|
|
if (!prekeys) {
|
2019-07-04 05:21:16 -04:00
|
|
|
goto out;
|
2019-02-25 11:27:11 -05:00
|
|
|
}
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
xmpp_stanza_t* prekey;
|
2019-03-06 11:10:28 -05:00
|
|
|
for (prekey = xmpp_stanza_get_children(prekeys); prekey != NULL; prekey = xmpp_stanza_get_next(prekey)) {
|
2019-10-29 17:48:14 -04:00
|
|
|
if (g_strcmp0(xmpp_stanza_get_name(prekey), "preKeyPublic") != 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
omemo_key_t* key = malloc(sizeof(omemo_key_t));
|
2019-07-08 10:33:58 -04:00
|
|
|
key->data = NULL;
|
2019-03-06 11:10:28 -05:00
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
const char* prekey_id_text = xmpp_stanza_get_attribute(prekey, "preKeyId");
|
2019-03-06 11:10:28 -05:00
|
|
|
if (!prekey_id_text) {
|
2019-07-04 05:55:53 -04:00
|
|
|
omemo_key_free(key);
|
2020-01-11 16:29:46 -05:00
|
|
|
continue;
|
2019-03-06 11:10:28 -05:00
|
|
|
}
|
2019-07-04 05:55:53 -04:00
|
|
|
|
2019-03-06 11:10:28 -05:00
|
|
|
key->id = strtoul(prekey_id_text, NULL, 10);
|
2020-07-07 08:18:57 -04:00
|
|
|
xmpp_stanza_t* prekey_text = xmpp_stanza_get_children(prekey);
|
2019-07-04 05:55:53 -04:00
|
|
|
|
2019-03-06 11:10:28 -05:00
|
|
|
if (!prekey_text) {
|
2019-07-04 05:55:53 -04:00
|
|
|
omemo_key_free(key);
|
2020-01-11 16:29:46 -05:00
|
|
|
continue;
|
2019-03-06 11:10:28 -05:00
|
|
|
}
|
2019-07-04 05:55:53 -04:00
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
char* prekey_b64 = xmpp_stanza_get_text(prekey_text);
|
2019-03-21 19:03:16 -04:00
|
|
|
key->data = g_base64_decode(prekey_b64, &key->length);
|
|
|
|
free(prekey_b64);
|
2020-01-11 16:29:46 -05:00
|
|
|
if (!key->data) {
|
|
|
|
omemo_key_free(key);
|
|
|
|
continue;
|
|
|
|
}
|
2019-03-06 11:10:28 -05:00
|
|
|
key->prekey = TRUE;
|
|
|
|
key->device_id = device_id;
|
|
|
|
|
|
|
|
prekeys_list = g_list_append(prekeys_list, key);
|
2019-02-25 11:27:11 -05:00
|
|
|
}
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
xmpp_stanza_t* signed_prekey = xmpp_stanza_get_child_by_name(bundle, "signedPreKeyPublic");
|
2019-02-26 12:23:08 -05:00
|
|
|
if (!signed_prekey) {
|
2019-07-04 05:21:16 -04:00
|
|
|
goto out;
|
2019-02-26 12:23:08 -05:00
|
|
|
}
|
2020-07-07 08:18:57 -04:00
|
|
|
const char* signed_prekey_id_text = xmpp_stanza_get_attribute(signed_prekey, "signedPreKeyId");
|
2019-02-26 12:23:08 -05:00
|
|
|
if (!signed_prekey_id_text) {
|
2019-07-04 05:21:16 -04:00
|
|
|
goto out;
|
2019-02-26 12:23:08 -05:00
|
|
|
}
|
2019-07-04 05:21:16 -04:00
|
|
|
|
2019-02-26 12:23:08 -05:00
|
|
|
uint32_t signed_prekey_id = strtoul(signed_prekey_id_text, NULL, 10);
|
2020-07-07 08:18:57 -04:00
|
|
|
xmpp_stanza_t* signed_prekey_text = xmpp_stanza_get_children(signed_prekey);
|
2019-02-26 12:23:08 -05:00
|
|
|
if (!signed_prekey_text) {
|
2019-07-04 05:21:16 -04:00
|
|
|
goto out;
|
2019-02-26 12:23:08 -05:00
|
|
|
}
|
2019-07-04 05:21:16 -04:00
|
|
|
|
2019-02-26 12:23:08 -05:00
|
|
|
size_t signed_prekey_len;
|
2020-07-07 08:18:57 -04:00
|
|
|
char* signed_prekey_b64 = xmpp_stanza_get_text(signed_prekey_text);
|
2019-07-04 05:21:16 -04:00
|
|
|
signed_prekey_raw = g_base64_decode(signed_prekey_b64, &signed_prekey_len);
|
2019-03-21 19:03:16 -04:00
|
|
|
free(signed_prekey_b64);
|
2020-01-11 16:29:46 -05:00
|
|
|
if (!signed_prekey_raw) {
|
|
|
|
goto out;
|
|
|
|
}
|
2019-02-26 12:23:08 -05:00
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
xmpp_stanza_t* signed_prekey_signature = xmpp_stanza_get_child_by_name(bundle, "signedPreKeySignature");
|
2019-02-26 12:23:08 -05:00
|
|
|
if (!signed_prekey_signature) {
|
2019-07-04 05:21:16 -04:00
|
|
|
goto out;
|
2019-02-26 12:23:08 -05:00
|
|
|
}
|
2020-07-07 08:18:57 -04:00
|
|
|
xmpp_stanza_t* signed_prekey_signature_text = xmpp_stanza_get_children(signed_prekey_signature);
|
2019-02-26 12:23:08 -05:00
|
|
|
if (!signed_prekey_signature_text) {
|
2019-07-04 05:21:16 -04:00
|
|
|
goto out;
|
2019-02-26 12:23:08 -05:00
|
|
|
}
|
|
|
|
size_t signed_prekey_signature_len;
|
2020-07-07 08:18:57 -04:00
|
|
|
char* signed_prekey_signature_b64 = xmpp_stanza_get_text(signed_prekey_signature_text);
|
2019-07-04 05:21:16 -04:00
|
|
|
signed_prekey_signature_raw = g_base64_decode(signed_prekey_signature_b64, &signed_prekey_signature_len);
|
2019-03-21 19:03:16 -04:00
|
|
|
free(signed_prekey_signature_b64);
|
2020-01-11 16:29:46 -05:00
|
|
|
if (!signed_prekey_signature_raw) {
|
|
|
|
goto out;
|
|
|
|
}
|
2019-02-26 12:23:08 -05:00
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
xmpp_stanza_t* identity_key = xmpp_stanza_get_child_by_name(bundle, "identityKey");
|
2019-02-26 12:23:08 -05:00
|
|
|
if (!identity_key) {
|
2019-07-04 05:21:16 -04:00
|
|
|
goto out;
|
2019-02-26 12:23:08 -05:00
|
|
|
}
|
2020-07-07 08:18:57 -04:00
|
|
|
xmpp_stanza_t* identity_key_text = xmpp_stanza_get_children(identity_key);
|
2019-02-26 12:23:08 -05:00
|
|
|
if (!identity_key_text) {
|
2019-07-04 05:21:16 -04:00
|
|
|
goto out;
|
2019-02-26 12:23:08 -05:00
|
|
|
}
|
|
|
|
size_t identity_key_len;
|
2020-07-07 08:18:57 -04:00
|
|
|
char* identity_key_b64 = xmpp_stanza_get_text(identity_key_text);
|
|
|
|
unsigned char* identity_key_raw = g_base64_decode(identity_key_b64, &identity_key_len);
|
2019-03-21 19:03:16 -04:00
|
|
|
free(identity_key_b64);
|
2020-01-11 16:29:46 -05:00
|
|
|
if (!identity_key_raw) {
|
|
|
|
goto out;
|
|
|
|
}
|
2019-02-26 12:23:08 -05:00
|
|
|
|
2019-03-06 11:10:28 -05:00
|
|
|
omemo_start_device_session(from, device_id, prekeys_list, signed_prekey_id,
|
2020-07-07 08:18:57 -04:00
|
|
|
signed_prekey_raw, signed_prekey_len, signed_prekey_signature_raw,
|
|
|
|
signed_prekey_signature_len, identity_key_raw, identity_key_len);
|
2019-03-21 19:03:16 -04:00
|
|
|
|
|
|
|
g_free(identity_key_raw);
|
2019-07-04 05:21:16 -04:00
|
|
|
|
|
|
|
out:
|
|
|
|
free(from);
|
|
|
|
if (signed_prekey_raw) {
|
|
|
|
g_free(signed_prekey_raw);
|
|
|
|
}
|
|
|
|
if (signed_prekey_signature_raw) {
|
|
|
|
g_free(signed_prekey_signature_raw);
|
|
|
|
}
|
|
|
|
if (prekeys_list) {
|
|
|
|
g_list_free_full(prekeys_list, (GDestroyNotify)omemo_key_free);
|
|
|
|
}
|
2019-02-25 11:27:11 -05:00
|
|
|
return 1;
|
2019-02-22 13:17:26 -05:00
|
|
|
}
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
char*
|
|
|
|
omemo_receive_message(xmpp_stanza_t* const stanza, gboolean* trusted)
|
2019-02-28 21:50:23 -05:00
|
|
|
{
|
2020-07-07 08:18:57 -04:00
|
|
|
char* plaintext = NULL;
|
|
|
|
const char* type = xmpp_stanza_get_type(stanza);
|
|
|
|
GList* keys = NULL;
|
|
|
|
unsigned char* iv_raw = NULL;
|
|
|
|
unsigned char* payload_raw = NULL;
|
|
|
|
char* iv_text = NULL;
|
|
|
|
char* payload_text = NULL;
|
|
|
|
|
|
|
|
xmpp_stanza_t* encrypted = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_OMEMO);
|
2019-02-28 21:50:23 -05:00
|
|
|
if (!encrypted) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
xmpp_stanza_t* header = xmpp_stanza_get_child_by_name(encrypted, "header");
|
2019-02-28 21:50:23 -05:00
|
|
|
if (!header) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
const char* sid_text = xmpp_stanza_get_attribute(header, "sid");
|
2019-02-28 21:50:23 -05:00
|
|
|
if (!sid_text) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
uint32_t sid = strtoul(sid_text, NULL, 10);
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
xmpp_stanza_t* iv = xmpp_stanza_get_child_by_name(header, "iv");
|
2019-02-28 21:50:23 -05:00
|
|
|
if (!iv) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-01-20 07:55:02 -05:00
|
|
|
iv_text = xmpp_stanza_get_text(iv);
|
2019-02-28 21:50:23 -05:00
|
|
|
if (!iv_text) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
size_t iv_len;
|
2020-01-20 07:55:02 -05:00
|
|
|
iv_raw = g_base64_decode(iv_text, &iv_len);
|
2020-01-11 16:29:46 -05:00
|
|
|
if (!iv_raw) {
|
|
|
|
goto out;
|
|
|
|
}
|
2019-02-28 21:50:23 -05:00
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
xmpp_stanza_t* payload = xmpp_stanza_get_child_by_name(encrypted, "payload");
|
2019-02-28 21:50:23 -05:00
|
|
|
if (!payload) {
|
2020-01-11 16:29:46 -05:00
|
|
|
goto out;
|
2019-02-28 21:50:23 -05:00
|
|
|
}
|
2020-01-20 07:55:02 -05:00
|
|
|
payload_text = xmpp_stanza_get_text(payload);
|
2019-02-28 21:50:23 -05:00
|
|
|
if (!payload_text) {
|
2020-01-11 16:29:46 -05:00
|
|
|
goto out;
|
2019-02-28 21:50:23 -05:00
|
|
|
}
|
|
|
|
size_t payload_len;
|
2020-01-20 07:55:02 -05:00
|
|
|
payload_raw = g_base64_decode(payload_text, &payload_len);
|
2020-01-11 16:29:46 -05:00
|
|
|
if (!payload_raw) {
|
|
|
|
goto out;
|
|
|
|
}
|
2019-02-28 21:50:23 -05:00
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
xmpp_stanza_t* key_stanza;
|
2019-02-28 21:50:23 -05:00
|
|
|
for (key_stanza = xmpp_stanza_get_children(header); key_stanza != NULL; key_stanza = xmpp_stanza_get_next(key_stanza)) {
|
|
|
|
if (g_strcmp0(xmpp_stanza_get_name(key_stanza), "key") != 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
omemo_key_t* key = malloc(sizeof(omemo_key_t));
|
|
|
|
char* key_text = xmpp_stanza_get_text(key_stanza);
|
2019-02-28 21:50:23 -05:00
|
|
|
if (!key_text) {
|
|
|
|
goto skip;
|
|
|
|
}
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
const char* rid_text = xmpp_stanza_get_attribute(key_stanza, "rid");
|
2019-02-28 21:50:23 -05:00
|
|
|
key->device_id = strtoul(rid_text, NULL, 10);
|
|
|
|
if (!key->device_id) {
|
|
|
|
goto skip;
|
|
|
|
}
|
2019-07-04 05:21:16 -04:00
|
|
|
|
2019-02-28 21:50:23 -05:00
|
|
|
key->data = g_base64_decode(key_text, &key->length);
|
2019-03-21 19:03:16 -04:00
|
|
|
free(key_text);
|
2020-01-11 16:29:46 -05:00
|
|
|
if (!key->data) {
|
|
|
|
goto skip;
|
|
|
|
}
|
2020-07-07 08:18:57 -04:00
|
|
|
key->prekey = g_strcmp0(xmpp_stanza_get_attribute(key_stanza, "prekey"), "true") == 0
|
|
|
|
|| g_strcmp0(xmpp_stanza_get_attribute(key_stanza, "prekey"), "1") == 0;
|
2019-02-28 21:50:23 -05:00
|
|
|
keys = g_list_append(keys, key);
|
|
|
|
continue;
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
skip:
|
2019-02-28 21:50:23 -05:00
|
|
|
free(key);
|
|
|
|
}
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
const char* from = xmpp_stanza_get_from(stanza);
|
2019-02-28 21:50:23 -05:00
|
|
|
|
2020-01-11 16:29:46 -05:00
|
|
|
plaintext = omemo_on_message_recv(from, sid, iv_raw, iv_len,
|
2020-07-07 08:18:57 -04:00
|
|
|
keys, payload_raw, payload_len,
|
|
|
|
g_strcmp0(type, STANZA_TYPE_GROUPCHAT) == 0, trusted);
|
2019-02-28 21:50:23 -05:00
|
|
|
|
2020-01-11 16:29:46 -05:00
|
|
|
out:
|
|
|
|
if (keys) {
|
|
|
|
g_list_free_full(keys, (GDestroyNotify)omemo_key_free);
|
|
|
|
}
|
2020-01-20 07:55:02 -05:00
|
|
|
|
|
|
|
g_free(iv_raw);
|
|
|
|
g_free(payload_raw);
|
|
|
|
g_free(iv_text);
|
|
|
|
g_free(payload_text);
|
2019-03-21 19:03:16 -04:00
|
|
|
|
2019-02-28 21:50:23 -05:00
|
|
|
return plaintext;
|
|
|
|
}
|
|
|
|
|
2019-02-22 13:17:26 -05:00
|
|
|
static int
|
2020-07-07 08:18:57 -04:00
|
|
|
_omemo_receive_devicelist(xmpp_stanza_t* const stanza, void* const userdata)
|
2019-02-22 13:17:26 -05:00
|
|
|
{
|
2020-07-07 08:18:57 -04:00
|
|
|
GList* device_list = NULL;
|
|
|
|
const char* from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
|
2019-02-22 13:17:26 -05:00
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
xmpp_stanza_t* root = NULL;
|
|
|
|
xmpp_stanza_t* event = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_PUBSUB_EVENT);
|
2019-02-24 23:59:41 -05:00
|
|
|
if (event) {
|
|
|
|
root = event;
|
|
|
|
}
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
xmpp_stanza_t* pubsub = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_PUBSUB);
|
2019-02-24 23:59:41 -05:00
|
|
|
if (pubsub) {
|
|
|
|
root = pubsub;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!root) {
|
2019-04-01 07:42:24 -04:00
|
|
|
return 1;
|
2019-02-22 13:17:26 -05:00
|
|
|
}
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
xmpp_stanza_t* items = xmpp_stanza_get_child_by_name(root, "items");
|
2019-02-22 13:17:26 -05:00
|
|
|
if (!items) {
|
2019-04-01 07:42:24 -04:00
|
|
|
return 1;
|
2019-02-22 13:17:26 -05:00
|
|
|
}
|
|
|
|
|
2020-07-03 12:30:23 -04:00
|
|
|
// Looking for "current" item - if there is no current, take the first item.
|
|
|
|
xmpp_stanza_t* first = NULL;
|
|
|
|
xmpp_stanza_t* current = NULL;
|
2020-07-07 08:18:57 -04:00
|
|
|
|
|
|
|
xmpp_stanza_t* item = xmpp_stanza_get_children(items);
|
|
|
|
while (item) {
|
2020-07-03 12:30:23 -04:00
|
|
|
if (g_strcmp0(xmpp_stanza_get_name(item), "item") == 0) {
|
|
|
|
first = item;
|
2020-07-07 08:18:57 -04:00
|
|
|
if (g_strcmp0(xmpp_stanza_get_id(item), "current") == 0) {
|
2020-07-03 12:30:23 -04:00
|
|
|
current = item;
|
|
|
|
break;
|
2019-04-24 09:20:54 -04:00
|
|
|
}
|
2020-07-03 12:30:23 -04:00
|
|
|
}
|
|
|
|
item = xmpp_stanza_get_next(item);
|
|
|
|
}
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
if (current) {
|
2020-07-03 12:30:23 -04:00
|
|
|
item = current;
|
2020-07-07 08:18:57 -04:00
|
|
|
} else if (first) {
|
2021-03-09 11:47:27 -05:00
|
|
|
log_warning("[OMEMO] User %s has a non 'current' device item list: %s.", from, xmpp_stanza_get_id(first));
|
2020-07-03 12:30:23 -04:00
|
|
|
item = first;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
xmpp_stanza_t* list = xmpp_stanza_get_child_by_ns(item, STANZA_NS_OMEMO);
|
2020-07-03 12:30:23 -04:00
|
|
|
if (!list) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
xmpp_stanza_t* device;
|
2020-07-03 12:30:23 -04:00
|
|
|
for (device = xmpp_stanza_get_children(list); device != NULL; device = xmpp_stanza_get_next(device)) {
|
|
|
|
if (g_strcmp0(xmpp_stanza_get_name(device), "device") != 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
const char* id = xmpp_stanza_get_id(device);
|
2020-07-03 12:30:23 -04:00
|
|
|
if (id != NULL) {
|
|
|
|
device_list = g_list_append(device_list, GINT_TO_POINTER(strtoul(id, NULL, 10)));
|
2020-05-29 04:28:41 -04:00
|
|
|
} else {
|
2021-03-09 11:47:27 -05:00
|
|
|
log_error("[OMEMO] received device without ID");
|
2019-03-14 14:03:14 -04:00
|
|
|
}
|
2019-02-22 13:17:26 -05:00
|
|
|
}
|
2020-05-23 01:41:06 -04:00
|
|
|
|
2019-02-22 13:17:26 -05:00
|
|
|
omemo_set_device_list(from, device_list);
|
|
|
|
|
2019-04-01 07:42:24 -04:00
|
|
|
return 1;
|
2019-03-28 18:30:28 -04:00
|
|
|
}
|
|
|
|
|
2021-03-09 11:48:29 -05:00
|
|
|
|
2019-03-28 18:30:28 -04:00
|
|
|
static int
|
2020-07-07 08:18:57 -04:00
|
|
|
_omemo_bundle_publish_result(xmpp_stanza_t* const stanza, void* const userdata)
|
2019-03-28 18:30:28 -04:00
|
|
|
{
|
2021-03-09 11:47:43 -05:00
|
|
|
log_debug("[OMEMO] _omemo_bundle_publish_result()");
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
const char* type = xmpp_stanza_get_type(stanza);
|
2019-03-28 18:30:28 -04:00
|
|
|
|
2021-03-09 11:48:29 -05:00
|
|
|
if (g_strcmp0(type, STANZA_TYPE_RESULT) == 0) {
|
2021-05-28 11:45:15 -04:00
|
|
|
log_debug("[OMEMO] bundle published successfully");
|
2019-03-28 18:30:28 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!GPOINTER_TO_INT(userdata)) {
|
2021-03-09 11:47:27 -05:00
|
|
|
log_error("[OMEMO] definitely cannot publish bundle with an open access model");
|
2019-03-28 18:30:28 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-05-28 11:45:15 -04:00
|
|
|
log_debug("[OMEMO] cannot publish bundle with open access model, trying to configure node");
|
2020-07-07 08:18:57 -04:00
|
|
|
xmpp_ctx_t* const ctx = connection_get_ctx();
|
|
|
|
Jid* jid = jid_create(connection_get_fulljid());
|
|
|
|
char* id = connection_create_stanza_id();
|
|
|
|
char* node = g_strdup_printf("%s:%d", STANZA_NS_OMEMO_BUNDLES, omemo_device_id());
|
2021-05-28 11:45:15 -04:00
|
|
|
log_debug("[OMEMO] node: %s", node);
|
2021-03-09 11:47:27 -05:00
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
xmpp_stanza_t* iq = stanza_create_pubsub_configure_request(ctx, id, jid->barejid, node);
|
2019-03-28 18:30:28 -04:00
|
|
|
g_free(node);
|
|
|
|
|
|
|
|
iq_id_handler_add(id, _omemo_bundle_publish_configure, NULL, userdata);
|
|
|
|
|
|
|
|
iq_send_stanza(iq);
|
|
|
|
|
|
|
|
xmpp_stanza_release(iq);
|
|
|
|
free(id);
|
|
|
|
jid_destroy(jid);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2020-07-07 08:18:57 -04:00
|
|
|
_omemo_bundle_publish_configure(xmpp_stanza_t* const stanza, void* const userdata)
|
2019-03-28 18:30:28 -04:00
|
|
|
{
|
2021-03-09 11:47:43 -05:00
|
|
|
log_debug("[OMEMO] _omemo_bundle_publish_configure()");
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
xmpp_stanza_t* pubsub = xmpp_stanza_get_child_by_name(stanza, "pubsub");
|
2021-03-09 11:47:43 -05:00
|
|
|
if (!pubsub) {
|
|
|
|
log_error("[OMEMO] The stanza doesn't contain a 'pubsub' child");
|
|
|
|
return 0;
|
|
|
|
}
|
2020-07-07 08:18:57 -04:00
|
|
|
xmpp_stanza_t* configure = xmpp_stanza_get_child_by_name(pubsub, STANZA_NAME_CONFIGURE);
|
2021-03-09 11:47:43 -05:00
|
|
|
if (!configure) {
|
|
|
|
log_error("[OMEMO] The stanza doesn't contain a 'configure' child");
|
|
|
|
return 0;
|
|
|
|
}
|
2020-07-07 08:18:57 -04:00
|
|
|
xmpp_stanza_t* x = xmpp_stanza_get_child_by_name(configure, "x");
|
2021-03-09 11:47:43 -05:00
|
|
|
if (!x) {
|
|
|
|
log_error("[OMEMO] The stanza doesn't contain an 'x' child");
|
|
|
|
return 0;
|
|
|
|
}
|
2019-03-28 18:30:28 -04:00
|
|
|
|
|
|
|
DataForm* form = form_create(x);
|
2020-07-07 08:18:57 -04:00
|
|
|
char* tag = g_hash_table_lookup(form->var_to_tag, "pubsub#access_model");
|
2019-03-28 18:30:28 -04:00
|
|
|
if (!tag) {
|
2021-03-09 11:47:27 -05:00
|
|
|
log_error("[OMEMO] cannot configure bundle to an open access model");
|
2019-03-28 18:30:28 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
xmpp_ctx_t* const ctx = connection_get_ctx();
|
|
|
|
Jid* jid = jid_create(connection_get_fulljid());
|
|
|
|
char* id = connection_create_stanza_id();
|
|
|
|
char* node = g_strdup_printf("%s:%d", STANZA_NS_OMEMO_BUNDLES, omemo_device_id());
|
|
|
|
xmpp_stanza_t* iq = stanza_create_pubsub_configure_submit(ctx, id, jid->barejid, node, form);
|
2019-03-28 18:30:28 -04:00
|
|
|
g_free(node);
|
|
|
|
|
|
|
|
iq_id_handler_add(id, _omemo_bundle_publish_configure_result, NULL, userdata);
|
|
|
|
|
|
|
|
iq_send_stanza(iq);
|
|
|
|
|
|
|
|
xmpp_stanza_release(iq);
|
|
|
|
free(id);
|
|
|
|
jid_destroy(jid);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2020-07-07 08:18:57 -04:00
|
|
|
_omemo_bundle_publish_configure_result(xmpp_stanza_t* const stanza, void* const userdata)
|
2019-03-28 18:30:28 -04:00
|
|
|
{
|
2020-07-07 08:18:57 -04:00
|
|
|
const char* type = xmpp_stanza_get_type(stanza);
|
2019-03-28 18:30:28 -04:00
|
|
|
|
|
|
|
if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) {
|
2021-03-09 11:47:27 -05:00
|
|
|
log_error("[OMEMO] cannot configure bundle to an open access model: Result error");
|
2019-03-28 18:30:28 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-05-28 11:45:15 -04:00
|
|
|
log_debug("[OMEMO] node configured");
|
2021-03-09 11:48:29 -05:00
|
|
|
|
|
|
|
// Try to publish again
|
2019-03-28 18:30:28 -04:00
|
|
|
omemo_bundle_publish(TRUE);
|
|
|
|
|
|
|
|
return 0;
|
2019-02-22 13:17:26 -05:00
|
|
|
}
|
2021-06-01 00:39:32 -04:00
|
|
|
|
|
|
|
static int
|
|
|
|
_omemo_device_list_publish_result(xmpp_stanza_t* const stanza, void* const userdata)
|
|
|
|
{
|
|
|
|
const char* type = xmpp_stanza_get_type(stanza);
|
|
|
|
if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) {
|
|
|
|
cons_show_error("Unable to publish own OMEMO device list");
|
|
|
|
log_error("[OMEMO] Publishing device list failed");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|