Zero Trust IoT library
Loading...
Searching...
No Matches
zt-tls-handshake.cc
Go to the documentation of this file.
1// zt-tls-handshake.cc
2
3#include "zt-tls-handshake.h"
4#include "ns3/log.h"
5#include <cryptopp/aes.h>
6#include <cryptopp/filters.h>
7#include <cryptopp/hex.h>
8#include <cryptopp/modes.h>
9#include <cryptopp/osrng.h>
10
11namespace ns3 {
12
13NS_LOG_COMPONENT_DEFINE("ZtTlsHandshake");
14
20 static TypeId tid = TypeId("ns3::ZtTlsHandshake")
21 .SetParent<Object>()
22 .SetGroupName("ZeroTrust")
23 .AddConstructor<ZtTlsHandshake>();
24 return tid;
25}
26
31 NS_LOG_FUNCTION(this);
32}
33
42void ZtTlsHandshake::StartHandshake(Ptr<Node> client, Ptr<Node> server, uint32_t clientId, uint32_t serverId) {
43 NS_LOG_FUNCTION(this << client << server);
44
45 if (m_policyValidator && !m_policyValidator(clientId, "client")) {
46 Log("[ZT-HANDSHAKE] Client not authorized by policy");
47 return;
48 }
49
50 if (m_policyValidator && !m_policyValidator(serverId, "server")) {
51 Log("[ZT-HANDSHAKE] Server not authorized by policy");
52 return;
53 }
54
55 CryptoPP::AutoSeededRandomPool prng;
56 CryptoPP::byte key[CryptoPP::AES::DEFAULT_KEYLENGTH];
57 prng.GenerateBlock(key, sizeof(key));
58
59 std::string encoded;
60 CryptoPP::StringSource ss(key, sizeof(key), true,
61 new CryptoPP::HexEncoder(new CryptoPP::StringSink(encoded)));
62
63 m_sessionKeys[serverId] = encoded;
64 m_sessionKeys[clientId] = encoded;
65
66 Log("[ZT-HANDSHAKE] Session established between Client " + std::to_string(clientId) +
67 " and Server " + std::to_string(serverId) + " | Key: " + encoded);
68}
69
76bool ZtTlsHandshake::HasSession(uint32_t peerId) const {
77 return m_sessionKeys.find(peerId) != m_sessionKeys.end();
78}
79
86std::string ZtTlsHandshake::GetSessionKey(uint32_t peerId) const {
87 auto it = m_sessionKeys.find(peerId);
88 return (it != m_sessionKeys.end()) ? it->second : "";
89}
90
96void ZtTlsHandshake::SetExternalLogger(std::function<void(std::string)> logger) {
97 m_logger = logger;
98}
99
105void ZtTlsHandshake::SetPolicyValidator(std::function<bool(uint32_t, std::string)> validator) {
106 m_policyValidator = validator;
107}
108
114void ZtTlsHandshake::Log(const std::string& msg) const {
115 if (m_logger) {
116 m_logger(msg);
117 } else {
118 NS_LOG_INFO(msg);
119 }
120}
121
122} // namespace ns3
123
Simulates a Zero Trust-based TLS handshake mechanism between NS-3 nodes.
void StartHandshake(Ptr< Node > client, Ptr< Node > server, uint32_t clientId, uint32_t serverId)
Start a simulated TLS handshake between client and server nodes.
void SetExternalLogger(std::function< void(std::string)> logger)
Set an external logger for emitting TLS logs.
ZtTlsHandshake()
Constructor.
std::function< void(std::string)> m_logger
Optional external logger.
static TypeId GetTypeId()
Get the NS-3 TypeId.
std::string GetSessionKey(uint32_t peerId) const
Retrieve the session key for a peer in hexadecimal string format.
std::map< uint32_t, std::string > m_sessionKeys
Maps node IDs to session keys.
std::function< bool(uint32_t, std::string)> m_policyValidator
Optional external policy validator.
bool HasSession(uint32_t peerId) const
Check if a session exists for a given peer.
void Log(const std::string &msg) const
Emit a log message using the external logger or NS_LOG fallback.
void SetPolicyValidator(std::function< bool(uint32_t, std::string)> validator)
Set a policy validator for enforcing Zero Trust identity checks.
NS_LOG_COMPONENT_DEFINE("ZtPolicyEngine")