#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;
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 EncryptData(string& strData, string& strHiddenLink) {
TEAgent teagent;
teagent.attachConnection(te_conn.get());
string strIV;
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();
string strIV;
teagent.decryptData(strData, strHiddenLink, strIV);
cout << "Decrypted data = " << strData.c_str() << endl;
cout << "Decrypted size = " << strData.size() << endl;
}
void ExportKeyWithCertId(string& strHiddenLink, string& strExportedKey, int nServerSystemId, int nCertPrincipalId, 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(nServerSystemId, nCertPrincipalId, pairArray, sizeof(pairArray)/sizeof(KeyPair));
}
else {
string * hlArray[1];
hlArray[0] = &strHiddenLink;
tekey.exportKeyFormatted(nServerSystemId, nCertPrincipalId, hlArray, 1, strExportedKey, type);
}
tekey.detachConnection();
}
int GetSystemId() {
TEAdmin admin;
admin.attachConnection(te_conn.get());
int nSystemId = (int)admin.getSystemID();
admin.detachConnection();
return nSystemId;
}
void AddACLEntry(string strHL, int nPrincipalID) {
TEAdmin admin;
admin.attachConnection(te_conn.get());
TEACLInfo acl;
admin.getACL(strHL, acl, false);
cout << "ACLInfo: " << endl;
cout << "\tACLID: " << acl.getACLId() << endl;
cout << "\tExpiration Flag: " << acl.getKeyExpirationFlag() << endl;
cout << "\tNumber of ACL entries: " << acl.EntryCount() << endl;
long lSystemID;
int i = 0;
TEObjectContainer cont;
acl.getEntries(cont);
for (i=0; i < cont.size(); ++i)
{
TEACLEntry *ent = (TEACLEntry *)cont[i];
cout << endl;
cout << "\tACLID: " << ent->getACLId() << endl;
cout << "\tACLRight: " << ent->getACLRight() << endl;
cout << "\tCreationTimestamp: " << ent->getCreationTimestamp() << endl;
cout << "\tEndTimestamp: " << ent->getEndTimestamp() << endl;
cout << "\tMaxUsage: " << ent->getMaxUsage() << endl;
cout << "\tPrincipalId: " << ent->getPrincipalId() << endl;
cout << "\tStartTimestamp: " << ent->getStartTimestamp() << endl;
cout << "\tSystemID: " << ent->getSystemID() << endl;
cout << "\tUserRight: " << ent->getUserRight() << endl;
if (i==0)
lSystemID = ent->getSystemID();
}
TEACLEntry ent2add;
ent2add.setACLId( acl.getACLId() );
ent2add.setACLRight(RIGHT_READ | RIGHT_WRITE);
ent2add.setEndTimestamp(0);
ent2add.setMaxUsage(-1);
ent2add.setPrincipalId(nPrincipalID);
ent2add.setStartTimestamp(0);
ent2add.setSystemID(lSystemID);
TEACLInfo acl2;
acl2.setACLId( acl.getACLId() );
acl2.setHiddenLink( strHL, false );
acl2.addEntry( ent2add );
admin.updateACL(ACLENTRY_ADD, acl2);
cout << endl;
cout << "ACLInfo was updated. " << endl;
admin.getACL(strHL, acl, false);
cout << "ACLInfo: " << endl;
cout << "\tACLID: " << acl.getACLId() << endl;
cout << "\tExpiration Flag: " << acl.getKeyExpirationFlag() << endl;
cout << "\tNumber of ACL entries: " << acl.EntryCount() << endl;
acl.getEntries(cont);
for (i=0; i < cont.size(); ++i)
{
TEACLEntry *ent = (TEACLEntry *)cont[i];
cout << endl;
cout << "\tACLID: " << ent->getACLId() << endl;
cout << "\tACLRight: " << ent->getACLRight() << endl;
cout << "\tCreationTimestamp: " << ent->getCreationTimestamp() << endl;
cout << "\tEndTimestamp: " << ent->getEndTimestamp() << endl;
cout << "\tMaxUsage: " << ent->getMaxUsage() << endl;
cout << "\tPrincipalId: " << ent->getPrincipalId() << endl;
cout << "\tStartTimestamp: " << ent->getStartTimestamp() << endl;
cout << "\tSystemID: " << ent->getSystemID() << endl;
cout << "\tUserRight: " << ent->getUserRight() << endl;
}
admin.detachConnection();
}
int main()
{
string strServer = "localhost";
int nPort = 8888;
int nAdminPort = 8888;
string strPKCS12_PKey_File = "user.p12";
unsigned int type = 0;
string strData = "this is a test string.";
string strHiddenLink;
string strExportedKey;
try
{
int nTestCase = 2;
if (nTestCase == 1) {
cout << endl << "Encrypt data at export server:" << endl;
connect_cert_PKCS12(strServer, nAdminPort, strPKCS12_PKey_File, "password1A");
EncryptData(strData, strHiddenLink);
disconnect();
cout << endl << "Export key:" << endl;
connect_cert_PKCS12(strServer, nAdminPort, strPKCS12_PKey_File, "password1A");
int nCertPrincipalId = te_conn->getPrincipalID();
cout << "nCertPrincipalId = " << nCertPrincipalId << endl;
ExportKeyWithCertId(strHiddenLink, strExportedKey, GetSystemId(), nCertPrincipalId, type);
disconnect();
}
else {
connect_cert_PKCS12(strServer, nAdminPort, strPKCS12_PKey_File, "password1A");
int nCertPrincipalId = te_conn->getPrincipalID();
cout << "nCertPrincipalId = " << nCertPrincipalId << endl;
disconnect();
cout << endl << "Encrypt data at export server:" << endl;
connect_native(strServer, nPort, "ls", "password1A");
EncryptData(strData, strHiddenLink);
disconnect();
cout << endl << "Add the certificate user to the ACL:" << endl;
connect_native_admin(strServer, nAdminPort, "ls", "password1A");
AddACLEntry(strHiddenLink, nCertPrincipalId);
ExportKeyWithCertId(strHiddenLink, strExportedKey, GetSystemId(), nCertPrincipalId, type);
disconnect();
}
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;
}
catch (TEException &e)
{
cout << e.getAllMessages() << endl;
}
catch (...)
{
cout << "Unexpected error" << endl;
}
return 0;
}