2016-04-17 40 views
0

我使用下面的代码导入证书作为信任根受信任的根证书:进口自动使用CryptUIWizImport与C++

#include "stdafx.h" 
#include "windows.h" 
#include "Cryptuiapi.h" 

#pragma comment(lib, "Cryptui.lib") 

int _tmain(int argc, _TCHAR* argv[]){ 
    CRYPTUI_WIZ_IMPORT_SRC_INFO importSrc;  
    memset(&importSrc, 0, sizeof(CRYPTUI_WIZ_IMPORT_SRC_INFO));  
    importSrc.dwSize = sizeof(CRYPTUI_WIZ_IMPORT_SRC_INFO);  
    importSrc.dwSubjectChoice = CRYPTUI_WIZ_IMPORT_SUBJECT_FILE;  
    importSrc.pwszFileName = L“C:\\PathToCert\\MyCertificate.cer”; 
    importSrc.pwszPassword = L"";  
    importSrc.dwFlags = CRYPT_EXPORTABLE | CRYPT_USER_PROTECTED;    
    if (CryptUIWizImport( 
     CRYPTUI_WIZ_NO_UI,  
     NULL,  
     NULL,  
     &importSrc,  
     NULL  
    ) == 0)  
    {  
     printf(“CryptUIWizImport error 0x%x\n”, GetLastError());  
    } 
    return 0; 
} 

然而,这种方法导入我的证书作为Intermediate Certificate Authorities,而我需要将其导入为Trusted Root Certificate Authorities。我不想使用任何向导方法,但我无法更改证书。

是否可以将此证书导入为受信任的根目录?

CryptUIWizImport中是否有任何属性将证书类型设置为受信任的根?

感谢进步

回答

0

我们应该从C++代码中执行批处理文件命令:

#include "stdafx.h"; 
#include "windows.h" 
#include "Cryptuiapi.h" 
#include <iostream> 
#include <string> 
using namespace std; 

#pragma comment(lib,"Cryptui.lib") 

int _tmain(int argc, _TCHAR* argv[]) 
{ 

    char buffer[MAX_PATH]; 
    GetModuleFileNameA(NULL, buffer, MAX_PATH); 
    string::size_type pos = string(buffer).find_last_of("\\/"); 
    string myPath = string(buffer).substr(0,pos);  
    string myCommand = "certutil -addstore -f -enterprise -user root \""+myPath+"\\IRIPO CA.cer\""; 
    system(myCommand.c_str()); 
    return 0; 
} 

注意,certificate文件应该被放在旁边的exe文件。