@@ -50,22 +50,23 @@ public unsafe void FindByThumbprint(byte[] thumbPrint)
5050 fixed ( byte * pThumbPrint = thumbPrint )
5151 {
5252 CRYPTOAPI_BLOB blob = new CRYPTOAPI_BLOB ( thumbPrint . Length , pThumbPrint ) ;
53- FindCore ( CertFindType . CERT_FIND_HASH , & blob ) ;
53+ FindCore < object > ( CertFindType . CERT_FIND_HASH , & blob ) ;
5454 }
5555 }
5656
5757 public unsafe void FindBySubjectName ( string subjectName )
5858 {
5959 fixed ( char * pSubjectName = subjectName )
6060 {
61- FindCore ( CertFindType . CERT_FIND_SUBJECT_STR , pSubjectName ) ;
61+ FindCore < object > ( CertFindType . CERT_FIND_SUBJECT_STR , pSubjectName ) ;
6262 }
6363 }
6464
6565 public void FindBySubjectDistinguishedName ( string subjectDistinguishedName )
6666 {
6767 FindCore (
68- delegate ( SafeCertContextHandle pCertContext )
68+ subjectDistinguishedName ,
69+ static ( subjectDistinguishedName , pCertContext ) =>
6970 {
7071 string actual = GetCertNameInfo ( pCertContext , CertNameType . CERT_NAME_RDN_TYPE , CertNameFlags . None ) ;
7172 return subjectDistinguishedName . Equals ( actual , StringComparison . OrdinalIgnoreCase ) ;
@@ -76,14 +77,15 @@ public unsafe void FindByIssuerName(string issuerName)
7677 {
7778 fixed ( char * pIssuerName = issuerName )
7879 {
79- FindCore ( CertFindType . CERT_FIND_ISSUER_STR , pIssuerName ) ;
80+ FindCore < object > ( CertFindType . CERT_FIND_ISSUER_STR , pIssuerName ) ;
8081 }
8182 }
8283
8384 public void FindByIssuerDistinguishedName ( string issuerDistinguishedName )
8485 {
8586 FindCore (
86- delegate ( SafeCertContextHandle pCertContext )
87+ issuerDistinguishedName ,
88+ static ( issuerDistinguishedName , pCertContext ) =>
8789 {
8890 string actual = GetCertNameInfo ( pCertContext , CertNameType . CERT_NAME_RDN_TYPE , CertNameFlags . CERT_NAME_ISSUER_FLAG ) ;
8991 return issuerDistinguishedName . Equals ( actual , StringComparison . OrdinalIgnoreCase ) ;
@@ -93,15 +95,16 @@ public void FindByIssuerDistinguishedName(string issuerDistinguishedName)
9395 public unsafe void FindBySerialNumber ( BigInteger hexValue , BigInteger decimalValue )
9496 {
9597 FindCore (
96- delegate ( SafeCertContextHandle pCertContext )
98+ ( hexValue , decimalValue ) ,
99+ static ( state , pCertContext ) =>
97100 {
98101 byte [ ] actual = pCertContext . CertContext ->pCertInfo ->SerialNumber . ToByteArray ( ) ;
99102 GC . KeepAlive ( pCertContext ) ;
100103
101104 // Convert to BigInteger as the comparison must not fail due to spurious leading zeros
102105 BigInteger actualAsBigInteger = PositiveBigIntegerFromByteArray ( actual ) ;
103106
104- return hexValue . Equals ( actualAsBigInteger ) || decimalValue . Equals ( actualAsBigInteger ) ;
107+ return state . hexValue . Equals ( actualAsBigInteger ) || state . decimalValue . Equals ( actualAsBigInteger ) ;
105108 } ) ;
106109 }
107110
@@ -125,19 +128,21 @@ private unsafe void FindByTime(DateTime dateTime, int compareResult)
125128 FILETIME fileTime = FILETIME . FromDateTime ( dateTime ) ;
126129
127130 FindCore (
128- delegate ( SafeCertContextHandle pCertContext )
131+ ( fileTime , compareResult ) ,
132+ static ( state , pCertContext ) =>
129133 {
130- int comparison = Interop . crypt32 . CertVerifyTimeValidity ( ref fileTime ,
134+ int comparison = Interop . crypt32 . CertVerifyTimeValidity ( ref state . fileTime ,
131135 pCertContext . CertContext ->pCertInfo ) ;
132136 GC . KeepAlive ( pCertContext ) ;
133- return comparison == compareResult ;
137+ return comparison == state . compareResult ;
134138 } ) ;
135139 }
136140
137141 public unsafe void FindByTemplateName ( string templateName )
138142 {
139143 FindCore (
140- delegate ( SafeCertContextHandle pCertContext )
144+ templateName ,
145+ static ( templateName , pCertContext ) =>
141146 {
142147 // The template name can have 2 different formats: V1 format (<= Win2K) is just a string
143148 // V2 format (XP only) can be a friendly name or an OID.
@@ -203,7 +208,8 @@ public unsafe void FindByTemplateName(string templateName)
203208 public unsafe void FindByApplicationPolicy ( string oidValue )
204209 {
205210 FindCore (
206- delegate ( SafeCertContextHandle pCertContext )
211+ oidValue ,
212+ static ( oidValue , pCertContext ) =>
207213 {
208214 int numOids ;
209215 int cbData = 0 ;
@@ -234,7 +240,8 @@ public unsafe void FindByApplicationPolicy(string oidValue)
234240 public unsafe void FindByCertificatePolicy ( string oidValue )
235241 {
236242 FindCore (
237- delegate ( SafeCertContextHandle pCertContext )
243+ oidValue ,
244+ static ( oidValue , pCertContext ) =>
238245 {
239246 CERT_INFO * pCertInfo = pCertContext . CertContext ->pCertInfo ;
240247 CERT_EXTENSION * pCertExtension = Interop . crypt32 . CertFindExtension ( Oids . CertPolicies ,
@@ -274,7 +281,8 @@ public unsafe void FindByCertificatePolicy(string oidValue)
274281 public unsafe void FindByExtension ( string oidValue )
275282 {
276283 FindCore (
277- delegate ( SafeCertContextHandle pCertContext )
284+ oidValue ,
285+ static ( oidValue , pCertContext ) =>
278286 {
279287 CERT_INFO * pCertInfo = pCertContext . CertContext ->pCertInfo ;
280288 CERT_EXTENSION * pCertExtension = Interop . crypt32 . CertFindExtension ( oidValue , pCertInfo ->cExtension , pCertInfo ->rgExtension ) ;
@@ -286,7 +294,8 @@ public unsafe void FindByExtension(string oidValue)
286294 public unsafe void FindByKeyUsage ( X509KeyUsageFlags keyUsage )
287295 {
288296 FindCore (
289- delegate ( SafeCertContextHandle pCertContext )
297+ keyUsage ,
298+ static ( keyUsage , pCertContext ) =>
290299 {
291300 CERT_INFO * pCertInfo = pCertContext . CertContext ->pCertInfo ;
292301 X509KeyUsageFlags actual ;
@@ -300,7 +309,8 @@ public unsafe void FindByKeyUsage(X509KeyUsageFlags keyUsage)
300309 public void FindBySubjectKeyIdentifier ( byte [ ] keyIdentifier )
301310 {
302311 FindCore (
303- delegate ( SafeCertContextHandle pCertContext )
312+ keyIdentifier ,
313+ static ( keyIdentifier , pCertContext ) =>
304314 {
305315 int cbData = 0 ;
306316 if ( ! Interop . crypt32 . CertGetCertificateContextProperty ( pCertContext , CertContextPropId . CERT_KEY_IDENTIFIER_PROP_ID , null , ref cbData ) )
@@ -319,12 +329,12 @@ public void Dispose()
319329 _storePal . Dispose ( ) ;
320330 }
321331
322- private unsafe void FindCore ( Func < SafeCertContextHandle , bool > filter )
332+ private unsafe void FindCore < TState > ( TState state , Func < TState , SafeCertContextHandle , bool > filter )
323333 {
324- FindCore ( CertFindType . CERT_FIND_ANY , null , filter ) ;
334+ FindCore ( CertFindType . CERT_FIND_ANY , null , state , filter ) ;
325335 }
326336
327- private unsafe void FindCore ( CertFindType dwFindType , void * pvFindPara , Func < SafeCertContextHandle , bool > ? filter = null )
337+ private unsafe void FindCore < TState > ( CertFindType dwFindType , void * pvFindPara , TState state = default ! , Func < TState , SafeCertContextHandle , bool > ? filter = null )
328338 {
329339 SafeCertStoreHandle findResults = Interop . crypt32 . CertOpenStore (
330340 CertStoreProvider . CERT_STORE_PROV_MEMORY ,
@@ -338,7 +348,7 @@ private unsafe void FindCore(CertFindType dwFindType, void* pvFindPara, Func<Saf
338348 SafeCertContextHandle ? pCertContext = null ;
339349 while ( Interop . crypt32 . CertFindCertificateInStore ( _storePal . SafeCertStoreHandle , dwFindType , pvFindPara , ref pCertContext ) )
340350 {
341- if ( filter != null && ! filter ( pCertContext ) )
351+ if ( filter != null && ! filter ( state , pCertContext ) )
342352 continue ;
343353
344354 if ( _validOnly )
0 commit comments