From a4e2f21b6dbf63c131a9a25c4dbd9655ea643123 Mon Sep 17 00:00:00 2001 From: Pavlo Myroniuk Date: Mon, 5 Aug 2024 11:26:03 +0300 Subject: [PATCH] refactor: fix cargo clippy warnings; --- src/credssp/mod.rs | 8 +----- src/credssp/ts_request/test.rs | 3 ++ src/lib.rs | 3 +- src/ntlm/messages/mod.rs | 10 +++++-- src/ntlm/messages/server/authenticate.rs | 14 ++++----- src/ntlm/messages/test.rs | 2 ++ src/ntlm/test.rs | 36 +++++++++++------------- tests/common.rs | 9 +++--- 8 files changed, 43 insertions(+), 42 deletions(-) diff --git a/src/credssp/mod.rs b/src/credssp/mod.rs index 9c052708..3c621b47 100644 --- a/src/credssp/mod.rs +++ b/src/credssp/mod.rs @@ -1,12 +1,6 @@ -cfg_if::cfg_if! { - if #[cfg(fuzzing)] { - pub mod ts_request; - } else { - mod ts_request; - } -} #[cfg(feature = "tsssp")] pub mod sspi_cred_ssp; +mod ts_request; use std::io; diff --git a/src/credssp/ts_request/test.rs b/src/credssp/ts_request/test.rs index 995e89ea..ac4b4f58 100644 --- a/src/credssp/ts_request/test.rs +++ b/src/credssp/ts_request/test.rs @@ -205,6 +205,7 @@ static AUTH_IDENTITY_ONE_SYMBOL_USER_AND_PASSWORD: LazyLock .into(), ) }); + static AUTH_IDENTITY_STRONG_USERNAME_AND_PASSWORD: LazyLock = LazyLock::new(|| { CredentialsBuffers::AuthIdentity( AuthIdentity { @@ -217,6 +218,7 @@ static AUTH_IDENTITY_STRONG_USERNAME_AND_PASSWORD: LazyLock .into(), ) }); + static AUTH_IDENTITY_SIMPLE_WITH_USERNAME_AND_DOMAIN_AND_PASSWORD: LazyLock = LazyLock::new(|| { CredentialsBuffers::AuthIdentity( AuthIdentity { @@ -226,6 +228,7 @@ static AUTH_IDENTITY_SIMPLE_WITH_USERNAME_AND_DOMAIN_AND_PASSWORD: LazyLock = LazyLock::new(|| { CredentialsBuffers::AuthIdentity( AuthIdentity { diff --git a/src/lib.rs b/src/lib.rs index b58677fc..3f8b8a2b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -81,8 +81,6 @@ mod utils; #[cfg(all(feature = "tsssp", not(target_os = "windows")))] compile_error!("tsssp feature should be used only on Windows"); -use std::{error, fmt, io, result, str, string}; - use bitflags::bitflags; #[cfg(feature = "tsssp")] use credssp::sspi_cred_ssp; @@ -94,6 +92,7 @@ use picky_asn1_x509::Certificate; use picky_krb::gss_api::GssApiMessageError; use picky_krb::messages::KrbError; pub use security_buffer::SecurityBuffer; +use std::{error, fmt, io, result, str, string}; use utils::map_keb_error_code_to_sspi_error; pub use utils::string_to_utf16; diff --git a/src/ntlm/messages/mod.rs b/src/ntlm/messages/mod.rs index 4c89d6d8..61df5e68 100644 --- a/src/ntlm/messages/mod.rs +++ b/src/ntlm/messages/mod.rs @@ -6,9 +6,8 @@ pub mod test; mod av_pair; mod computations; -use std::io; - use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; +use std::io; use crate::ntlm::{NegotiateFlags, NTLM_VERSION_SIZE}; @@ -71,6 +70,13 @@ impl MessageFields { Ok(()) } + + fn read_buffer_from_cursor(&mut self, cursor: &mut io::Cursor) -> io::Result<()> + where + io::Cursor: io::Read + io::Seek, + { + self.read_buffer_from(cursor) + } } fn try_read_version(flags: NegotiateFlags, mut cursor: impl io::Read) -> io::Result> { diff --git a/src/ntlm/messages/server/authenticate.rs b/src/ntlm/messages/server/authenticate.rs index fa7c0fc7..5b8163a8 100644 --- a/src/ntlm/messages/server/authenticate.rs +++ b/src/ntlm/messages/server/authenticate.rs @@ -113,7 +113,7 @@ fn read_header(mut buffer: impl io::Read) -> crate::Result<(AuthenticateMessageF fn read_payload( negotiate_flags: NegotiateFlags, message_fields: &mut AuthenticateMessageFields, - mut buffer: &mut io::Cursor, + buffer: &mut io::Cursor, ) -> crate::Result> where io::Cursor: io::Read + io::Seek, @@ -127,14 +127,14 @@ where None }; - message_fields.domain_name.read_buffer_from(&mut buffer)?; - message_fields.user_name.read_buffer_from(&mut buffer)?; - message_fields.workstation.read_buffer_from(&mut buffer)?; - message_fields.lm_challenge_response.read_buffer_from(&mut buffer)?; - message_fields.nt_challenge_response.read_buffer_from(&mut buffer)?; + message_fields.domain_name.read_buffer_from_cursor(buffer)?; + message_fields.user_name.read_buffer_from_cursor(buffer)?; + message_fields.workstation.read_buffer_from_cursor(buffer)?; + message_fields.lm_challenge_response.read_buffer_from_cursor(buffer)?; + message_fields.nt_challenge_response.read_buffer_from_cursor(buffer)?; message_fields .encrypted_random_session_key - .read_buffer_from(&mut buffer)?; + .read_buffer_from_cursor(buffer)?; Ok(mic) } diff --git a/src/ntlm/messages/test.rs b/src/ntlm/messages/test.rs index 5331d8c4..c9fec2fa 100644 --- a/src/ntlm/messages/test.rs +++ b/src/ntlm/messages/test.rs @@ -99,6 +99,7 @@ pub static LOCAL_NEGOTIATE_MESSAGE: LazyLock<[u8; LOCAL_NEGOTIATE_MESSAGE_SIZE]> result }); + pub static LOCAL_CHALLENGE_MESSAGE: LazyLock<[u8; LOCAL_CHALLENGE_MESSAGE_SIZE]> = LazyLock::new(|| { let mut message = Vec::with_capacity(LOCAL_CHALLENGE_MESSAGE_SIZE); message.extend_from_slice(NTLM_SIGNATURE.as_ref()); @@ -117,6 +118,7 @@ pub static LOCAL_CHALLENGE_MESSAGE: LazyLock<[u8; LOCAL_CHALLENGE_MESSAGE_SIZE]> result }); + pub static TEST_CREDENTIALS: LazyLock = LazyLock::new(|| { AuthIdentity { username: Username::new("User", Some("Domain")).unwrap(), diff --git a/src/ntlm/test.rs b/src/ntlm/test.rs index 5c3c6772..b46deaef 100644 --- a/src/ntlm/test.rs +++ b/src/ntlm/test.rs @@ -1,5 +1,3 @@ -use std::sync::LazyLock; - use crate::crypto::{Rc4, HASH_SIZE}; use crate::ntlm::messages::test::TEST_CREDENTIALS; use crate::ntlm::{ @@ -16,19 +14,17 @@ const SIGNING_KEY: [u8; HASH_SIZE] = [ 0x20, 0xc0, 0x2b, 0x3d, 0xc0, 0x61, 0xa7, 0x73, 0xa4, 0xf1, 0xba, 0xa6, 0x7c, 0xdc, 0x1a, 0x12, ]; -pub static TEST_DATA: LazyLock> = LazyLock::new(|| b"Hello, World!!!".to_vec()); -pub static ENCRYPTED_TEST_DATA: LazyLock> = LazyLock::new(|| { - vec![ - 0x20, 0x2e, 0xdd, 0xd9, 0x56, 0x5e, 0xc4, 0x59, 0x42, 0xdb, 0x94, 0xfd, 0x6b, 0xf3, 0x11, - ] -}); -pub static DIGEST_FOR_TEST_DATA: LazyLock> = - LazyLock::new(|| vec![0x58, 0x27, 0x4d, 0x35, 0x1f, 0x2d, 0x3c, 0xfd]); -pub static SIGNATURE_FOR_TEST_DATA: LazyLock> = LazyLock::new(|| { - vec![ - 0x1, 0x0, 0x0, 0x0, 0x58, 0x27, 0x4d, 0x35, 0x1f, 0x2d, 0x3c, 0xfd, 0xd2, 0x2, 0x96, 0x49, - ] -}); +pub const TEST_DATA: &'static [u8] = b"Hello, World!!!"; + +pub const ENCRYPTED_TEST_DATA: [u8; 15] = [ + 0x20, 0x2e, 0xdd, 0xd9, 0x56, 0x5e, 0xc4, 0x59, 0x42, 0xdb, 0x94, 0xfd, 0x6b, 0xf3, 0x11, +]; + +pub const DIGEST_FOR_TEST_DATA: [u8; 8] = [0x58, 0x27, 0x4d, 0x35, 0x1f, 0x2d, 0x3c, 0xfd]; + +pub const SIGNATURE_FOR_TEST_DATA: [u8; 16] = [ + 0x1, 0x0, 0x0, 0x0, 0x58, 0x27, 0x4d, 0x35, 0x1f, 0x2d, 0x3c, 0xfd, 0xd2, 0x2, 0x96, 0x49, +]; #[test] fn encrypt_message_crypts_data() { @@ -41,7 +37,7 @@ fn encrypt_message_crypts_data() { SecurityBuffer::Token(token.as_mut_slice()), SecurityBuffer::Data(data.as_mut_slice()), ]; - let expected = &*ENCRYPTED_TEST_DATA; + let expected = &ENCRYPTED_TEST_DATA; let result = context .encrypt_message(EncryptionFlags::empty(), &mut buffers, 0) @@ -49,7 +45,7 @@ fn encrypt_message_crypts_data() { let output = SecurityBuffer::find_buffer(&buffers, SecurityBufferType::Data).unwrap(); assert_eq!(result, SecurityStatus::Ok); - assert_eq!(expected.as_slice(), output.data()); + assert_eq!(expected, output.data()); } #[test] @@ -64,7 +60,7 @@ fn encrypt_message_correct_computes_digest() { SecurityBuffer::Token(token.as_mut_slice()), SecurityBuffer::Data(data.as_mut_slice()), ]; - let expected = &*DIGEST_FOR_TEST_DATA; + let expected = &DIGEST_FOR_TEST_DATA; let result = context .encrypt_message(EncryptionFlags::empty(), &mut buffers, TEST_SEQ_NUM) @@ -72,7 +68,7 @@ fn encrypt_message_correct_computes_digest() { let signature = SecurityBuffer::find_buffer(&buffers, SecurityBufferType::Token).unwrap(); assert_eq!(result, SecurityStatus::Ok); - assert_eq!(expected.as_slice(), &signature.data()[4..12]); + assert_eq!(expected, &signature.data()[4..12]); } #[test] @@ -111,7 +107,7 @@ fn decrypt_message_decrypts_data() { SecurityBuffer::Data(&mut encrypted_test_data), SecurityBuffer::Token(&mut signature_test_data), ]; - let expected = &*TEST_DATA; + let expected = TEST_DATA; context.decrypt_message(&mut buffers, TEST_SEQ_NUM).unwrap(); let data = SecurityBuffer::find_buffer(&buffers, SecurityBufferType::Data).unwrap(); diff --git a/tests/common.rs b/tests/common.rs index 69ccefcc..1cc495c8 100644 --- a/tests/common.rs +++ b/tests/common.rs @@ -12,7 +12,8 @@ pub static CREDENTIALS: LazyLock = LazyLock::new(|| AuthIdentity { username: Username::new("Username", Some("Domain")).unwrap(), password: String::from("Password").into(), }); -static MESSAGE_TO_CLIENT: LazyLock> = LazyLock::new(|| b"Hello, client!".to_vec()); + +const MESSAGE_TO_CLIENT: &'static [u8] = b"Hello, client!"; pub struct CredentialsProxyImpl<'a> { credentials: &'a AuthIdentity, @@ -182,11 +183,11 @@ pub fn check_messages_encryption(client: &mut impl Sspi, server: &mut impl Sspi) SecurityBuffer::Data(data.as_mut_slice()), ]; server.encrypt_message(EncryptionFlags::empty(), &mut messages, sequence_number)?; - assert_ne!(*MESSAGE_TO_CLIENT, messages[1].data()); + assert_ne!(MESSAGE_TO_CLIENT, messages[1].data()); println!( "Message to client: {:x?}, encrypted message: {:x?}, token: {:x?}", - *MESSAGE_TO_CLIENT, + MESSAGE_TO_CLIENT, messages[0].data(), messages[1].data() ); @@ -200,7 +201,7 @@ pub fn check_messages_encryption(client: &mut impl Sspi, server: &mut impl Sspi) client.decrypt_message(&mut messages, sequence_number)?; - assert_eq!(*MESSAGE_TO_CLIENT, messages[0].data()); + assert_eq!(MESSAGE_TO_CLIENT, messages[0].data()); Ok(()) }