CA证书导入

来源:互联网 发布:paxos算法原理与推导 编辑:程序博客网 时间:2024/04/27 22:26

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 = CertCreateCertificateContext(MY_ENCODING_TYPE, (BYTE*)pfx,nFilesize );
    if(pctx == NULL)
    {
        //AfxMessageBox("Error in 'CertCreateCertificateContext'", 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(!CertAddCertificateContextToStore(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 (!CertAddCertificateContextToStore(hFileStore, pctx, CERT_STORE_ADD_REPLACE_EXISTING, 0))
                  {
                      // AfxMessageBox("Error in 'CertAddCertificateContextToStore'", MB_ICONERROR);
                      FreeHandles(hfile, hsection, hFileStore, pfx, pctx, pfxStore, myStore); 
                       return 0;
                   }
              }
         }
         else
         {
              //AfxMessageBox("Error in 'CertAddCertificateContextToStore'", MB_ICONERROR);
              FreeHandles(hfile, hsection, hFileStore, pfx, pctx, pfxStore, myStore); 
              return 0;
         }
    }
    return 1;
}

转载自:http://blog.sina.com.cn/s/blog_539a66320100mwuz.html
其它文章:http://blog.sina.com.cn/s/blog_4e6c32b30100i3iq.html

0 0
原创粉丝点击