Skip to content

Commit ed723ac

Browse files
committed
Add compatibility shim code for windows-sys type changes
1 parent 740bd23 commit ed723ac

File tree

1 file changed

+32
-11
lines changed
  • rustls-platform-verifier/src/verification

1 file changed

+32
-11
lines changed

rustls-platform-verifier/src/verification/windows.rs

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use rustls::{
3535
};
3636
use windows_sys::Win32::{
3737
Foundation::{
38-
BOOL, CERT_E_CN_NO_MATCH, CERT_E_EXPIRED, CERT_E_INVALID_NAME, CERT_E_UNTRUSTEDROOT,
38+
CERT_E_CN_NO_MATCH, CERT_E_EXPIRED, CERT_E_INVALID_NAME, CERT_E_UNTRUSTEDROOT,
3939
CERT_E_WRONG_USAGE, CRYPT_E_REVOKED, FILETIME, TRUE,
4040
},
4141
Security::Cryptography::{
@@ -66,7 +66,7 @@ struct CERT_CHAIN_PARA {
6666
pub RequestedUsage: CERT_USAGE_MATCH,
6767
pub RequestedIssuancePolicy: CERT_USAGE_MATCH,
6868
pub dwUrlRetrievalTimeout: u32,
69-
pub fCheckRevocationFreshnessTime: BOOL,
69+
pub fCheckRevocationFreshnessTime: i32, // BOOL
7070
pub dwRevocationFreshnessTime: u32,
7171
pub pftCacheResync: *mut FILETIME,
7272
// XXX: `pStrongSignPara` and `dwStrongSignFlags` might or might not be defined on the current system. It started
@@ -213,6 +213,30 @@ impl Drop for Certificate {
213213
}
214214
}
215215

216+
/// An abstraction trait over the different ways various `windows-sys` versions represent
217+
/// the type of `HCERTCHAINENGINE`.
218+
trait EnginePtr: Sized {
219+
const NULL: Self;
220+
fn from_raw(val: NonNull<c_void>) -> Self;
221+
}
222+
223+
// `windows-sys` >= 0.60
224+
impl EnginePtr for *mut c_void {
225+
const NULL: Self = ptr::null_mut();
226+
fn from_raw(val: NonNull<c_void>) -> Self {
227+
val.as_ptr()
228+
}
229+
}
230+
231+
// `windows-sys` 0.52-0.59
232+
impl EnginePtr for isize {
233+
const NULL: Self = 0;
234+
#[allow(clippy::as_conversions)]
235+
fn from_raw(val: NonNull<c_void>) -> Self {
236+
val.as_ptr() as isize
237+
}
238+
}
239+
216240
#[derive(Debug)]
217241
struct CertEngine {
218242
inner: NonNull<c_void>, // HCERTENGINECONTEXT
@@ -230,7 +254,7 @@ impl CertEngine {
230254
let mut config = CERT_CHAIN_ENGINE_CONFIG::zeroed_with_size();
231255
config.hExclusiveRoot = exclusive_store.inner.as_ptr();
232256

233-
let mut engine = 0;
257+
let mut engine = EnginePtr::NULL;
234258
// SAFETY: `engine` is valid to be written to and the config is valid to be read.
235259
let res = unsafe { CertCreateCertificateChainEngine(&config, &mut engine) };
236260

@@ -264,7 +288,7 @@ impl CertEngine {
264288
config.dwFlags = CERT_CHAIN_CACHE_ONLY_URL_RETRIEVAL | CERT_CHAIN_ENABLE_CACHE_AUTO_UPDATE;
265289
config.hExclusiveRoot = root_store.inner.as_ptr();
266290

267-
let mut engine = 0;
291+
let mut engine = EnginePtr::NULL;
268292
// SAFETY: `engine` is valid to be written to and the config is valid to be read.
269293
let res = unsafe { CertCreateCertificateChainEngine(&config, &mut engine) };
270294

@@ -276,17 +300,12 @@ impl CertEngine {
276300

277301
Ok(Self { inner: engine })
278302
}
279-
280-
#[allow(clippy::as_conversions)]
281-
fn as_ptr(&self) -> isize {
282-
self.inner.as_ptr() as isize
283-
}
284303
}
285304

286305
impl Drop for CertEngine {
287306
fn drop(&mut self) {
288307
// SAFETY: The engine pointer is guaranteed to be non-null.
289-
unsafe { CertFreeCertificateChainEngine(self.as_ptr()) };
308+
unsafe { CertFreeCertificateChainEngine(EnginePtr::from_raw(self.inner)) };
290309
}
291310
}
292311

@@ -452,7 +471,9 @@ impl CertificateStore {
452471
let parameters = NonNull::from(&parameters).cast().as_ptr();
453472

454473
CertGetCertificateChain(
455-
engine.map(CertEngine::as_ptr).unwrap_or(0),
474+
engine
475+
.map(|eng| EnginePtr::from_raw(eng.inner))
476+
.unwrap_or(EnginePtr::NULL),
456477
certificate.inner.as_ptr(),
457478
&time,
458479
self.inner.as_ptr(),

0 commit comments

Comments
 (0)