Skip to main content

cpp_connect_cert.cpp

/*****************************************************************************************
* cpp_connect_cert.cpp
*
* Note: See Tricryption Engine SDK for details on how to set the project environment.
*
******************************************************************************************/

#include <iostream>
#include "teconnection.h"
#include "teagent.h"
#include "teexception.h"
#include "teauthldapctx.h"
#include "teutil_keycert.h"

using namespace ERUCES;
using namespace std;


int main()
{
string strServerName = "localhost"; // server name
long lPort = 8888; // port number KeyServer 9090
string strPKCS7_CAChainFile = "C:\\Documents and Settings\\Administrator\\pkcs7_ca_chain.pem"; // PKCS#7 CA chain file.
string strPKCS8_PKey_File = "C:\\Documents and Settings\\Administrator\\pkcs8_private_key.pem"; // PKCS#8 private key file.
string strPKCS12_PKey_File = "C:\\Documents and Settings\\Administrator\\pkcs12_private_key.pfx"; // PKCS#12 private key file.
string strPassword = "password"; // password of private key

try
{
auto_ptr<TEConnection> conn =
TEConnection::createInstance(TEConnection::SSLMutualConnection); // SSLMutualConnection = 0x00000004

string strCertChain = "";
string strPrivKey = "";

bool bUsePKCS12 = false;

if (bUsePKCS12)
{
// Read Certificate and Private Key from PKCS#12 private key file.
TEKeyCertUtil::loadFromFile(strPKCS12_PKey_File, strPassword, strCertChain, strPrivKey, false);
}
else
{
// Read Certificate and Private Key from PKCS#7 and PKCS#8 files.
strCertChain = TEKeyCertUtil::loadFromFile(strPKCS7_CAChainFile, false);
strPrivKey = TEKeyCertUtil::loadFromFile(strPKCS8_PKey_File, strPassword, false);
}

// Open connection to the server for certificate user
conn->open(strServerName, lPort, strCertChain, strPrivKey);

cout << "Connected to " << strServerName << " at " << lPort << " as certificate user.\n" << endl;

// Close conneciton
conn->close();

cout << "Disconnected from server." << endl;
}
catch (TEException &e)
{
cout << e.getAllMessages() << endl;
}
catch (...)
{
cout << "Unexpected error" << endl;
}

return 0;
}