Reg Certificate to IE

本文介绍了一种使用C语言将证书导入到特定用户容器的方法。该过程涉及打开系统证书存储、创建证书上下文、设置证书提供者信息并最终将证书添加到存储中。此方法适用于需要自动化证书管理的应用场景。


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值