Skip to main content

cpp_admin_trustedserver.cpp

/*****************************************************************************************
* cpp_admin_trustedserver.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"
#include "teauthuserctx.h"
#include "teadmin.h"
#include "teparameter.h"
#include "teadmin_config.h"
#include "tekey.h"
#include "teadmin_trustedserver.h"

using namespace ERUCES;
using namespace std;


auto_ptr<TEConnection> te_conn;




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 disconnect()
{
te_conn->close();
cout << "Disconnected from server" << endl;
}



void TestTrustedServer(string strTrusedtServerName) {
int i;
TEAdminTrustedServer teAdminTS;
teAdminTS.attachConnection(te_conn.operator->());


string strTrustedServerInfo = teAdminTS.getTrustInformation(strTrusedtServerName);
cout << endl;
cout << "strTrustedServerInfo = " << strTrustedServerInfo << endl;


// Get trusted server info
cout << endl;
cout << "Get trusted server info:" << endl;
TEObjectContainer trustedServerContainer;
if(trustedServerContainer.empty()) {
teAdminTS.getTrustedServerList(trustedServerContainer);
}
if(trustedServerContainer.empty()) {
cout << "No trusted server was found." << endl;
}
else {
for (i=0; i<trustedServerContainer.size(); i++) {
TETrustedServerInfo* info = static_cast<TETrustedServerInfo*>(trustedServerContainer[i]);
string servername = info->getServerName();
string friendlyname = info->getFriendlyName().c_str();
uint32_t systemid = info->getSystemID();
cout << endl;
cout << "Server name = " << servername << endl;
cout << "Friendly name = " << friendlyname << endl;
cout << "System id = " << systemid << endl;
}
}

// Get external certificate info
cout << endl;
cout << "Get external certificate info:" << endl;
TECertObjectInfo certObj;
teAdminTS.getExternalCertificateInfo(certObj);

string subjectname;
certObj.getSubjectName(subjectname);
cout << "Subject name = " << subjectname << endl;

string issuername;
certObj.getIssuerName(issuername);
cout << "Issuer name = " << issuername << endl;

string certificate;
certObj.getCertificate(certificate);
cout << "Certificate = ";
for(i=0; i<certificate.size(); i++) {
cout << "[" << (int)certificate[i] << "]";
}
cout << endl;
cout << "certificate size = " << certificate.size() << endl;

teAdminTS.detachConnection();
}



void TestAddTrustMatrix(int pid, int tsid, int flag) {
TEAdminTrustedServer teAdminTS;
teAdminTS.attachConnection(te_conn.operator->());

TEPrincipalTrustMatrix matrix;
matrix.setPrincipalId(pid);
matrix.setTrustedServerId(tsid);
matrix.setFlag(flag); // flag: 1 - deny access, 2 - allow access.

teAdminTS.addPrincipalTrustMatrix(matrix);

cout << endl;
cout << "Principal trust matrix was added:" << endl;
cout << "principalId = " << matrix.getPrincipalId() << endl;
cout << "trustedServerId = " << matrix.getTrustedServerId() << endl;
cout << "flag = " << matrix.getFlag() << endl;

}


void TestGetTrustMatrix() {
int i;
TEAdminTrustedServer teAdminTS;
teAdminTS.attachConnection(te_conn.operator->());

// Get trusted server info
cout << endl;
cout << "Get all principal trust matrices:" << endl;
TEObjectContainer matrixContainer;
if(matrixContainer.empty()) {
teAdminTS.getAllPrincipalTrustMatrices(matrixContainer);
}
if(matrixContainer.empty()) {
cout << "No principal trust matrix was found." << endl;
}
else {
for (i=0; i<matrixContainer.size(); i++) {
TEPrincipalTrustMatrix* matrix = static_cast<TEPrincipalTrustMatrix*>(matrixContainer[i]);
int id = matrix->getId();
int principalId = matrix->getPrincipalId();
int trustedServerId = matrix->getTrustedServerId();
int flag = matrix->getFlag();
cout << endl;
cout << "id = " << id << endl;
cout << "principalId = " << principalId << endl;
cout << "trustedServerId = " << trustedServerId << endl;
cout << "flag = " << flag << endl;
}
}
}


void TestUpdateTrustMatrix(int pid, int tsid) {
TEAdminTrustedServer teAdminTS;
teAdminTS.attachConnection(te_conn.operator->());

TEPrincipalTrustMatrix matrix;
teAdminTS.getPrincipalTrustMatrix(pid, tsid, matrix);

int id = matrix.getId();
int principalId = matrix.getPrincipalId();
int trustedServerId = matrix.getTrustedServerId();
int flag = matrix.getFlag();
cout << endl;
cout << "Principal trust matrix was found:" << endl;
cout << "id = " << id << endl;
cout << "principalId = " << principalId << endl;
cout << "trustedServerId = " << trustedServerId << endl;
cout << "flag = " << flag << endl;

(flag == 1) ? matrix.setFlag(2) : matrix.setFlag(1);

teAdminTS.updatePrincipalTrustMatrix(matrix);

id = matrix.getId();
principalId = matrix.getPrincipalId();
trustedServerId = matrix.getTrustedServerId();
flag = matrix.getFlag();
cout << endl;
cout << "Principal trust matrix was updated:" << endl;
cout << "id = " << id << endl;
cout << "principalId = " << principalId << endl;
cout << "trustedServerId = " << trustedServerId << endl;
cout << "flag = " << flag << endl;

}


void TestRemoveTrustMatrix(int pid, int tsid) {
TEAdminTrustedServer teAdminTS;
teAdminTS.attachConnection(te_conn.operator->());

TEPrincipalTrustMatrix matrix;
teAdminTS.getPrincipalTrustMatrix(pid, tsid, matrix);

int id = matrix.getId();
int principalId = matrix.getPrincipalId();
int trustedServerId = matrix.getTrustedServerId();
int flag = matrix.getFlag();
cout << endl;
cout << "Principal trust matrix was found:" << endl;
cout << "id = " << id << endl;
cout << "principalId = " << principalId << endl;
cout << "trustedServerId = " << trustedServerId << endl;
cout << "flag = " << flag << endl;

teAdminTS.removePrincipalTrustMatrix(matrix);

id = matrix.getId();
principalId = matrix.getPrincipalId();
trustedServerId = matrix.getTrustedServerId();
flag = matrix.getFlag();
cout << endl;
cout << "Principal trust matrix was removed:" << endl;
cout << "id = " << id << endl;
cout << "principalId = " << principalId << endl;
cout << "trustedServerId = " << trustedServerId << endl;
cout << "flag = " << flag << endl;
}





int main()
{

try
{
connect_native_admin("localhost", 8888, "admin", "password1A");

TestTrustedServer("xyangxpvm");

TestAddTrustMatrix(3001, 1912, 2);

TestGetTrustMatrix();

TestUpdateTrustMatrix(3001, 1912);

TestRemoveTrustMatrix(3001, 1912);

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

return 0;
}





/*************************************************************************************
Connected to localhost at 8888 as admin

strTrustedServerInfo = 1 xyangxpvm 9288 MIIJiQYJKoZIhvcNAQcCoIIJ
ejCCCXYCAQExADALBgkqhkiG9w0BBwGgggleMIIEYDCCA0igAwIBAgIQEGKh+LVYiLJAyWu5oAX0MjAN
BgkqhkiG9w0BAQUFADA0MQswCQYDVQQGEwJVUzEPMA0GA1UEChMGRVJVQ0VTMRQwEgYDVQQDEwtFUlVD
RVMgUm9vdDAeFw0wMjA4MjgxNTEyMDlaFw0wODAzMDYyMjM5MDBaMDQxCzAJBgNVBAYTAlVTMQ8wDQYD
VQQKEwZFUlVDRVMxFDASBgNVBAMTC0VSVUNFUyBSb290MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB
CgKCAQEAx4yb+hTC2gwkvKLFzAHIiH1JX5dQ1LPd+aCmDRBvPgjrcOPJmaXuHTnaMDZjyZTzpkJYZg06
1kQ2udbwAKsBjJfKisFaoWeuI9FLOwznEJtmYNb/ansdFVV1OUDxksd+CqmWGvMMzSGBqGLYSLmt0eV7
9Q/rRaWHU+bGnht3m/AzxGUkhWXLh4F1I32EpuFUBVDgl3WotiqYuv/90Eo59+Z5YQwSwUU4CS/VTpnY
2icZN+H/I39vOF5QAUjey2W9y1GCUmEFtE8Foh3qhgZ2JdNK8iQmOmM4Xr2BsIQBQG22YZLQsEPFNXPo
pxDWWmhn51TcWZIrb5NLAvTsbnuaHwIDAQABo4IBbDCCAWgwEwYJKwYBBAGCNxQCBAYeBABDAEEwCwYD
VR0PBAQDAgFGMA8GA1UdEwEB/wQFMAMBAf8wHQYDVR0OBBYEFOp6DG92OiAuLEBsHkLXeck4YM5AMIIB
AAYDVR0fBIH4MIH1MIG4oIG1oIGyhoGvbGRhcDovLy9DTj1FUlVDRVMlMjBSb290LENO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5rZ2PKKfQvr7Mu2eZAgLfcFvfIN603uZ/sSbkem
6FpHhV4aon8lHqrPkuFpLvBpbWw8q0sySKVeAI4R/WRytlc0qKsVobKszZ4cVD4lQ43YePdZOtQFIk4+
Rhf4DVe5rcI9EwnJUXddnDVt6uChnaEK6D6CQgs7q15iBSRbGmCbsubYTCH/2lB5+zhISO7W97PY4Huo
6fC7teJq5DFtOV4q3E3sFTZIV/ywEsHwFDEA

Get trusted server info:

Server name = xyangxpvm
Friendly name = xyangxpvm
System id = 1912

Get external certificate info:
Subject name = /CN=xyang1_server
Issuer name = /C=US/O=ERUCES/CN=ERUCES Root
Certificate = [48][-126][4][-10][48][-126][3][-34][-96][3][2][1][2][2][10][7][99
][-126][-114][0][2][0][0][5][82][48][13][6][9][42][-122][72][-122][-9][13][1][1]
[5][5][0][48][52][49][11][48][9][6][3][85][4][6][19][2][85][83][49][15][48][13][
6][3][85][4][10][19][6][69][82][85][67][69][83][49][20][48][18][6][3][85][4][3][
19][11][69][82][85][67][69][83][32][82][111][111][116][48][30][23][13][48][55][4
9][49][50][56][49][57][53][52][53][49][90][23][13][48][56][48][51][48][54][50][5
0][51][57][48][48][90][48][24][49][22][48][20][6][3][85][4][3][20][13][120][121]
[97][110][103][49][95][115][101][114][118][101][114][48][-127][-97][48][13][6][9
][42][-122][72][-122][-9][13][1][1][1][5][0][3][-127][-115][0][48][-127][-119][2
][-127][-127][0][-54][56][43][110][34][-105][64][105][-121][87][114][9][-8][-126
][-9][12][98][-10][-104][118][-28][-77][81][-122][57][-93][88][37][33][-49][87][
10][124][34][6][47][69][98][-103][-128][12][-116][18][-20][81][90][-11][-75][-34
][-11][-123][-56][91][86][124][20][106][-85][-26][-60][-20][-2][-46][6][-92][30]
[82][-78][79][-50][122][7][-125][56][74][76][109][83][33][68][-98][78][-81][-30]
[86][81][-106][102][-22][-120][65][-51][-77][62][-46][-63][-28][56][-103][107][-
118][-90][-51][112][20][112][95][-84][77][50][-128][1][37][106][-89][44][79][-39
][-117][109][13][118][41][12][-126][-32][-126][-81][2][3][1][0][1][-93][-126][2]
[-88][48][-126][2][-92][48][29][6][3][85][29][14][4][22][4][20][71][76][86][-111
][-69][17][-112][-93][99][117][-39][7][17][-93][-40][-116][27][111][92][-10][48]
[107][6][3][85][29][35][4][100][48][98][-128][20][-22][122][12][111][118][58][32
][46][44][64][108][30][66][-41][121][-55][56][96][-50][64][-95][56][-92][54][48]
[52][49][11][48][9][6][3][85][4][6][19][2][85][83][49][15][48][13][6][3][85][4][
10][19][6][69][82][85][67][69][83][49][20][48][18][6][3][85][4][3][19][11][69][8
2][85][67][69][83][32][82][111][111][116][-126][16][16][98][-95][-8][-75][88][-1
20][-78][64][-55][107][-71][-96][5][-12][50][48][-126][1][0][6][3][85][29][31][4
][-127][-8][48][-127][-11][48][-127][-72][-96][-127][-75][-96][-127][-78][-122][
-127][-81][108][100][97][112][58][47][47][47][67][78][61][69][82][85][67][69][83
][37][50][48][82][111][111][116][44][67][78][61][99][97][48][49][44][67][78][61]
[67][68][80][44][67][78][61][80][117][98][108][105][99][37][50][48][75][101][121
][37][50][48][83][101][114][118][105][99][101][115][44][67][78][61][83][101][114
][118][105][99][101][115][44][67][78][61][67][111][110][102][105][103][117][114]
[97][116][105][111][110][44][68][67][61][109][101][100][110][97][44][68][67][61]
[99][111][109][63][99][101][114][116][105][102][105][99][97][116][101][82][101][
118][111][99][97][116][105][111][110][76][105][115][116][63][98][97][115][101][6
3][111][98][106][101][99][116][99][108][97][115][115][61][99][82][76][68][105][1
15][116][114][105][98][117][116][105][111][110][80][111][105][110][116][48][56][
-96][54][-96][52][-122][50][104][116][116][112][58][47][47][99][97][48][49][46][
109][101][100][110][97][46][99][111][109][47][67][101][114][116][69][110][114][1
11][108][108][47][69][82][85][67][69][83][37][50][48][82][111][111][116][46][99]
[114][108][48][-126][1][16][6][8][43][6][1][5][5][7][1][1][4][-126][1][2][48][-1
27][-1][48][-127][-86][6][8][43][6][1][5][5][7][48][2][-122][-127][-99][108][100
][97][112][58][47][47][47][67][78][61][69][82][85][67][69][83][37][50][48][82][1
11][111][116][44][67][78][61][65][73][65][44][67][78][61][80][117][98][108][105]
[99][37][50][48][75][101][121][37][50][48][83][101][114][118][105][99][101][115]
[44][67][78][61][83][101][114][118][105][99][101][115][44][67][78][61][67][111][
110][102][105][103][117][114][97][116][105][111][110][44][68][67][61][109][101][
100][110][97][44][68][67][61][99][111][109][63][99][65][67][101][114][116][105][
102][105][99][97][116][101][63][98][97][115][101][63][111][98][106][101][99][116
][99][108][97][115][115][61][99][101][114][116][105][102][105][99][97][116][105]
[111][110][65][117][116][104][111][114][105][116][121][48][80][6][8][43][6][1][5
][5][7][48][2][-122][68][104][116][116][112][58][47][47][99][97][48][49][46][109
][101][100][110][97][46][99][111][109][47][67][101][114][116][69][110][114][111]
[108][108][47][99][97][48][49][46][109][101][100][110][97][46][99][111][109][95]
[69][82][85][67][69][83][37][50][48][82][111][111][116][40][50][41][46][99][114]
[116][48][13][6][9][42][-122][72][-122][-9][13][1][1][5][5][0][3][-126][1][1][0]
[85][-115][-113][16][-76][-24][19][-83][-55][-88][-42][101][-37][82][-45][-64][-
128][-104][61][-128][29][-108][45][-51][-120][2][65][-108][-8][44][-70][15][58][
94][84][82][-115][75][85][-47][23][-62][99][40][-1][-27][-79][82][63][54][109][-
47][-6][48][-6][19][-128][-83][-84][94][-6][-8][-86][79][39][37][63][114][31][-4
][-107][102][47][-30][-23][27][114][67][-25][-40][-41][127][-102][-39][-40][-14]
[-118][125][11][-21][-20][-53][-74][121][-112][32][45][-9][5][-67][-14][13][-21]
[77][-18][103][-5][18][110][71][-90][-24][90][71][-123][94][26][-94][127][37][30
][-86][-49][-110][-31][105][46][-16][105][109][108][60][-85][75][50][72][-91][94
][0][-114][17][-3][100][114][-74][87][52][-88][-85][21][-95][-78][-84][-51][-98]
[28][84][62][37][67][-115][-40][120][-9][89][58][-44][5][34][78][62][70][23][-8]
[13][87][-71][-83][-62][61][19][9][-55][81][119][93][-100][53][109][-22][-32][-9
5][-99][-95][10][-24][62][-126][66][11][59][-85][94][98][5][36][91][26][96][-101
][-78][-26][-40][76][33][-1][-38][80][121][-5][56][72][72][-18][-42][-9][-77][-4
0][-32][123][-88][-23][-16][-69][-75][-30][106][-28][49][109][57][94][42][-36][7
7][-20][21][54][72][87][-4][-80][18][-63][-16][20]
certificate size = 1274

Principal trust matrix was added:
principalId = 3001
trustedServerId = 1912
flag = 2

Get all principal trust matrices:

id = 2
principalId = 3001
trustedServerId = 1912
flag = 2

Principal trust matrix was found:
id = 2
principalId = 3001
trustedServerId = 1912
flag = 2

Principal trust matrix was updated:
id = 2
principalId = 3001
trustedServerId = 1912
flag = 1

Principal trust matrix was found:
id = 2
principalId = 3001
trustedServerId = 1912
flag = 1

Principal trust matrix was removed:
id = 2
principalId = 3001
trustedServerId = 1912
flag = 1
Disconnected from server
Press any key to continue
*************************************************************************************/