CA证书导入

news/2025/2/22 1:08:16/

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

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


http://www.ppmy.cn/news/467822.html

相关文章

pycharm-设置python文件模板及常用的模板内容使用

pycharm设置文件和代码&#xff08;File and Code Templates&#xff09;模板步骤如下&#xff1a; 1.pycharm工具栏"file"->"Settings"->"Editor"->"Code Style"->"File and Code Templates",点击右侧python …

Unity开发对电脑配置要求高吗

在开发Unity游戏时&#xff0c;电脑的配置并不是很高&#xff0c;但是如果你想让你的游戏在高端电脑上流畅运行&#xff0c;或者想要使用更高级的功能&#xff0c;则需要更高的配置。 一般来说&#xff0c;使用Unity开发游戏所需的最低配置如下&#xff1a; 处理器&#xff1a;…

酷家乐对电脑配置有要求?

酷家乐软硬件配置要求如下&#xff1a; 1、浏览器&#xff1a;PC客户端、谷歌浏览器。 2、64位系统&#xff1a;操作系统indows7&#xff0f;8&#xff0f;10&#xff0c;或者Mac&#xff0c;不能为indowsXP。 3、cpu&#xff1a;IntelCorei5&#xff0d;8600Ke3&#xff0e;60…

教你如何将CA证书导入Android手机

第一&#xff1a;Android只能理解CA的二进制格式&#xff0c;只能使用文件格式* .crt。 第二&#xff1a;Android只能理解* .p12文件格式的用户证书。 因此&#xff0c;您可以检查您的CA文件二进制文件或文本是否非常简单&#xff1a;使用任何文本编辑器打开它^ 如果有类似的…

Cacti安装及模板导入

文章目录 前言简介安装安装http以及php及php相关扩展安装数据库mariadb&#xff0c;配置yum源&#xff1a;安装snmp及其相关扩展&#xff08;注意版本号得一致&#xff09;安装绘图工具RRDTool绘图工具安装spine安装中文字体安装cacti初始化cacti 应用管理设备以及创建图形加入…

AutoCADElectrical制图软件对电脑配置的要求

AutoCAD Electrical 2010 32位配置要求 Microsoft Windows XP Professional或Home版本&#xff08;SP2或更高&#xff09; 支持SSE2技术的英特尔奔腾 4或AMD Athlon双核处理器&#xff08;1.6 GHz或更高主频&#xff09; 2 GB内存 1 GB可用磁盘空间&#xff08;用于安装&am…

java编程对电脑配置_java编程对电脑配置有什么要求?如何选择合适电脑?

对于写编程的人来说&#xff0c;一定要有一个好的电脑&#xff0c;这样在写编程的时候才不会卡&#xff0c;那么java编程对电脑配置有什么要求?今天我们就给大家分享一下&#xff0c;大家可以参考以下电脑相关配置哦! Eclipse和Tomcat加在一起挺耗内存的&#xff0c;按现在来说…

计算机视觉应用要什么硬件?对电脑配置要求高吗?

计算机视觉应用通常需要高性能的计算机和图形处理器&#xff08;GPU&#xff09;来处理大量的图像和视频数据&#xff0c;以及进行复杂的图像处理和计算。另外&#xff0c;一些特定的应用场景可能还需要专门的硬件设备&#xff0c;例如机器人导航需要激光雷达等传感器。 计算机…