From e1b5eb56ae5ba138bcb0514958ac9c3f855afe0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Sun, 23 Apr 2023 11:34:03 +0200 Subject: [PATCH] secret: Store secret in MessagePack format Should be more compact and efficient --- Cargo.lock | 1 + Cargo.toml | 1 + src/secret.rs | 35 +++++++++++++++++++++++++---------- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e29d2252..f0803614 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1315,6 +1315,7 @@ dependencies = [ "qrcode", "rand 0.8.5", "regex", + "rmp-serde", "rqrr", "ruma", "secular", diff --git a/Cargo.toml b/Cargo.toml index 79e83fea..5f7107a8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -55,6 +55,7 @@ pulldown-cmark = "0.9" qrcode = "0.12" rand = "0.8" regex = "1" +rmp-serde = "1" rqrr = "0.6" secular = { version = "1", features = ["bmp", "normalization"] } serde = "1" diff --git a/src/secret.rs b/src/secret.rs index 899a59c8..1c927ecd 100644 --- a/src/secret.rs +++ b/src/secret.rs @@ -230,16 +230,31 @@ impl StoredSession { } }; let secret = match item.secret().await { - Ok(secret) => match Secret::from_utf8(&secret) { - Ok(secret) => secret, - Err(error) => { - error!("Could not parse secret in stored session: {error:?}"); - return Err(SecretError::CorruptSession { - error: gettext("Malformed secret in stored session"), - item, - }); + Ok(secret) => { + if version == 0 { + match Secret::from_utf8(&secret) { + Ok(secret) => secret, + Err(error) => { + error!("Could not parse secret in stored session: {error:?}"); + return Err(SecretError::CorruptSession { + error: gettext("Malformed secret in stored session"), + item, + }); + } + } + } else { + match rmp_serde::from_slice::(&secret) { + Ok(secret) => secret, + Err(error) => { + error!("Could not parse secret in stored session: {error}"); + return Err(SecretError::CorruptSession { + error: gettext("Malformed secret in stored session"), + item, + }); + } + } } - }, + } Err(error) => { error!("Could not get secret in stored session: {error}"); return Err(SecretError::CorruptSession { @@ -347,7 +362,7 @@ pub async fn store_session(session: &StoredSession) -> Result<(), SecretError> { let attrs = session.attributes(); let attributes = attrs.iter().map(|(k, v)| (*k, v.as_ref())).collect(); - let secret = serde_json::to_string(&session.secret).unwrap(); + let secret = rmp_serde::to_vec_named(&session.secret).unwrap(); keyring .create_item(