aboutsummaryrefslogtreecommitdiffstats
path: root/matrix-e2e.c
diff options
context:
space:
mode:
authorDr. David Alan Gilbert <dave@treblig.org>2017-04-10 18:05:41 +0100
committerDr. David Alan Gilbert <dave@treblig.org>2018-02-25 02:08:49 +0000
commit21605558fa314518e6aae6f20f6728a746cfe320 (patch)
treef95aa257ad9fc8545afb1923a6aded84c8533c34 /matrix-e2e.c
parentf76e7f4acb289fe29e64b10f6d2e6679b7aa5316 (diff)
downloadpurple-matrix-21605558fa314518e6aae6f20f6728a746cfe320.tar.gz
e2e: Update sessions
Update sessions stored in the db after starting sessions. Signed-off-by: Dr. David Alan Gilbert <dave@treblig.org>
Diffstat (limited to 'matrix-e2e.c')
-rw-r--r--matrix-e2e.c71
1 files changed, 68 insertions, 3 deletions
diff --git a/matrix-e2e.c b/matrix-e2e.c
index 2782b93..a33176c 100644
--- a/matrix-e2e.c
+++ b/matrix-e2e.c
@@ -494,6 +494,72 @@ err:
return NULL;
}
+/* Update an existing OLM session in the database */
+static int update_olm_session(MatrixConnectionData *conn,
+ MatrixOlmSession *mos)
+{
+ size_t pickle_len = olm_pickle_session_length(mos->session);
+ gchar *pickle = g_malloc(pickle_len+1);
+ sqlite3_stmt *dbstmt = NULL;
+ int ret = -1;
+
+ pickle_len = olm_pickle_session(mos->session, "!", 1, pickle, pickle_len);
+ if (pickle_len == olm_error()) {
+ purple_debug_warning("matrixprpl",
+ "%s: Failed to pickle session for %s/%s: %s\n",
+ __func__, mos->sender_id, mos->sender_key,
+ olm_session_last_error(mos->session));
+ goto err;
+ }
+ pickle[pickle_len] = '\0';
+
+ const char *query ="UPDATE olmsessions SET session_pickle=? "
+ "WHERE sender_name = ? AND sender_key = ? AND "
+ "ROWID = ?";
+ ret = sqlite3_prepare_v2(conn->e2e->db, query, -1, &dbstmt, NULL);
+ if (ret != SQLITE_OK || !dbstmt) {
+ purple_debug_warning("matrixprpl",
+ "%s: Failed to prep update %d '%s'\n",
+ __func__, ret, query);
+ ret = -1;
+ goto err;
+ }
+ ret = sqlite3_bind_text(dbstmt, 1, pickle, -1, NULL);
+ if (ret == SQLITE_OK) {
+ ret = sqlite3_bind_text(dbstmt, 2, mos->sender_id, -1, NULL);
+ }
+ if (ret == SQLITE_OK) {
+ ret = sqlite3_bind_text(dbstmt, 3, mos->sender_key, -1, NULL);
+ }
+ if (ret == SQLITE_OK) {
+ ret = sqlite3_bind_int64(dbstmt, 4, mos->unique);
+ }
+
+ if (ret != SQLITE_OK) {
+ purple_debug_warning("matrixprpl",
+ "%s: Failed to bind %d\n", __func__, ret);
+ ret = -1;
+ goto err;
+ }
+
+ ret = sqlite3_step(dbstmt);
+ if (ret != SQLITE_DONE) {
+ purple_debug_warning("matrixprpl",
+ "%s: Update failed %d (%s)\n", __func__,
+ ret, query);
+ ret = -1;
+ goto err;
+
+ }
+ ret = 0;
+
+err:
+ sqlite3_finalize(dbstmt);
+ g_free(pickle);
+
+ return ret;
+}
+
/* 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
@@ -1408,20 +1474,19 @@ static void decrypt_olm(PurpleConnection *pc, MatrixConnectionData *conn, JsonOb
__func__, olm_session_last_error(session));
goto err;
}
+ update_olm_session(conn, mos);
plaintext[pt_len] = '\0';
handle_decrypted_olm(pc, conn, cevent_sender, sender_key, plaintext);
- // TODO: Store session in db
} else {
purple_debug_info("matrixprpl", "%s: Type %zd olm\n", __func__, type);
}
- // TODO resave account? Or just session?
- //
if (plaintext) {
clear_mem(plaintext, max_plaintext_len);
}
g_free(plaintext);
g_free(cevent_body_copy);
+ matrix_store_e2e_account(conn);
return;
err: