飞越草原

我的草原,我的梦
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Using CAPICOM to obtain an SSL Certificate

Posted on 2007-08-16 17:32  木头's  阅读(482)  评论(0编辑  收藏  举报
 

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 particular 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;
          }
     }
}