6#include <cryptopp/base64.h>
7#include <cryptopp/filters.h>
8#include <cryptopp/pssr.h>
9#include <cryptopp/sha.h>
20 static TypeId tid = TypeId(
"ZtPolicyEngine")
22 .SetGroupName(
"ZeroTrust")
78 NS_LOG_UNCOND(
"ZT-CERT: Node " << nodeId <<
" is revoked");
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);
88 std::string decodedSig;
89 CryptoPP::StringSource(sig,
true,
new CryptoPP::Base64Decoder(
new CryptoPP::StringSink(decodedSig)));
91 CryptoPP::RSASS<CryptoPP::PSSR, CryptoPP::SHA1>::Verifier verifier(
caPublicKey);
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));
99 NS_LOG_UNCOND(
"ZT-CERT: Signature invalid");
103 std::istringstream ss(content);
105 uint32_t idParsed = 0;
106 std::string roleParsed;
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));
118 if (idParsed != nodeId || roleParsed != role) {
119 NS_LOG_UNCOND(
"ZT-CERT: Identity mismatch");
123 if (std::time(
nullptr) > expiry) {
124 NS_LOG_UNCOND(
"ZT-CERT: Certificate expired");
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")