Commit 527bb6cc authored by Richard Bayerle's avatar Richard Bayerle
Browse files

Use key data directly as parameter for fingerprint generation

Instead of taking a char array as returned by purple_base16_encode()
which was manually called on the key data before every invocation
parent 8dc62869
......@@ -159,7 +159,8 @@ $(BDIR)/test_lurch_util: $(OBJECTS_W_COVERAGE) $(VENDOR_LIBS) $(BDIR)/test_lurch
-Wl,--wrap=purple_prefs_get_int \
-Wl,--wrap=purple_debug_error \
-Wl,--wrap=purple_debug_info \
-Wl,--wrap=purple_debug_misc
-Wl,--wrap=purple_debug_misc \
-Wl,--wrap=purple_base16_encode_chunked
bash -c "set -o pipefail; $@ 2>&1 | grep -Ev ".*CRITICAL.*" | tr -s '\n'" # filter annoying and irrelevant glib output
$(BDIR)/test_lurch_api: $(OBJECTS_W_COVERAGE) $(VENDOR_LIBS) $(BDIR)/test_lurch_api.o
......
......@@ -2142,7 +2142,6 @@ static PurpleCmdRet lurch_cmd_func(PurpleConversation * conv_p,
uint32_t id = 0;
uint32_t remove_id = 0;
axc_buf * key_buf_p = (void *) 0;
gchar * fp = (void *) 0;
char * fp_printable = (void *) 0;
omemo_devicelist * own_dl_p = (void *) 0;
omemo_devicelist * other_dl_p = (void *) 0;
......@@ -2302,8 +2301,7 @@ static PurpleCmdRet lurch_cmd_func(PurpleConversation * conv_p,
goto cleanup;
}
fp = purple_base16_encode_chunked(axc_buf_get_data(key_buf_p), axc_buf_get_len(key_buf_p));
fp_printable = lurch_util_fp_get_printable(fp);
fp_printable = lurch_util_fp_get_printable(key_buf_p);
msg = g_strdup_printf("This device's fingerprint is:\n%s\n"
"You should make sure that your conversation partner gets displayed the same for this device.", fp_printable);
} else if (!g_strcmp0(args[2], "conv")) {
......@@ -2314,8 +2312,7 @@ static PurpleCmdRet lurch_cmd_func(PurpleConversation * conv_p,
goto cleanup;
}
fp = purple_base16_encode_chunked(axc_buf_get_data(key_buf_p), axc_buf_get_len(key_buf_p));
fp_printable = lurch_util_fp_get_printable(fp);
fp_printable = lurch_util_fp_get_printable(key_buf_p);
temp_msg_1 = g_strdup_printf("The devices participating in this conversation and their fingerprints are as follows:\n"
"This device's (%s:%i) fingerprint:\n%s\n", uname, id, fp_printable);
......@@ -2337,9 +2334,7 @@ static PurpleCmdRet lurch_cmd_func(PurpleConversation * conv_p,
continue;
}
g_free(fp);
fp = purple_base16_encode_chunked(axc_buf_get_data(key_buf_p), axc_buf_get_len(key_buf_p));
fp_printable = lurch_util_fp_get_printable(fp);
fp_printable = lurch_util_fp_get_printable(key_buf_p);
axc_buf_free(key_buf_p);
key_buf_p = (void *) 0;
......@@ -2371,9 +2366,7 @@ static PurpleCmdRet lurch_cmd_func(PurpleConversation * conv_p,
continue;
}
g_free(fp);
fp = purple_base16_encode_chunked(axc_buf_get_data(key_buf_p), axc_buf_get_len(key_buf_p));
fp_printable = lurch_util_fp_get_printable(fp);
fp_printable = lurch_util_fp_get_printable(key_buf_p);
axc_buf_free(key_buf_p);
key_buf_p = (void *) 0;
......@@ -2475,7 +2468,6 @@ cleanup:
axc_context_destroy_all(axc_ctx_p);
g_free(msg);
axc_buf_free(key_buf_p);
g_free(fp);
g_free(fp_printable);
omemo_devicelist_destroy(own_dl_p);
omemo_devicelist_destroy(other_dl_p);
......
......@@ -190,7 +190,6 @@ void lurch_api_fp_get_handler(PurpleAccount * acc_p, void (*cb)(int32_t err, con
char * uname = (void *) 0;
axc_context * axc_ctx_p = (void *) 0;
axc_buf * key_buf_p = (void *) 0;
gchar * fp = (void *) 0;
char * fp_printable = (void *) 0;
uname = lurch_util_uname_strip(purple_account_get_username(acc_p));
......@@ -207,20 +206,18 @@ void lurch_api_fp_get_handler(PurpleAccount * acc_p, void (*cb)(int32_t err, con
goto cleanup;
}
fp = purple_base16_encode_chunked(axc_buf_get_data(key_buf_p), axc_buf_get_len(key_buf_p));
fp_printable = lurch_util_fp_get_printable(fp);
fp_printable = lurch_util_fp_get_printable(key_buf_p);
cleanup:
cb(ret_val, fp_printable, user_data_p);
g_free(fp_printable);
g_free(fp);
axc_buf_free(key_buf_p);
axc_context_destroy_all(axc_ctx_p);
}
/**
* Given a list of IDs, retrieves the public keys from the libsignal sessions and creates hash table with ID to fingerprint pairs.
* Given a list of IDs, retrieves the public keys from the libsignal sessions and creates a hash table with ID to fingerprint pairs.
* If there is an entry in the devicelist, but no session yet, the fingerprint cannot be retrieved this way and the value will be NULL.
* g_hash_table_destroy() the table when done with it.
*/
......@@ -230,7 +227,6 @@ static int32_t lurch_api_fp_create_table(const char * jid, axc_context * axc_ct
const GList * curr_p = (void *) 0;
uint32_t curr_device_id = 0;
axc_buf * key_buf_p = (void *) 0;
gchar * fp = (void *) 0;
id_fp_table = g_hash_table_new_full(g_int_hash, g_int_equal, NULL, g_free);
......@@ -247,13 +243,10 @@ static int32_t lurch_api_fp_create_table(const char * jid, axc_context * axc_ct
continue;
}
fp = purple_base16_encode_chunked(axc_buf_get_data(key_buf_p), axc_buf_get_len(key_buf_p));
(void) g_hash_table_insert(id_fp_table, curr_p->data, lurch_util_fp_get_printable(fp));
(void) g_hash_table_insert(id_fp_table, curr_p->data, lurch_util_fp_get_printable(key_buf_p));
axc_buf_free(key_buf_p);
key_buf_p = (void *) 0;
g_free(fp);
fp = (void *) 0;
ret_val = 0;
}
......@@ -276,7 +269,6 @@ void lurch_api_fp_list_handler(PurpleAccount * acc_p, void (*cb)(int32_t err, GH
axc_context * axc_ctx_p = (void *) 0;
GHashTable * id_fp_table = (void *) 0;
axc_buf * key_buf_p = (void *) 0;
gchar * fp = (void *) 0;
ret_val = lurch_api_id_list_get_own(acc_p, &own_id_list);
if (ret_val) {
......@@ -306,10 +298,7 @@ void lurch_api_fp_list_handler(PurpleAccount * acc_p, void (*cb)(int32_t err, GH
goto cleanup;
}
fp = purple_base16_encode_chunked(axc_buf_get_data(key_buf_p), axc_buf_get_len(key_buf_p));
(void) g_hash_table_insert(id_fp_table, own_id_list->data, lurch_util_fp_get_printable(fp));
g_free(fp);
fp = (void *) 0;
(void) g_hash_table_insert(id_fp_table, own_id_list->data, lurch_util_fp_get_printable(key_buf_p));
cleanup:
cb(ret_val, id_fp_table, user_data_p);
......@@ -319,7 +308,6 @@ cleanup:
axc_context_destroy_all(axc_ctx_p);
g_hash_table_destroy(id_fp_table);
axc_buf_free(key_buf_p);
g_free(fp);
}
// returns NULL as hash table if devicelist is empty
......
......@@ -127,14 +127,22 @@ char * lurch_util_uname_get_db_fn(const char * uname, const char * which) {
return g_strconcat(purple_user_dir(), "/", uname, "_", which, LURCH_DB_SUFFIX, NULL);
}
char * lurch_util_fp_get_printable(const char * fp) {
char * lurch_util_fp_get_printable(axc_buf * key_buf_p) {
gchar * fp = (void *) 0;
char ** split = (void *) 0;
const size_t fp_parts_len = 32;
char * printable = (void *) 0;
const size_t printable_len = 72;
if (!key_buf_p) {
purple_debug_warning("lurch", "%s: Key buffer is null, aborting\n", __func__);
goto cleanup;
}
fp = purple_base16_encode_chunked(axc_buf_get_data(key_buf_p), axc_buf_get_len(key_buf_p));
if (!fp || strlen(fp) != 98) {
return (void *) 0;
purple_debug_warning("lurch", "%s: Unexpected fingerprint length, aborting\n", __func__);
goto cleanup;
}
// first part is dismissed for display
......@@ -149,6 +157,10 @@ char * lurch_util_fp_get_printable(const char * fp) {
}
}
cleanup:
g_free(fp);
g_strfreev(split);
return printable;
}
\ No newline at end of file
}
......@@ -44,7 +44,7 @@ char * lurch_util_uname_get_db_fn(const char * uname, const char * which);
* Creates a fingerprint which resembles the one displayed by Conversations etc.
* Also useful for avoiding the smileys produced by ':d'...
*
* @param fp The fingerprint string as returned by purple_base16_encode_chunked
* @param key_buf_p The buffer containing the public key data.
* @return A newly allocated string which contains the fingerprint in printable format, or NULL. g_free() when done.
*/
char * lurch_util_fp_get_printable(const char * fp);
\ No newline at end of file
char * lurch_util_fp_get_printable(axc_buf * key_buf_p);
\ No newline at end of file
......@@ -48,6 +48,12 @@ int __wrap_purple_prefs_get_int(const char * pref_name) {
return pref_val;
}
char * __wrap_purple_base16_encode_chunked(const guchar *data, gsize len) {
char * fp;
fp = mock_ptr_type(char *);
return fp;
}
/**
* Log only errors when log level is set to AXC_LOG_ERROR, using purple_debug_error().
*/
......@@ -165,10 +171,11 @@ static void test_lurch_util_fp_get_printable(void ** state) {
const char * fp_as_returned_by_pidgin =
"12:34:56:78:12:34:56:78:12:34:56:78:12:34:56:78:12:34:56:78:12:34:56:78:12:34:56:78:12:ab:cd:ef:gh";
char * printable_fp = lurch_util_fp_get_printable(fp_as_returned_by_pidgin);
will_return(__wrap_purple_base16_encode_chunked, g_strdup(fp_as_returned_by_pidgin));
char * printable_fp = lurch_util_fp_get_printable((void *) &"fake non-null pointer");
assert_non_null(printable_fp);
assert_string_equal(printable_fp, "34567812 34567812 34567812 34567812 34567812 34567812 34567812 abcdefgh");
}
static void test_lurch_util_fp_get_printable_invalid(void ** state) {
......@@ -176,13 +183,19 @@ static void test_lurch_util_fp_get_printable_invalid(void ** state) {
assert_null(lurch_util_fp_get_printable(NULL));
will_return(__wrap_purple_base16_encode_chunked, NULL);
assert_null(lurch_util_fp_get_printable((void *) &"fake non-null pointer"));
const char * too_short =
"12:34:56:78:12:34:56:78:12:34:56:78:12:34:56:78:12:34:56:78:12:34:56:78:12:34:56:78:12:ab:cd:efgh";
will_return(__wrap_purple_base16_encode_chunked, g_strdup(too_short));
assert_null(lurch_util_fp_get_printable((void *) &"fake non-null pointer"));
const char * too_long =
"12:34:56:78:12:34:56:78:12:34:56:78:12:34:56:78:12:34:56:78:12:34:56:78:12:34:56:78:12:ab:cd:ef:gh:";
will_return(__wrap_purple_base16_encode_chunked, g_strdup(too_long));
assert_null(lurch_util_fp_get_printable(too_short));
assert_null(lurch_util_fp_get_printable(too_long));
assert_null(lurch_util_fp_get_printable((void *) &"fake non-null pointer"));
}
int main(void) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment