#include <iostream>
#include "teconnection.h"
#include "teagent.h"
#include "teexception.h"
#include "teauthldapctx.h"
#include "teutil_keycert.h"
#include "teauthuserctx.h"
#include "teadmin.h"
#include "teparameter.h"
#include "teadmin_config.h"
#include "tekey.h"
using namespace ERUCES;
using namespace std;
auto_ptr<TEConnection> te_conn;
string strIV = "";
void connect_native(string strServer, int nPort, string strUsername, string strPassword)
{
te_conn = TEConnection::createInstance(TEConnection::SecureConnection);
auto_ptr<TEUserAuthenticationContext> ctx = TEUserAuthenticationContext::getInstance();
ctx->setUser(strUsername);
ctx->setPasswd(strPassword);
te_conn->open(strServer, nPort, *ctx.operator->());
cout << "Connected to " << strServer << " at " << nPort << " as " << strUsername << endl;
}
void connect_native_admin(string strServer, int nPort, string strUsername, string strPassword)
{
te_conn = TEConnection::createInstance(TEConnection::SecureConnection);
auto_ptr<TEUserAuthenticationContext> ctx = TEUserAuthenticationContext::getInstance();
ctx->setUser(strUsername);
ctx->setPasswd(strPassword);
te_conn->open(strServer, nPort, *ctx.operator->());
cout << "Connected to " << strServer << " at " << nPort << " as " << strUsername << endl;
}
void connect_cert_PKCS12(string strServer, int nPort, string strPKCS12_PKey_File, string strPassword)
{
te_conn = TEConnection::createInstance(TEConnection::SSLMutualConnection);
string strCertChain = "";
string strPrivKey = "";
TEKeyCertUtil::loadFromFile(strPKCS12_PKey_File, strPassword, strCertChain, strPrivKey, false);
te_conn->open(strServer, nPort, strCertChain, strPrivKey);
cout << "Connected to " << strServer << " at " << nPort << " as certificate user" << endl;
}
void disconnect()
{
te_conn->close();
cout << "Disconnected from server" << endl;
}
void setTrustedServerOfflinePolicy(int nTrustedServerSystemId, bool isOffline) {
TEParameter param;
char buffer[40];
sprintf(buffer, "TrustedServer.%d.offline", nTrustedServerSystemId);
TEAdminConfig admin;
admin.attachConnection(te_conn.get());
string strKey = buffer;
string strValue;
isOffline ? strValue = "YES" : strValue = "NO";
param.setKey(strKey);
param.setValue(strValue);
admin.setParameter(param);
admin.detachConnection();
if (isOffline)
cout << "Trusted server offline policy was set." << endl;
else
cout << "Trusted server offline policy was unset." << endl;
}
void EncryptData(string& strData, string& strHiddenLink) {
TEAgent teagent;
teagent.attachConnection(te_conn.get());
cout << "Original data = " << strData.c_str() << endl;
cout << "Original data size = " << strData.size() << endl;
teagent.encryptData(strData, strHiddenLink, strIV);
teagent.detachConnection();
cout << "Encrypted data = ";
int i;
for(i=0; i<strData.size(); i++) {
cout << "[" << (int)strData[i] << "]";
}
cout << endl;
cout << "Encrypted size = " << strData.size() << endl;
cout << "HiddenLink = ";
for(i=0; i<strHiddenLink.size(); i++) {
cout << "[" << (int)strHiddenLink[i] << "]";
}
cout << endl;
cout << "HiddenLink size = " << strHiddenLink.size() << endl;
}
void DecryptData(string& strData, string& strHiddenLink) {
TEAgent teagent;
teagent.attachConnection(te_conn.get());
teagent.detachConnection();
teagent.decryptData(strData, strHiddenLink, strIV);
cout << "Decrypted data = " << strData.c_str() << endl;
cout << "Decrypted size = " << strData.size() << endl;
}
void ExportKeyWithServerName(string& strHiddenLink, string& strExportedKey, string strTrustedServerName, unsigned int type) {
TEKey tekey;
tekey.attachConnection(te_conn.get());
if(type == 0) {
KeyPair strPair = make_pair<string *, string *>(&strHiddenLink, &strExportedKey);
KeyPair pairArray[1];
pairArray[0] = strPair;
tekey.exportKey(strTrustedServerName, pairArray, sizeof(pairArray)/sizeof(KeyPair));
}
else {
string * hlArray[1];
hlArray[0] = &strHiddenLink;
tekey.exportKeyFormatted(strTrustedServerName, hlArray, 1, strExportedKey, type);
}
tekey.detachConnection();
}
void ImportKey(string& strHiddenLink, string& strExportedKey, unsigned int type) {
TEKey tekey;
tekey.attachConnection(te_conn.get());
if(type == 0) {
KeyPair strPair = make_pair<string *, string *>(&strHiddenLink, &strExportedKey);
KeyPair pairArray[1];
pairArray[0] = strPair;
tekey.importKey(pairArray, sizeof(pairArray)/sizeof(KeyPair));
}
else {
tekey.importKeyFormatted(strExportedKey, type);
}
tekey.detachConnection();
}
int main()
{
string strExportServer = "xyangxpvm";
string strImportServer = "xyang1";
int nExportSystemId = 1912;
int nPort = 8888;
int nAdminPort = 8888;
string strPKCS12_PKey_File2 = "C:\\Documents and Settings\\Administrator\\pkcs12_private_key2.pfx";
string strPKCS12_PKey_File = "C:\\Documents and Settings\\Administrator\\pkcs12_private_key.pfx";
unsigned int type = 0;
string strData = "this is a test string.";
string strHiddenLink;
string strExportedKey;
try
{
cout << endl << "Encrypt data at export server:" << endl;
connect_native(strExportServer, nPort, "ls", "password");
EncryptData(strData, strHiddenLink);
disconnect();
cout << endl << "Export key:" << endl;
connect_native_admin(strExportServer, nAdminPort, "ls", "password");
ExportKeyWithServerName(strHiddenLink, strExportedKey, strImportServer, type);
cout << "export key done." << endl;
cout << "HiddenLink = ";
int i;
for(i=0; i<strHiddenLink.size(); i++) {
cout << "[" << (int)strHiddenLink[i] << "]";
}
cout << endl;
cout << "HiddenLink size = " << strHiddenLink.size() << endl;
cout << "Exported key = ";
for(i=0; i<strExportedKey.size(); i++) {
cout << "[" << (int)strExportedKey[i] << "]";
}
cout << endl;
cout << "Exported key size = " << strExportedKey.size() << endl;
disconnect();
if (type != 1)
{
cout << endl << "Set trusted server offline policy:" << endl;
connect_native_admin(strImportServer, nAdminPort, "admin", "password1A");
setTrustedServerOfflinePolicy(nExportSystemId, true);
disconnect();
cout << endl << "Import key:" << endl;
connect_native_admin(strImportServer, nAdminPort, "ls", "password");
ImportKey(strHiddenLink, strExportedKey, type);
cout << "import key done." << endl;
disconnect();
cout << endl << "Decrypt data at import server:" << endl;
connect_native(strImportServer, nPort, "ls", "password");
DecryptData(strData, strHiddenLink);
disconnect();
cout << endl << "Unset trusted server offline policy:" << endl;
connect_native_admin(strImportServer, nAdminPort, "admin", "password1A");
setTrustedServerOfflinePolicy(nExportSystemId, false);
disconnect();
}
}
catch (TEException &e)
{
cout << e.getAllMessages() << endl;
}
catch (...)
{
cout << "Unexpected error" << endl;
}
return 0;
}