crypto: Use the serde feature of olm-rs.

This commit is contained in:
Damir Jelić 2020-02-26 09:18:10 +01:00
parent 7605f7e6e8
commit b171f64f1d
3 changed files with 10 additions and 100 deletions

View file

@ -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 }

View file

@ -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)
}

View file

@ -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<String, String>,
}
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<String, String> {
self.keys.values()
}
/// An iterator visiting all key types of the account.
pub fn keys(&self) -> Keys<String, String> {
self.keys.keys()
}
/// An iterator visiting all key-type, key pairs of the account.
pub fn iter(&self) -> Iter<String, String> {
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<String, HashMap<String, String>>,
}
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<String, String> {
&self.keys["curve25519"]
}
/// Get a reference to the hashmap corresponding to given key type.
pub fn get(&self, key_type: &str) -> Option<&HashMap<String, String>> {
self.keys.get(key_type)
}
/// An iterator visiting all one-time key hashmaps in an arbitrary order.
pub fn values(&self) -> Values<String, HashMap<String, String>> {
self.keys.values()
}
/// An iterator visiting all one-time key types in an arbitrary order.
pub fn keys(&self) -> Keys<String, HashMap<String, String>> {
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<String, HashMap<String, String>> {
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.