Loading src/crypto/x509.cpp +34 −38 Changes for src/crypto/x509.cpp: 34 added lines, 38 removed lines. Original line number Diff line number Diff line Loading @@ -35,12 +35,6 @@ #include "base64.h" #include "x509.h" netplus::ASN1Node::ASN1Node(const ASN1Node &src){ tag=src.tag; data=src.data; children=src.children; } netplus::x509cert::x509cert() {} netplus::x509cert::x509cert(const x509cert &cert) { Loading Loading @@ -403,57 +397,59 @@ std::string netplus::x509cert::getSubjectCN() { std::vector<std::string> netplus::x509cert::getSubjectAltNames() { std::vector<std::string> dnsNames; if (root.children.empty()) return dnsNames; // TBSCertificate is the first child of the root sequence auto& tbs = root.children[0]; ASN1Node* extensionsSequence = nullptr; // 1. Find the Extensions Wrapper (Tag 0xA3) // 1) Find Extensions wrapper (A3) ASN1Node* extWrapper = nullptr; for (auto& child : tbs.children) { if (child.tag == 0xA3) { // The 0xA3 tag is a wrapper. The actual Sequence is its first child. if (!child.children.empty()) { extensionsSequence = &child.children[0]; } else { // If the children vector is empty, we must parse the internal // data of the 0xA3 container manually. static ASN1Node manualSync; if (parseInternal(child.data.data(), child.data.size(), manualSync)) { extensionsSequence = &manualSync; } } if (child.tag == 0xA3) { // [3] EXPLICIT Extensions extWrapper = &child; break; } } if (!extWrapper) return dnsNames; // 2) Get the actual Extensions SEQUENCE (usually first child) // If children are missing, parse the wrapper's data into a local node (threadsafe!) const ASN1Node* extensionsSeq = nullptr; ASN1Node parsedWrapper; if (!extensionsSequence) return dnsNames; if (!extWrapper->children.empty()) { extensionsSeq = &extWrapper->children[0]; } else { if (!parseInternal(extWrapper->data.data(), extWrapper->data.size(), parsedWrapper)) { return dnsNames; } if (parsedWrapper.children.empty()) return dnsNames; extensionsSeq = &parsedWrapper.children[0]; } // 2. Iterate through extensions to find OID 2.5.29.17 (Subject Alt Name) for (auto& ext : extensionsSequence->children) { // Each extension is a SEQUENCE: { OID, Critical (optional), ExtnValue (OCTET STRING) } // 3) Iterate extensions, find SAN (2.5.29.17) for (const auto& ext : extensionsSeq->children) { if (ext.children.empty()) continue; std::string oid = decodeOID(ext.children[0].data); if (oid == "2.5.29.17") { // Find the OCTET STRING (Tag 0x04) which holds the SAN data for (auto& valNode : ext.children) { if (valNode.tag == 0x04) { if (oid != "2.5.29.17") continue; // extnValue is OCTET STRING (04) holding DER of GeneralNames for (const auto& valNode : ext.children) { if (valNode.tag != 0x04) continue; ASN1Node sanData; // The OCTET STRING contains a DER-encoded SEQUENCE of GeneralNames if (parseInternal(valNode.data.data(), valNode.data.size(), sanData)) { for (auto& name : sanData.children) { // 0x82 is Context-Specific Tag [2] (dNSName) if (!parseInternal(valNode.data.data(), valNode.data.size(), sanData)) continue; for (const auto& name : sanData.children) { // 0x82 = [2] dNSName if (name.tag == 0x82) { dnsNames.push_back(std::string(name.data.begin(), name.data.end())); } } } dnsNames.emplace_back(name.data.begin(), name.data.end()); } } } } return dnsNames; } Loading src/crypto/x509.h +8 −3 Changes for src/crypto/x509.h: 8 added lines, 3 removed lines. Original line number Diff line number Diff line Loading @@ -35,11 +35,16 @@ namespace netplus { struct ASN1Node { ASN1Node() = default; ASN1Node(const ASN1Node &src); uint8_t tag; uint8_t tag{}; std::vector<uint8_t> data; std::vector<ASN1Node> children; ASN1Node() = default; ASN1Node(const ASN1Node&) = default; ASN1Node& operator=(const ASN1Node&) = default; ASN1Node(ASN1Node&&) noexcept = default; ASN1Node& operator=(ASN1Node&&) noexcept = default; ~ASN1Node() = default; }; class x509cert { Loading src/event/epoll.cpp +37 −17 Changes for src/event/epoll.cpp: 37 added lines, 17 removed lines. Original line number Diff line number Diff line Loading @@ -239,11 +239,19 @@ namespace netplus { for (;;) { buffer buf(BLOCKSIZE); // recvData throws NetException::Note on EAGAIN/EWOULDBLOCK size_t rcv = rcon->csock->recvData(buf); size_t rcv = 0; try { rcv = rcon->csock->recvData(buf, 0); } catch (NetException& e) { if (e.getErrorType() == NetException::Note) { // would-block: stop draining break; } throw; // real error } if (rcv == 0) { // recv()==0 => peer closed // peer closed NetException e; e[NetException::Close] << "ReadEventHandler: peer closed connection (EOF)"; throw e; Loading @@ -252,27 +260,36 @@ namespace netplus { rcon->RecvData.append(buf.data.buf, rcv); rcon->lasteventime = time(nullptr); // Let protocol layer consume/produce data NOW evconnection->RequestEvent(*rcon, tid, args); // If we now have pending output -> switch to write if (rcon->SendOff < rcon->SendData.size()) { setpollEvents(pos, EPOLLOUT | EPOLLONESHOT); return; } // else continue draining reads until would-block } // Draining done. Rearm interest. if (rcon->SendOff < rcon->SendData.size()) setpollEvents(pos, EPOLLOUT | EPOLLONESHOT); else setpollEvents(pos, EPOLLIN | EPOLLONESHOT); return; } catch (NetException& e) { if (e.getErrorType() == NetException::Note) { // would-block: re-arm read unless we have pending output // Usually won't happen anymore because we catch Note above, // but keep it defensive. if (rcon->SendOff < rcon->SendData.size()) setpollEvents(pos, EPOLLOUT | EPOLLONESHOT); else setpollEvents(pos, EPOLLIN | EPOLLONESHOT); return; } // Close/error: you probably want to cleanup connection here instead of throw // but keep your current behavior: throw; throw; // Close/Error -> your outer loop should cleanup } } Loading @@ -284,18 +301,15 @@ namespace netplus { if (!wcon->csock) return; try { // IMPORTANT: do not generate new response here repeatedly. // If you want: call it once when entering write mode (optional). // evconnection->ResponseEvent(*wcon, tid, args); // 1) Flush existing queued plaintext as far as possible while (!wcon->SendData.empty()) { size_t toSend = std::min<size_t>(BLOCKSIZE, wcon->SendData.size()); const size_t toSend = std::min<size_t>(BLOCKSIZE, wcon->SendData.size()); buffer out(wcon->SendData.data(), toSend); size_t consumed = wcon->csock->sendData(out, 0); // consumed plaintext const size_t consumed = wcon->csock->sendData(out, 0); // plaintext consumed if (consumed == 0) { // would-block OR ssl still flushing its internal pending record // would-block OR ssl is still flushing internal TLS record setpollEvents(pos, EPOLLOUT | EPOLLONESHOT); return; } Loading @@ -305,10 +319,16 @@ namespace netplus { wcon->lasteventime = time(nullptr); } // 2) Queue is empty -> ask protocol layer for more (ONCE) evconnection->ResponseEvent(*wcon, tid, args); // queue empty -> go back to read if(wcon->SendData.empty()) // 3) If ResponseEvent produced more output, stay in write mode if (!wcon->SendData.empty()) { setpollEvents(pos, EPOLLOUT | EPOLLONESHOT); return; } // 4) Otherwise go back to read setpollEvents(pos, EPOLLIN | EPOLLONESHOT); return; Loading src/ssl.cpp +1 −0 Changes for src/ssl.cpp: 1 added line, 0 removed lines. Original line number Diff line number Diff line Loading @@ -312,6 +312,7 @@ namespace netplus { netplus::ssl::ssl(const netplus::x509cert &cert) : _cert(cert) , _aes(nullptr) { _Type=sockettype::SSL; } netplus::ssl::ssl(const netplus::x509cert &cert,int sock) : tcp (sock), _cert(cert) ,_aes(nullptr) { _Type=sockettype::SSL; }; Loading Loading
src/crypto/x509.cpp +34 −38 Changes for src/crypto/x509.cpp: 34 added lines, 38 removed lines. Original line number Diff line number Diff line Loading @@ -35,12 +35,6 @@ #include "base64.h" #include "x509.h" netplus::ASN1Node::ASN1Node(const ASN1Node &src){ tag=src.tag; data=src.data; children=src.children; } netplus::x509cert::x509cert() {} netplus::x509cert::x509cert(const x509cert &cert) { Loading Loading @@ -403,57 +397,59 @@ std::string netplus::x509cert::getSubjectCN() { std::vector<std::string> netplus::x509cert::getSubjectAltNames() { std::vector<std::string> dnsNames; if (root.children.empty()) return dnsNames; // TBSCertificate is the first child of the root sequence auto& tbs = root.children[0]; ASN1Node* extensionsSequence = nullptr; // 1. Find the Extensions Wrapper (Tag 0xA3) // 1) Find Extensions wrapper (A3) ASN1Node* extWrapper = nullptr; for (auto& child : tbs.children) { if (child.tag == 0xA3) { // The 0xA3 tag is a wrapper. The actual Sequence is its first child. if (!child.children.empty()) { extensionsSequence = &child.children[0]; } else { // If the children vector is empty, we must parse the internal // data of the 0xA3 container manually. static ASN1Node manualSync; if (parseInternal(child.data.data(), child.data.size(), manualSync)) { extensionsSequence = &manualSync; } } if (child.tag == 0xA3) { // [3] EXPLICIT Extensions extWrapper = &child; break; } } if (!extWrapper) return dnsNames; // 2) Get the actual Extensions SEQUENCE (usually first child) // If children are missing, parse the wrapper's data into a local node (threadsafe!) const ASN1Node* extensionsSeq = nullptr; ASN1Node parsedWrapper; if (!extensionsSequence) return dnsNames; if (!extWrapper->children.empty()) { extensionsSeq = &extWrapper->children[0]; } else { if (!parseInternal(extWrapper->data.data(), extWrapper->data.size(), parsedWrapper)) { return dnsNames; } if (parsedWrapper.children.empty()) return dnsNames; extensionsSeq = &parsedWrapper.children[0]; } // 2. Iterate through extensions to find OID 2.5.29.17 (Subject Alt Name) for (auto& ext : extensionsSequence->children) { // Each extension is a SEQUENCE: { OID, Critical (optional), ExtnValue (OCTET STRING) } // 3) Iterate extensions, find SAN (2.5.29.17) for (const auto& ext : extensionsSeq->children) { if (ext.children.empty()) continue; std::string oid = decodeOID(ext.children[0].data); if (oid == "2.5.29.17") { // Find the OCTET STRING (Tag 0x04) which holds the SAN data for (auto& valNode : ext.children) { if (valNode.tag == 0x04) { if (oid != "2.5.29.17") continue; // extnValue is OCTET STRING (04) holding DER of GeneralNames for (const auto& valNode : ext.children) { if (valNode.tag != 0x04) continue; ASN1Node sanData; // The OCTET STRING contains a DER-encoded SEQUENCE of GeneralNames if (parseInternal(valNode.data.data(), valNode.data.size(), sanData)) { for (auto& name : sanData.children) { // 0x82 is Context-Specific Tag [2] (dNSName) if (!parseInternal(valNode.data.data(), valNode.data.size(), sanData)) continue; for (const auto& name : sanData.children) { // 0x82 = [2] dNSName if (name.tag == 0x82) { dnsNames.push_back(std::string(name.data.begin(), name.data.end())); } } } dnsNames.emplace_back(name.data.begin(), name.data.end()); } } } } return dnsNames; } Loading
src/crypto/x509.h +8 −3 Changes for src/crypto/x509.h: 8 added lines, 3 removed lines. Original line number Diff line number Diff line Loading @@ -35,11 +35,16 @@ namespace netplus { struct ASN1Node { ASN1Node() = default; ASN1Node(const ASN1Node &src); uint8_t tag; uint8_t tag{}; std::vector<uint8_t> data; std::vector<ASN1Node> children; ASN1Node() = default; ASN1Node(const ASN1Node&) = default; ASN1Node& operator=(const ASN1Node&) = default; ASN1Node(ASN1Node&&) noexcept = default; ASN1Node& operator=(ASN1Node&&) noexcept = default; ~ASN1Node() = default; }; class x509cert { Loading
src/event/epoll.cpp +37 −17 Changes for src/event/epoll.cpp: 37 added lines, 17 removed lines. Original line number Diff line number Diff line Loading @@ -239,11 +239,19 @@ namespace netplus { for (;;) { buffer buf(BLOCKSIZE); // recvData throws NetException::Note on EAGAIN/EWOULDBLOCK size_t rcv = rcon->csock->recvData(buf); size_t rcv = 0; try { rcv = rcon->csock->recvData(buf, 0); } catch (NetException& e) { if (e.getErrorType() == NetException::Note) { // would-block: stop draining break; } throw; // real error } if (rcv == 0) { // recv()==0 => peer closed // peer closed NetException e; e[NetException::Close] << "ReadEventHandler: peer closed connection (EOF)"; throw e; Loading @@ -252,27 +260,36 @@ namespace netplus { rcon->RecvData.append(buf.data.buf, rcv); rcon->lasteventime = time(nullptr); // Let protocol layer consume/produce data NOW evconnection->RequestEvent(*rcon, tid, args); // If we now have pending output -> switch to write if (rcon->SendOff < rcon->SendData.size()) { setpollEvents(pos, EPOLLOUT | EPOLLONESHOT); return; } // else continue draining reads until would-block } // Draining done. Rearm interest. if (rcon->SendOff < rcon->SendData.size()) setpollEvents(pos, EPOLLOUT | EPOLLONESHOT); else setpollEvents(pos, EPOLLIN | EPOLLONESHOT); return; } catch (NetException& e) { if (e.getErrorType() == NetException::Note) { // would-block: re-arm read unless we have pending output // Usually won't happen anymore because we catch Note above, // but keep it defensive. if (rcon->SendOff < rcon->SendData.size()) setpollEvents(pos, EPOLLOUT | EPOLLONESHOT); else setpollEvents(pos, EPOLLIN | EPOLLONESHOT); return; } // Close/error: you probably want to cleanup connection here instead of throw // but keep your current behavior: throw; throw; // Close/Error -> your outer loop should cleanup } } Loading @@ -284,18 +301,15 @@ namespace netplus { if (!wcon->csock) return; try { // IMPORTANT: do not generate new response here repeatedly. // If you want: call it once when entering write mode (optional). // evconnection->ResponseEvent(*wcon, tid, args); // 1) Flush existing queued plaintext as far as possible while (!wcon->SendData.empty()) { size_t toSend = std::min<size_t>(BLOCKSIZE, wcon->SendData.size()); const size_t toSend = std::min<size_t>(BLOCKSIZE, wcon->SendData.size()); buffer out(wcon->SendData.data(), toSend); size_t consumed = wcon->csock->sendData(out, 0); // consumed plaintext const size_t consumed = wcon->csock->sendData(out, 0); // plaintext consumed if (consumed == 0) { // would-block OR ssl still flushing its internal pending record // would-block OR ssl is still flushing internal TLS record setpollEvents(pos, EPOLLOUT | EPOLLONESHOT); return; } Loading @@ -305,10 +319,16 @@ namespace netplus { wcon->lasteventime = time(nullptr); } // 2) Queue is empty -> ask protocol layer for more (ONCE) evconnection->ResponseEvent(*wcon, tid, args); // queue empty -> go back to read if(wcon->SendData.empty()) // 3) If ResponseEvent produced more output, stay in write mode if (!wcon->SendData.empty()) { setpollEvents(pos, EPOLLOUT | EPOLLONESHOT); return; } // 4) Otherwise go back to read setpollEvents(pos, EPOLLIN | EPOLLONESHOT); return; Loading
src/ssl.cpp +1 −0 Changes for src/ssl.cpp: 1 added line, 0 removed lines. Original line number Diff line number Diff line Loading @@ -312,6 +312,7 @@ namespace netplus { netplus::ssl::ssl(const netplus::x509cert &cert) : _cert(cert) , _aes(nullptr) { _Type=sockettype::SSL; } netplus::ssl::ssl(const netplus::x509cert &cert,int sock) : tcp (sock), _cert(cert) ,_aes(nullptr) { _Type=sockettype::SSL; }; Loading