Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TSSSP module #95

Merged
merged 28 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
0178f70
sspi: ffi: handle credssp credentials
RRRadicalEdward Dec 16, 2022
989dc1f
sspi:
RRRadicalEdward Dec 16, 2022
4244da7
sspi: sspi_cred_ssp: implement CredSspState::NegoToken state
RRRadicalEdward Dec 16, 2022
759941a
sspi: sspi credssp: add tls encryption/decryption for the TsRequests
RRRadicalEdward Dec 17, 2022
3d618ce
sspi: ntlm: add more debug logs
RRRadicalEdward Dec 19, 2022
dc0ac6c
ffi: QueryContextAttributesW: add support of the following attributes:
RRRadicalEdward Dec 27, 2022
22233e3
QueryContextAttributes: implement SECPKG_ATTR_CERT_TRUST_STATUS and S…
RRRadicalEdward Jan 7, 2023
9e22b5c
tsspi finaly works under the mstsx
RRRadicalEdward Jan 13, 2023
3f51b3d
sspi: add doc comments, remove some (not all) debug logs
RRRadicalEdward Jan 13, 2023
d47813c
sspi: credssp: refactoring
RRRadicalEdward Jan 13, 2023
1b1ef64
sspi: sspi_cred_ssp: refactoring
RRRadicalEdward Jan 15, 2023
4e30a5c
ffi: refactoring
RRRadicalEdward Jan 15, 2023
129c0f7
sspi: add and implement Sspi::query_context_stream_sizes method;
RRRadicalEdward Jan 16, 2023
fc713c4
ffi: refactor AcquireCredentialsHandleW function: remove hardcoded cr…
RRRadicalEdward Jan 18, 2023
be009c2
ffi: refactoring
RRRadicalEdward Jan 19, 2023
af830bf
Merge branch 'master' into tsssp; fixed merge conflicts
RRRadicalEdward Jan 19, 2023
80e6a29
ffi: fix compilation on Linux
RRRadicalEdward Jan 19, 2023
a1e7f0b
sspi-rs & ffi: general refactoring
RRRadicalEdward Jan 20, 2023
f396fd3
sspi: SspiCredSsp: TlsConnection: fixed buffer length on TLS decrypt
RRRadicalEdward Jan 23, 2023
2cf1562
sspi & ffi: add doc comments about creds and sspi tables; small refac…
RRRadicalEdward Jan 25, 2023
d85b892
sspi: SspiCredSsp: improve conditional compilation
RRRadicalEdward Jan 25, 2023
70f5bba
sspi: add tsssp feature. improve conditional compilation
RRRadicalEdward Jan 25, 2023
a511e6d
ffi: add tsssp feature. sspi: improve tsssp feature
RRRadicalEdward Jan 25, 2023
c308584
run cargo fmt
RRRadicalEdward Jan 26, 2023
a9b950d
ffi: small refactoring
RRRadicalEdward Jan 26, 2023
629d553
ffi: add doc comment and link for the SEC_WINNT_AUTH_IDENTITY_EX2
RRRadicalEdward Jan 27, 2023
b605f8d
ci: fix Lints and Tests steps
CBenoit Jan 27, 2023
4f8c1bd
ffi: fix sec_pkj_info tests
RRRadicalEdward Jan 30, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ trust-dns-resolver = { version = "0.21.2", optional = true }
portpicker = { version = "0.1.1", optional = true }
num-bigint-dig = "0.8.1"
tracing = { version = "0.1.37" }

[target.'cfg(not(wasm))'.dependencies]
rustls = { version = "0.20.7", features = ["dangerous_configuration"] }
CBenoit marked this conversation as resolved.
Show resolved Hide resolved

[target.'cfg(windows)'.dependencies]
Expand Down
9 changes: 6 additions & 3 deletions ffi/src/sec_winnt_auth_identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ pub struct CredSspCred {
}

#[cfg(not(target_os = "windows"))]
pub unsafe fn unpack_sec_winnt_auth_identity_ex2_a(_p_auth_data: *const c_void) -> Result<AuthIdentityBuffers> {
pub fn unpack_sec_winnt_auth_identity_ex2_a(_p_auth_data: *const c_void) -> Result<AuthIdentityBuffers> {
Err(Error::new(
ErrorKind::UnsupportedFunction,
"SecWinntIdentityEx2 is not supported on non Windows systems".into(),
Expand All @@ -133,17 +133,20 @@ pub unsafe fn unpack_sec_winnt_auth_identity_ex2_a(_p_auth_data: *const c_void)

#[cfg(target_os = "windows")]
unsafe fn get_sec_winnt_auth_identity_ex2_a_size(p_auth_data: *const c_void) -> u32 {
// username length is placed after the first 8 bytes
let user_len_ptr = (p_auth_data as *const u16).add(4);
let user_buffer_len = *user_len_ptr as u32;

// domain length is placed after 16 bytes from the username length
let domain_len_ptr = user_len_ptr.add(8);
let domain_buffer_len = *domain_len_ptr as u32;

// packet credentials length is placed after 16 bytes from the domain length
let creds_len_ptr = domain_len_ptr.add(8);
let creds_buffer_len = *creds_len_ptr as u32;

// header size + buffers size
64 /* size of SEC_WINNT_AUTH_IDENTITY_EX2 */ + user_buffer_len + domain_buffer_len + creds_buffer_len
64 /* size of the SEC_WINNT_AUTH_IDENTITY_EX2 */ + user_buffer_len + domain_buffer_len + creds_buffer_len
RRRadicalEdward marked this conversation as resolved.
Show resolved Hide resolved
}

#[cfg(target_os = "windows")]
Expand Down Expand Up @@ -227,7 +230,7 @@ pub unsafe fn unpack_sec_winnt_auth_identity_ex2_a(p_auth_data: *const c_void) -
}

#[cfg(not(target_os = "windows"))]
pub unsafe fn unpack_sec_winnt_auth_identity_ex2_w(_p_auth_data: *const c_void) -> Result<AuthIdentityBuffers> {
pub fn unpack_sec_winnt_auth_identity_ex2_w(_p_auth_data: *const c_void) -> Result<AuthIdentityBuffers> {
Err(Error::new(
ErrorKind::UnsupportedFunction,
"SecWinntIdentityEx2 is not supported on non Windows systems".into(),
Expand Down
4 changes: 4 additions & 0 deletions ffi/src/security_tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ pub struct SecurityFunctionTableA {
pub VerifySignature: VerifySignatureFn,
pub FreeContextBuffer: FreeContextBufferFn,
pub QuerySecurityPackageInfoA: QuerySecurityPackageInfoFnA,
// In the Windows sspicli.dll, the `Reserved3` field is used as EncryptFunction
pub Reserved3: EncryptMessageFn,
// In the Windows sspicli.dll, the `Reserved4` field is used as DecryptFunction
pub Reserved4: DecryptMessageFn,
pub ExportSecurityContext: ExportSecurityContextFn,
pub ImportSecurityContextA: ImportSecurityContextFnA,
Expand Down Expand Up @@ -94,7 +96,9 @@ pub struct SecurityFunctionTableW {
pub VerifySignature: VerifySignatureFn,
pub FreeContextBuffer: FreeContextBufferFn,
pub QuerySecurityPackageInfoW: QuerySecurityPackageInfoFnW,
// In the Windows sspicli.dll, the `Reserved3` field is used as EncryptFunction
pub Reserved3: EncryptMessageFn,
// In the Windows sspicli.dll, the `Reserved4` field is used as DecryptFunction
pub Reserved4: DecryptMessageFn,
pub ExportSecurityContext: ExportSecurityContextFn,
pub ImportSecurityContextW: ImportSecurityContextFnW,
Expand Down
120 changes: 53 additions & 67 deletions src/credssp/sspi_cred_ssp/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[cfg(not(target = "wasm32-unknown-unknown"))]
CBenoit marked this conversation as resolved.
Show resolved Hide resolved
mod tls_connection;

use std::sync::Arc;
Expand All @@ -8,7 +9,8 @@ use rand::rngs::OsRng;
use rand::Rng;
use rustls::{ClientConfig, ClientConnection, Connection, ServerConfig, ServerConnection};

use self::tls_connection::{TlsConnection, TLS_PACKET_HEADER_LEN};
#[cfg(not(target = "wasm32-unknown-unknown"))]
use self::tls_connection::{danger, TlsConnection, TLS_PACKET_HEADER_LEN};
use super::ts_request::NONCE_SIZE;
use super::{CredSspContext, CredSspMode, EndpointType, SspiContext, TsRequest};
use crate::builders::EmptyInitializeSecurityContext;
Expand All @@ -33,34 +35,6 @@ lazy_static! {
};
}

// [Processing Events and Sequencing Rules](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-cssp/385a7489-d46b-464c-b224-f7340e308a5c)
// The CredSSP server does not request the client's X.509 certificate (thus far, the client is anonymous).
// Also, the CredSSP Protocol does not require the client to have a commonly trusted certification authority root with the CredSSP server.
//
// This configuration just accepts any certificate
pub mod danger {
use std::time::SystemTime;

use rustls::client::{ServerCertVerified, ServerCertVerifier};
use rustls::{Certificate, Error, ServerName};

pub struct NoCertificateVerification;

impl ServerCertVerifier for NoCertificateVerification {
fn verify_server_cert(
&self,
_end_entity: &Certificate,
_intermediates: &[Certificate],
_server_name: &ServerName,
_scts: &mut dyn Iterator<Item = &[u8]>,
_ocsp_response: &[u8],
_now: SystemTime,
) -> Result<ServerCertVerified, Error> {
Ok(rustls::client::ServerCertVerified::assertion())
}
}
}

#[derive(Debug, Clone)]
enum CredSspState {
Tls,
Expand All @@ -80,50 +54,62 @@ pub struct SspiCredSsp {

impl SspiCredSsp {
pub fn new_client(sspi_context: SspiContext) -> Result<Self> {
// "stub_string" - we don't check the server's certificate validity so we can use any server name
let example_com = "stub_string".try_into().unwrap();
let client_config = ClientConfig::builder()
.with_safe_defaults()
.with_custom_certificate_verifier(Arc::new(danger::NoCertificateVerification))
.with_no_client_auth();
let config = Arc::new(client_config);

Ok(Self {
state: CredSspState::Tls,
cred_ssp_context: Box::new(CredSspContext::new(sspi_context)),
auth_identity: None,
tls_connection: TlsConnection::Rustls(Connection::Client(
ClientConnection::new(config, example_com)
.map_err(|err| Error::new(ErrorKind::InternalError, err.to_string()))?,
)),
nonce: Some(OsRng::default().gen::<[u8; NONCE_SIZE]>()),
})
cfg_if::cfg_if! {
if #[cfg(not(target = "wasm32-unknown-unknown"))] {
// "stub_string" - we don't check the server's certificate validity so we can use any server name
let example_com = "stub_string".try_into().unwrap();
let client_config = ClientConfig::builder()
.with_safe_defaults()
.with_custom_certificate_verifier(Arc::new(danger::NoCertificateVerification))
.with_no_client_auth();
let config = Arc::new(client_config);

Ok(Self {
state: CredSspState::Tls,
cred_ssp_context: Box::new(CredSspContext::new(sspi_context)),
auth_identity: None,
tls_connection: TlsConnection::Rustls(Connection::Client(
ClientConnection::new(config, example_com)
.map_err(|err| Error::new(ErrorKind::InternalError, err.to_string()))?,
)),
nonce: Some(OsRng::default().gen::<[u8; NONCE_SIZE]>()),
})
} else {
Err(Error::new(ErrorKind::Unsupported, "SspiCredSsp is not supported on wasm".into()))
}
}
}

/// * `sspi_context` is a security package that will be used for authorization
/// * `certificates` is a vector of DER-encoded X.509 certificates
/// * `private_key` is a raw private key. it is DER-encoded ASN.1 in either PKCS#8 or PKCS#1 format.
pub fn new_server(sspi_context: SspiContext, certificates: Vec<Vec<u8>>, private_key: Vec<u8>) -> Result<Self> {
let server_config = ServerConfig::builder()
.with_safe_defaults()
.with_no_client_auth()
.with_single_cert(
certificates.into_iter().map(rustls::Certificate).collect(),
rustls::PrivateKey(private_key),
)
.map_err(|err| Error::new(ErrorKind::InternalError, err.to_string()))?;
let config = Arc::new(server_config);

Ok(Self {
state: CredSspState::Tls,
cred_ssp_context: Box::new(CredSspContext::new(sspi_context)),
auth_identity: None,
tls_connection: TlsConnection::Rustls(Connection::Server(
ServerConnection::new(config).map_err(|err| Error::new(ErrorKind::InternalError, err.to_string()))?,
)),
// nonce for the server will be in the incoming TsRequest
nonce: None,
})
cfg_if::cfg_if! {
if #[cfg(not(target = "wasm32-unknown-unknown"))] {
let server_config = ServerConfig::builder()
.with_safe_defaults()
.with_no_client_auth()
.with_single_cert(
certificates.into_iter().map(rustls::Certificate).collect(),
rustls::PrivateKey(private_key),
)
.map_err(|err| Error::new(ErrorKind::InternalError, err.to_string()))?;
let config = Arc::new(server_config);

Ok(Self {
state: CredSspState::Tls,
cred_ssp_context: Box::new(CredSspContext::new(sspi_context)),
auth_identity: None,
tls_connection: TlsConnection::Rustls(Connection::Server(
ServerConnection::new(config).map_err(|err| Error::new(ErrorKind::InternalError, err.to_string()))?,
)),
// nonce for the server will be in the incoming TsRequest
nonce: None,
})
} else {
Err(Error::new(ErrorKind::Unsupported, "SspiCredSsp is not supported on wasm".into()))
}
}
}

fn raw_peer_public_key(&mut self) -> Result<Vec<u8>> {
Expand Down
28 changes: 28 additions & 0 deletions src/credssp/sspi_cred_ssp/tls_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,34 @@ pub const TLS_PACKET_HEADER_LEN: usize = 1 /* ContentType */ + 2 /* ProtocolVers
// The block size of the AES cipher is 128 bits
const AES_BLOCK_SIZE: usize = 16;

// [Processing Events and Sequencing Rules](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-cssp/385a7489-d46b-464c-b224-f7340e308a5c)
// The CredSSP server does not request the client's X.509 certificate (thus far, the client is anonymous).
// Also, the CredSSP Protocol does not require the client to have a commonly trusted certification authority root with the CredSSP server.
//
// This configuration just accepts any certificate
pub mod danger {
use std::time::SystemTime;

use rustls::client::{ServerCertVerified, ServerCertVerifier};
use rustls::{Certificate, Error, ServerName};

pub struct NoCertificateVerification;

impl ServerCertVerifier for NoCertificateVerification {
fn verify_server_cert(
&self,
_end_entity: &Certificate,
_intermediates: &[Certificate],
_server_name: &ServerName,
_scts: &mut dyn Iterator<Item = &[u8]>,
_ocsp_response: &[u8],
_now: SystemTime,
) -> Result<ServerCertVerified, Error> {
Ok(rustls::client::ServerCertVerified::assertion())
}
}
}

#[derive(Debug)]
pub enum TlsConnection {
Rustls(Connection),
Expand Down