@@ -4,7 +4,7 @@ use std::{os::raw::c_void, ptr};
44
55use windows_sys:: Win32 :: Security :: Cryptography :: * ;
66
7- use crate :: { cert:: CertContext , error:: CngError , Result } ;
7+ use crate :: { Result , cert:: CertContext , error:: CngError } ;
88
99const 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