From dc842c44d1d5c1eaa6e88e536ae3af4b82faad61 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Wed, 25 Sep 2024 21:55:02 +0200 Subject: [PATCH] sasl: Make this crate no_std We mostly had to import from alloc some structs that are part of the std prelude, such as Vec and String. --- sasl/CHANGELOG.md | 4 ++++ sasl/README.md | 2 ++ sasl/src/client/mechanisms/plain.rs | 2 ++ sasl/src/client/mechanisms/scram.rs | 23 ++++++++++++++--------- sasl/src/client/mod.rs | 5 +++-- sasl/src/common/mod.rs | 6 ++++-- sasl/src/common/scram.rs | 10 +++++++--- sasl/src/error.rs | 1 + sasl/src/lib.rs | 5 +++++ sasl/src/secret.rs | 3 +++ sasl/src/server/mechanisms/anonymous.rs | 2 ++ sasl/src/server/mechanisms/plain.rs | 2 ++ sasl/src/server/mechanisms/scram.rs | 6 +++++- sasl/src/server/mod.rs | 5 +++-- 14 files changed, 57 insertions(+), 19 deletions(-) diff --git a/sasl/CHANGELOG.md b/sasl/CHANGELOG.md index f42fda32..c703b5f7 100644 --- a/sasl/CHANGELOG.md +++ b/sasl/CHANGELOG.md @@ -1,3 +1,7 @@ +Version NEXT, released 20??-??-??: + * Improvements + - This crate is now `no_std`, you can use it even on platforms which don’t provide the `std` crate. + Version 0.5.2, released 2024-07-22: * Improvements - Add SCRAM client extensions support (thanks to Lucas Kent) diff --git a/sasl/README.md b/sasl/README.md index 5972c591..5eb2266e 100644 --- a/sasl/README.md +++ b/sasl/README.md @@ -6,6 +6,8 @@ What's this? A crate which handles SASL authentication. Still unstable until 1.0.0. +It can be used in `no_std` environments. + Can I see an example? --------------------- diff --git a/sasl/src/client/mechanisms/plain.rs b/sasl/src/client/mechanisms/plain.rs index bc08fd85..05f4bd29 100644 --- a/sasl/src/client/mechanisms/plain.rs +++ b/sasl/src/client/mechanisms/plain.rs @@ -2,6 +2,8 @@ use crate::client::{Mechanism, MechanismError}; use crate::common::{Credentials, Identity, Password, Secret}; +use alloc::string::String; +use alloc::vec::Vec; /// A struct for the SASL PLAIN mechanism. pub struct Plain { diff --git a/sasl/src/client/mechanisms/scram.rs b/sasl/src/client/mechanisms/scram.rs index 05f78744..2f8dc501 100644 --- a/sasl/src/client/mechanisms/scram.rs +++ b/sasl/src/client/mechanisms/scram.rs @@ -8,7 +8,10 @@ use crate::common::{parse_frame, xor, ChannelBinding, Credentials, Identity, Pas use crate::error::Error; -use std::marker::PhantomData; +use alloc::format; +use alloc::string::String; +use alloc::vec::Vec; +use core::marker::PhantomData; enum ScramState { Init, @@ -226,6 +229,8 @@ mod tests { use crate::client::mechanisms::Scram; use crate::client::Mechanism; use crate::common::scram::{Sha1, Sha256}; + use alloc::borrow::ToOwned; + use alloc::string::String; #[test] fn scram_sha1_works() { @@ -293,13 +298,13 @@ mod tests { .with_first_extensions("tokenauth=true".to_owned()); let init = mechanism.initial(); assert_eq!( - std::str::from_utf8(&init).unwrap(), - std::str::from_utf8(client_init).unwrap() + core::str::from_utf8(&init).unwrap(), + core::str::from_utf8(client_init).unwrap() ); // depends on ordering… let resp = mechanism.response(server_init).unwrap(); assert_eq!( - std::str::from_utf8(&resp).unwrap(), - std::str::from_utf8(client_final).unwrap() + core::str::from_utf8(&resp).unwrap(), + core::str::from_utf8(client_final).unwrap() ); // again, depends on ordering… mechanism.success(server_final).unwrap(); } @@ -318,13 +323,13 @@ mod tests { .with_final_extensions("foo=true".to_owned()); let init = mechanism.initial(); assert_eq!( - std::str::from_utf8(&init).unwrap(), - std::str::from_utf8(client_init).unwrap() + core::str::from_utf8(&init).unwrap(), + core::str::from_utf8(client_init).unwrap() ); // depends on ordering… let resp = mechanism.response(server_init).unwrap(); assert_eq!( - std::str::from_utf8(&resp).unwrap(), - std::str::from_utf8(client_final).unwrap() + core::str::from_utf8(&resp).unwrap(), + core::str::from_utf8(client_final).unwrap() ); // again, depends on ordering… } } diff --git a/sasl/src/client/mod.rs b/sasl/src/client/mod.rs index 8acca9ff..c04eabf8 100644 --- a/sasl/src/client/mod.rs +++ b/sasl/src/client/mod.rs @@ -1,4 +1,5 @@ -use std::fmt; +use alloc::vec::Vec; +use core::fmt; use crate::common::Credentials; @@ -84,7 +85,7 @@ impl fmt::Display for MechanismError { } } -impl std::error::Error for MechanismError {} +impl core::error::Error for MechanismError {} /// A trait which defines SASL mechanisms. pub trait Mechanism { diff --git a/sasl/src/common/mod.rs b/sasl/src/common/mod.rs index bcf65430..7313c9ec 100644 --- a/sasl/src/common/mod.rs +++ b/sasl/src/common/mod.rs @@ -1,5 +1,7 @@ -use std::collections::BTreeMap; -use std::string::FromUtf8Error; +use alloc::borrow::ToOwned; +use alloc::collections::BTreeMap; +use alloc::string::{FromUtf8Error, String}; +use alloc::vec::Vec; #[cfg(feature = "scram")] pub mod scram; diff --git a/sasl/src/common/scram.rs b/sasl/src/common/scram.rs index 689a4d78..652ac079 100644 --- a/sasl/src/common/scram.rs +++ b/sasl/src/common/scram.rs @@ -1,3 +1,7 @@ +use alloc::string::{String, ToString}; +use alloc::vec; +use alloc::vec::Vec; +use core::fmt; use getrandom::{getrandom, Error as RngError}; use hmac::{digest::InvalidLength, Hmac, Mac}; use pbkdf2::pbkdf2; @@ -25,8 +29,8 @@ pub enum DeriveError { IncompatibleIterationCount(u32, u32), } -impl std::fmt::Display for DeriveError { - fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result { +impl fmt::Display for DeriveError { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { match self { DeriveError::IncompatibleHashingMethod(one, two) => { write!(fmt, "incompatible hashing method, {} is not {}", one, two) @@ -40,7 +44,7 @@ impl std::fmt::Display for DeriveError { } } -impl std::error::Error for DeriveError {} +impl core::error::Error for DeriveError {} impl From for DeriveError { fn from(_err: hmac::digest::InvalidLength) -> DeriveError { diff --git a/sasl/src/error.rs b/sasl/src/error.rs index eb885909..0347e8ac 100644 --- a/sasl/src/error.rs +++ b/sasl/src/error.rs @@ -1,3 +1,4 @@ +use alloc::string::String; #[cfg(feature = "scram")] use getrandom::Error as RngError; diff --git a/sasl/src/lib.rs b/sasl/src/lib.rs index 0c99962d..a776c8c3 100644 --- a/sasl/src/lib.rs +++ b/sasl/src/lib.rs @@ -1,8 +1,11 @@ //#![deny(missing_docs)] +#![no_std] #![cfg_attr(docsrs, feature(doc_auto_cfg))] //! This crate provides a framework for SASL authentication and a few authentication mechanisms. //! +//! It can be used in `no_std` environments. +//! //! # Examples //! //! ## Simple client-sided usage @@ -184,6 +187,8 @@ //! sasl = "*" //! ``` +extern crate alloc; + mod error; pub mod client; diff --git a/sasl/src/secret.rs b/sasl/src/secret.rs index d78e81f3..d663fb0f 100644 --- a/sasl/src/secret.rs +++ b/sasl/src/secret.rs @@ -1,5 +1,8 @@ #[cfg(feature = "scram")] use crate::common::scram::DeriveError; +use alloc::borrow::ToOwned; +use alloc::string::String; +use alloc::vec::Vec; pub trait Secret {} diff --git a/sasl/src/server/mechanisms/anonymous.rs b/sasl/src/server/mechanisms/anonymous.rs index 92031673..c171f25d 100644 --- a/sasl/src/server/mechanisms/anonymous.rs +++ b/sasl/src/server/mechanisms/anonymous.rs @@ -1,5 +1,7 @@ use crate::common::Identity; use crate::server::{Mechanism, MechanismError, Response}; +use alloc::format; +use alloc::vec::Vec; use getrandom::getrandom; diff --git a/sasl/src/server/mechanisms/plain.rs b/sasl/src/server/mechanisms/plain.rs index eae74a17..0cba1d84 100644 --- a/sasl/src/server/mechanisms/plain.rs +++ b/sasl/src/server/mechanisms/plain.rs @@ -1,6 +1,8 @@ use crate::common::Identity; use crate::secret; use crate::server::{Mechanism, MechanismError, Response, Validator}; +use alloc::string::String; +use alloc::vec::Vec; pub struct Plain> { validator: V, diff --git a/sasl/src/server/mechanisms/scram.rs b/sasl/src/server/mechanisms/scram.rs index 7bc48edf..62aa1651 100644 --- a/sasl/src/server/mechanisms/scram.rs +++ b/sasl/src/server/mechanisms/scram.rs @@ -1,4 +1,8 @@ -use std::marker::PhantomData; +use alloc::borrow::ToOwned; +use alloc::format; +use alloc::string::{String, ToString}; +use alloc::vec::Vec; +use core::marker::PhantomData; use base64::{engine::general_purpose::STANDARD as Base64, Engine}; diff --git a/sasl/src/server/mod.rs b/sasl/src/server/mod.rs index 889abac4..5032bb5d 100644 --- a/sasl/src/server/mod.rs +++ b/sasl/src/server/mod.rs @@ -1,6 +1,8 @@ use crate::common::Identity; use crate::secret::Secret; -use std::fmt; +use alloc::vec::Vec; +use core::error::Error; +use core::fmt; #[cfg(feature = "scram")] use crate::common::scram::DeriveError; @@ -171,7 +173,6 @@ impl Error for ProviderError {} impl Error for ValidatorError {} -use std::error::Error; impl Error for MechanismError { fn source(&self) -> Option<&(dyn Error + 'static)> { match self {