CA证书导入


root  : 受信任的根证书颁发机构
trust 受信任的发布者
ca     中级证书颁发机构
my 个人证书
 
 
下面是导入root证书的,其它的改一下即可

#include <wincrypt.h>
#pragma comment( lib, "crypt32.lib")

#define MY_ENCODING_TYPE  (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
 
BOOL ImportCACert(CString &m_pathCA)
{
    HCERTSTORE pfxStore = 0;
     HCERTSTORE myStore = 0;
     HCERTSTORE hFileStore = 0;
     HANDLE hsection = 0;
     void* pfx = NULL;
     HANDLE hfile = INVALID_HANDLE_VALUE;
     PCCERT_CONTEXT pctx = NULL;
     // Get path of the CA certificate from the edit box
     // Open it...
     hfile = CreateFile(m_pathCA, FILE_READ_DATA, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
 
     // FOR WINDOWS 98 .... 
     // hfile = CreateFile(m_pathCA, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
     if (INVALID_HANDLE_VALUE == hfile)
     {
          MessageBox(NULL,_T("Certificate not found. Check that the path indicated is correct."),_T("information"), MB_ICONERROR);
         return 0;
    }
    hsection = CreateFileMapping(hfile, 0, PAGE_READONLY, 0, 0, 0);
     if (!hsection)
     {
          //AfxMessageBox("Error in 'CreateFileMapping'", MB_ICONERROR);
           FreeHandles(hfile, hsection, hFileStore, pfx, pctx, pfxStore, myStore);   
           return 0;
     }
     pfx = MapViewOfFile(hsection, FILE_MAP_READ, 0, 0, 0);
     if (!pfx)
     {
          //AfxMessageBox("Error in 'MapViewOfFile'", MB_ICONERROR);
          FreeHandles(hfile, hsection, hFileStore, pfx, pctx, pfxStore, myStore); 
          return 0;
     }
     int nFilesize=GetFileSize(hfile,0);
     pctx = CertCreateCertificateCon text(MY_ENCODING_TYPE, (BYTE*)pfx,nFilesize );
     if(pctx == NULL)
     {
          //AfxMessageBox("Error in 'CertCreateCertificateCon text'", MB_ICONERROR);
          FreeHandles(hfile, hsection, hFileStore, pfx, pctx, pfxStore, myStore); 
          return 0;
    }
     // we open the store for the CA
     hFileStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0, CERT_STORE_OPEN_EXISTING_FLAG | CERT_SYSTEM_STORE_LOCAL_MACHINE, L"Root");
   
     if (!hFileStore)
     {
          //AfxMessageBox("Error in 'CertOpenStore'", MB_ICONERROR);
          FreeHandles(hfile, hsection, hFileStore, pfx, pctx, pfxStore, myStore); 
          return 0;
     }
     if(!CertAddCertificateContex tToStore(hFileStore, pctx, CERT_STORE_ADD_NEW, 0))
    {
          DWORD err = GetLastError();
          if (CRYPT_E_EXISTS == err)
          {
               if(MessageBox(NULL,_T("An equivalent previous personal certificate already exists. Overwrite ? (Yes/No)"),_T("Prompt"),MB_YESNO) == IDYES)
               {
                     if (!CertAddCertificateContex tToStore(hFileStore, pctx, CERT_STORE_ADD_REPLACE_EXISTING, 0))
                     {
                          //  AfxMessageBox("Error in 'CertAddCertificateContex tToStore'", MB_ICONERROR);
                           FreeHandles(hfile, hsection, hFileStore, pfx, pctx, pfxStore, myStore); 
                            return 0;
                      }
                }
          }
         else
           {
                //AfxMessageBox("Error in 'CertAddCertificateContex tToStore'", MB_ICONERROR);
                 FreeHandles(hfile, hsection, hFileStore, pfx, pctx, pfxStore, myStore); 
                return 0;
          }
     }
    return 1;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值