Export Multiple Keys in C
Export Multiple Keys in C
The TEExportMultiKeys() function is used to export multiple keys. You need to login as a certificate user in order to export keys. You need to provide the system id, principal id and the T-tags hidden links) when you call the TEExportMultiKeys() function. The returned keys are encrypted with the public key of the certificate user. You can use the certificate user's private key to decrypt the exported keys.
Code Samples
See the C environment settings for details on setting the project environment.
< c_exportmultiplekey.c>
/****************************************************************************************
* c_exportmultiplekey.c
*
*
*****************************************************************************************/
#include <stdio.h>
#include "teagent_c.h"
void PrintError(const char * msg)
{
char szErr[1024];
uint32_t nLength = 1024;
uint32_t nCode = GetLastTEError(szErr, &nLength);
printf("%s Code = 0x%x, Message = %s\n", msg, nCode, szErr);
}
int main()
{
TE_HANDLE h;
TE_RETCODE ret;
uint32_t sid;
uint32_t pid;
/* Initialize the TE environment */
TEEnvInit();
/* Open connection to KeyServer */
{
char * szServerName = "localhost"; /* server name */
long lPort = 8888; /* port number */
const char * p12_file = "/test_key.pfx";
const char * password = "password";
h = OpenConnectionWithPKCS12File(
szServerName, /* TE server name */
lPort, /* TE server port */
p12_file, /* PKCS12 file name */
password, /* password */
strlen(password) /* password length */
);
if (h == 0)
{
PrintError("Connection error:");
goto CLEANUP;
}
else
printf("Connected to %s at %d as certificate user with %s.\n", szServerName, lPort, p12_file);
ret = TEGetSystemId(h, &sid);
if(ret) {
PrintError("TEGetSystemId() failed:");
goto CLEANUP;
}
else
printf("sid = %d\n", sid);
ret = CurrentLogonPrincipalId(h, &pid);
if(ret) {
PrintError("CurrentLogonPrincipalId() failed:");
goto CLEANUP;
}
else
printf("pid = %d\n", pid);
}
/* Create and export multiple keys with TEExportMultiKeys() */
{
#define KEY_NUMBER 3
#define KEY_BUFFER_SIZE 256
#define HIDDENLINK_BUFFER_SIZE 60
TE_KEY_BUFFER tekeybuffer[KEY_NUMBER];
int i, j;
// Initialize key and T-tag buffers
for (i=0; i<KEY_NUMBER; i++)
{
tekeybuffer[i].key.buffer = (unsigned char*)malloc(KEY_BUFFER_SIZE);
if(tekeybuffer[i].key.buffer == NULL) {
printf("Error: out of memory.\n");
for(j=0; j<i; j++) {
free(tekeybuffer[j].key.buffer); /* clean up */
}
goto CLEANUP;
}
tekeybuffer[i].key.size = KEY_BUFFER_SIZE;
tekeybuffer[i].key.len = 0;
tekeybuffer[i].hiddenlink.buffer = (unsigned char*)malloc(HIDDENLINK_BUFFER_SIZE);
if(tekeybuffer[i].hiddenlink.buffer == NULL) {
printf("Error: out of memory.\n");
for(j=0; j<=i; j++) {
free(tekeybuffer[j].key.buffer); /* clean up */
}
for(j=0; j<i; j++) {
free(tekeybuffer[j].hiddenlink.buffer); /* clean up */
}
goto CLEANUP;
}
tekeybuffer[i].hiddenlink.size = HIDDENLINK_BUFFER_SIZE;
tekeybuffer[i].hiddenlink.len = 0; /* new key will be created. */
}
ret = TEExportMultiKeys(
h, /* connection handle */
sid, /* system id */
pid, /* principal id */
tekeybuffer, /* keys and their T-tag information */
KEY_NUMBER /* number of key data */
);
if(ret) {
PrintError("TEExportMultiKeys() failed:");
}
else {
for (i=0; i<KEY_NUMBER; i++)
{
printf("tekeybuffer[%d].key.buffer (len = %d) :\n", i, tekeybuffer[i].key.len);
for(j=0; j<tekeybuffer[i].key.len; j++) {
printf("[%2x] ", tekeybuffer[i].key.buffer[j]);
}
printf("\n");
}
}
/* Free key and hiddenlink buffers */
for (i=0; i<KEY_NUMBER; i++)
{
free(tekeybuffer[i].key.buffer);
free(tekeybuffer[i].hiddenlink.buffer);
}
}
CLEANUP:
/* Close a connection */
CloseConnection(h);
printf("Disconnected from server\n\n");
/* Uninitialize the TE environment */
TEEnvClose();
return 0;
}
Output
Connected to localhost at 8888 as certificate user with /test_key.pfx.
sid = 1
pid = 5009
tekeybuffer[0].key.buffer (len = 128) :
[39] [14] [9d] [20] [cd] [2f] [ea] [64] [50] [aa] [1e] [7f] [b6] [e0] [ef] [52] [9c] [31] [50] [b7] [8a] [34] [8b] [99]
[2a] [7d] [ef] [42] [7b] [37] [5d] [95] [27] [f6] [60] [2a] [3c] [79] [ec] [c1] [b3] [53] [fb] [85] [ 0] [60] [b8] [e5]
[d5] [70] [ f] [98] [b5] [fd] [55] [b4] [93] [f4] [16] [6c] [bc] [d8] [f2] [47] [fe] [4e] [e5] [ a] [5f] [af] [3e] [aa]
[25] [a7] [60] [1b] [10] [ca] [df] [2d] [6d] [dd] [56] [91] [72] [61] [a4] [af] [ad] [ 9] [7b] [e5] [84] [76] [a4] [57]
[97] [e3] [c9] [ 8] [ 5] [37] [eb] [42] [cf] [bc] [d1] [69] [60] [c0] [96] [68] [f8] [54] [ 2] [15] [90] [70] [68] [e3]
[23] [4a] [d1] [b5] [f2] [d2] [c6] [94]
tekeybuffer[1].key.buffer (len = 128) :
[81] [d7] [f8] [55] [11] [48] [e6] [79] [4f] [c6] [9f] [ f] [ 1] [1a] [e1] [1a] [47] [cf] [47] [aa] [cf] [58] [c1] [64]
[db] [be] [92] [4d] [d1] [d3] [1b] [5f] [39] [e4] [1e] [2c] [42] [85] [75] [d9] [d5] [eb] [ 9] [3b] [e9] [2a] [45] [ee]
[f2] [ba] [cc] [ 2] [9b] [9e] [d2] [d0] [db] [e8] [be] [1f] [40] [c4] [ 8] [35] [ff] [ b] [21] [e1] [f3] [ba] [8e] [f3]
[57] [46] [ 5] [dc] [90] [6b] [91] [9b] [63] [1d] [cb] [bc] [ab] [f6] [f9] [7b] [e2] [48] [17] [eb] [6b] [bc] [70] [48]
[67] [79] [d4] [92] [52] [41] [ca] [af] [a0] [fe] [90] [8b] [3b] [80] [6d] [9e] [3b] [4b] [90] [c8] [5c] [42] [1c] [d3]
[7b] [cb] [2c] [ 5] [dd] [9a] [9d] [71]
tekeybuffer[2].key.buffer (len = 128) :
[1e] [e0] [44] [cf] [90] [ac] [8b] [29] [da] [4c] [2b] [e2] [6f] [10] [79] [da] [28] [b7] [7e] [56] [ a] [ef] [f0] [63]
[40] [b1] [5b] [ad] [ 2] [72] [cd] [6a] [70] [a7] [87] [6d] [66] [e1] [73] [58] [99] [dd] [15] [46] [78] [8f] [6f] [31]
[48] [ee] [88] [76] [4f] [c6] [23] [47] [e6] [28] [eb] [10] [f9] [66] [80] [73] [5f] [d9] [d3] [23] [56] [ 0] [39] [32]
[e5] [87] [6e] [f4] [cc] [b8] [a8] [e9] [1d] [59] [61] [7e] [f9] [ 9] [45] [ff] [30] [4a] [85] [92] [ 9] [ f] [3a] [d0]
[32] [56] [9c] [9a] [ec] [ad] [a1] [e8] [d9] [79] [74] [59] [65] [67] [57] [7e] [32] [6d] [eb] [be] [7f] [e6] [3e] [94]
[5f] [df] [10] [bd] [f5] [6e] [ b] [aa]
Disconnected from server