From b171f64f1d2fe30e4d9519272eac1bf2d1d881be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Wed, 26 Feb 2020 09:18:10 +0100 Subject: [PATCH] crypto: Use the serde feature of olm-rs. --- Cargo.toml | 2 +- src/crypto/machine.rs | 10 +++-- src/crypto/olm.rs | 98 ++----------------------------------------- 3 files changed, 10 insertions(+), 100 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 493595c9..45aa89a6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,7 +28,7 @@ log = "0.4.8" ruma-identifiers = "0.14.1" url = "2.1.1" -olm-rs = { git = "https://gitlab.gnome.org/jhaye/olm-rs/", optional = true} +olm-rs = { git = "https://gitlab.gnome.org/jhaye/olm-rs/", optional = true, features = ["serde"]} serde = { version = "1.0.104", optional = true, features = ["derive"] } serde_json = { version = "1.0.48", optional = true } cjson = { version = "0.1.0", optional = true } diff --git a/src/crypto/machine.rs b/src/crypto/machine.rs index fbee5e3d..447f3c99 100644 --- a/src/crypto/machine.rs +++ b/src/crypto/machine.rs @@ -105,12 +105,14 @@ impl OlmMachine { match self.uploaded_key_count { Some(count) => { let max_keys = self.account.max_one_time_keys() as u64; - let key_count = (max_keys / 2) - count; + let max_on_server = max_keys / 2; - if key_count <= 0 { + if count >= (max_on_server) { return Err(()); } + let key_count = (max_on_server) - count; + let key_count: usize = key_count .try_into() .unwrap_or_else(|_| self.account.max_one_time_keys()); @@ -153,8 +155,8 @@ impl OlmMachine { /// Convert a JSON value to the canonical representation and sign the JSON string. fn sign_json(&self, json: &Value) -> String { - let canonical_json = - cjson::to_string(json).expect(&format!("Can't serialize {} to canonical JSON", json)); + let canonical_json = cjson::to_string(json) + .unwrap_or_else(|_| panic!(format!("Can't serialize {} to canonical JSON", json))); self.account.sign(&canonical_json) } diff --git a/src/crypto/olm.rs b/src/crypto/olm.rs index e1a44b54..df3f9a42 100644 --- a/src/crypto/olm.rs +++ b/src/crypto/olm.rs @@ -12,99 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use olm_rs::account::OlmAccount; -use std::collections::{hash_map::Iter, hash_map::Keys, hash_map::Values, HashMap}; - -use serde; -use serde::Deserialize; - -/// Struct representing the parsed result of `OlmAccount::identity_keys()`. -#[derive(Deserialize, Debug, PartialEq)] -pub struct IdentityKeys { - #[serde(flatten)] - keys: HashMap, -} - -impl IdentityKeys { - /// Get the public part of the ed25519 key of the account. - pub fn ed25519(&self) -> &str { - &self.keys["ed25519"] - } - - /// Get the public part of the curve25519 key of the account. - pub fn curve25519(&self) -> &str { - &self.keys["curve25519"] - } - - /// Get a reference to the key of the given key type. - pub fn get(&self, key_type: &str) -> Option<&str> { - let ret = self.keys.get(key_type); - ret.map(|x| &**x) - } - - /// An iterator visiting all public keys of the account. - pub fn values(&self) -> Values { - self.keys.values() - } - - /// An iterator visiting all key types of the account. - pub fn keys(&self) -> Keys { - self.keys.keys() - } - - /// An iterator visiting all key-type, key pairs of the account. - pub fn iter(&self) -> Iter { - self.keys.iter() - } - - /// Returns true if the account contains a key with the given key type. - pub fn contains_key(&self, key_type: &str) -> bool { - self.keys.contains_key(key_type) - } -} - -#[derive(Deserialize, Debug, PartialEq)] -/// Struct representing the the one-time keys. -/// The keys can be accessed in a map-like fashion. -pub struct OneTimeKeys { - #[serde(flatten)] - keys: HashMap>, -} - -impl OneTimeKeys { - /// Get the HashMap containing the curve25519 one-time keys. - /// This is the same as using `get("curve25519").unwrap()` - pub fn curve25519(&self) -> &HashMap { - &self.keys["curve25519"] - } - - /// Get a reference to the hashmap corresponding to given key type. - pub fn get(&self, key_type: &str) -> Option<&HashMap> { - self.keys.get(key_type) - } - - /// An iterator visiting all one-time key hashmaps in an arbitrary order. - pub fn values(&self) -> Values> { - self.keys.values() - } - - /// An iterator visiting all one-time key types in an arbitrary order. - pub fn keys(&self) -> Keys> { - self.keys.keys() - } - - /// An iterator visiting all one-time key types and their respective - /// key hashmaps in an arbitrary order. - pub fn iter(&self) -> Iter> { - self.keys.iter() - } - - /// Returns `true` if the struct contains the given key type. - /// This does not mean that there are any keys for the given key type. - pub fn contains_key(&self, key_type: &str) -> bool { - self.keys.contains_key(key_type) - } -} +use olm_rs::account::{IdentityKeys, OlmAccount, OneTimeKeys}; pub struct Account { inner: OlmAccount, @@ -122,7 +30,7 @@ impl Account { /// Get the public parts of the identity keys for the account. pub fn identity_keys(&self) -> IdentityKeys { - serde_json::from_str(&self.inner.identity_keys()).expect("Can't parse the identity keys") + self.inner.parsed_identity_keys() } /// Has the account been shared with the server. @@ -134,7 +42,7 @@ impl Account { /// /// This can be empty, keys need to be generated first. pub fn one_time_keys(&self) -> OneTimeKeys { - serde_json::from_str(&self.inner.one_time_keys()).expect("Can't parse the one-time keys") + self.inner.parsed_one_time_keys() } /// Generate count number of one-time keys.