Loading src/crypto/aes.cpp +90 −0 Changes for src/crypto/aes.cpp: 90 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -565,5 +565,95 @@ namespace netplus { return state; // plaintext in AES State byte order (matches how you pass vectors) } /** * encryptCBC * @param plaintext: The raw data to encrypt * @param iv: 16-byte Initialization Vector * @return: Encrypted ciphertext (length will be multiple of 16) */ std::vector<uint8_t> aes::encryptCBC(const std::vector<uint8_t>& plaintext, const std::vector<uint8_t>& iv) { if (iv.size() != 16) { throw std::invalid_argument("AES CBC requires a 16-byte IV."); } // 1. Handle PKCS#7 Padding // Even if data is 16 bytes, we add a full block of padding per standard std::vector<uint8_t> data = plaintext; uint8_t paddingLen = 16 - (data.size() % 16); for (int i = 0; i < paddingLen; ++i) { data.push_back(paddingLen); } std::vector<uint8_t> ciphertext; ciphertext.reserve(data.size()); // The "previous" block for the first iteration is the IV block128 previousBlock(16); std::copy(iv.begin(), iv.end(), previousBlock.begin()); // 2. Process block by block for (size_t i = 0; i < data.size(); i += 16) { block128 currentBlock(16); // XOR current plaintext block with the previous ciphertext block (or IV) for (int j = 0; j < 16; ++j) { currentBlock[j] = data[i + j] ^ previousBlock[j]; } // Encrypt the XORed block using your existing AES-128 core block128 encrypted = encrypt(currentBlock); // Store result and update 'previousBlock' for the next chain link ciphertext.insert(ciphertext.end(), encrypted.begin(), encrypted.end()); previousBlock = encrypted; } return ciphertext; } /** * decryptCBC * @param ciphertext: The encrypted data * @param iv: 16-byte Initialization Vector * @return: Decrypted and unpadded plaintext */ std::vector<uint8_t> aes::decryptCBC(const std::vector<uint8_t>& ciphertext, const std::vector<uint8_t>& iv) { if (ciphertext.size() % 16 != 0 || ciphertext.empty()) { throw std::invalid_argument("Ciphertext length must be a multiple of 16."); } std::vector<uint8_t> plaintext; plaintext.reserve(ciphertext.size()); block128 previousBlock(16); std::copy(iv.begin(), iv.end(), previousBlock.begin()); for (size_t i = 0; i < ciphertext.size(); i += 16) { block128 currentCipherBlock(16); std::copy(ciphertext.begin() + i, ciphertext.begin() + i + 16, currentCipherBlock.begin()); // Decrypt the block first block128 decrypted = decrypt(currentCipherBlock); // XOR with the previous ciphertext block (or IV) to get plaintext for (int j = 0; j < 16; ++j) { plaintext.push_back(decrypted[j] ^ previousBlock[j]); } // Update previousBlock to the current ciphertext block for the next iteration previousBlock = currentCipherBlock; } // 3. Remove PKCS#7 Padding if (!plaintext.empty()) { uint8_t paddingLen = plaintext.back(); if (paddingLen > 0 && paddingLen <= 16) { plaintext.resize(plaintext.size() - paddingLen); } } return plaintext; } // In aes.cpp } // namespace netplus src/crypto/aes.h +16 −0 Changes for src/crypto/aes.h: 16 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -92,6 +92,22 @@ public: * @return The 16-byte plaintext block. */ block128 decrypt(const block128& ciphertext); /** * encryptCBC * @param plaintext: The raw data to encrypt * @param iv: 16-byte Initialization Vector * @return: Encrypted ciphertext (length will be multiple of 16) */ std::vector<uint8_t> encryptCBC(const std::vector<uint8_t>& plaintext, const std::vector<uint8_t>& iv); /** * decryptCBC * @param ciphertext: The encrypted data * @param iv: 16-byte Initialization Vector * @return: Decrypted and unpadded plaintext */ std::vector<uint8_t> decryptCBC(const std::vector<uint8_t>& ciphertext, const std::vector<uint8_t>& iv); }; } // namespace netplus src/crypto/x509.cpp +23 −5 Changes for src/crypto/x509.cpp: 23 added lines, 5 removed lines. Original line number Diff line number Diff line Loading @@ -26,14 +26,30 @@ #include <fstream> #include <iomanip> #include <sstream> #include <ctime> #include "x509.h" netplus::ASN1Node::ASN1Node(const ASN1Node &src){ tag=src.tag; data=src.data; children=src.children; } netplus::x509cert::x509cert() {} #include <ctime> #include <iomanip> #include <sstream> netplus::x509cert::x509cert(const x509cert &cert) { root=cert.root; derData=cert.derData; } netplus::x509cert& netplus::x509cert::operator=(const x509cert &cert) { if (this != &cert) { root=cert.root; derData=cert.derData; } return *this; } time_t netplus::x509cert::parseASN1Time(const std::vector<uint8_t>& data, uint8_t tag) { std::string timeStr(data.begin(), data.end()); Loading Loading @@ -99,11 +115,13 @@ bool netplus::x509cert::checkValidity() { bool netplus::x509cert::loadFromBuffer(const std::vector<uint8_t>& buffer) { if (buffer.empty()) return false; derData=buffer; // Clear any existing tree state before parsing new data root = ASN1Node(); // Start the recursive parsing from the beginning of the buffer size_t bytesRead = parseInternal(buffer.data(), buffer.size(), root); // Start the recursive parsing from the beginning of the derData size_t bytesRead = parseInternal(derData.data(), derData.size(), root); // Return true if we successfully parsed at least the root sequence return bytesRead > 0; Loading src/crypto/x509.h +7 −6 Changes for src/crypto/x509.h: 7 added lines, 6 removed lines. Original line number Diff line number Diff line Loading @@ -35,6 +35,8 @@ namespace netplus { struct ASN1Node { ASN1Node() = default; ASN1Node(const ASN1Node &src); uint8_t tag; std::vector<uint8_t> data; std::vector<ASN1Node> children; Loading @@ -43,19 +45,19 @@ namespace netplus { class x509cert { public: x509cert(); x509cert(const x509cert &cert); x509cert& operator=(const x509cert &cert); // Load binary DER data bool loadFromBuffer(const std::vector<uint8_t>& buffer); bool loadFromFile(const std::string& filePath); bool checkValidity(); std::string getSubjectCN(); bool extractPublicKey(netplus::rsa& outRsa); std::vector<std::string> getSubjectAltNames(); std::vector<uint8_t> derData; private: struct CertificateValidity { Loading @@ -63,9 +65,7 @@ namespace netplus { std::tm notAfter; }; std::string serialNumber; std::string issuer; std::string subject; std::vector<uint8_t> derData; // Internal ASN.1 parsing helpers ASN1Node root; Loading @@ -74,5 +74,6 @@ namespace netplus { std::string decodeOID(const std::vector<uint8_t>& data); std::string bytesToHex(const std::vector<uint8_t>& bytes); void bytesToBigInt(const std::vector<uint8_t>& bytes, netplus::rsa::bigInt& out); friend class ssl; }; }; src/socket.h +43 −8 Changes for src/socket.h: 43 added lines, 8 removed lines. Original line number Diff line number Diff line Loading @@ -200,14 +200,13 @@ namespace netplus { class ssl : public tcp { public: using tcp::operator=; ssl(const netplus::x509cert cert) : _cert(cert) , _aes(nullptr) {} ssl(const netplus::x509cert cert,int sock) : tcp (sock), _cert(cert) ,_aes(nullptr) {}; ssl(const netplus::x509cert &cert); ssl(const netplus::x509cert &cert,int sock); ~ssl(){}; ssl(const std::string &addr,int port,int maxconnections,int sockopts,const netplus::x509cert cert); ssl(const std::string &addr,int port,int maxconnections,int sockopts,const netplus::x509cert &cert); void accept(std::unique_ptr<socket> &csock) override; void connect(std::unique_ptr<socket> &csock) override; Loading @@ -216,14 +215,50 @@ namespace netplus { size_t recvData(buffer &data, int flags = 0) override; private: // Cryptographic Components // --- Cryptographic Components --- netplus::x509cert _cert; netplus::rsa _rsa; std::unique_ptr<netplus::aes> _aes; // Created after handshake // Encryption engines (One for sending, one for receiving) std::unique_ptr<netplus::aes> _aes; // Server Write Key (outbound) std::unique_ptr<netplus::aes> _aes_recv; // Client Write Key (inbound) // --- Key Material (TLS 1.2) --- std::vector<uint8_t> _mac_key; // Server Write MAC Key std::vector<uint8_t> _client_mac_key; // Client Write MAC Key // Handshake Randoms (Required for Key Expansion PRF) std::vector<uint8_t> _clientRandom; std::vector<uint8_t> _serverRandom; // Sequence numbers (Prevents replay attacks, required for HMAC) uint64_t _send_seq = 0; uint64_t _recv_seq = 0; std::vector<uint8_t> _sha1_hash(const std::vector<uint8_t>& input) ; bool _handshakeDone; void performServerHandshake(); void performClientHandshake(); // --- Internal Handshake Helpers --- void _sendHandshake( uint8_t handshakeType, const std::vector<uint8_t>& data, std::unique_ptr<socket>& csock ); void _sendServerHello(std::unique_ptr<socket> &csock); // PRF (Pseudo-Random Function) for Key Derivation std::vector<uint8_t> _prf( const std::vector<uint8_t>& secret, const std::string& label, const std::vector<uint8_t>& seed, size_t outLen ); std::vector<uint8_t> _hmac_sha1(const std::vector<uint8_t>& key, const std::vector<uint8_t>& data) ; // HMAC calculation for Record Layer std::vector<uint8_t> _calculateHMAC(const std::vector<uint8_t>& data, uint8_t type); friend class poll; }; }; Loading
src/crypto/aes.cpp +90 −0 Changes for src/crypto/aes.cpp: 90 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -565,5 +565,95 @@ namespace netplus { return state; // plaintext in AES State byte order (matches how you pass vectors) } /** * encryptCBC * @param plaintext: The raw data to encrypt * @param iv: 16-byte Initialization Vector * @return: Encrypted ciphertext (length will be multiple of 16) */ std::vector<uint8_t> aes::encryptCBC(const std::vector<uint8_t>& plaintext, const std::vector<uint8_t>& iv) { if (iv.size() != 16) { throw std::invalid_argument("AES CBC requires a 16-byte IV."); } // 1. Handle PKCS#7 Padding // Even if data is 16 bytes, we add a full block of padding per standard std::vector<uint8_t> data = plaintext; uint8_t paddingLen = 16 - (data.size() % 16); for (int i = 0; i < paddingLen; ++i) { data.push_back(paddingLen); } std::vector<uint8_t> ciphertext; ciphertext.reserve(data.size()); // The "previous" block for the first iteration is the IV block128 previousBlock(16); std::copy(iv.begin(), iv.end(), previousBlock.begin()); // 2. Process block by block for (size_t i = 0; i < data.size(); i += 16) { block128 currentBlock(16); // XOR current plaintext block with the previous ciphertext block (or IV) for (int j = 0; j < 16; ++j) { currentBlock[j] = data[i + j] ^ previousBlock[j]; } // Encrypt the XORed block using your existing AES-128 core block128 encrypted = encrypt(currentBlock); // Store result and update 'previousBlock' for the next chain link ciphertext.insert(ciphertext.end(), encrypted.begin(), encrypted.end()); previousBlock = encrypted; } return ciphertext; } /** * decryptCBC * @param ciphertext: The encrypted data * @param iv: 16-byte Initialization Vector * @return: Decrypted and unpadded plaintext */ std::vector<uint8_t> aes::decryptCBC(const std::vector<uint8_t>& ciphertext, const std::vector<uint8_t>& iv) { if (ciphertext.size() % 16 != 0 || ciphertext.empty()) { throw std::invalid_argument("Ciphertext length must be a multiple of 16."); } std::vector<uint8_t> plaintext; plaintext.reserve(ciphertext.size()); block128 previousBlock(16); std::copy(iv.begin(), iv.end(), previousBlock.begin()); for (size_t i = 0; i < ciphertext.size(); i += 16) { block128 currentCipherBlock(16); std::copy(ciphertext.begin() + i, ciphertext.begin() + i + 16, currentCipherBlock.begin()); // Decrypt the block first block128 decrypted = decrypt(currentCipherBlock); // XOR with the previous ciphertext block (or IV) to get plaintext for (int j = 0; j < 16; ++j) { plaintext.push_back(decrypted[j] ^ previousBlock[j]); } // Update previousBlock to the current ciphertext block for the next iteration previousBlock = currentCipherBlock; } // 3. Remove PKCS#7 Padding if (!plaintext.empty()) { uint8_t paddingLen = plaintext.back(); if (paddingLen > 0 && paddingLen <= 16) { plaintext.resize(plaintext.size() - paddingLen); } } return plaintext; } // In aes.cpp } // namespace netplus
src/crypto/aes.h +16 −0 Changes for src/crypto/aes.h: 16 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -92,6 +92,22 @@ public: * @return The 16-byte plaintext block. */ block128 decrypt(const block128& ciphertext); /** * encryptCBC * @param plaintext: The raw data to encrypt * @param iv: 16-byte Initialization Vector * @return: Encrypted ciphertext (length will be multiple of 16) */ std::vector<uint8_t> encryptCBC(const std::vector<uint8_t>& plaintext, const std::vector<uint8_t>& iv); /** * decryptCBC * @param ciphertext: The encrypted data * @param iv: 16-byte Initialization Vector * @return: Decrypted and unpadded plaintext */ std::vector<uint8_t> decryptCBC(const std::vector<uint8_t>& ciphertext, const std::vector<uint8_t>& iv); }; } // namespace netplus
src/crypto/x509.cpp +23 −5 Changes for src/crypto/x509.cpp: 23 added lines, 5 removed lines. Original line number Diff line number Diff line Loading @@ -26,14 +26,30 @@ #include <fstream> #include <iomanip> #include <sstream> #include <ctime> #include "x509.h" netplus::ASN1Node::ASN1Node(const ASN1Node &src){ tag=src.tag; data=src.data; children=src.children; } netplus::x509cert::x509cert() {} #include <ctime> #include <iomanip> #include <sstream> netplus::x509cert::x509cert(const x509cert &cert) { root=cert.root; derData=cert.derData; } netplus::x509cert& netplus::x509cert::operator=(const x509cert &cert) { if (this != &cert) { root=cert.root; derData=cert.derData; } return *this; } time_t netplus::x509cert::parseASN1Time(const std::vector<uint8_t>& data, uint8_t tag) { std::string timeStr(data.begin(), data.end()); Loading Loading @@ -99,11 +115,13 @@ bool netplus::x509cert::checkValidity() { bool netplus::x509cert::loadFromBuffer(const std::vector<uint8_t>& buffer) { if (buffer.empty()) return false; derData=buffer; // Clear any existing tree state before parsing new data root = ASN1Node(); // Start the recursive parsing from the beginning of the buffer size_t bytesRead = parseInternal(buffer.data(), buffer.size(), root); // Start the recursive parsing from the beginning of the derData size_t bytesRead = parseInternal(derData.data(), derData.size(), root); // Return true if we successfully parsed at least the root sequence return bytesRead > 0; Loading
src/crypto/x509.h +7 −6 Changes for src/crypto/x509.h: 7 added lines, 6 removed lines. Original line number Diff line number Diff line Loading @@ -35,6 +35,8 @@ namespace netplus { struct ASN1Node { ASN1Node() = default; ASN1Node(const ASN1Node &src); uint8_t tag; std::vector<uint8_t> data; std::vector<ASN1Node> children; Loading @@ -43,19 +45,19 @@ namespace netplus { class x509cert { public: x509cert(); x509cert(const x509cert &cert); x509cert& operator=(const x509cert &cert); // Load binary DER data bool loadFromBuffer(const std::vector<uint8_t>& buffer); bool loadFromFile(const std::string& filePath); bool checkValidity(); std::string getSubjectCN(); bool extractPublicKey(netplus::rsa& outRsa); std::vector<std::string> getSubjectAltNames(); std::vector<uint8_t> derData; private: struct CertificateValidity { Loading @@ -63,9 +65,7 @@ namespace netplus { std::tm notAfter; }; std::string serialNumber; std::string issuer; std::string subject; std::vector<uint8_t> derData; // Internal ASN.1 parsing helpers ASN1Node root; Loading @@ -74,5 +74,6 @@ namespace netplus { std::string decodeOID(const std::vector<uint8_t>& data); std::string bytesToHex(const std::vector<uint8_t>& bytes); void bytesToBigInt(const std::vector<uint8_t>& bytes, netplus::rsa::bigInt& out); friend class ssl; }; };
src/socket.h +43 −8 Changes for src/socket.h: 43 added lines, 8 removed lines. Original line number Diff line number Diff line Loading @@ -200,14 +200,13 @@ namespace netplus { class ssl : public tcp { public: using tcp::operator=; ssl(const netplus::x509cert cert) : _cert(cert) , _aes(nullptr) {} ssl(const netplus::x509cert cert,int sock) : tcp (sock), _cert(cert) ,_aes(nullptr) {}; ssl(const netplus::x509cert &cert); ssl(const netplus::x509cert &cert,int sock); ~ssl(){}; ssl(const std::string &addr,int port,int maxconnections,int sockopts,const netplus::x509cert cert); ssl(const std::string &addr,int port,int maxconnections,int sockopts,const netplus::x509cert &cert); void accept(std::unique_ptr<socket> &csock) override; void connect(std::unique_ptr<socket> &csock) override; Loading @@ -216,14 +215,50 @@ namespace netplus { size_t recvData(buffer &data, int flags = 0) override; private: // Cryptographic Components // --- Cryptographic Components --- netplus::x509cert _cert; netplus::rsa _rsa; std::unique_ptr<netplus::aes> _aes; // Created after handshake // Encryption engines (One for sending, one for receiving) std::unique_ptr<netplus::aes> _aes; // Server Write Key (outbound) std::unique_ptr<netplus::aes> _aes_recv; // Client Write Key (inbound) // --- Key Material (TLS 1.2) --- std::vector<uint8_t> _mac_key; // Server Write MAC Key std::vector<uint8_t> _client_mac_key; // Client Write MAC Key // Handshake Randoms (Required for Key Expansion PRF) std::vector<uint8_t> _clientRandom; std::vector<uint8_t> _serverRandom; // Sequence numbers (Prevents replay attacks, required for HMAC) uint64_t _send_seq = 0; uint64_t _recv_seq = 0; std::vector<uint8_t> _sha1_hash(const std::vector<uint8_t>& input) ; bool _handshakeDone; void performServerHandshake(); void performClientHandshake(); // --- Internal Handshake Helpers --- void _sendHandshake( uint8_t handshakeType, const std::vector<uint8_t>& data, std::unique_ptr<socket>& csock ); void _sendServerHello(std::unique_ptr<socket> &csock); // PRF (Pseudo-Random Function) for Key Derivation std::vector<uint8_t> _prf( const std::vector<uint8_t>& secret, const std::string& label, const std::vector<uint8_t>& seed, size_t outLen ); std::vector<uint8_t> _hmac_sha1(const std::vector<uint8_t>& key, const std::vector<uint8_t>& data) ; // HMAC calculation for Record Layer std::vector<uint8_t> _calculateHMAC(const std::vector<uint8_t>& data, uint8_t type); friend class poll; }; };