// ExportCert.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include "Certificate.h"
#include "CertificateStore.h"
#include "conio.h"
#include <iostream>
#include <wchar.h>
#include <stdio.h>
#include "WinCrypt.h"
using namespace std;
const char CONST_CURRENTUSER_CERTSTORE[] = "MY";//certificate
const char CONST_VALVE_CYBER_CAFE_PROGRAM[] = "Valve Cyber Cafe Program";//certificate
int _tmain(int argc, _TCHAR* argv[])
{
CCertificateStore* CertStore = new CCertificateStore();
if(CertStore->Open(CONST_CURRENTUSER_CERTSTORE))
{
//_cprintf("CertStore opened.\n");
if(PCCERT_CONTEXT pCertContext = CertStore->FindNewestCert(CONST_VALVE_CYBER_CAFE_PROGRAM, PKCS_7_ASN_ENCODING | X509_ASN_ENCODING, CERT_FIND_ISSUER_STR_A))
{
//Certificate Found
CCertificate* Cert = new CCertificate(pCertContext);
//Login
if (Cert->GetUserKey())
{
BYTE holder[1000];
Cert->hUserKey;
DWORD pdwBlobLen = 1000;
DWORD dwBlobLength;
LPBYTE ppbKeyBlob = &holder[0];
pdwBlobLen = 0;
DWORD dwBlobType = PUBLICKEYBLOB;
// Export the public key. Here the public key is exported to a
// PUBLICKEYBLOB. This BLOB can be written to a file and
// sent to another user.
if(CryptExportKey(
Cert->hUserKey,
NULL,
dwBlobType,
0,
NULL,
&dwBlobLength))
{
printf("Size of the BLOB for the public key determined. \n");
// Allocate memory for the pbKeyBlob.
if(ppbKeyBlob = (LPBYTE)malloc(dwBlobLength))
{
printf("Memory has been allocated for the BLOB. \n");
if(CryptExportKey(
Cert->hUserKey,
NULL,
dwBlobType,
0,
ppbKeyBlob,
&dwBlobLength))
{
printf("Contents have been written to the BLOB. \n");
pdwBlobLen = dwBlobLength;
}
else
{
printf("Error exporting key.\n");
free(ppbKeyBlob);
ppbKeyBlob = NULL;
}
}
else
{
printf("Out of memory. \n");
}
}
else
{
printf("Error computing BLOB length.\n");
}
}
}
CertStore->Close();
delete CertStore;
}
return 0;
}