From a238a0dda5b06fad146f8f01d690cbe011d13245 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20K=C3=B6sters?= Date: Fri, 21 May 2021 12:37:41 +0200 Subject: [PATCH] federation-api: Improve get_keys --- crates/ruma-client-api/src/r0/keys.rs | 90 +------------------ .../ruma-client-api/src/r0/keys/claim_keys.rs | 3 +- .../ruma-client-api/src/r0/keys/get_keys.rs | 2 +- .../src/r0/keys/upload_keys.rs | 4 +- .../src/r0/keys/upload_signing_keys.rs | 2 +- crates/ruma-common/src/encryption.rs | 82 +++++++++++++++++ crates/ruma-federation-api/CHANGELOG.md | 4 + .../src/keys/get_keys/v1.rs | 17 +++- 8 files changed, 110 insertions(+), 94 deletions(-) diff --git a/crates/ruma-client-api/src/r0/keys.rs b/crates/ruma-client-api/src/r0/keys.rs index da548af4..8aafd3a2 100644 --- a/crates/ruma-client-api/src/r0/keys.rs +++ b/crates/ruma-client-api/src/r0/keys.rs @@ -1,9 +1,9 @@ //! Endpoints for key management -use std::{collections::BTreeMap, fmt::Debug}; - -use ruma_identifiers::{DeviceKeyId, UserId}; -use serde::{Deserialize, Serialize}; +pub use ruma_common::encryption::{ + CrossSigningKey, CrossSigningKeySignatures, KeyUsage, OneTimeKey, SignedKey, + SignedKeySignatures, +}; pub mod claim_keys; pub mod get_key_changes; @@ -16,85 +16,3 @@ pub mod upload_signatures; #[cfg(feature = "unstable-pre-spec")] #[cfg_attr(docsrs, doc(cfg(feature = "unstable-pre-spec")))] pub mod upload_signing_keys; - -/// Signatures for a `SignedKey` object. -pub type SignedKeySignatures = BTreeMap>; - -/// A key for the SignedCurve25519 algorithm -#[derive(Debug, Clone, Serialize, Deserialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -pub struct SignedKey { - /// Base64-encoded 32-byte Curve25519 public key. - pub key: String, - - /// Signatures for the key object. - pub signatures: SignedKeySignatures, -} - -impl SignedKey { - /// Creates a new `SignedKey` with the given key and signatures. - pub fn new(key: String, signatures: SignedKeySignatures) -> Self { - Self { key, signatures } - } -} - -/// A one-time public key for "pre-key" messages. -#[derive(Debug, Clone, Serialize, Deserialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -#[serde(untagged)] -pub enum OneTimeKey { - /// A key containing signatures, for the SignedCurve25519 algorithm. - SignedKey(SignedKey), - - /// A string-valued key, for the Ed25519 and Curve25519 algorithms. - Key(String), -} - -/// Signatures for a `CrossSigningKey` object. -pub type CrossSigningKeySignatures = BTreeMap>; - -/// A cross signing key. -#[derive(Clone, Debug, Deserialize, Serialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -pub struct CrossSigningKey { - /// The ID of the user the key belongs to. - pub user_id: UserId, - - /// What the key is used for. - pub usage: Vec, - - /// The public key. The object must have exactly one property. - pub keys: BTreeMap, - - /// Signatures of the key. Only optional for master key. - #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] - pub signatures: CrossSigningKeySignatures, -} - -impl CrossSigningKey { - /// Creates a new `CrossSigningKey` with the given user ID, usage, keys and - /// signatures. - pub fn new( - user_id: UserId, - usage: Vec, - keys: BTreeMap, - signatures: CrossSigningKeySignatures, - ) -> Self { - Self { user_id, usage, keys, signatures } - } -} - -/// The usage of a cross signing key. -#[derive(Clone, Debug, Deserialize, Serialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -#[serde(rename_all = "snake_case")] -pub enum KeyUsage { - /// Master key. - Master, - - /// Self-signing key. - SelfSigning, - - /// User-signing key. - UserSigning, -} diff --git a/crates/ruma-client-api/src/r0/keys/claim_keys.rs b/crates/ruma-client-api/src/r0/keys/claim_keys.rs index 7f14fc1c..14a88032 100644 --- a/crates/ruma-client-api/src/r0/keys/claim_keys.rs +++ b/crates/ruma-client-api/src/r0/keys/claim_keys.rs @@ -3,11 +3,10 @@ use std::{collections::BTreeMap, time::Duration}; use ruma_api::ruma_api; +use ruma_common::encryption::OneTimeKey; use ruma_identifiers::{DeviceIdBox, DeviceKeyAlgorithm, DeviceKeyId, UserId}; use serde_json::Value as JsonValue; -use super::OneTimeKey; - ruma_api! { metadata: { description: "Claims one-time keys for use in pre-key messages.", diff --git a/crates/ruma-client-api/src/r0/keys/get_keys.rs b/crates/ruma-client-api/src/r0/keys/get_keys.rs index f35b6b63..ff11be13 100644 --- a/crates/ruma-client-api/src/r0/keys/get_keys.rs +++ b/crates/ruma-client-api/src/r0/keys/get_keys.rs @@ -8,7 +8,7 @@ use ruma_identifiers::{DeviceIdBox, UserId}; use serde_json::Value as JsonValue; #[cfg(feature = "unstable-pre-spec")] -use super::CrossSigningKey; +use ruma_common::encryption::CrossSigningKey; ruma_api! { metadata: { diff --git a/crates/ruma-client-api/src/r0/keys/upload_keys.rs b/crates/ruma-client-api/src/r0/keys/upload_keys.rs index 49cfc13c..b5e313b2 100644 --- a/crates/ruma-client-api/src/r0/keys/upload_keys.rs +++ b/crates/ruma-client-api/src/r0/keys/upload_keys.rs @@ -4,11 +4,9 @@ use std::collections::BTreeMap; use js_int::UInt; use ruma_api::ruma_api; -use ruma_common::encryption::DeviceKeys; +use ruma_common::encryption::{DeviceKeys, OneTimeKey}; use ruma_identifiers::{DeviceKeyAlgorithm, DeviceKeyId}; -use super::OneTimeKey; - ruma_api! { metadata: { description: "Publishes end-to-end encryption keys for the device.", diff --git a/crates/ruma-client-api/src/r0/keys/upload_signing_keys.rs b/crates/ruma-client-api/src/r0/keys/upload_signing_keys.rs index e46c7dac..69e42544 100644 --- a/crates/ruma-client-api/src/r0/keys/upload_signing_keys.rs +++ b/crates/ruma-client-api/src/r0/keys/upload_signing_keys.rs @@ -3,8 +3,8 @@ //! Defined in [MSC 1756](https://github.com/matrix-org/matrix-doc/blob/master/proposals/1756-cross-signing.md#uploading-signing-keys) use ruma_api::ruma_api; +use ruma_common::encryption::CrossSigningKey; -use super::CrossSigningKey; use crate::r0::uiaa::{AuthData, IncomingAuthData, UiaaResponse}; ruma_api! { diff --git a/crates/ruma-common/src/encryption.rs b/crates/ruma-common/src/encryption.rs index c20ef307..0a1a39d6 100644 --- a/crates/ruma-common/src/encryption.rs +++ b/crates/ruma-common/src/encryption.rs @@ -66,3 +66,85 @@ impl UnsignedDeviceInfo { self.device_display_name.is_none() } } + +/// Signatures for a `SignedKey` object. +pub type SignedKeySignatures = BTreeMap>; + +/// A key for the SignedCurve25519 algorithm +#[derive(Debug, Clone, Serialize, Deserialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] +pub struct SignedKey { + /// Base64-encoded 32-byte Curve25519 public key. + pub key: String, + + /// Signatures for the key object. + pub signatures: SignedKeySignatures, +} + +impl SignedKey { + /// Creates a new `SignedKey` with the given key and signatures. + pub fn new(key: String, signatures: SignedKeySignatures) -> Self { + Self { key, signatures } + } +} + +/// A one-time public key for "pre-key" messages. +#[derive(Debug, Clone, Serialize, Deserialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] +#[serde(untagged)] +pub enum OneTimeKey { + /// A key containing signatures, for the SignedCurve25519 algorithm. + SignedKey(SignedKey), + + /// A string-valued key, for the Ed25519 and Curve25519 algorithms. + Key(String), +} + +/// Signatures for a `CrossSigningKey` object. +pub type CrossSigningKeySignatures = BTreeMap>; + +/// A cross signing key. +#[derive(Clone, Debug, Deserialize, Serialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] +pub struct CrossSigningKey { + /// The ID of the user the key belongs to. + pub user_id: UserId, + + /// What the key is used for. + pub usage: Vec, + + /// The public key. The object must have exactly one property. + pub keys: BTreeMap, + + /// Signatures of the key. Only optional for master key. + #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] + pub signatures: CrossSigningKeySignatures, +} + +impl CrossSigningKey { + /// Creates a new `CrossSigningKey` with the given user ID, usage, keys and + /// signatures. + pub fn new( + user_id: UserId, + usage: Vec, + keys: BTreeMap, + signatures: CrossSigningKeySignatures, + ) -> Self { + Self { user_id, usage, keys, signatures } + } +} + +/// The usage of a cross signing key. +#[derive(Clone, Debug, Deserialize, Serialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] +#[serde(rename_all = "snake_case")] +pub enum KeyUsage { + /// Master key. + Master, + + /// Self-signing key. + SelfSigning, + + /// User-signing key. + UserSigning, +} diff --git a/crates/ruma-federation-api/CHANGELOG.md b/crates/ruma-federation-api/CHANGELOG.md index 27ab9638..aeb29f24 100644 --- a/crates/ruma-federation-api/CHANGELOG.md +++ b/crates/ruma-federation-api/CHANGELOG.md @@ -1,5 +1,9 @@ # [unreleased] +Improvements: + +* Add master_keys and self_signing keys to keys::get_keys::v1 response + # 0.1.0 Breaking Changes: diff --git a/crates/ruma-federation-api/src/keys/get_keys/v1.rs b/crates/ruma-federation-api/src/keys/get_keys/v1.rs index 25c08c40..981afb4e 100644 --- a/crates/ruma-federation-api/src/keys/get_keys/v1.rs +++ b/crates/ruma-federation-api/src/keys/get_keys/v1.rs @@ -3,6 +3,8 @@ use std::collections::BTreeMap; use ruma_api::ruma_api; +#[cfg(feature = "unstable-pre-spec")] +use ruma_common::encryption::CrossSigningKey; use ruma_common::encryption::DeviceKeys; use ruma_identifiers::{DeviceIdBox, UserId}; @@ -22,9 +24,22 @@ ruma_api! { pub device_keys: BTreeMap>, } + #[derive(Default)] response: { /// Keys from the queried devices. pub device_keys: BTreeMap>, + + /// Information on the master cross-signing keys of the queried users. + #[cfg(feature = "unstable-pre-spec")] + #[cfg_attr(docsrs, doc(cfg(feature = "unstable-pre-spec")))] + #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] + pub master_keys: BTreeMap, + + /// Information on the self-signing keys of the queried users. + #[cfg(feature = "unstable-pre-spec")] + #[cfg_attr(docsrs, doc(cfg(feature = "unstable-pre-spec")))] + #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] + pub self_signing_keys: BTreeMap, } } @@ -38,6 +53,6 @@ impl Request { impl Response { /// Creates a new `Response` with the given device keys. pub fn new(device_keys: BTreeMap>) -> Self { - Self { device_keys } + Self { device_keys, ..Default::default() } } }