Libraries
network_bp_destination_encrypt_decrypt/main.c
// ////////////////////////////////////////////////////////////////////
//
// The network_bp_destination_encrypt_decrypt program receives twelve
// encrypted packets from the network_bp_source_encrypt_decrypt
// program using ION's bundle protocol. The program decrypts the
// bundles and displays the decrypted messages. The program then
// encrypts the messages prior to using ION's bundle potocol to send
// them back to the network_bp_source_encrypt_decrypt program for
// decryption and display on the source platform. The program
// creates both the source public/private Elliptic Curve Cryptography
// (ECC) key pair and the destination public/private ECC key pair
// in the home directory of the destination platform.
// It uses the destination public and private key in conjunction with
// the source public key to encrypt and decrypt packets. The
// source public and private keys along with the destination
// public key must be moved to the home directory of the source
// platform prior to running the network_bp_source_encrypt_decrypt
// program. Program uses RegisterPrintMessage,
// CreateBPDevice, GeneratePublicPrivateKeyPair,
// GeneratePublicPrivateKeyPairWithPassphrase,
// AddCipherToDeviceWithPeerBPNodeNumber,
// RegisterReceivePacket and SendPacket API functions. Note, TReK
// encryption library support is provided on 32 bit and 64 bit Linux
// operating systems and 64 bit Windows operating systems. TReK
// encryption library support is not available on 32 bit Window
// operating systems.
//
// This program must be running on the destination platform prior to
// starting the network_bp_source_encrypt_decrypt example.
//
// ION must be configured with BP service numbers 3, 4
// and 5. Destination eid/node = 2. Source eid/node = 1.
//
// ////////////////////////////////////////////////////////////////////
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include "ds_shared.h"
#include "bp_shared.h"
#include "trek_error.h"
#ifdef __linux__
#include <unistd.h>
#else
#include <windows.h>
#endif
char bp_device_key3[50];
// ////////////////////////////////////////////////////////////////////
//
// PrintTheMessage controls how messages are processed and displayed to
// the user.
//
// ////////////////////////////////////////////////////////////////////
void PrintTheMessage(message_struct_type *mess_struct_ptr)
{
if (mess_struct_ptr->category == MSG_CAT_INFO ||
mess_struct_ptr->category == MSG_CAT_INFO_ALERT ||
mess_struct_ptr->category == MSG_CAT_ERROR ||
mess_struct_ptr->category == MSG_CAT_ERROR_ALERT)
{
printf("%s\t%s\n",GetMessageCategoryAsString(mess_struct_ptr->category),
mess_struct_ptr->message);
}
}
// ////////////////////////////////////////////////////////////////////
//
// ReceivePacket's key parameter is set to the packet's origin or
// source endpoint id string (e.g.,<source node number>.<source service
// number>). The packet_buffer_ptr parameter points to the newly
// received packet.
//
// ////////////////////////////////////////////////////////////////////
void ReceivePacket (const char *key,
int packet_length,
unsigned char *packet_buffer_ptr)
{
char *arg[2];
long long source_node_number;
unsigned int source_service_number;
// Print the data zone packets.
printf("Data\t%s\n",(char *)packet_buffer_ptr + 5);
// Send the packet back to its source by retrieving the
// packet's source node number and source service number from the
// key string that is returned with the packet.
// NOTE: The key string is formatted as follows:
// <source node number>.<source service number> (e.g., 1.64)
arg[0] = strtok((char *)key,".");
if (arg[0] != NULL)
{
arg[1] = strtok(NULL,"."); // NULL allows function to continue scanning
// where a previous call to function ended
if (arg[1] != NULL)
{
#ifdef _WIN32
source_node_number = _strtoi64(arg[0],NULL,0);
#else
source_node_number = strtoll(arg[0],NULL,0);
#endif
source_service_number = (unsigned int)atof(arg[1]);
SendPacketFromBPDevice (bp_device_key3,
packet_length,packet_buffer_ptr,
source_node_number,
source_service_number);
}
}
}
// ////////////////////////////////////////////////////////////////////
//
// The network_bp_destination_encrypt_decrypt program receives twelve
// encrypted packets from the network_bp_source_encrypt_decrypt
// program using ION's bundle protocol. The program decrypts the
// bundles and displays the decrypted messages. The program then
// encrypts the messages prior to using ION's bundle potocol to send
// them back to the network_bp_source_encrypt_decrypt program for
// decryption and display on the source platform. The program
// creates both the source public/private Elliptic Curve Cryptography
// (ECC) key pair and the destination public/private ECC key pair
// in the home directory of the destination platform.
// It uses the destination public and private key in conjunction with
// the source public key to encrypt and decrypt packets. The
// source public and private keys along with the destination
// public key must be moved to the home directory of the source
// platform prior to running the network_bp_source_encrypt_decrypt
// program. Program uses RegisterPrintMessage,
// CreateBPDevice, GeneratePublicPrivateKeyPair,
// GeneratePublicPrivateKeyPairWithPassphrase,
// AddCipherToDeviceWithPeerBPNodeNumber,
// RegisterReceivePacket and SendPacket API functions. Note, TReK
// encryption library support is provided on 32 bit and 64 bit Linux
// operating systems and 64 bit Windows operating systems. TReK
// encryption library support is not available on 32 bit Window
// operating systems.
//
// This program must be running on the destination platform prior to
// starting the network_bp_source_encrypt_decrypt example.
//
// ION must be configured with BP service numbers 3, 4
// and 5. Destination eid/node = 2. Source eid/node = 1.
//
// ////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
unsigned int receive_service_number;
unsigned int lifespan;
bp_class_of_service_type cos; // BPD_BULK_PRIORITY, BPD_STD_PRIORITY or BPD_EXPEDITED_PRIORITY
unsigned int ordinal; // Values from 0-254. Only associated with EXPEDITED_PRIORITY class_of_service.
bp_transmission_mode_type mode; // BPD_BEST_EFFORT, BPD_ASSURED or BPD_ASSURED_WITH_CUSTODY_TRANSFER
bp_criticality_type criticality; // BPD_NOT_CRITICAL or BPD_CRITICAL
unsigned int key_buffer_size;
unsigned short exit_flag = 0;
char arg1[50];
char bp_device_key4[50];
char bp_device_key5[50];
char home_path[256];
char source_public_key_pathname[256];
char source_private_key_pathname[256];
char destination_public_key_pathname[256];
char destination_private_key_pathname[256];
char user_passphrase[64];
cipher_class_type cipher_class;
int pkt_key_encrypt_time_interval_sec;
unsigned int source_bp_node_number;
// Register the PrintMessage callback function prior to InitToolkitCfdp
// to process error messages that may be generated during initialization.
RegisterMessage(&PrintTheMessage);
printf("\nNetwork BP Destination Encrypt Decrypt\n\n");
if (TCAACGetHomeDirectory(home_path, sizeof(home_path)) != SUCCESS)
{
printf("\nFailed to get the home directory\n");
return(FAIL);
}
// Create a ECC source public/private key pair
sprintf(source_public_key_pathname, "%s/source_bp_public.key", home_path);
sprintf(source_private_key_pathname, "%s/source_bp_private.key", home_path);
if (GeneratePublicPrivateKeyPair(source_public_key_pathname, source_private_key_pathname) != SUCCESS)
{
return 0;
}
// Create a ECC destination public/private key pair and wrap private key
// using a user_passphrase
sprintf(destination_public_key_pathname, "%s/destination_bp_public.key", home_path);
sprintf(destination_private_key_pathname, "%s/destination_bp_private.key", home_path);
strcpy(user_passphrase, "qwert12345");
if (GeneratePublicPrivateKeyPairWithPassphrase(destination_public_key_pathname,
destination_private_key_pathname, user_passphrase) != SUCCESS)
{
return 0;
}
// Create a BP receive device
receive_service_number = 3;
lifespan = 86400;
ordinal = 0;
mode = BPD_ASSURED;
criticality = BPD_NOT_CRITICAL;
key_buffer_size = sizeof(bp_device_key3);
if (CreateBPDevice(receive_service_number,
lifespan,cos,ordinal,mode,criticality,&key_buffer_size,
bp_device_key3) != SUCCESS)
{
return 0;
}
// Attach a cipher to the BP device. The cipher will encyrpt
// the bundles of data that are sent by the device to the
// source node and decrypt the bundles of data that are
// received by the device from the source node.
source_bp_node_number = 1;
// The same cipher must be used for both the send and recieve devices
cipher_class = AES_256_GCM;
// Set the time interval defining how often the encryption key
// is changed (A zero value will change the encryption key
// with every bundle though this could impact performance for
// high bundle rate streams). The default value is 10 seconds.
pkt_key_encrypt_time_interval_sec = 0;
cipher_class,
destination_public_key_pathname,
destination_private_key_pathname,
source_public_key_pathname,
pkt_key_encrypt_time_interval_sec,
source_bp_node_number,
user_passphrase) != SUCCESS)
{
return 0;
}
// Associate the ReceivePacket callback function with the BP
// device so all packets received by the device will
// be processed by ReceivePacket callback function.
if (RegisterReceivePacket(bp_device_key3,&ReceivePacket) != SUCCESS)
{
return 0;
}
// Create and register a second BP receive device
receive_service_number = 4;
if (CreateBPDevice(receive_service_number,
lifespan,cos,ordinal,mode,criticality,&key_buffer_size,
bp_device_key4) != SUCCESS)
{
return 0;
}
// Attach a cipher to the BP device. The cipher will encyrpt
// the bundles of data that are sent by the device to the
// source node and decrypt the bundles of data that are
// received by the device from the source node.
cipher_class,
destination_public_key_pathname,
destination_private_key_pathname,
source_public_key_pathname,
pkt_key_encrypt_time_interval_sec,
source_bp_node_number,
user_passphrase) != SUCCESS)
{
return 0;
}
if (RegisterReceivePacket(bp_device_key4,&ReceivePacket) != SUCCESS)
{
return 0;
}
// Create and register a third BP receive device
receive_service_number = 5;
if (CreateBPDevice(receive_service_number,
lifespan, cos, ordinal, mode, criticality, &key_buffer_size,
bp_device_key5) != SUCCESS)
{
return 0;
}
// Attach a cipher to the BP device. The cipher will encyrpt
// the bundles of data that are sent by the device to the
// source node and decrypt the bundles of data that are
// received by the device from the source node.
cipher_class,
destination_public_key_pathname,
destination_private_key_pathname,
source_public_key_pathname,
pkt_key_encrypt_time_interval_sec,
source_bp_node_number,
user_passphrase) != SUCCESS)
{
return 0;
}
if (RegisterReceivePacket(bp_device_key5, &ReceivePacket) != SUCCESS)
{
return 0;
}
printf("To exit application enter: \"e\" or \"q\" or \"exit\" or \"quit\".\n\n");
while (exit_flag == 0)
{
fgets(arg1, 50, stdin);
arg1[strlen(arg1)-1] = '\0';
if (strcmp(arg1,"exit") == 0 || strcmp(arg1,"quit") == 0 ||
strcmp(arg1,"e") == 0 || strcmp(arg1,"q") == 0)
{
exit_flag = 1;
}
else
{
printf("\nTo exit application enter: \"e\" or \"q\" or \"exit\" or \"quit\".\n\n");
}
}
return 0;
}