Zero Trust IoT library
Loading...
Searching...
No Matches
zt-policy-engine.cc
Go to the documentation of this file.
1// === zt-policy-engine.cc ===
2#include "zt-policy-engine.h"
3#include <ns3/log.h>
4#include <sstream>
5#include <ctime>
6#include <cryptopp/base64.h>
7#include <cryptopp/filters.h>
8#include <cryptopp/pssr.h>
9#include <cryptopp/sha.h>
10
11namespace ns3 {
12
13NS_LOG_COMPONENT_DEFINE("ZtPolicyEngine");
14
20 static TypeId tid = TypeId("ZtPolicyEngine")
21 .SetParent<Object>()
22 .SetGroupName("ZeroTrust")
23 .AddConstructor<ZtPolicyEngine>();
24 return tid;
25}
26
32void ZtPolicyEngine::AddAuthorized(uint32_t nodeId, const std::string& role) {
33 authTable[nodeId] = role;
34}
35
42bool ZtPolicyEngine::Authorize(uint32_t nodeId, const std::string& role) {
43 return authTable.find(nodeId) != authTable.end() && authTable[nodeId] == role;
44}
45
50void ZtPolicyEngine::SetCaPublicKey(RSA::PublicKey pub) {
51 caPublicKey = pub;
52}
53
58void ZtPolicyEngine::Revoke(uint32_t nodeId) {
59 revoke.insert(nodeId);
60}
61
76bool ZtPolicyEngine::AuthorizeWithCert(uint32_t nodeId, const std::string& role, const std::string& certStr) {
77 if (revoke.find(nodeId) != revoke.end()) {
78 NS_LOG_UNCOND("ZT-CERT: Node " << nodeId << " is revoked");
79 return false;
80 }
81
82 std::string content, sig;
83 size_t sigPos = certStr.find("|SIG:");
84 if (sigPos == std::string::npos) return false;
85 content = certStr.substr(0, sigPos);
86 sig = certStr.substr(sigPos + 5);
87
88 std::string decodedSig;
89 CryptoPP::StringSource(sig, true, new CryptoPP::Base64Decoder(new CryptoPP::StringSink(decodedSig)));
90
91 CryptoPP::RSASS<CryptoPP::PSSR, CryptoPP::SHA1>::Verifier verifier(caPublicKey);
92 bool valid = false;
93 CryptoPP::StringSource(decodedSig + content, true,
94 new CryptoPP::SignatureVerificationFilter(verifier,
95 new CryptoPP::ArraySink((byte*)&valid, sizeof(valid)),
96 CryptoPP::SignatureVerificationFilter::PUT_RESULT | CryptoPP::SignatureVerificationFilter::SIGNATURE_AT_BEGIN));
97
98 if (!valid) {
99 NS_LOG_UNCOND("ZT-CERT: Signature invalid");
100 return false;
101 }
102
103 std::istringstream ss(content);
104 std::string token;
105 uint32_t idParsed = 0;
106 std::string roleParsed;
107 time_t expiry = 0;
108
109 while (std::getline(ss, token, '|')) {
110 if (token.find("ID:") == 0)
111 idParsed = std::stoul(token.substr(3));
112 else if (token.find("ROLE:") == 0)
113 roleParsed = token.substr(5);
114 else if (token.find("EXP:") == 0)
115 expiry = std::stol(token.substr(4));
116 }
117
118 if (idParsed != nodeId || roleParsed != role) {
119 NS_LOG_UNCOND("ZT-CERT: Identity mismatch");
120 return false;
121 }
122
123 if (std::time(nullptr) > expiry) {
124 NS_LOG_UNCOND("ZT-CERT: Certificate expired");
125 return false;
126 }
127
128 return true;
129}
130
131} // namespace ns3
132
Implements policy enforcement for Zero Trust security in NS-3 simulations.
void AddAuthorized(uint32_t nodeId, const std::string &role)
Add a node to the authorized list with a specified role.
std::unordered_set< uint32_t > revoke
List of revoked node IDs.
std::unordered_map< uint32_t, std::string > authTable
Maps node ID to assigned role.
static TypeId GetTypeId()
Get the TypeId for ZtPolicyEngine.
void Revoke(uint32_t nodeId)
Add a node ID to the revocation list.
bool Authorize(uint32_t nodeId, const std::string &role)
Check if a node is authorized for a given role.
RSA::PublicKey caPublicKey
Public key for certificate signature verification.
void SetCaPublicKey(RSA::PublicKey pub)
Set the Certificate Authority's public key for verifying digital signatures.
bool AuthorizeWithCert(uint32_t nodeId, const std::string &role, const std::string &certStr)
Perform certificate-based authorization for a node.
NS_LOG_COMPONENT_DEFINE("ZtPolicyEngine")