Zero Trust IoT library
Loading...
Searching...
No Matches
ns3::ZtPolicyEngine Class Reference

Implements policy enforcement for Zero Trust security in NS-3 simulations. More...

#include <zt-policy-engine.h>

Inheritance diagram for ns3::ZtPolicyEngine:
Collaboration diagram for ns3::ZtPolicyEngine:

Public Member Functions

void AddAuthorized (uint32_t nodeId, const std::string &role)
 Add a node to the authorized list with a specified role.
 
bool Authorize (uint32_t nodeId, const std::string &role)
 Check if a node is authorized for a given role.
 
void SetCaPublicKey (RSA::PublicKey pub)
 Set the Certificate Authority's public key for verifying digital signatures.
 
void Revoke (uint32_t nodeId)
 Add a node ID to the revocation list.
 
bool AuthorizeWithCert (uint32_t nodeId, const std::string &role, const std::string &certStr)
 Perform certificate-based authorization for a node.
 

Static Public Member Functions

static TypeId GetTypeId ()
 Get the TypeId for ZtPolicyEngine.
 

Private Attributes

std::unordered_map< uint32_t, std::string > authTable
 Maps node ID to assigned role.
 
std::unordered_set< uint32_t > revoke
 List of revoked node IDs.
 
RSA::PublicKey caPublicKey
 Public key for certificate signature verification.
 

Detailed Description

Implements policy enforcement for Zero Trust security in NS-3 simulations.

The ZtPolicyEngine class is responsible for enforcing identity-based access control, managing certificate validation, revocations, and dynamic role-based authorization.

Definition at line 22 of file zt-policy-engine.h.

Member Function Documentation

◆ AddAuthorized()

void ns3::ZtPolicyEngine::AddAuthorized ( uint32_t  nodeId,
const std::string &  role 
)

Add a node to the authorized list with a specified role.

Add an authorized node and its assigned role.

Parameters
nodeIdThe node ID to authorize.
roleThe role assigned to the node (e.g., "sensor", "gateway").
nodeIdThe ID of the node.
roleThe assigned role for the node (e.g., "sensor", "gateway").

Definition at line 32 of file zt-policy-engine.cc.

32 {
33 authTable[nodeId] = role;
34}
std::unordered_map< uint32_t, std::string > authTable
Maps node ID to assigned role.

◆ Authorize()

bool ns3::ZtPolicyEngine::Authorize ( uint32_t  nodeId,
const std::string &  role 
)

Check if a node is authorized for a given role.

Check if a node is authorized based on its ID and expected role.

Parameters
nodeIdThe ID of the requesting node.
roleThe required role for access.
Returns
True if authorized, false otherwise.
Parameters
nodeIdThe ID of the node requesting access.
roleThe expected role the node must have.
Returns
True if the node is authorized and role matches, false otherwise.

Definition at line 42 of file zt-policy-engine.cc.

42 {
43 return authTable.find(nodeId) != authTable.end() && authTable[nodeId] == role;
44}

◆ AuthorizeWithCert()

bool ns3::ZtPolicyEngine::AuthorizeWithCert ( uint32_t  nodeId,
const std::string &  role,
const std::string &  certStr 
)

Perform certificate-based authorization for a node.

Authorize a node using certificate validation.

This checks:

  • If the node is revoked
  • If the certificate signature is valid
  • If the certificate identity matches the request
  • If the certificate is not expired
Parameters
nodeIdThe ID of the requesting node.
roleThe required role for access.
certStrThe certificate string (including fields and base64-encoded signature).
Returns
True if certificate is valid and authorized, false otherwise.

This function checks:

  • Whether the node is revoked.
  • If the certificate has a valid signature.
  • If the ID and role match the expected values.
  • Whether the certificate has expired.
Parameters
nodeIdThe ID of the node requesting access.
roleThe expected role for the node.
certStrThe full certificate string including signature.
Returns
True if all certificate checks pass and the node is authorized, false otherwise.

Definition at line 76 of file zt-policy-engine.cc.

76 {
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}
std::unordered_set< uint32_t > revoke
List of revoked node IDs.
RSA::PublicKey caPublicKey
Public key for certificate signature verification.

◆ GetTypeId()

TypeId ns3::ZtPolicyEngine::GetTypeId ( )
static

Get the TypeId for ZtPolicyEngine.

Returns
TypeId object used for NS-3 runtime type identification.
The TypeId object for this class.

Definition at line 19 of file zt-policy-engine.cc.

19 {
20 static TypeId tid = TypeId("ZtPolicyEngine")
21 .SetParent<Object>()
22 .SetGroupName("ZeroTrust")
23 .AddConstructor<ZtPolicyEngine>();
24 return tid;
25}

◆ Revoke()

void ns3::ZtPolicyEngine::Revoke ( uint32_t  nodeId)

Add a node ID to the revocation list.

Revoke a node by adding its ID to the revocation list.

Parameters
nodeIdThe ID of the node to revoke.
nodeIdThe ID of the node to be revoked.

Definition at line 58 of file zt-policy-engine.cc.

58 {
59 revoke.insert(nodeId);
60}

◆ SetCaPublicKey()

void ns3::ZtPolicyEngine::SetCaPublicKey ( RSA::PublicKey  pub)

Set the Certificate Authority's public key for verifying digital signatures.

Set the public key of the Certificate Authority used for signature verification.

Parameters
pubThe RSA public key of the CA.

Definition at line 50 of file zt-policy-engine.cc.

50 {
51 caPublicKey = pub;
52}

Field Documentation

◆ authTable

std::unordered_map<uint32_t, std::string> ns3::ZtPolicyEngine::authTable
private

Maps node ID to assigned role.

Definition at line 74 of file zt-policy-engine.h.

◆ caPublicKey

RSA::PublicKey ns3::ZtPolicyEngine::caPublicKey
private

Public key for certificate signature verification.

Definition at line 76 of file zt-policy-engine.h.

◆ revoke

std::unordered_set<uint32_t> ns3::ZtPolicyEngine::revoke
private

List of revoked node IDs.

Definition at line 75 of file zt-policy-engine.h.


The documentation for this class was generated from the following files: