Using system;
Using system. collections;
Using system. Security. cryptography. x509certificates;
Using InterOP. CAPICOM;
Namespace capicomwrapper
{
/// <Summary>
/// Provides methods to interact with Windows certificate stores.
/// </Summary>
Public class certificatemanager
{
/// <Summary>
/// Searches for and returns a participant X509 Certificate.
/// </Summary>
/// <Param name = "searchstring"> a full or partial Certificate Name </param>
/// <Returns> an instance of the x509certificate class. </returns>
Public static x509certificate get (string searchstring)
{
String storename = "my"; // "my" indicates the. Default store
Storeclass ostore;
Certificates ocerts;
X509certificate foundcert = NULL; system;
// Get a reference to the local machine certificate store
Ostore = new storeclass ();
Ostore. Open (
Capicom_store_location.capicom_local_machine_store,
Storename,
Capicom_store_open_mode.capicom_store_open_existing_only |
Capicom_store_open_mode.capicom_store_open_read_only );
// Get a list of all certificates in the store
Ocerts = (certificates) ostore. certificates;
// Get a list of only the matching certificates
Ocerts = (certificates) ocerts. Find (
Capicom_certificate_find_type.capicom_certificate_find_subject_name,
Searchstring,
False );
// Do we have any certs?
If (ocerts. Count> 0)
{
// Reference the first certificate
Certificate firstcert = (certificate) ocerts [1];
// Get a certificate context from that Cert
Icertcontext icertcntxt = (icertcontext) firstcert;
// Now get a pointer to the context
Int certcntxt = icertcntxt. certcontext;
// Turn the int pointer into a managed intptr
Intptr hcertcntxt = new intptr (certcntxt );
// Was all of this successful?
If (hcertcntxt! = Intptr. Zero)
{
// Create an x509certificate from the CERT Context
Foundcert = new x509certificate (hcertcntxt );
}
// Free the certificate Context
Icertcntxt. freecontext (certcntxt );
}
Else
{
Foundcert = NULL;
}
Return foundcert;
}
}
}