Zero Trust IoT library
Loading...
Searching...
No Matches
zt-certificate.cc
Go to the documentation of this file.
1#include "zt-certificate.h"
2#include <cryptopp/osrng.h>
3#include <cryptopp/pssr.h>
4#include <cryptopp/sha.h>
5#include <cryptopp/filters.h>
6#include <cryptopp/files.h>
7#include <sstream>
8#include <ns3/core-module.h>
9
10using namespace CryptoPP;
11
17 AutoSeededRandomPool prng;
18 privateKey.GenerateRandomWithKeySize(prng, 1024);
20}
21
29std::string CertificateAuthority::SignIdentity(uint32_t nodeId, const std::string& role, time_t expiry) {
30 AutoSeededRandomPool prng;
31
32 std::ostringstream cert;
33 cert << "ID:" << nodeId << "|ROLE:" << role << "|EXP:" << expiry;
34
35 RSASS<PSSR, SHA1>::Signer signer(privateKey);
36 std::string signature;
37 StringSource(cert.str(), true,
38 new SignerFilter(prng, signer,
39 new StringSink(signature)));
40
41 std::string encodedSig;
42 StringSource(signature, true,
43 new Base64Encoder(new StringSink(encodedSig), false));
44
45 return cert.str() + "|SIG:" + encodedSig;
46}
47
52RSA::PublicKey CertificateAuthority::GetPublicKey() const {
53 return publicKey;
54}
55
61 caPublicKey = pub;
62}
63
68void ZtPolicyEngineWithCert::Revoke(uint32_t nodeId) {
69 revoke.insert(nodeId);
70}
71
79bool ZtPolicyEngineWithCert::Authorize(uint32_t nodeId, const std::string& role, const std::string& certStr) {
80 using namespace ns3;
81
82 if (revoke.find(nodeId) != revoke.end()) {
83 NS_LOG_UNCOND("ZT-CERT: Node " << nodeId << " is revoked");
84 return false;
85 }
86
87 std::string content, sig;
88 size_t sigPos = certStr.find("|SIG:");
89 if (sigPos == std::string::npos) return false;
90 content = certStr.substr(0, sigPos);
91 sig = certStr.substr(sigPos + 5);
92
93 std::string decodedSig;
94 StringSource(sig, true, new Base64Decoder(new StringSink(decodedSig)));
95
96 RSASS<PSSR, SHA1>::Verifier verifier(caPublicKey);
97 bool valid = false;
98 StringSource(decodedSig + content, true,
99 new SignatureVerificationFilter(verifier,
100 new ArraySink((byte*)&valid, sizeof(valid)),
101 SignatureVerificationFilter::PUT_RESULT | SignatureVerificationFilter::SIGNATURE_AT_BEGIN));
102
103 if (!valid) {
104 NS_LOG_UNCOND("ZT-CERT: Signature invalid");
105 return false;
106 }
107
108 std::istringstream ss(content);
109 std::string token;
110 uint32_t idParsed = 0;
111 std::string roleParsed;
112 time_t expiry = 0;
113
114 while (std::getline(ss, token, '|')) {
115 if (token.find("ID:") == 0)
116 idParsed = std::stoul(token.substr(3));
117 else if (token.find("ROLE:") == 0)
118 roleParsed = token.substr(5);
119 else if (token.find("EXP:") == 0)
120 expiry = std::stol(token.substr(4));
121 }
122
123 if (idParsed != nodeId || roleParsed != role) {
124 NS_LOG_UNCOND("ZT-CERT: Identity mismatch");
125 return false;
126 }
127
128 time_t now = std::time(nullptr);
129 if (now > expiry) {
130 NS_LOG_UNCOND("ZT-CERT: Certificate expired");
131 return false;
132 }
133
134 return true;
135}
136
CryptoPP::RSA::PublicKey GetPublicKey() const
Retrieves the public RSA key of the CA.
CryptoPP::RSA::PublicKey publicKey
RSA public key distributed for verification.
CryptoPP::RSA::PrivateKey privateKey
RSA private key used for signing certificates.
CertificateAuthority()
Constructor that initializes and generates RSA key pair.
std::string SignIdentity(uint32_t nodeId, const std::string &role, time_t expiry)
Signs an identity certificate with node ID, role, and expiry.
void Revoke(uint32_t nodeId)
Revokes a node by its ID, preventing it from being authorized.
CryptoPP::RSA::PublicKey caPublicKey
Trusted public key used for signature verification.
bool Authorize(uint32_t nodeId, const std::string &role, const std::string &certStr)
Authorizes a node based on its certificate.
std::unordered_set< uint32_t > revoke
Set of node IDs that are explicitly revoked.
void SetCaPublicKey(CryptoPP::RSA::PublicKey pub)
Sets the CA's public key used for certificate verification.