Skip to content

Commit f4a4f27

Browse files
committed
Updated to 2024 edition.
1 parent d7ab425 commit f4a4f27

File tree

3 files changed

+53
-44
lines changed

3 files changed

+53
-44
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ repository = "https://github.com/rustls/rustls-cng"
88
documentation = "https://docs.rs/rustls-cng"
99
readme = "README.md"
1010
keywords = ["cng", "tls", "rustls", "windows"]
11-
edition = "2021"
11+
edition = "2024"
1212
publish = false
1313

1414
[package.metadata.docs.rs]
@@ -19,7 +19,7 @@ no-default-features = true
1919
rustls = { git = "https://github.com/rustls/rustls.git", default-features = false, features = ["std"] }
2020
rustls-pki-types = "1"
2121
time = { version = "0.3.43", default-features = false, optional = true }
22-
windows-sys = { version = "0.60", features = ["Win32_Foundation", "Win32_Security_Cryptography"] }
22+
windows-sys = { version = "0.61", features = ["Win32_Foundation", "Win32_Security_Cryptography"] }
2323

2424
[dev-dependencies]
2525
anyhow = "1"

examples/server.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ use std::{
77

88
use clap::Parser;
99
use rustls::{
10+
RootCertStore, ServerConfig, ServerConnection, Stream,
1011
server::{ClientHello, ResolvesServerCert, WebPkiClientVerifier},
1112
sign::CertifiedKey,
12-
RootCertStore, ServerConfig, ServerConnection, Stream,
1313
};
1414

1515
use rustls_cng::{
@@ -98,10 +98,7 @@ fn handle_connection(mut stream: TcpStream, config: Arc<ServerConfig>) -> anyhow
9898
tls_stream.conn.negotiated_cipher_suite()
9999
);
100100
println!("SNI host name: {:?}", tls_stream.conn.server_name());
101-
println!(
102-
"Peer certificates: {:?}",
103-
tls_stream.conn.peer_certificates().map(|c| c.len())
104-
);
101+
println!("Peer identity: {:?}", tls_stream.conn.peer_identity());
105102

106103
let mut buf = [0u8; 4];
107104
tls_stream.read_exact(&mut buf)?;

src/store.rs

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{os::raw::c_void, ptr};
44

55
use windows_sys::Win32::Security::Cryptography::*;
66

7-
use crate::{cert::CertContext, error::CngError, Result};
7+
use crate::{Result, cert::CertContext, error::CngError};
88

99
const MY_ENCODING_TYPE: CERT_QUERY_ENCODING_TYPE = PKCS_7_ASN_ENCODING | X509_ASN_ENCODING;
1010

@@ -184,16 +184,25 @@ impl CertStore {
184184
) -> Result<Vec<CertContext>> {
185185
let mut certs = Vec::new();
186186

187-
let mut cert: *mut CERT_CONTEXT = ptr::null_mut();
188-
189-
loop {
190-
cert = CertFindCertificateInStore(self.0, MY_ENCODING_TYPE, 0, flags, find_param, cert);
191-
if cert.is_null() {
192-
break;
193-
} else {
194-
// increase refcount because it will be released by next call to CertFindCertificateInStore
195-
let cert = CertDuplicateCertificateContext(cert);
196-
certs.push(CertContext::new_owned(cert))
187+
unsafe {
188+
let mut cert: *mut CERT_CONTEXT = ptr::null_mut();
189+
190+
loop {
191+
cert = CertFindCertificateInStore(
192+
self.0,
193+
MY_ENCODING_TYPE,
194+
0,
195+
flags,
196+
find_param,
197+
cert,
198+
);
199+
if cert.is_null() {
200+
break;
201+
} else {
202+
// increase refcount because it will be released by next call to CertFindCertificateInStore
203+
let cert = CertDuplicateCertificateContext(cert);
204+
certs.push(CertContext::new_owned(cert))
205+
}
197206
}
198207
}
199208
Ok(certs)
@@ -204,34 +213,37 @@ impl CertStore {
204213
find_param: *const c_void,
205214
) -> Result<Vec<CertContext>> {
206215
let mut certs = Vec::new();
207-
let mut cert: *mut CERT_CONTEXT = ptr::null_mut();
208-
let hash_blob = &*(find_param as *const CRYPT_INTEGER_BLOB);
209-
let sha256_hash = std::slice::from_raw_parts(hash_blob.pbData, hash_blob.cbData as usize);
210-
loop {
211-
cert = CertFindCertificateInStore(
212-
self.0,
213-
MY_ENCODING_TYPE,
214-
0,
215-
CERT_FIND_ANY,
216-
find_param,
217-
cert,
218-
);
219-
if cert.is_null() {
220-
break;
221-
} else {
222-
let mut prop_data = [0u8; 32];
223-
let mut prop_data_len = prop_data.len() as u32;
224216

225-
if CertGetCertificateContextProperty(
217+
unsafe {
218+
let mut cert: *mut CERT_CONTEXT = ptr::null_mut();
219+
let hash_blob = &*(find_param as *const CRYPT_INTEGER_BLOB);
220+
let sha256_hash = std::slice::from_raw_parts(hash_blob.pbData, hash_blob.cbData as usize);
221+
loop {
222+
cert = CertFindCertificateInStore(
223+
self.0,
224+
MY_ENCODING_TYPE,
225+
0,
226+
CERT_FIND_ANY,
227+
find_param,
226228
cert,
227-
CERT_SHA256_HASH_PROP_ID,
228-
prop_data.as_mut_ptr() as *mut c_void,
229-
&mut prop_data_len,
230-
) != 0
231-
&& prop_data[..prop_data_len as usize] == sha256_hash[..]
232-
{
233-
let cert = CertDuplicateCertificateContext(cert);
234-
certs.push(CertContext::new_owned(cert))
229+
);
230+
if cert.is_null() {
231+
break;
232+
} else {
233+
let mut prop_data = [0u8; 32];
234+
let mut prop_data_len = prop_data.len() as u32;
235+
236+
if CertGetCertificateContextProperty(
237+
cert,
238+
CERT_SHA256_HASH_PROP_ID,
239+
prop_data.as_mut_ptr() as *mut c_void,
240+
&mut prop_data_len,
241+
) != 0
242+
&& prop_data[..prop_data_len as usize] == sha256_hash[..]
243+
{
244+
let cert = CertDuplicateCertificateContext(cert);
245+
certs.push(CertContext::new_owned(cert))
246+
}
235247
}
236248
}
237249
}

0 commit comments

Comments
 (0)