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 f42fda32bfcef052a52e2a2da4256d52247a990a..c703b5f77b1d867daabfc9c699798ddea64eded3 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 5972c5915932aacc3e975bf3fabc8464d57bb965..5eb2266e71794f8888055e6847008a0cdf7452b7 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 bc08fd85b711a5b8d035b98764e00573601750b8..05f4bd291d12ca3b47fa9f6220f4cfbde7f10a49 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 05f7874478ecec6cae4a6ed4e705fe6e665e06d9..2f8dc5019fe5ff3507076ca97d72ab37b9f4d06c 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 8acca9ffbc200c86e19d89c60fb1fd3a2876ed74..c04eabf8b3db2b19d3acf46ef1c7525033e4b5c7 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 bcf6543087a28838121fb17ea6e577201ee84bef..7313c9ec539698ea4f9499d593ed9e5be5b7107e 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 689a4d7824318bae18bd0d7ac4a9c632084ebe95..652ac0795df24611a6b378412935ae4650fb0089 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 eb8859092c993f0b266268f82786879d1911ee59..0347e8ac82ac04a203d7ca2051b81e6a6c4cf296 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 0c99962d56c5125ea9f7eb26faa912847d29b40e..a776c8c3555f4928778f8a94781806b42dc2257f 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 d78e81f3122d7b6178792659460907865b077fe1..d663fb0fa29269a1b34a685976a7e8e2bbb81e04 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 92031673b5a75616f31e7aabb6c18204175b3aca..c171f25d94cd3657cdf3221494307eee0f5e7369 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 eae74a17aea4f1533095275f48560718ffe9716c..0cba1d8406fdff6606a8b2bd7ba0332e9301f8d1 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 7bc48edf81c9aefcd4307fdd65b73f8318fc1c8d..62aa165131de79aa4bb3fe4fb23af9814d392879 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 889abac4a856e6f4b8bcf118a734f1836c8d3a22..5032bb5ddd3dfedfa4b7dcc2c42c535f796b003e 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 {