diff --git a/sasl/Cargo.toml b/sasl/Cargo.toml index f3b63493..c6572471 100644 --- a/sasl/Cargo.toml +++ b/sasl/Cargo.toml @@ -15,8 +15,9 @@ edition = "2018" gitlab = { repository = "lumi/sasl-rs" } [features] -default = ["scram"] +default = ["scram", "anonymous"] scram = ["base64", "getrandom", "sha-1", "sha2", "hmac", "pbkdf2"] +anonymous = ["getrandom"] [dependencies] base64 = { version = "0.13", optional = true } diff --git a/sasl/src/client/mod.rs b/sasl/src/client/mod.rs index 8bf078f6..8acca9ff 100644 --- a/sasl/src/client/mod.rs +++ b/sasl/src/client/mod.rs @@ -1,8 +1,12 @@ -use crate::common::scram::DeriveError; -use crate::common::Credentials; -use hmac::digest::InvalidLength; use std::fmt; +use crate::common::Credentials; + +#[cfg(feature = "scram")] +use crate::common::scram::DeriveError; +#[cfg(feature = "scram")] +use hmac::digest::InvalidLength; + #[derive(Debug, PartialEq)] pub enum MechanismError { AnonymousRequiresNoCredentials, @@ -18,7 +22,9 @@ pub enum MechanismError { NoServerNonce, NoServerSalt, NoServerIterations, + #[cfg(feature = "scram")] DeriveError(DeriveError), + #[cfg(feature = "scram")] InvalidKeyLength(InvalidLength), InvalidState, @@ -27,12 +33,14 @@ pub enum MechanismError { NoSignatureInSuccessResponse, } +#[cfg(feature = "scram")] impl From for MechanismError { fn from(err: DeriveError) -> MechanismError { MechanismError::DeriveError(err) } } +#[cfg(feature = "scram")] impl From for MechanismError { fn from(err: InvalidLength) -> MechanismError { MechanismError::InvalidKeyLength(err) @@ -60,7 +68,9 @@ impl fmt::Display for MechanismError { MechanismError::NoServerNonce => "no server nonce", MechanismError::NoServerSalt => "no server salt", MechanismError::NoServerIterations => "no server iterations", + #[cfg(feature = "scram")] MechanismError::DeriveError(err) => return write!(fmt, "derive error: {}", err), + #[cfg(feature = "scram")] MechanismError::InvalidKeyLength(err) => return write!(fmt, "invalid key length: {}", err), MechanismError::InvalidState => "not in the right state to receive this response", diff --git a/sasl/src/server/mechanisms/anonymous.rs b/sasl/src/server/mechanisms/anonymous.rs index 1ee311b0..5a5381e7 100644 --- a/sasl/src/server/mechanisms/anonymous.rs +++ b/sasl/src/server/mechanisms/anonymous.rs @@ -1,5 +1,6 @@ use crate::common::Identity; use crate::server::{Mechanism, MechanismError, Response}; + use getrandom::getrandom; pub struct Anonymous; diff --git a/sasl/src/server/mechanisms/mod.rs b/sasl/src/server/mechanisms/mod.rs index 9d4c9a5e..1989a580 100644 --- a/sasl/src/server/mechanisms/mod.rs +++ b/sasl/src/server/mechanisms/mod.rs @@ -1,8 +1,10 @@ +#[cfg(feature = "anonymous")] mod anonymous; mod plain; #[cfg(feature = "scram")] mod scram; +#[cfg(feature = "anonymous")] pub use self::anonymous::Anonymous; pub use self::plain::Plain; #[cfg(feature = "scram")] diff --git a/sasl/src/server/mod.rs b/sasl/src/server/mod.rs index 5ee233c6..d3fcd7e9 100644 --- a/sasl/src/server/mod.rs +++ b/sasl/src/server/mod.rs @@ -1,8 +1,10 @@ -use crate::common::scram::DeriveError; use crate::common::Identity; use crate::secret::Secret; use std::fmt; +#[cfg(feature = "scram")] +use crate::common::scram::DeriveError; + #[macro_export] macro_rules! impl_validator_using_provider { ( $validator:ty, $secret:ty ) => { @@ -33,6 +35,7 @@ pub trait Validator { #[derive(Debug, PartialEq)] pub enum ProviderError { AuthenticationFailed, + #[cfg(feature = "scram")] DeriveError(DeriveError), } @@ -61,7 +64,9 @@ pub enum MechanismError { ProviderError(ProviderError), CannotDecodeResponse, + #[cfg(feature = "scram")] InvalidKeyLength(hmac::digest::InvalidLength), + #[cfg(any(feature = "scram", feature = "anonymous"))] RandomFailure(getrandom::Error), NoProof, CannotDecodeProof, @@ -69,6 +74,7 @@ pub enum MechanismError { SaslSessionAlreadyOver, } +#[cfg(feature = "scram")] impl From for ProviderError { fn from(err: DeriveError) -> ProviderError { ProviderError::DeriveError(err) @@ -93,12 +99,14 @@ impl From for MechanismError { } } +#[cfg(feature = "scram")] impl From for MechanismError { fn from(err: hmac::digest::InvalidLength) -> MechanismError { MechanismError::InvalidKeyLength(err) } } +#[cfg(any(feature = "scram", feature = "anonymous"))] impl From for MechanismError { fn from(err: getrandom::Error) -> MechanismError { MechanismError::RandomFailure(err) @@ -145,7 +153,9 @@ impl fmt::Display for MechanismError { MechanismError::ProviderError(err) => write!(fmt, "provider error: {}", err), MechanismError::CannotDecodeResponse => write!(fmt, "can’t decode response"), + #[cfg(feature = "scram")] MechanismError::InvalidKeyLength(err) => write!(fmt, "invalid key length: {}", err), + #[cfg(any(feature = "scram", feature = "anonymous"))] MechanismError::RandomFailure(err) => { write!(fmt, "failure to get random data: {}", err) }