Skip to content

Commit

Permalink
refactor: fix cargo clippy warnings;
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBestTvarynka committed Oct 9, 2024
1 parent 3d30a6d commit a4e2f21
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 42 deletions.
8 changes: 1 addition & 7 deletions src/credssp/mod.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
3 changes: 3 additions & 0 deletions src/credssp/ts_request/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ static AUTH_IDENTITY_ONE_SYMBOL_USER_AND_PASSWORD: LazyLock<CredentialsBuffers>
.into(),
)
});

static AUTH_IDENTITY_STRONG_USERNAME_AND_PASSWORD: LazyLock<CredentialsBuffers> = LazyLock::new(|| {
CredentialsBuffers::AuthIdentity(
AuthIdentity {
Expand All @@ -217,6 +218,7 @@ static AUTH_IDENTITY_STRONG_USERNAME_AND_PASSWORD: LazyLock<CredentialsBuffers>
.into(),
)
});

static AUTH_IDENTITY_SIMPLE_WITH_USERNAME_AND_DOMAIN_AND_PASSWORD: LazyLock<CredentialsBuffers> = LazyLock::new(|| {
CredentialsBuffers::AuthIdentity(
AuthIdentity {
Expand All @@ -226,6 +228,7 @@ static AUTH_IDENTITY_SIMPLE_WITH_USERNAME_AND_DOMAIN_AND_PASSWORD: LazyLock<Cred
.into(),
)
});

static AUTH_IDENTITY_WITH_RESTRICTED_ADMIN_MODE_REQUIRED: LazyLock<CredentialsBuffers> = LazyLock::new(|| {
CredentialsBuffers::AuthIdentity(
AuthIdentity {
Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down
10 changes: 8 additions & 2 deletions src/ntlm/messages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -71,6 +70,13 @@ impl MessageFields {

Ok(())
}

fn read_buffer_from_cursor<T>(&mut self, cursor: &mut io::Cursor<T>) -> io::Result<()>
where
io::Cursor<T>: io::Read + io::Seek,
{
self.read_buffer_from(cursor)
}
}

fn try_read_version(flags: NegotiateFlags, mut cursor: impl io::Read) -> io::Result<Option<[u8; NTLM_VERSION_SIZE]>> {
Expand Down
14 changes: 7 additions & 7 deletions src/ntlm/messages/server/authenticate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ fn read_header(mut buffer: impl io::Read) -> crate::Result<(AuthenticateMessageF
fn read_payload<T>(
negotiate_flags: NegotiateFlags,
message_fields: &mut AuthenticateMessageFields,
mut buffer: &mut io::Cursor<T>,
buffer: &mut io::Cursor<T>,
) -> crate::Result<Option<Mic>>
where
io::Cursor<T>: io::Read + io::Seek,
Expand All @@ -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)
}
Expand Down
2 changes: 2 additions & 0 deletions src/ntlm/messages/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -117,6 +118,7 @@ pub static LOCAL_CHALLENGE_MESSAGE: LazyLock<[u8; LOCAL_CHALLENGE_MESSAGE_SIZE]>

result
});

pub static TEST_CREDENTIALS: LazyLock<AuthIdentityBuffers> = LazyLock::new(|| {
AuthIdentity {
username: Username::new("User", Some("Domain")).unwrap(),
Expand Down
36 changes: 16 additions & 20 deletions src/ntlm/test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::sync::LazyLock;

use crate::crypto::{Rc4, HASH_SIZE};
use crate::ntlm::messages::test::TEST_CREDENTIALS;
use crate::ntlm::{
Expand All @@ -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<Vec<u8>> = LazyLock::new(|| b"Hello, World!!!".to_vec());
pub static ENCRYPTED_TEST_DATA: LazyLock<Vec<u8>> = 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<Vec<u8>> =
LazyLock::new(|| vec![0x58, 0x27, 0x4d, 0x35, 0x1f, 0x2d, 0x3c, 0xfd]);
pub static SIGNATURE_FOR_TEST_DATA: LazyLock<Vec<u8>> = 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() {
Expand All @@ -41,15 +37,15 @@ 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)
.unwrap();
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]
Expand All @@ -64,15 +60,15 @@ 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)
.unwrap();
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]
Expand Down Expand Up @@ -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();
Expand Down
9 changes: 5 additions & 4 deletions tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ pub static CREDENTIALS: LazyLock<AuthIdentity> = LazyLock::new(|| AuthIdentity {
username: Username::new("Username", Some("Domain")).unwrap(),
password: String::from("Password").into(),
});
static MESSAGE_TO_CLIENT: LazyLock<Vec<u8>> = LazyLock::new(|| b"Hello, client!".to_vec());

const MESSAGE_TO_CLIENT: &'static [u8] = b"Hello, client!";

pub struct CredentialsProxyImpl<'a> {
credentials: &'a AuthIdentity,
Expand Down Expand Up @@ -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()
);
Expand All @@ -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(())
}

0 comments on commit a4e2f21

Please sign in to comment.