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>
8#include <ns3/core-module.h>
10using namespace CryptoPP;
17 AutoSeededRandomPool prng;
18 privateKey.GenerateRandomWithKeySize(prng, 1024);
30 AutoSeededRandomPool prng;
32 std::ostringstream cert;
33 cert <<
"ID:" << nodeId <<
"|ROLE:" << role <<
"|EXP:" << expiry;
36 std::string signature;
37 StringSource(cert.str(),
true,
38 new SignerFilter(prng, signer,
39 new StringSink(signature)));
41 std::string encodedSig;
42 StringSource(signature,
true,
43 new Base64Encoder(
new StringSink(encodedSig),
false));
45 return cert.str() +
"|SIG:" + encodedSig;
83 NS_LOG_UNCOND(
"ZT-CERT: Node " << nodeId <<
" is revoked");
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);
93 std::string decodedSig;
94 StringSource(sig,
true,
new Base64Decoder(
new StringSink(decodedSig)));
98 StringSource(decodedSig + content,
true,
99 new SignatureVerificationFilter(verifier,
100 new ArraySink((
byte*)&valid,
sizeof(valid)),
101 SignatureVerificationFilter::PUT_RESULT | SignatureVerificationFilter::SIGNATURE_AT_BEGIN));
104 NS_LOG_UNCOND(
"ZT-CERT: Signature invalid");
108 std::istringstream ss(content);
110 uint32_t idParsed = 0;
111 std::string roleParsed;
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));
123 if (idParsed != nodeId || roleParsed != role) {
124 NS_LOG_UNCOND(
"ZT-CERT: Identity mismatch");
128 time_t now = std::time(
nullptr);
130 NS_LOG_UNCOND(
"ZT-CERT: Certificate expired");
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.