diff --git a/httpsig/Cargo.toml b/httpsig/Cargo.toml index b20d835..8791149 100644 --- a/httpsig/Cargo.toml +++ b/httpsig/Cargo.toml @@ -15,10 +15,11 @@ rust-version.workspace = true [features] default = [] rsa-signature = ["rsa"] +tracing = ["dep:tracing"] [dependencies] thiserror = { version = "2.0.18" } -tracing = { version = "0.1.44" } +tracing = { version = "0.1.44", optional = true } rustc-hash = { version = "2.1.1" } indexmap = { version = "2.11.4" } rand = { version = "0.10.0" } diff --git a/httpsig/src/crypto/asymmetric.rs b/httpsig/src/crypto/asymmetric.rs index d6d790d..0d1ed15 100644 --- a/httpsig/src/crypto/asymmetric.rs +++ b/httpsig/src/crypto/asymmetric.rs @@ -1,8 +1,5 @@ use super::AlgorithmName; -use crate::{ - error::{HttpSigError, HttpSigResult}, - trace::*, -}; +use crate::error::{HttpSigError, HttpSigResult}; use ecdsa::{ elliptic_curve::{PublicKey as EcPublicKey, SecretKey as EcSecretKey, sec1::ToEncodedPoint}, signature::{DigestSigner, DigestVerifier}, @@ -13,6 +10,8 @@ use p384::NistP384; use pkcs8::{Document, PrivateKeyInfo, der::Decode}; use sha2::{Digest, Sha256, Sha384}; use spki::SubjectPublicKeyInfoRef; +#[cfg(feature = "tracing")] +use tracing::debug; #[cfg(feature = "rsa-signature")] use rsa::{ @@ -65,16 +64,19 @@ impl SecretKey { pub fn from_bytes(alg: &AlgorithmName, bytes: &[u8]) -> HttpSigResult { match alg { AlgorithmName::EcdsaP256Sha256 => { + #[cfg(feature = "tracing")] debug!("Read P256 private key"); let sk = EcSecretKey::from_bytes(bytes.into()).map_err(|e| HttpSigError::ParsePrivateKeyError(e.to_string()))?; Ok(Self::EcdsaP256Sha256(sk)) } AlgorithmName::EcdsaP384Sha384 => { + #[cfg(feature = "tracing")] debug!("Read P384 private key"); let sk = EcSecretKey::from_bytes(bytes.into()).map_err(|e| HttpSigError::ParsePrivateKeyError(e.to_string()))?; Ok(Self::EcdsaP384Sha384(sk)) } AlgorithmName::Ed25519 => { + #[cfg(feature = "tracing")] debug!("Read Ed25519 private key"); let mut seed = [0u8; 32]; seed.copy_from_slice(bytes); @@ -176,6 +178,7 @@ impl super::SigningKey for SecretKey { fn sign(&self, data: &[u8]) -> HttpSigResult> { match &self { Self::EcdsaP256Sha256(sk) => { + #[cfg(feature = "tracing")] debug!("Sign EcdsaP256Sha256"); let sk = ecdsa::SigningKey::from(sk); let mut digest = ::new(); @@ -184,6 +187,7 @@ impl super::SigningKey for SecretKey { Ok(sig.to_bytes().to_vec()) } Self::EcdsaP384Sha384(sk) => { + #[cfg(feature = "tracing")] debug!("Sign EcdsaP384Sha384"); let sk = ecdsa::SigningKey::from(sk); let mut digest = ::new(); @@ -192,6 +196,7 @@ impl super::SigningKey for SecretKey { Ok(sig.to_bytes().to_vec()) } Self::Ed25519(sk) => { + #[cfg(feature = "tracing")] debug!("Sign Ed25519"); let sig = sk.sign(data, Some(ed25519_compact::Noise::default())); Ok(sig.as_ref().to_vec()) @@ -260,16 +265,19 @@ impl PublicKey { pub fn from_bytes(alg: &AlgorithmName, bytes: &[u8]) -> HttpSigResult { match alg { AlgorithmName::EcdsaP256Sha256 => { + #[cfg(feature = "tracing")] debug!("Read P256 public key"); let pk = EcPublicKey::from_sec1_bytes(bytes).map_err(|e| HttpSigError::ParsePublicKeyError(e.to_string()))?; Ok(Self::EcdsaP256Sha256(pk)) } AlgorithmName::EcdsaP384Sha384 => { + #[cfg(feature = "tracing")] debug!("Read P384 public key"); let pk = EcPublicKey::from_sec1_bytes(bytes).map_err(|e| HttpSigError::ParsePublicKeyError(e.to_string()))?; Ok(Self::EcdsaP384Sha384(pk)) } AlgorithmName::Ed25519 => { + #[cfg(feature = "tracing")] debug!("Read Ed25519 public key"); let pk = ed25519_compact::PublicKey::from_slice(bytes).map_err(|e| HttpSigError::ParsePublicKeyError(e.to_string()))?; Ok(Self::Ed25519(pk)) @@ -358,6 +366,7 @@ impl super::VerifyingKey for PublicKey { fn verify(&self, data: &[u8], signature: &[u8]) -> HttpSigResult<()> { match self { Self::EcdsaP256Sha256(pk) => { + #[cfg(feature = "tracing")] debug!("Verify EcdsaP256Sha256"); let signature = ecdsa::Signature::::from_bytes(signature.into()) .map_err(|e| HttpSigError::ParseSignatureError(e.to_string()))?; @@ -368,6 +377,7 @@ impl super::VerifyingKey for PublicKey { .map_err(|e| HttpSigError::InvalidSignature(e.to_string())) } Self::EcdsaP384Sha384(pk) => { + #[cfg(feature = "tracing")] debug!("Verify EcdsaP384Sha384"); let signature = ecdsa::Signature::::from_bytes(signature.into()) .map_err(|e| HttpSigError::ParseSignatureError(e.to_string()))?; @@ -378,6 +388,7 @@ impl super::VerifyingKey for PublicKey { .map_err(|e| HttpSigError::InvalidSignature(e.to_string())) } Self::Ed25519(pk) => { + #[cfg(feature = "tracing")] debug!("Verify Ed25519"); let sig = ed25519_compact::Signature::from_slice(signature).map_err(|e| HttpSigError::ParseSignatureError(e.to_string()))?; @@ -386,6 +397,7 @@ impl super::VerifyingKey for PublicKey { } #[cfg(feature = "rsa-signature")] Self::RsaV1_5Sha256(pk) => { + #[cfg(feature = "tracing")] debug!("Verify RsaV1_5Sha256"); let sig = pkcs1v15::Signature::try_from(signature).map_err(|e| HttpSigError::ParseSignatureError(e.to_string()))?; pk.verify(data, &sig) diff --git a/httpsig/src/crypto/symmetric.rs b/httpsig/src/crypto/symmetric.rs index 75502d8..5fee83b 100644 --- a/httpsig/src/crypto/symmetric.rs +++ b/httpsig/src/crypto/symmetric.rs @@ -1,11 +1,10 @@ use super::AlgorithmName; -use crate::{ - error::{HttpSigError, HttpSigResult}, - trace::*, -}; +use crate::error::{HttpSigError, HttpSigResult}; use base64::{Engine as _, engine::general_purpose}; use hmac::{Hmac, Mac}; use sha2::{Digest, Sha256}; +#[cfg(feature = "tracing")] +use tracing::debug; type HmacSha256 = Hmac; @@ -21,6 +20,7 @@ pub enum SharedKey { impl SharedKey { /// Create a new shared key from base64 encoded string pub fn from_base64(alg: &AlgorithmName, key: &str) -> HttpSigResult { + #[cfg(feature = "tracing")] debug!("Create SharedKey from base64 string"); let key = general_purpose::STANDARD.decode(key)?; match alg { @@ -38,6 +38,7 @@ impl super::SigningKey for SharedKey { fn sign(&self, data: &[u8]) -> HttpSigResult> { match self { SharedKey::HmacSha256(key) => { + #[cfg(feature = "tracing")] debug!("Sign HmacSha256"); let mut mac = HmacSha256::new_from_slice(key).unwrap(); mac.update(data); @@ -61,6 +62,7 @@ impl super::VerifyingKey for SharedKey { fn verify(&self, data: &[u8], expected_mac: &[u8]) -> HttpSigResult<()> { match self { SharedKey::HmacSha256(key) => { + #[cfg(feature = "tracing")] debug!("Verify HmacSha256"); let mut mac = HmacSha256::new_from_slice(key).unwrap(); mac.update(data); diff --git a/httpsig/src/lib.rs b/httpsig/src/lib.rs index dcc69e7..05a8d17 100644 --- a/httpsig/src/lib.rs +++ b/httpsig/src/lib.rs @@ -10,7 +10,6 @@ mod error; mod message_component; mod signature_base; mod signature_params; -mod trace; mod util; pub mod prelude { diff --git a/httpsig/src/message_component/component.rs b/httpsig/src/message_component/component.rs index b6a724c..335bad9 100644 --- a/httpsig/src/message_component/component.rs +++ b/httpsig/src/message_component/component.rs @@ -4,11 +4,10 @@ use super::{ component_param::{HttpMessageComponentParam, handle_params_key_into, handle_params_sf}, component_value::HttpMessageComponentValue, }; -use crate::{ - error::{HttpSigError, HttpSigResult}, - trace::*, -}; +use crate::error::{HttpSigError, HttpSigResult}; +#[cfg(feature = "tracing")] +use tracing::debug; /* ---------------------------------------------------------------- */ #[derive(Debug, Clone)] /// Http message component @@ -160,6 +159,7 @@ pub(super) fn build_http_field_component( return Err(HttpSigError::NotYetImplemented("`bs` is not supported yet".to_string())); } HttpMessageComponentParam::Req => { + #[cfg(feature = "tracing")] debug!("`req` is given for http field component"); } HttpMessageComponentParam::Tr => return Err(HttpSigError::NotYetImplemented("`tr` is not supported yet".to_string())), diff --git a/httpsig/src/signature_params.rs b/httpsig/src/signature_params.rs index 122b66c..bb77144 100644 --- a/httpsig/src/signature_params.rs +++ b/httpsig/src/signature_params.rs @@ -2,7 +2,6 @@ use crate::{ crypto::{AlgorithmName, SigningKey}, error::{HttpSigError, HttpSigResult}, message_component::HttpMessageComponentId, - trace::*, util::has_unique_elements, }; use base64::{Engine as _, engine::general_purpose}; @@ -10,6 +9,9 @@ use rand::RngExt; use sfv::{FieldType, ListEntry, Parser}; use std::time::{SystemTime, UNIX_EPOCH}; +#[cfg(feature = "tracing")] +use tracing::error; + const DEFAULT_DURATION: u64 = 300; /* ---------------------------------------- */ @@ -197,6 +199,7 @@ impl TryFrom<&ListEntry> for HttpSignatureParams { "keyid" => params.keyid = bare_item.as_string().map(|v| v.to_string()), "tag" => params.tag = bare_item.as_string().map(|v| v.to_string()), _ => { + #[cfg(feature = "tracing")] error!("Ignore invalid signature parameter: {}", key) } }); diff --git a/httpsig/src/trace.rs b/httpsig/src/trace.rs deleted file mode 100644 index 7255693..0000000 --- a/httpsig/src/trace.rs +++ /dev/null @@ -1,2 +0,0 @@ -#[allow(unused)] -pub use tracing::{debug, error, info, trace, warn};