Reg Certificate to IE

来源:互联网 发布:局域网通话软件 编辑:程序博客网 时间:2024/05/17 04:31


char *CSPName = "Your USBKEY CSP";
int CSP_ImportUserCert(
                                      unsigned char *containerName,
                                      unsigned long containerNameLen,
                                      unsigned long keyUsage,
                                      unsigned char *certificate,
                                      unsigned long certificateLen)
{
       HCRYPTPROV hProv = 0;
       HCRYPTKEY hUserKey = NULL;
       BYTE ErrInfo[1024];
       BYTE pbContiner[1024];
       DWORD dwFlag;
       int rv;
       PCCERT_CONTEXT pCertContext = NULL;
       HCERTSTORE hCertStore;
       memcpy(pbContiner,containerName,containerNameLen);
       pbContiner[containerNameLen] = '/0'
       dwFlag = 0;
       CRYPT_KEY_PROV_INFO KeyProvInfo;
      
      
 
             
       hCertStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_W, 0, 0,
              CERT_STORE_OPEN_EXISTING_FLAG |
              CERT_SYSTEM_STORE_CURRENT_USER, L"MY");
       if(hCertStore == NULL)
       {
              sprintf((char *)ErrInfo,"### CertOpenSystemStore Error.ErrCode:0x%x",GetLastError());
              DEBUGMSG(ErrInfo);
              CryptReleaseContext(hProv,0);
              return -1;
       }
       DEBUGMSG("*** CertOpenSystemStore OK.");

      
       if(pCertContext = CertCreateCertificateContext(
              X509_ASN_ENCODING   ,            // The encoding type
              certificate,   // The encoded data from
              // the certificate retrieved
              certificateLen))  // The length of the encoded data
       {
              printf("A new certificate as been created./n");
       }
       else
       {
              printf("A new certificate could not be created.");
              return -1;
       }
       ZeroMemory((PVOID)&KeyProvInfo, sizeof(CRYPT_KEY_PROV_INFO));
       CString strcontainerName;
       CString strCSPName;

       strcontainerName = containerName;
       strCSPName = CSPName;
       KeyProvInfo.pwszContainerName = strcontainerName.AllocSysString();
       KeyProvInfo.pwszProvName      = strCSPName.AllocSysString();
    KeyProvInfo.dwProvType        = PROV_RSA_FULL;
    KeyProvInfo.dwFlags           = 0;
    KeyProvInfo.dwKeySpec         = keyUsage;
       if(!CertSetCertificateContextProperty(pCertContext,
              CERT_KEY_PROV_INFO_PROP_ID,
              0,
              (const void *) &KeyProvInfo))
       {      
              sprintf((char *)ErrInfo,"### CertSetCertificateContextProperty Error.ErrCode:0x%x",GetLastError());
              DEBUGMSG(ErrInfo);
       }
      
       if(CertAddCertificateContextToStore(hCertStore,pCertContext,CERT_STORE_ADD_REPLACE_EXISTING,0))
       {
              printf("A new certificate as been add to strore./n");
       }
       else
       {
              sprintf((char *)ErrInfo,"### CertAddCertificateContextToStore Error.ErrCode:0x%x",GetLastError());
              DEBUGMSG(ErrInfo);
       }
       CertFreeCertificateContext(pCertContext);
      
       DEBUGMSG("*** CSP_ImportUserCert OK.");
       return 0;
}