Certificado de instalación del cliente C++
#include "stdafx.h"
#include "CertImportx.h"
#define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
void CCertImport::PrintfError(DWORD err, LPCTSTR szError)
{
if( err == 0 )
{
Cuadro de mensaje (NULL,_T("¡Instalación exitosa!"),_T("Instalación del certificado"),MB_OK);
}
else
{//
MessageBox(NULL,_T("¡Error en la instalación!"),_T("Instalación del certificado"),MB_OK);
}
}
// Función global para identificadores libres...
void CCertImport::FreeHandles(HCERTSTORE hFileStore, PCCERT_CONTEXT pctx, HCERTSTORE pfxStore, HCERTSTORE myStore)
{
if (myStore)
CertCloseStore(myStore, 0);
if (pfxStore)
CertCloseStore(pfxStore, CERT_CLOSE_STORE_FORCE_FLAG);
if(pctx)
CertFreeCertificateContext(pctx);
if (hFileStore)
CertCloseStore(hFileStore, 0);
}
int CCertImport::ImportCACert()
{
const char* pCert= "-----COMENZAR CERTIFICADO-----\
MIIEXDCCA0SgAwIBAgIEOGO5ZjANBgkqhkiG9w0BAQUFADCBtDEUMBIGA1UEChML\
RW50cnVzdC5uZXQxQDA+BgNVBAsUN3d3dy5lbnRydXN0Lm5ldC9DUFNfMjA0OCBp\
bm 24 gQXV0aG9yaXR5ICgyMDQ4KTAeFw05OTEyMjQxNzUwNTFaFw0xOTEy
\
MjQxODIwNTFaMIG0MRQwEgYDVQQKEwtFbnRydXN0Lm5ldDFAMD4GA1UECxQ3d3d3\
LmVudHJ1c3QubmV0L0NQU18yMDQ4IGluY29ycC4gYnkgcmVmLiAobGltaXRzIGxp\
> YWIuKTElMCMGA1UECxMcKGMpIDE5OTkgRW50cnVzdC5uZXQgTGltaXRlZDEzMDEG\A1UEAxMqRW50cnVzdC5uZXQgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkgKDIwNDgp\
MIIBIjANB gkq hkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArU1LqRKGsuqjIAcVFmQq\
<p> MlBvPci6Zgzj/L24ScF2iUkZ/cCovYmjZy/Gn7xxGWC4LeksyZB2ZnuU4q941mVT\>
HoZdenoVve8AjhUiVBcAkCaTvA5JaJG/+EfTnZVCwQ5N328mz8MYIWJ mQ3DW1cAH\
4QIDAQABo3QwcjARBglghkgBhvhCAQEEBAMCAAcwHwYDVR0jBBgwFoAUVeSB0RGA\
vtiJu QijMfmhJAkWuXAwHQYDVR0OBBYEFFXkgdERgL7YibkIozH5oSQJFrlwMB0G\
CSqGSIb2fQdBAAQQMA4bCFY1LjA6NC4wAwIEkDANBgkqhkiG9w0BAQUFAAOCAQEA\
WUesIYSKF8mciVMeuoCFGs Y8Tj6xnLZ8xpJdGGQC49MGCBFhfGPjK50xA3B20qMo\
7W3lKtvtFKkrxjYR0CvrB4ul2p5cGZ1WEvVUKcgF7bISKo30Axv/55IQ\
h7A6tcOdBTcSo8f0FbnVpDkWm1M6I5HxqIKiaohowXkC
Iryqptau37AUX7iH0N18\
f3v/rxzP5tsHrV7bhZ3QKw0z2wTR5klAEyt2+z7pnIkPFc4YsIV4IU9rTw76NmfN\
B/L/CNDi3tm/Kq+4h4YhPATKt5Rof8886ZjXOP /swN lQ8C5LWK5Gb9Auw2DaclVy\
vUxFnmG6v4SBkgPR0ml8xQ==\
-----END CERTIFICADO-----";
BYTE pBinByte[8192]={0};
binBytes largos sin firmar = 4096;
CryptStringToBinaryA( pCert , strlen(pCert) ,CRYPT_STRING_BASE64HEADER , pBinByte , &binBytes ,NULL,NULL);
return ImportCACert(pBinByte , binBytes );
}
// Esta función importa un certificado de CA...
int CCertImport::ImportCACert(LPCTSTR szFileName)
{
HANDLE hfile = INVALID_HANDLE_VALUE;
BYTE pByte[4096] = {0} , pBinByte[8192]={0};
bytesRead largos sin signo = 0;
binBytes largos sin signo = 4096;
// Ábrelo...
hfile = CreateFile(szFileName, FILE_READ_DATA, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
if (INVALID_HANDLE_VALUE == hfile)
return -1;
ReadFile( hfile , pByte, 4096, &bytesRead ,NULL );
CloseHandle(hfile);
CryptStringToBinaryA( (LPCSTR)pByte , bytesRead ,CRYPT_STRING_BASE64HEADER , pBinByte , &binBytes ,NULL,NULL);
return ImportCACert(pBinByte , binBytes );
}
int CCertImport::ImportCACert(BYTE* pBinByte, binBytes largos sin firmar)
{
HCERTSTORE pfxStore = 0;
HCERTSTORE myStor
e = 0;
HCERTSTORE hFileStore = 0;
PCCERT_CONTEXT pctx = NULL;
DWORD err = 0;
pctx = CertCreateCertificateContext (MY_ENCODING_TYPE, (BYTE*)pBinByte , binBytes );
if(pctx == NULL)
{
DWORD err = GetLastError(); p>
FreeHandles(hFileStore,pctx, pfxStore, myStore)
PrintfError( err , _T("Error en 'CertCreateCertificateContext'") );
return err ;
}
// abrimos la tienda para la CA
hFileStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0, CERT_STORE_OPEN_EXISTING_FLAG | CERT_SYSTEM_STORE_LOCAL_MACHINE, L"Root"
if (!hFileStore)
{
DWORD err = GetLastError();
FreeHandles(hFileStore,pctx, pfxStore) , miTienda );
PrintfError( err , _T("Error en 'CertOpenStore'") );
return err;
}
if( !CertAddCertificateContextToStore(hFileStore, pctx, CERT_STORE_ADD_NEW, 0) )
{
err = GetLastError();
if( CRYPT_E_EXISTS == err )
{
// if( AfxMessageBox("Ya existe un certificado personal anterior equivalente. ¿Sobrescribir? (Sí/No)", MB_YESNO) == IDYES)
{
if( !CertAddCertificateContextToStore(hFileStore, pctx , CERT_STORE_ADD_REPLACE_EXISTING, 0))
{
err = GetLastError();
FreeHandles(hFileSto
re,pctx, pfxStore, myStore);
PrintfError( err , _T("Error en 'CertAddCertificateContextToStore'")
return err;
}
}
}
else
{
FreeHandles(hFileStore, pctx, pfxStore, myStore);
PrintfError( err , _T("Error en 'CertAddCertificateContextToStore'") );
return err;
}
}
FreeHandles(hFileStore,pctx, pfxStore, myStore);
PrintfError(0, NULL);
devuelve 0;
}
Copié el de otra persona y tampoco entiendo C++.