signatures: Merge SplitError into Error
This commit is contained in:
parent
acba6fbbc4
commit
8d15c3d0f9
@ -1,5 +1,9 @@
|
|||||||
# [unreleased]
|
# [unreleased]
|
||||||
|
|
||||||
|
Breaking changes:
|
||||||
|
|
||||||
|
* Merge `SplitError` into `Error`
|
||||||
|
|
||||||
Improvements:
|
Improvements:
|
||||||
|
|
||||||
* Move Room Version 9 keys out of `unstable-pre-spec` in `allowed_content_keys_for`
|
* Move Room Version 9 keys out of `unstable-pre-spec` in `allowed_content_keys_for`
|
||||||
|
@ -22,9 +22,17 @@ pub enum Error {
|
|||||||
#[error("DER Parse error: {0}")]
|
#[error("DER Parse error: {0}")]
|
||||||
DerParse(pkcs8::der::Error),
|
DerParse(pkcs8::der::Error),
|
||||||
|
|
||||||
/// [`SplitError`] wrapper.
|
/// The signature's ID does not have exactly two components separated by a colon.
|
||||||
#[error("Split error: {0}")]
|
#[error("malformed signature ID: expected exactly 2 segment separated by a colon, found {0}")]
|
||||||
SplitError(#[from] SplitError),
|
InvalidLength(usize),
|
||||||
|
|
||||||
|
/// The signature's ID contains invalid characters in its version.
|
||||||
|
#[error("malformed signature ID: expected version to contain only characters in the character set `[a-zA-Z0-9_]`, found `{0}`")]
|
||||||
|
InvalidVersion(String),
|
||||||
|
|
||||||
|
/// The signature uses an unsupported algorithm.
|
||||||
|
#[error("signature uses an unsupported algorithm: {0}")]
|
||||||
|
UnsupportedAlgorithm(String),
|
||||||
|
|
||||||
/// PDU was too large
|
/// PDU was too large
|
||||||
#[error("PDU is larger than maximum of 65535 bytes")]
|
#[error("PDU is larger than maximum of 65535 bytes")]
|
||||||
@ -251,20 +259,3 @@ impl ParseError {
|
|||||||
Self::Base64 { of_type: of_type.into(), string: string.into(), source }.into()
|
Self::Base64 { of_type: of_type.into(), string: string.into(), source }.into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An error when trying to extract the algorithm and version from a key identifier.
|
|
||||||
#[derive(Error, Debug)]
|
|
||||||
#[non_exhaustive]
|
|
||||||
pub enum SplitError {
|
|
||||||
/// The signature's ID does not have exactly two components separated by a colon.
|
|
||||||
#[error("malformed signature ID: expected exactly 2 segment separated by a colon, found {0}")]
|
|
||||||
InvalidLength(usize),
|
|
||||||
|
|
||||||
/// The signature's ID contains invalid characters in its version.
|
|
||||||
#[error("malformed signature ID: expected version to contain only characters in the character set `[a-zA-Z0-9_]`, found `{0}`")]
|
|
||||||
InvalidVersion(String),
|
|
||||||
|
|
||||||
/// The signature uses an unsupported algorithm.
|
|
||||||
#[error("unsupported algorithm: {0}")]
|
|
||||||
UnsupportedAlgorithm(String),
|
|
||||||
}
|
|
||||||
|
@ -46,7 +46,7 @@
|
|||||||
|
|
||||||
use ruma_serde::{AsRefStr, DisplayAsRefStr};
|
use ruma_serde::{AsRefStr, DisplayAsRefStr};
|
||||||
|
|
||||||
pub use error::{Error, JsonError, JsonType, ParseError, SplitError, VerificationError};
|
pub use error::{Error, JsonError, JsonType, ParseError, VerificationError};
|
||||||
pub use functions::{
|
pub use functions::{
|
||||||
canonical_json, content_hash, hash_and_sign_event, redact, redact_content_in_place,
|
canonical_json, content_hash, hash_and_sign_event, redact, redact_content_in_place,
|
||||||
redact_in_place, reference_hash, sign_json, verify_event, verify_json,
|
redact_in_place, reference_hash, sign_json, verify_event, verify_json,
|
||||||
@ -72,7 +72,7 @@ pub enum Algorithm {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Extract the algorithm and version from a key identifier.
|
/// Extract the algorithm and version from a key identifier.
|
||||||
fn split_id(id: &str) -> Result<(Algorithm, String), SplitError> {
|
fn split_id(id: &str) -> Result<(Algorithm, String), Error> {
|
||||||
/// The length of a valid signature ID.
|
/// The length of a valid signature ID.
|
||||||
const SIGNATURE_ID_LENGTH: usize = 2;
|
const SIGNATURE_ID_LENGTH: usize = 2;
|
||||||
|
|
||||||
@ -81,20 +81,20 @@ fn split_id(id: &str) -> Result<(Algorithm, String), SplitError> {
|
|||||||
let signature_id_length = signature_id.len();
|
let signature_id_length = signature_id.len();
|
||||||
|
|
||||||
if signature_id_length != SIGNATURE_ID_LENGTH {
|
if signature_id_length != SIGNATURE_ID_LENGTH {
|
||||||
return Err(SplitError::InvalidLength(signature_id_length));
|
return Err(Error::InvalidLength(signature_id_length));
|
||||||
}
|
}
|
||||||
|
|
||||||
let version = signature_id[1];
|
let version = signature_id[1];
|
||||||
|
|
||||||
if !version.bytes().all(|ch| ch.is_ascii_alphanumeric() || ch == b'_') {
|
if !version.bytes().all(|ch| ch.is_ascii_alphanumeric() || ch == b'_') {
|
||||||
return Err(SplitError::InvalidVersion(version.into()));
|
return Err(Error::InvalidVersion(version.into()));
|
||||||
}
|
}
|
||||||
|
|
||||||
let algorithm_input = signature_id[0];
|
let algorithm_input = signature_id[0];
|
||||||
|
|
||||||
let algorithm = match algorithm_input {
|
let algorithm = match algorithm_input {
|
||||||
"ed25519" => Algorithm::Ed25519,
|
"ed25519" => Algorithm::Ed25519,
|
||||||
algorithm => return Err(SplitError::UnsupportedAlgorithm(algorithm.into())),
|
algorithm => return Err(Error::UnsupportedAlgorithm(algorithm.into())),
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok((algorithm, signature_id[1].to_owned()))
|
Ok((algorithm, signature_id[1].to_owned()))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user