aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDr. David Alan Gilbert <dave@treblig.org>2017-02-05 18:58:02 +0000
committerDr. David Alan Gilbert <dave@treblig.org>2018-02-25 02:08:49 +0000
commit3b8e5996977b33a3523a6569648e74deb378f5bc (patch)
tree370fde33a9eb34fa0b3962828ac666d1f3df9876
parent22274a84c0255cc39134bd06896d3518a66a38d4 (diff)
downloadpurple-matrix-3b8e5996977b33a3523a6569648e74deb378f5bc.tar.gz
e2e: Add matrix_sign_json
Given a JsonObject, produce a canonical json blob, sign it and add the signature back into the JsonObject. Signed-off-by: Dr. David Alan Gilbert <dave@treblig.org>
-rw-r--r--matrix-e2e.c46
-rw-r--r--matrix-e2e.h2
2 files changed, 48 insertions, 0 deletions
diff --git a/matrix-e2e.c b/matrix-e2e.c
index ec28820..71c1426 100644
--- a/matrix-e2e.c
+++ b/matrix-e2e.c
@@ -17,8 +17,10 @@
*/
#include <stdio.h>
+#include <string.h>
#include "libmatrix.h"
#include "matrix-e2e.h"
+#include "matrix-json.h"
#include "connection.h"
#ifndef MATRIX_NO_E2E
@@ -29,6 +31,50 @@ struct _MatrixE2EData {
gchar *device_id;
};
+/* Sign the JsonObject with olm_account_sign and add it to the object
+ * as a 'signatures' member of the top level object.
+ * 0 on success
+ */
+int matrix_sign_json(MatrixConnectionData *conn, JsonObject *tosign)
+{
+ int ret = -1;
+ OlmAccount *account = conn->e2e->oa;
+ const gchar *device_id = conn->e2e->device_id;
+ PurpleConnection *pc = conn->pc;
+ GString *can_json = matrix_canonical_json(tosign);
+ gchar *can_json_c = g_string_free(can_json, FALSE);
+ size_t sig_length = olm_account_signature_length(account);
+ gchar *sig = g_malloc0(sig_length+1);
+ if (olm_account_sign(account, can_json_c, strlen(can_json_c),
+ sig, sig_length)==olm_error()) {
+ purple_connection_error_reason(pc,
+ PURPLE_CONNECTION_ERROR_OTHER_ERROR,
+ olm_account_last_error(account));
+ goto out;
+ }
+
+ /* We need to add a "signatures" member which is an object, with
+ * a "user_id" member that is itself an object which has an "ed25519:$DEVICEID" member
+ * that is the signature.
+ */
+ GString *alg_dev = g_string_new(NULL);
+ g_string_printf(alg_dev, "ed25519:%s", device_id);
+ gchar *alg_dev_c = g_string_free(alg_dev, FALSE);
+ JsonObject *sig_dev = json_object_new();
+ json_object_set_string_member(sig_dev, alg_dev_c, sig);
+ JsonObject *sig_obj = json_object_new();
+ json_object_set_object_member(sig_obj, conn->user_id, sig_dev);
+ json_object_set_object_member(tosign, "signatures", sig_obj);
+
+ g_free(alg_dev_c);
+ ret = 0;
+out:
+ g_free(can_json_c);
+ g_free(sig);
+
+ return ret;
+}
+
#else
/* ==== Stubs for when e2e is configured out of the build === */
#endif
diff --git a/matrix-e2e.h b/matrix-e2e.h
index a2d8f9b..1ac190a 100644
--- a/matrix-e2e.h
+++ b/matrix-e2e.h
@@ -19,6 +19,8 @@
#ifndef MATRIX_E2E_H
#define MATRIX_E2E_H
+#include "matrix-connection.h"
+
typedef struct _MatrixE2EData MatrixE2EData;
#endif