diff options
author | Dr. David Alan Gilbert <dave@treblig.org> | 2017-02-05 02:47:49 +0000 |
---|---|---|
committer | Dr. David Alan Gilbert <dave@treblig.org> | 2018-02-25 02:08:49 +0000 |
commit | 08b25e67e6e7189e4ba268ffe9326071adccc5e1 (patch) | |
tree | 3e5577f2cb2cd8397392d7283dba0e4c5b471aec | |
parent | 01705b7b797a969be0708c66654e32842c034df3 (diff) | |
download | purple-matrix-08b25e67e6e7189e4ba268ffe9326071adccc5e1.tar.gz |
e2e: get_id_keys
Parse the output of olm_account_identity_keys to get a list of
algorithms and the keys for them.
There really should be a better way; the algorithm names in here
match what we need in the keys section, but we have to append
the device id later, so it's a pain that it returns (undocumented)
JSON.
Signed-off-by: Dr. David Alan Gilbert <dave@treblig.org>
-rw-r--r-- | matrix-e2e.c | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/matrix-e2e.c b/matrix-e2e.c index 2e3acd9..792c580 100644 --- a/matrix-e2e.c +++ b/matrix-e2e.c @@ -238,6 +238,64 @@ out: return ret; } +/* Returns the list of algorithms and our keys for those algorithms on the current account */ +static int get_id_keys(PurpleConnection *pc, OlmAccount *account, gchar ***algorithms, gchar ***keys) +{ + /* There has to be an easier way than this.... */ + size_t id_key_len = olm_account_identity_keys_length(account); + gchar *id_keys = g_malloc0(id_key_len+1); + if (olm_account_identity_keys(account, id_keys, id_key_len) == + olm_error()) { + purple_connection_error_reason(pc, + PURPLE_CONNECTION_ERROR_OTHER_ERROR, + olm_account_last_error(account)); + g_free(id_keys); + return -1; + } + + /* We get back a json string, something like: + * {"curve25519":"encodedkey...","ed25519":"encodedkey...."}' + */ + JsonParser *json_parser = json_parser_new(); + GError *err = NULL; + if (!json_parser_load_from_data(json_parser, + id_keys, strlen(id_keys), &err)) { + purple_connection_error_reason(pc, + PURPLE_CONNECTION_ERROR_OTHER_ERROR, + "Failed to parse olm account ID keys"); + purple_debug_info("matrixprpl", + "unable to parse olm account ID keys: %s", + err->message); + g_error_free(err); + g_free(id_keys); + g_object_unref(json_parser); + return -1; + } + /* We have one object with a series of string members where + * each member is named after the algorithm. + */ + JsonNode *id_node = json_parser_get_root(json_parser); + JsonObject *id_body = matrix_json_node_get_object(id_node); + guint n_keys = json_object_get_size(id_body); + *algorithms = g_new(gchar *, n_keys); + *keys = g_new(gchar *, n_keys); + JsonObjectIter iter; + const gchar *key_algo; + JsonNode *key_node; + guint i = 0; + json_object_iter_init(&iter, id_body); + while (json_object_iter_next(&iter, &key_algo, &key_node)) { + (*algorithms)[i] = g_strdup(key_algo); + (*keys)[i] = g_strdup(matrix_json_object_get_string_member(id_body, key_algo)); + i++; + } + + g_free(id_keys); + g_object_unref(json_parser); + + return n_keys; +} + #else /* ==== Stubs for when e2e is configured out of the build === */ #endif |