From 0f63731d5ab662d538b14a728d9245fb52c415c3 Mon Sep 17 00:00:00 2001 From: Toni Peter Date: Wed, 27 Nov 2024 09:22:40 +0100 Subject: [PATCH] Fix clippy lints --- .../nasl/builtin/cryptographic/aes_cmac.rs | 4 +-- rust/src/nasl/builtin/cryptographic/mod.rs | 6 ++-- rust/src/nasl/builtin/cryptographic/rc4.rs | 12 ++++---- rust/src/nasl/builtin/cryptographic/rsa.rs | 28 +++++++++---------- rust/src/nasl/builtin/network/socket.rs | 17 +++++------ .../src/nasl/builtin/raw_ip/packet_forgery.rs | 2 +- rust/src/nasl/builtin/raw_ip/raw_ip_utils.rs | 2 +- rust/src/nasl/utils/mod.rs | 2 +- 8 files changed, 35 insertions(+), 38 deletions(-) diff --git a/rust/src/nasl/builtin/cryptographic/aes_cmac.rs b/rust/src/nasl/builtin/cryptographic/aes_cmac.rs index 7acab3e91..4e80eb718 100644 --- a/rust/src/nasl/builtin/cryptographic/aes_cmac.rs +++ b/rust/src/nasl/builtin/cryptographic/aes_cmac.rs @@ -20,8 +20,8 @@ fn aes_cmac(register: &Register, _: &Context) -> Result { let key = get_key(register)?; let data = get_data(register)?; - let mut mac = Cmac::::new_from_slice(key) - .map_err(|e| CryptographicError::AesCmacInvalidLength(e))?; + let mut mac = + Cmac::::new_from_slice(key).map_err(CryptographicError::AesCmacInvalidLength)?; mac.update(data); Ok(mac.finalize().into_bytes().to_vec().into()) diff --git a/rust/src/nasl/builtin/cryptographic/mod.rs b/rust/src/nasl/builtin/cryptographic/mod.rs index 27362e849..1702331aa 100644 --- a/rust/src/nasl/builtin/cryptographic/mod.rs +++ b/rust/src/nasl/builtin/cryptographic/mod.rs @@ -36,9 +36,9 @@ pub enum CryptographicError { #[error("Invalid length of key in AesCmac {0}.")] AesCmacInvalidLength(digest::InvalidLength), #[error("Error in RSA: {0}.")] - RSA(String), + Rsa(String), #[error("Error in RC4: {0}.")] - RC4(String), + Rc4(String), } enum Crypt { @@ -62,7 +62,7 @@ fn get_required_named_data<'a>( "a String or Data Value", format!("{:?}", x).as_str(), )), - _ => Err(ArgumentError::MissingNamed(vec![key.into()]).into()), + _ => Err(ArgumentError::MissingNamed(vec![key.into()])), } } diff --git a/rust/src/nasl/builtin/cryptographic/rc4.rs b/rust/src/nasl/builtin/cryptographic/rc4.rs index c9874cf5e..224e41027 100644 --- a/rust/src/nasl/builtin/cryptographic/rc4.rs +++ b/rust/src/nasl/builtin/cryptographic/rc4.rs @@ -65,7 +65,7 @@ impl CipherHandlers { ) -> Result { let hd = match register.named("hd") { Some(ContextType::Value(NaslValue::Number(x))) => *x as i32, - _ => return Err(CryptographicError::RC4("Handler ID not found".to_string()).into()), + _ => return Err(CryptographicError::Rc4("Handler ID not found".to_string()).into()), }; let mut handlers = lock_handlers(&self.cipher_handlers)?; @@ -74,7 +74,7 @@ impl CipherHandlers { handlers.remove(i); Ok(NaslValue::Number(0)) } - _ => Err(CryptographicError::RC4(format!("Handler ID {} not found", hd)).into()), + _ => Err(CryptographicError::Rc4(format!("Handler ID {} not found", hd)).into()), } } @@ -89,7 +89,7 @@ impl CipherHandlers { let key = match get_key(register) { Ok(k) if !k.is_empty() => k.to_vec(), - _ => return Err(CryptographicError::RC4("Missing Key argument".to_string()).into()), + _ => return Err(CryptographicError::Rc4("Missing Key argument".to_string()).into()), }; let rc_handler = Rc4Key::build_handler_from_key(key.to_vec())?; @@ -116,7 +116,7 @@ impl CipherHandlers { pub fn rc4_encrypt(&self, register: &Register, _: &Context) -> Result { let data = match get_data(register) { Ok(d) if !d.is_empty() => d.to_vec(), - _ => return Err(CryptographicError::RC4("Missing data argument".to_string()).into()), + _ => return Err(CryptographicError::Rc4("Missing data argument".to_string()).into()), }; let hd = match register.named("hd") { @@ -135,7 +135,7 @@ impl CipherHandlers { let key = match get_key(register) { Ok(k) if !k.is_empty() => k.to_vec(), - _ => return Err(CryptographicError::RC4("Missing Key argument".to_string()).into()), + _ => return Err(CryptographicError::Rc4("Missing Key argument".to_string()).into()), }; let mut rc_handler = Rc4Key::build_handler_from_key(key.to_vec())?; @@ -156,7 +156,7 @@ macro_rules! build_rc4key_enum { fn build_handler_from_key(bl: Vec) -> Result { match bl.len() { $($l => Ok(Self::$i(Rc4::new_from_slice(bl.as_slice()).unwrap())),)* - _ => {return Err(CryptographicError::RC4("RC4 Key size not supported".into()).into())} + _ => {return Err(CryptographicError::Rc4("RC4 Key size not supported".into()).into())} } } diff --git a/rust/src/nasl/builtin/cryptographic/rsa.rs b/rust/src/nasl/builtin/cryptographic/rsa.rs index 214d5fa2a..2ce383d8a 100644 --- a/rust/src/nasl/builtin/cryptographic/rsa.rs +++ b/rust/src/nasl/builtin/cryptographic/rsa.rs @@ -26,15 +26,15 @@ fn rsa_public_encrypt( rsa::BigUint::from_bytes_be(n), rsa::BigUint::from_bytes_be(e), ) - .map_err(|e| CryptographicError::RSA(e.to_string()))?; + .map_err(|e| CryptographicError::Rsa(e.to_string()))?; let biguint_data = BigUint::from_bytes_be(data); let enc_data = if pad { pub_key .encrypt(&mut rng, Pkcs1v15Encrypt, data) - .map_err(|e| CryptographicError::RSA(e.to_string()))? + .map_err(|e| CryptographicError::Rsa(e.to_string()))? } else { rsa::hazmat::rsa_encrypt(&pub_key, &biguint_data) - .map_err(|e| CryptographicError::RSA(e.to_string()))? + .map_err(|e| CryptographicError::Rsa(e.to_string()))? .to_bytes_be() }; Ok(enc_data.to_vec().into()) @@ -57,7 +57,7 @@ fn rsa_private_decrypt( ) { Ok(val) => Ok(val), Err(code) => Err( - FnError::from(CryptographicError::RSA(format!("Error code {}", code))).with( + FnError::from(CryptographicError::Rsa(format!("Error code {}", code))).with( ReturnValue(NaslValue::Array(vec![ NaslValue::Data(n.to_vec()), NaslValue::Data(e.to_vec()), @@ -66,22 +66,22 @@ fn rsa_private_decrypt( ), ), } - .map_err(|e| CryptographicError::RSA(e.to_string()))?; + .map_err(|e| CryptographicError::Rsa(e.to_string()))?; let mut rng = OsRng; let biguint_data = BigUint::from_bytes_be(data); let dec_data = if pad { match priv_key.decrypt(Pkcs1v15Encrypt, data) { Ok(val) => Ok(val), - Err(code) => Err(FnError::from(CryptographicError::RSA(format!( + Err(code) => Err(FnError::from(CryptographicError::Rsa(format!( "Error code {}", code ))) .with(ReturnValue(NaslValue::Data(data.to_vec())))), } - .map_err(|e| CryptographicError::RSA(e.to_string()))? + .map_err(|e| CryptographicError::Rsa(e.to_string()))? } else { rsa::hazmat::rsa_decrypt_and_check(&priv_key, Some(&mut rng), &biguint_data) - .map_err(|e| CryptographicError::RSA(e.to_string()))? + .map_err(|e| CryptographicError::Rsa(e.to_string()))? .to_bytes_be() }; @@ -90,20 +90,20 @@ fn rsa_private_decrypt( #[nasl_function(named(data, pem, passphrase))] fn rsa_sign(data: &[u8], pem: &[u8], passphrase: Option<&str>) -> Result { - let pem_str = std::str::from_utf8(pem).map_err(|e| CryptographicError::RSA(e.to_string()))?; + let pem_str = std::str::from_utf8(pem).map_err(|e| CryptographicError::Rsa(e.to_string()))?; let rsa: RsaPrivateKey = if passphrase.unwrap_or_default() != "" { pkcs8::DecodePrivateKey::from_pkcs8_encrypted_pem(pem_str, passphrase.unwrap_or_default()) - .map_err(|e| CryptographicError::RSA(e.to_string()))? + .map_err(|e| CryptographicError::Rsa(e.to_string()))? } else { RsaPrivateKey::from_pkcs8_pem(pem_str) - .map_err(|e| CryptographicError::RSA(e.to_string()))? + .map_err(|e| CryptographicError::Rsa(e.to_string()))? }; let mut hasher = Sha1::new_with_prefix(data); hasher.update(data); let hashed_data = hasher.finalize(); let signature = rsa .sign(Pkcs1v15Sign::new_unprefixed(), &hashed_data) - .map_err(|e| CryptographicError::RSA(e.to_string()))?; + .map_err(|e| CryptographicError::Rsa(e.to_string()))?; Ok(signature.into()) } @@ -112,11 +112,11 @@ fn rsa_public_decrypt(sign: &[u8], n: &[u8], e: &[u8]) -> Result i32::MAX as i64 { return Err(SocketError::WrongArgument( "the given flags value is out of range".to_string(), - ) - .into()); + )); } Ok(conn.send_with_flags(data, flags as i32)?) } else { @@ -245,7 +244,7 @@ impl NaslSockets { match conn.read_with_timeout(&mut data[pos..], timeout) { Ok(n) => pos += n, Err(e) if e.kind() == io::ErrorKind::TimedOut => break, - Err(e) => return Err(SocketError::from(e).into()), + Err(e) => return Err(SocketError::from(e)), } } Ok(NaslValue::Data(data[..pos].to_vec())) @@ -295,12 +294,10 @@ impl NaslSockets { } NaslSocket::Udp(_) => Err(SocketError::Diagnostic( "This function is only available for TCP connections".to_string(), - ) - .into()), + )), NaslSocket::Closed => Err(SocketError::WrongArgument( "the given socket FD is already closed".to_string(), - ) - .into()), + )), } } @@ -335,9 +332,9 @@ impl NaslSockets { Some(_) => Err(SocketError::Diagnostic( "KB key 'Secret/kdc_port' has wrong type".to_string(), )), - None => Err( - SocketError::Diagnostic("KB key 'Secret/kdc_port' is not set".to_string()).into(), - ), + None => Err(SocketError::Diagnostic( + "KB key 'Secret/kdc_port' is not set".to_string(), + )), }?; let use_tcp: bool = get_kb_item(context, "Secret/kdc_use_tcp")? diff --git a/rust/src/nasl/builtin/raw_ip/packet_forgery.rs b/rust/src/nasl/builtin/raw_ip/packet_forgery.rs index 8e43b0cf0..2a58b31ec 100644 --- a/rust/src/nasl/builtin/raw_ip/packet_forgery.rs +++ b/rust/src/nasl/builtin/raw_ip/packet_forgery.rs @@ -2074,7 +2074,7 @@ fn nasl_send_packet(register: &Register, configs: &Context) -> Result Result { SocketAddr::V4(_) => UdpSocket::bind("0.0.0.0:0"), SocketAddr::V6(_) => UdpSocket::bind(" 0:0:0:0:0:0:0:0:0"), } - .map_err(|e| RawIpError::FailedToBind(e)) + .map_err(RawIpError::FailedToBind) } /// Return the source IP address given the destination IP address diff --git a/rust/src/nasl/utils/mod.rs b/rust/src/nasl/utils/mod.rs index f2e7bd39c..77d690815 100644 --- a/rust/src/nasl/utils/mod.rs +++ b/rust/src/nasl/utils/mod.rs @@ -52,7 +52,7 @@ pub fn get_named_parameter<'a>( match registrat.named(key) { None => { if required { - Err(ArgumentError::MissingNamed(vec![key.to_owned()]).into()) + Err(ArgumentError::MissingNamed(vec![key.to_owned()])) } else { // we use exit because a named value can be intentionally set to null and may be // treated differently when it is not set compared to set but null.