Skip to content

Commit

Permalink
Merge pull request #159 from russellbanks/windows-sys
Browse files Browse the repository at this point in the history
Migrate to windows-sys from winapi
  • Loading branch information
brotskydotcom authored Jan 1, 2024
2 parents 19625f8 + 1ca361f commit fbeb36b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ platform-freebsd = ["linux-secret-service"]
platform-openbsd = ["linux-secret-service"]
platform-macos = ["security-framework"]
platform-ios = ["security-framework"]
platform-windows = ["winapi", "byteorder"]
platform-windows = ["windows-sys", "byteorder"]
linux-secret-service = ["linux-secret-service-rt-async-io-crypto-rust"]
linux-secret-service-rt-async-io-crypto-rust = ["secret-service/rt-async-io-crypto-rust"]
linux-secret-service-rt-tokio-crypto-rust = ["secret-service/rt-tokio-crypto-rust"]
Expand Down Expand Up @@ -50,7 +50,7 @@ secret-service = { version = "3", optional = true }

[target.'cfg(target_os = "windows")'.dependencies]
byteorder = { version = "1.2", optional = true }
winapi = { version = "0.3", features = ["wincred", "winerror", "errhandlingapi", "minwindef"], optional = true }
windows-sys = { version = "0.52", features = ["Win32_Foundation", "Win32_Security_Credentials"], optional = true }

[[example]]
name = "iostest"
Expand Down
28 changes: 13 additions & 15 deletions src/windows.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*!
# Windows Crendential Manager credential store
# Windows Credential Manager credential store
This module uses Windows Generic credentials to store entries.
These are identified by a single string (called their _target name_).
Expand All @@ -25,16 +25,14 @@ use byteorder::{ByteOrder, LittleEndian};
use std::iter::once;
use std::mem::MaybeUninit;
use std::str;
use winapi::shared::minwindef::{DWORD, FILETIME};
use winapi::shared::winerror::{
ERROR_BAD_USERNAME, ERROR_INVALID_FLAGS, ERROR_INVALID_PARAMETER, ERROR_NOT_FOUND,
ERROR_NO_SUCH_LOGON_SESSION,
use windows_sys::Win32::Foundation::{
GetLastError, ERROR_BAD_USERNAME, ERROR_INVALID_FLAGS, ERROR_INVALID_PARAMETER,
ERROR_NOT_FOUND, ERROR_NO_SUCH_LOGON_SESSION, FILETIME,
};
use winapi::um::errhandlingapi::GetLastError;
use winapi::um::wincred::{
CredDeleteW, CredFree, CredReadW, CredWriteW, CREDENTIALW, CRED_MAX_CREDENTIAL_BLOB_SIZE,
CRED_MAX_GENERIC_TARGET_NAME_LENGTH, CRED_MAX_STRING_LENGTH, CRED_MAX_USERNAME_LENGTH,
CRED_PERSIST_ENTERPRISE, CRED_TYPE_GENERIC, PCREDENTIALW, PCREDENTIAL_ATTRIBUTEW,
use windows_sys::Win32::Security::Credentials::{
CredDeleteW, CredFree, CredReadW, CredWriteW, CREDENTIALW, CREDENTIAL_ATTRIBUTEW, CRED_FLAGS,
CRED_MAX_CREDENTIAL_BLOB_SIZE, CRED_MAX_GENERIC_TARGET_NAME_LENGTH, CRED_MAX_STRING_LENGTH,
CRED_MAX_USERNAME_LENGTH, CRED_PERSIST_ENTERPRISE, CRED_TYPE_GENERIC,
};

use super::credential::{Credential, CredentialApi, CredentialBuilder, CredentialBuilderApi};
Expand Down Expand Up @@ -77,7 +75,7 @@ impl CredentialApi for WinCredential {
let mut blob = vec![0; blob_u16.len() * 2];
LittleEndian::write_u16_into(&blob_u16, &mut blob);
let blob_len = blob.len() as u32;
let flags = 0;
let flags = CRED_FLAGS::default();
let cred_type = CRED_TYPE_GENERIC;
let persist = CRED_PERSIST_ENTERPRISE;
// Ignored by CredWriteW
Expand All @@ -86,7 +84,7 @@ impl CredentialApi for WinCredential {
dwHighDateTime: 0,
};
let attribute_count = 0;
let attributes: PCREDENTIAL_ATTRIBUTEW = std::ptr::null_mut();
let attributes: *mut CREDENTIAL_ATTRIBUTEW = std::ptr::null_mut();
let mut credential = CREDENTIALW {
Flags: flags,
Type: cred_type,
Expand All @@ -102,7 +100,7 @@ impl CredentialApi for WinCredential {
UserName: username.as_mut_ptr(),
};
// raw pointer to credential, is coerced from &mut
let p_credential: PCREDENTIALW = &mut credential;
let p_credential: *const CREDENTIALW = &mut credential;
// Call windows API
match unsafe { CredWriteW(p_credential, 0) } {
0 => Err(decode_error()),
Expand Down Expand Up @@ -387,7 +385,7 @@ pub fn decode_error() -> ErrorCode {
}
}

fn wrap(code: DWORD) -> Box<dyn std::error::Error + Send + Sync> {
fn wrap(code: u32) -> Box<dyn std::error::Error + Send + Sync> {
Box::new(Error(code))
}

Expand Down Expand Up @@ -419,7 +417,7 @@ mod tests {
dwHighDateTime: 0,
};
let attribute_count = 0;
let attributes: PCREDENTIAL_ATTRIBUTEW = std::ptr::null_mut();
let attributes: *mut CREDENTIAL_ATTRIBUTEW = std::ptr::null_mut();
CREDENTIALW {
Flags: 0,
Type: CRED_TYPE_GENERIC,
Expand Down

0 comments on commit fbeb36b

Please sign in to comment.