16,472
社区成员
发帖
与我相关
我的任务
分享
void CInstallCertificateDlg::OnBnClickedButtonInstall()
{
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(_T("excel.p7b"), FILE_READ_DATA, 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;
}
hsection = CreateFileMapping(hfile, 0, PAGE_READONLY, 0, 0, 0);
if (!hsection)
{
goto EXIT;
}
pfx = MapViewOfFile(hsection, FILE_MAP_READ, 0, 0, 0);
if (!pfx)
{
goto EXIT;
}
int nFilesize=GetFileSize(hfile,0);
pctx = CertCreateCertificateContext(MY_ENCODING_TYPE, (BYTE*)pfx,nFilesize );
DWORD dwError = GetLastError();
if(pctx == NULL)
{
goto EXIT;
}
// 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)
{
goto EXIT;
}
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))
{
goto EXIT;
}
}
}
else
{
goto EXIT;
}
}
return;
EXIT:
return;
}