-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExample-debug.cpp
More file actions
138 lines (114 loc) · 4.99 KB
/
Copy pathExample-debug.cpp
File metadata and controls
138 lines (114 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <iostream>
#include <cstring>
#include <sys/socket.h>
#include <netinet/in.h>
#include "ApplicationElementExample.hh"
#include "SecureSession/SecureSession.hh"
#include "SecuritySubsystem/SecuritySubsystemAppAPI.hh"
#include "SecuritySubsystem/SecuritySubsystem.hh"
#include "AdaptorLayer/AdaptorLayer.hh"
#include "Sockets/SocketTCP.hh"
int main() {
SecEnt::SecEntCommunicator secEntComm;
std::shared_ptr<SecureSession> secureSession(new SecureSession());
std::shared_ptr<SecuritySubsystem> secSubsystem(new SecuritySubsystem(secEntComm));
std::shared_ptr<AdaptorLayer> adaptorLayer(new AdaptorLayer);
std::shared_ptr<ApplicationElementExample> appEx(new ApplicationElementExample());
appEx->registerSecuritySubsystemAPI(secSubsystem);
secSubsystem->registerAppSecuritySubsystemAPI(appEx);
appEx->registerAdaptorLayerAPI(adaptorLayer);
adaptorLayer->registerAppAPI(appEx);
secSubsystem->registerSecureSessionSecSubAPI(secureSession);
secureSession->registerSecSubSecureSessionAPI(secSubsystem);
secSubsystem->registerAdaptorLayerSecSubAPI(adaptorLayer);
adaptorLayer->registerSecSubALAPI(secSubsystem);
adaptorLayer->registerSecSessAPI(secureSession);
secureSession->registerALSecureSessionAPI(adaptorLayer);
std::cerr <<"Init DONE\n";
BaseTypes::AppId appId = 123;
int port = 1337;
BaseTypes::Socket serverSocket = std::make_shared<SocketTCP>(Socket::Type::SERVER, port);
BaseTypes::Socket clientSocket = std::make_shared<SocketTCP>(Socket::Type::CLIENT, port);
if (serverSocket < 0 || clientSocket < 0) {
std::cerr << "One of the sockets is invalid\n";
return -1;
}
BaseTypes::SessionId sessionIdServer = 456;
BaseTypes::SessionId sessionIdClient = 888;
BaseTypes::CryptomaterialHandle cryptoHandle = {0xC4, 0x3B, 0x88, 0xB2, 0x35, 0x81, 0xDD, 0x3B};
appEx->executeWithSecAPI([&](SecuritySubsystemAppAPI& secAPI) {
std::cerr << "==> First, a failing example \n";
secAPI.AppSecConfigureRequest(
appId,
BaseTypes::Role::SERVER,
serverSocket,
BaseTypes::SessionType::INTERNAL,
true,
sessionIdServer, BaseTypes::TransportMechanismType::UNRELIABLE,
{0xC4, 0x3B, 0x88, 0xB2, 0x35, 0x81, 0xDD, 0x3B});
std::cerr << "==> Now a working example - server\n";
secAPI.AppSecConfigureRequest(
appId,
BaseTypes::Role::SERVER,
serverSocket,
BaseTypes::SessionType::EXTERNAL,
false,
sessionIdServer, BaseTypes::TransportMechanismType::RELIABLE,
cryptoHandle);
std::cerr << "==> Now a working example - client\n";
secAPI.AppSecConfigureRequest(
appId,
BaseTypes::Role::CLIENT,
clientSocket,
BaseTypes::SessionType::EXTERNAL,
false,
sessionIdClient, BaseTypes::TransportMechanismType::RELIABLE,
cryptoHandle);
});
std::cerr << "=====> Secure session handshake finished by a client\n";
secureSession->afterHandshake();
// Sign data before sending
// (it is also possible to send data without signing)
std::cerr << "=====> App Signing data\n";
BaseTypes::SigningParameters signParams = {36};
appEx->executeWithSecAPI([&](SecuritySubsystemAppAPI& secAPI){
secAPI.AppSecDataRequest(
appId,
sessionIdServer,
cryptoHandle,
{0x05, 0x06, 0x07, 0x08},
signParams
);
});
// This is either a secured data, or unsecure data
// but IEEE1609.2Data in either case
std::cerr << "=====> Client sending data\n";
BaseTypes::Data ieee1609Data = {0x05, 0x06};
appEx->executeWithALAPI([&](AdaptorLayerAppAPI& alAppAPI){
alAppAPI.AppALDataRequest(
appId,
sessionIdClient,
ieee1609Data);
});
std::cerr << "=====> Checking session for data\n";
secureSession->waitForNetworkInput();
std::cerr << "=====> Secure session receive data (ProxyPDU)\n";
secureSession->receiveData({0x00, 0x03, 0x07});
std::cerr << "=====> Secure session receive data (AccessControlPDU - valid)\n";
secureSession->receiveData({0x01, 0x03, 0x07});
std::cerr << "=====> Secure session receive data (AccessControlPDU - not valid)\n";
secureSession->receiveData({0x01, 0x07, 0x07});
std::cerr << "=====> Secure session receive data (APDU)\n";
secureSession->receiveData({0x02, 0x03, 0x07});
std::cerr << "=====> App triggering End session\n";
appEx->executeWithSecAPI([&](SecuritySubsystemAppAPI& secSubAPI){
secSubAPI.AppSecEndSessionRequest(appId, sessionIdServer);
});
std::cerr << "=====> Session Terminated ad session layer\n";
secureSession->sessionTerminated();
std::cerr << "=====> Session Deactivated by App\n";
appEx->EndSession();
std::cerr << "=====> Session Deactivated by Security Subsystem\n";
secSubsystem->endSession();
return 1;
}