identifiers: Stop rejecting unknown crypto algorithms
This commit is contained in:
parent
4f11a5eb38
commit
63678df887
@ -17,7 +17,6 @@ default = ["serde"]
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
serde = { version = "1.0.114", optional = true, features = ["derive"] }
|
serde = { version = "1.0.114", optional = true, features = ["derive"] }
|
||||||
strum = { version = "0.19.2", features = ["derive"] }
|
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
ruma-serde = { version = "0.2.3", path = "../ruma-serde" }
|
ruma-serde = { version = "0.2.3", path = "../ruma-serde" }
|
||||||
|
@ -1,13 +1,10 @@
|
|||||||
use std::{num::NonZeroU8, str::FromStr};
|
use std::num::NonZeroU8;
|
||||||
|
|
||||||
use crate::{crypto_algorithms::DeviceKeyAlgorithm, Error};
|
use crate::Error;
|
||||||
|
|
||||||
pub fn validate(s: &str) -> Result<NonZeroU8, Error> {
|
pub fn validate(s: &str) -> Result<NonZeroU8, Error> {
|
||||||
let colon_idx = NonZeroU8::new(s.find(':').ok_or(Error::MissingDelimiter)? as u8)
|
let colon_idx = NonZeroU8::new(s.find(':').ok_or(Error::MissingDelimiter)? as u8)
|
||||||
.ok_or(Error::UnknownKeyAlgorithm)?;
|
.ok_or(Error::InvalidKeyAlgorithm)?;
|
||||||
|
|
||||||
DeviceKeyAlgorithm::from_str(&s[0..colon_idx.get() as usize])
|
|
||||||
.map_err(|_| Error::UnknownKeyAlgorithm)?;
|
|
||||||
|
|
||||||
Ok(colon_idx)
|
Ok(colon_idx)
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,9 @@ pub enum Error {
|
|||||||
/// Only relevant for user IDs.
|
/// Only relevant for user IDs.
|
||||||
InvalidCharacters,
|
InvalidCharacters,
|
||||||
|
|
||||||
|
/// The key algorithm is invalid (e.g. empty).
|
||||||
|
InvalidKeyAlgorithm,
|
||||||
|
|
||||||
/// The key version contains outside of [a-zA-Z0-9_].
|
/// The key version contains outside of [a-zA-Z0-9_].
|
||||||
InvalidKeyVersion,
|
InvalidKeyVersion,
|
||||||
|
|
||||||
@ -28,9 +31,6 @@ pub enum Error {
|
|||||||
|
|
||||||
/// The ID is missing the correct leading sigil.
|
/// The ID is missing the correct leading sigil.
|
||||||
MissingLeadingSigil,
|
MissingLeadingSigil,
|
||||||
|
|
||||||
/// The key algorithm is not recognized.
|
|
||||||
UnknownKeyAlgorithm,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for Error {
|
impl Display for Error {
|
||||||
@ -38,12 +38,12 @@ impl Display for Error {
|
|||||||
let message = match self {
|
let message = match self {
|
||||||
Error::EmptyRoomVersionId => "room version ID is empty",
|
Error::EmptyRoomVersionId => "room version ID is empty",
|
||||||
Error::InvalidCharacters => "localpart contains invalid characters",
|
Error::InvalidCharacters => "localpart contains invalid characters",
|
||||||
|
Error::InvalidKeyAlgorithm => "unknown key algorithm specified",
|
||||||
Error::InvalidKeyVersion => "key ID version contains invalid characters",
|
Error::InvalidKeyVersion => "key ID version contains invalid characters",
|
||||||
Error::InvalidServerName => "server name is not a valid IP address or domain name",
|
Error::InvalidServerName => "server name is not a valid IP address or domain name",
|
||||||
Error::MaximumLengthExceeded => "ID exceeds 255 bytes",
|
Error::MaximumLengthExceeded => "ID exceeds 255 bytes",
|
||||||
Error::MissingDelimiter => "required colon is missing",
|
Error::MissingDelimiter => "required colon is missing",
|
||||||
Error::MissingLeadingSigil => "leading sigil is incorrect or missing",
|
Error::MissingLeadingSigil => "leading sigil is incorrect or missing",
|
||||||
Error::UnknownKeyAlgorithm => "unknown key algorithm specified",
|
|
||||||
};
|
};
|
||||||
|
|
||||||
write!(f, "{}", message)
|
write!(f, "{}", message)
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
pub mod crypto_algorithms;
|
|
||||||
pub mod device_key_id;
|
pub mod device_key_id;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
pub mod event_id;
|
pub mod event_id;
|
||||||
|
@ -1,12 +1,11 @@
|
|||||||
use std::{num::NonZeroU8, str::FromStr};
|
use std::num::NonZeroU8;
|
||||||
|
|
||||||
use crate::{crypto_algorithms::SigningKeyAlgorithm, Error};
|
use crate::Error;
|
||||||
|
|
||||||
pub fn validate(s: &str) -> Result<NonZeroU8, Error> {
|
pub fn validate(s: &str) -> Result<NonZeroU8, Error> {
|
||||||
let colon_idx = NonZeroU8::new(s.find(':').ok_or(Error::MissingDelimiter)? as u8)
|
let colon_idx = NonZeroU8::new(s.find(':').ok_or(Error::MissingDelimiter)? as u8)
|
||||||
.ok_or(Error::UnknownKeyAlgorithm)?;
|
.ok_or(Error::InvalidKeyAlgorithm)?;
|
||||||
|
|
||||||
validate_signing_key_algorithm(&s[..colon_idx.get() as usize])?;
|
|
||||||
validate_version(&s[colon_idx.get() as usize + 1..])?;
|
validate_version(&s[colon_idx.get() as usize + 1..])?;
|
||||||
|
|
||||||
Ok(colon_idx)
|
Ok(colon_idx)
|
||||||
@ -21,10 +20,3 @@ fn validate_version(version: &str) -> Result<(), Error> {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn validate_signing_key_algorithm(algorithm: &str) -> Result<(), Error> {
|
|
||||||
match SigningKeyAlgorithm::from_str(algorithm) {
|
|
||||||
Ok(_) => Ok(()),
|
|
||||||
Err(_) => Err(Error::UnknownKeyAlgorithm),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -31,6 +31,7 @@ ruma-identifiers-validation = { version = "0.1.1", path = "../ruma-identifiers-v
|
|||||||
ruma-serde = { version = "0.2.3", path = "../ruma-serde" }
|
ruma-serde = { version = "0.2.3", path = "../ruma-serde" }
|
||||||
# Renamed so we can have a serde feature.
|
# Renamed so we can have a serde feature.
|
||||||
serde1 = { package = "serde", version = "1.0.114", optional = true, features = ["derive"] }
|
serde1 = { package = "serde", version = "1.0.114", optional = true, features = ["derive"] }
|
||||||
|
strum = { version = "0.19.2", features = ["derive"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
matches = "0.1.8"
|
matches = "0.1.8"
|
||||||
|
@ -12,7 +12,11 @@ use strum::{AsRefStr, Display, EnumString};
|
|||||||
|
|
||||||
/// The basic key algorithms in the specification.
|
/// The basic key algorithms in the specification.
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, AsRefStr, Display, EnumString)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, AsRefStr, Display, EnumString)]
|
||||||
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize), serde(rename_all = "snake_case"))]
|
#[cfg_attr(
|
||||||
|
feature = "serde",
|
||||||
|
derive(Deserialize, Serialize),
|
||||||
|
serde(rename_all = "snake_case", crate = "serde")
|
||||||
|
)]
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
#[strum(serialize_all = "snake_case")]
|
#[strum(serialize_all = "snake_case")]
|
||||||
pub enum DeviceKeyAlgorithm {
|
pub enum DeviceKeyAlgorithm {
|
||||||
@ -44,7 +48,11 @@ impl TryFrom<String> for DeviceKeyAlgorithm {
|
|||||||
|
|
||||||
/// The signing key algorithms defined in the Matrix spec.
|
/// The signing key algorithms defined in the Matrix spec.
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, AsRefStr, Display, EnumString)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, AsRefStr, Display, EnumString)]
|
||||||
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize), serde(rename_all = "snake_case"))]
|
#[cfg_attr(
|
||||||
|
feature = "serde",
|
||||||
|
derive(Deserialize, Serialize),
|
||||||
|
serde(rename_all = "snake_case", crate = "serde")
|
||||||
|
)]
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
#[strum(serialize_all = "snake_case")]
|
#[strum(serialize_all = "snake_case")]
|
||||||
pub enum SigningKeyAlgorithm {
|
pub enum SigningKeyAlgorithm {
|
||||||
@ -77,7 +85,7 @@ impl TryFrom<String> for SigningKeyAlgorithm {
|
|||||||
#[cfg_attr(
|
#[cfg_attr(
|
||||||
feature = "serde",
|
feature = "serde",
|
||||||
derive(Deserialize, Serialize),
|
derive(Deserialize, Serialize),
|
||||||
serde(from = "String", into = "String")
|
serde(from = "String", into = "String", crate = "serde")
|
||||||
)]
|
)]
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
pub enum EventEncryptionAlgorithm {
|
pub enum EventEncryptionAlgorithm {
|
@ -2,9 +2,7 @@
|
|||||||
|
|
||||||
use std::{convert::TryInto, num::NonZeroU8, str::FromStr};
|
use std::{convert::TryInto, num::NonZeroU8, str::FromStr};
|
||||||
|
|
||||||
use ruma_identifiers_validation::{crypto_algorithms::DeviceKeyAlgorithm, Error};
|
use crate::{crypto_algorithms::DeviceKeyAlgorithm, DeviceId, Error};
|
||||||
|
|
||||||
use crate::DeviceId;
|
|
||||||
|
|
||||||
/// A key algorithm and a device id, combined with a ':'
|
/// A key algorithm and a device id, combined with a ':'
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
@ -56,11 +54,11 @@ common_impls!(DeviceKeyId, try_from, "Device key ID with algorithm and device ID
|
|||||||
mod test {
|
mod test {
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
use ruma_identifiers_validation::{crypto_algorithms::DeviceKeyAlgorithm, Error};
|
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
|
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
|
||||||
|
|
||||||
use super::DeviceKeyId;
|
use super::DeviceKeyId;
|
||||||
|
use crate::{crypto_algorithms::DeviceKeyAlgorithm, Error};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn convert_device_key_id() {
|
fn convert_device_key_id() {
|
||||||
@ -93,7 +91,7 @@ mod test {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn missing_key_algorithm() {
|
fn missing_key_algorithm() {
|
||||||
assert_eq!(DeviceKeyId::try_from(":JLAFKJWSCS").unwrap_err(), Error::UnknownKeyAlgorithm);
|
assert_eq!(DeviceKeyId::try_from(":JLAFKJWSCS").unwrap_err(), Error::InvalidKeyAlgorithm);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -104,14 +102,6 @@ mod test {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn unknown_key_algorithm() {
|
|
||||||
assert_eq!(
|
|
||||||
DeviceKeyId::try_from("signed_curve25510:JLAFKJWSCS").unwrap_err(),
|
|
||||||
Error::UnknownKeyAlgorithm,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn empty_device_id_ok() {
|
fn empty_device_id_ok() {
|
||||||
assert!(DeviceKeyId::try_from("ed25519:").is_ok());
|
assert!(DeviceKeyId::try_from("ed25519:").is_ok());
|
||||||
|
@ -21,6 +21,7 @@ use serde::de::{self, Deserializer, Unexpected};
|
|||||||
|
|
||||||
#[doc(inline)]
|
#[doc(inline)]
|
||||||
pub use crate::{
|
pub use crate::{
|
||||||
|
crypto_algorithms::{DeviceKeyAlgorithm, EventEncryptionAlgorithm, SigningKeyAlgorithm},
|
||||||
device_id::{DeviceId, DeviceIdBox},
|
device_id::{DeviceId, DeviceIdBox},
|
||||||
device_key_id::DeviceKeyId,
|
device_key_id::DeviceKeyId,
|
||||||
event_id::EventId,
|
event_id::EventId,
|
||||||
@ -33,10 +34,7 @@ pub use crate::{
|
|||||||
user_id::UserId,
|
user_id::UserId,
|
||||||
};
|
};
|
||||||
#[doc(inline)]
|
#[doc(inline)]
|
||||||
pub use ruma_identifiers_validation::{
|
pub use ruma_identifiers_validation::error::Error;
|
||||||
crypto_algorithms::{DeviceKeyAlgorithm, EventEncryptionAlgorithm, SigningKeyAlgorithm},
|
|
||||||
error::Error,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod macros;
|
mod macros;
|
||||||
@ -44,6 +42,7 @@ mod macros;
|
|||||||
pub mod device_id;
|
pub mod device_id;
|
||||||
pub mod user_id;
|
pub mod user_id;
|
||||||
|
|
||||||
|
mod crypto_algorithms;
|
||||||
mod device_key_id;
|
mod device_key_id;
|
||||||
mod event_id;
|
mod event_id;
|
||||||
mod room_alias_id;
|
mod room_alias_id;
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
use std::{convert::TryInto, num::NonZeroU8, str::FromStr};
|
use std::{convert::TryInto, num::NonZeroU8, str::FromStr};
|
||||||
|
|
||||||
use ruma_identifiers_validation::{crypto_algorithms::SigningKeyAlgorithm, Error};
|
use crate::{crypto_algorithms::SigningKeyAlgorithm, Error};
|
||||||
|
|
||||||
/// Key identifiers used for homeserver signing keys.
|
/// Key identifiers used for homeserver signing keys.
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
@ -56,10 +56,9 @@ mod tests {
|
|||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
|
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
|
||||||
|
|
||||||
use crate::{Error, ServerSigningKeyId};
|
|
||||||
|
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
use ruma_identifiers_validation::crypto_algorithms::SigningKeyAlgorithm;
|
use crate::crypto_algorithms::SigningKeyAlgorithm;
|
||||||
|
use crate::{Error, ServerSigningKeyId};
|
||||||
|
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
#[test]
|
#[test]
|
||||||
@ -87,10 +86,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn invalid_key_algorithm() {
|
fn invalid_key_algorithm() {
|
||||||
assert_eq!(
|
assert_eq!(ServerSigningKeyId::try_from(":Abc-1").unwrap_err(), Error::InvalidKeyAlgorithm,);
|
||||||
ServerSigningKeyId::try_from("signed_curve25519:Abc-1").unwrap_err(),
|
|
||||||
Error::UnknownKeyAlgorithm,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user