Commit 1695791d authored by jan.koester's avatar jan.koester
Browse files

test

parent ab7b9365
Loading
Loading
Loading
Loading
+2 −1
Changes for CMakeLists.txt: 2 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -39,7 +39,8 @@ endif()
configure_file(config.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h)

include_directories(
    ${CMAKE_CURRENT_BINARY_DIR}
   "${CMAKE_CURRENT_BINARY_DIR}"
   "${CMAKE_CURRENT_SOURCE_DIR}/src/crypto"
)

install(
+2 −1
Changes for src/crypto/x509.h: 2 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -54,6 +54,8 @@ namespace netplus {
        bool extractPublicKey(netplus::rsa& outRsa);
        std::vector<std::string> getSubjectAltNames();

        std::vector<uint8_t> derData;

    private:

        struct CertificateValidity {
@@ -61,7 +63,6 @@ namespace netplus {
            std::tm notAfter;
        };

        std::vector<uint8_t> derData;
        std::string serialNumber;
        std::string issuer;
        std::string subject;
+2 −0
Changes for src/event/iocp.cpp: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -113,6 +113,8 @@ namespace netplus {

			if (g_serversocket->_Type == TCP) {
				pClientContext->CurCon->csock = std::make_unique<tcp>();
			}else if (g_serversocket->_Type == SSL) {
				pClientContext->CurCon->csock = std::make_unique<ssl>();
			}else {
				NetException exp;
				exp[NetException::Error] << "Wrong Protocol only tcp supported at the moment !";
+21 −15
Changes for src/socket.h: 21 added lines, 15 removed lines.
Original line number Diff line number Diff line
@@ -31,6 +31,9 @@

#include "config.h"

#include "aes.h"
#include "x509.h"

#pragma once

#ifndef Windows  
@@ -197,27 +200,30 @@ namespace netplus {

        class ssl : public tcp {
        public:

            using tcp::operator=;

            ssl();
            /*client socket*/
            // Client constructor with CA certificate for verification
            ssl(const std::string &addr, int port, int maxconnections, int sockopts, const unsigned char *ca, size_t calen);
            /*server socket*/
            ssl(const std::string &addr,int port,int maxconnections,
                int sockopts,const unsigned char *cert,size_t certlen,const unsigned char *key, size_t keylen);
            // Server constructor with own certificate and private key
            ssl(const std::string &addr, int port, int maxconnections, int sockopts, const unsigned char *cert, size_t certlen, const unsigned char *key, size_t keylen);
            ~ssl();

            void          accept(std::unique_ptr<socket> &csock);
            void accept(std::unique_ptr<socket> &csock) override;
            void connect(std::unique_ptr<socket> &csock) override;

            ssl&          operator=(int socket);
            int           getMaxconnections();
            
            size_t sendData(buffer &data,int flags=0);
            size_t recvData(buffer &data,int flags=0);
            
            void connect(std::unique_ptr<socket> &csock);
            size_t sendData(buffer &data, int flags = 0) override;
            size_t recvData(buffer &data, int flags = 0) override;

        private:
            int                      _Maxconnections;
            int                      _Port;
            char                     _Addr[255];
            // Cryptographic Components
            netplus::x509cert _cert;
            netplus::rsa _rsa;
            std::unique_ptr<netplus::aes> _aes; // Created after handshake

            bool _handshakeDone;
            void performServerHandshake();
            void performClientHandshake();
        };
};
+107 −25
Changes for src/ssl.cpp: 107 added lines, 25 removed lines.
Original line number Diff line number Diff line
#include <cstring>
#include <memory>
#include <vector>
#include <random>

#include "exception.h"
#include "socket.h"

#define SSL_DEBUG_LEVEL 0

#define HIDDEN __attribute__ ((visibility ("hidden")))

namespace netplus {
    // Helper to convert decrypted RSA bigInt back to a 16-byte AES key
    std::vector<uint8_t> bigIntToBytes(const rsa::bigInt &plain) {
        std::vector<uint8_t> Key;
        for (size_t i = 0; i < plain.size && i < 16; ++i) {
            Key.push_back(static_cast<uint8_t>(plain.digits[i]));
        }
        // Ensure exact 16 bytes for AES-128
        while (Key.size() < 16) Key.push_back(0);
        if (Key.size() > 16) Key.resize(16);
        return Key;
    }
};

void netplus::ssl::accept(std::unique_ptr<socket> &csock) {
    tcp::accept(csock);
    ssl* ssock = static_cast<ssl*>(csock.get());

    // 1. Send Server Certificate
    buffer certBuf((const char*)_cert.derData.data(), _cert.derData.size());
    ssock->tcp::sendData(certBuf);

    // 2. Receive Encrypted AES Key
    buffer rsaEncryptedKey(512);
    ssock->tcp::recvData(rsaEncryptedKey);

    // 3. Decrypt
    netplus::rsa::bigInt cipher, plain;
    cipher.fromString(std::string(rsaEncryptedKey.data.buf, rsaEncryptedKey.size));
    plain = _rsa.decrypt(cipher);

    // 4. Initialize AES (Fixed function call here)
    std::vector<uint8_t> aesKey = bigIntToBytes(plain);
    ssock->_aes = std::make_unique<netplus::aes>(aesKey);
    ssock->_handshakeDone = true;
}

netplus::ssl::ssl(const std::string &addr,int port,int maxconnections,int sockopts,const unsigned char *ca,size_t calen) : ssl() {
void netplus::ssl::connect(std::unique_ptr<socket> &csock) {
    tcp::connect(csock);

}
    // 1. Receive Server Certificate
    std::vector<uint8_t> certData(4096);
    buffer recvBuf((char*)certData.data(), certData.size());
    size_t len = tcp::recvData(recvBuf);
    certData.resize(len);

netplus::ssl::ssl(const std::string &addr,int port,int maxconnections,int sockopts,const unsigned char *cert,
    size_t certlen, const unsigned char* key, size_t keylen) : ssl() {
    _Port=port;
    _Type=sockettype::SSL;
    // 2. Load and Verify
    netplus::x509cert serverCert;
    serverCert.loadFromBuffer(certData);
    if (!serverCert.checkValidity()) {
        throw std::runtime_error("Invalid Server Certificate");
    }

netplus::ssl::ssl() : tcp(){
     _Type=sockettype::SSL;
}
    // 3. Extract Key and Generate Seed (Logic moved inside function)
    netplus::rsa serverPubKey;
    serverCert.extractPublicKey(serverPubKey);

netplus::ssl::~ssl(){
    std::vector<uint8_t> aesSeed(16);
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dis(0, 255);
    for (int i = 0; i < 16; ++i) {
        aesSeed[i] = static_cast<uint8_t>(dis(gen));
    }

void netplus::ssl::accept(std::unique_ptr<socket> &csock){
    // Convert bytes to bigInt for RSA encryption
    netplus::rsa::bigInt keyInt, encrypted;
    keyInt.fromString("0");
    for (uint8_t b : aesSeed) {
        netplus::rsa::bigInt byteVal, tempMul, base256;
        base256.fromString("256");
        byteVal.fromString(std::to_string((int)b));

        netplus::rsa::multiply(keyInt, base256, tempMul);
        netplus::rsa::add(tempMul, byteVal, keyInt);
    }

netplus::ssl& netplus::ssl::operator=(int sock){
     return *this;
};
    encrypted = serverPubKey.encrypt(keyInt);

int netplus::ssl::getMaxconnections(){
    return _Maxconnections;
    // 4. Send encrypted key
    std::string encStr = encrypted.toString();
    buffer sendKey(encStr.c_str(), encStr.size());
    tcp::sendData(sendKey);

    // 5. Initialize AES
    _aes = std::make_unique<netplus::aes>(aesSeed);
    _handshakeDone = true;
}

size_t netplus::ssl::sendData(buffer &data, int flags) {
    NetException exception;
    return 0;
    if (!_handshakeDone) return tcp::sendData(data, flags);

    std::vector<uint8_t> plaintext(data.data.buf, data.data.buf + data.size);

    // AES Block Padding (Simple Zero Padding for now)
    while(plaintext.size() % 16 != 0) plaintext.push_back(0);

    // Process block by block as your AES class likely takes 16-byte blocks
    std::vector<uint8_t> ciphertext;
    for(size_t i=0; i < plaintext.size(); i+=16) {
        std::vector<uint8_t> block(plaintext.begin()+i, plaintext.begin()+i+16);
        std::vector<uint8_t> encBlock = _aes->encrypt(block);
        ciphertext.insert(ciphertext.end(), encBlock.begin(), encBlock.end());
    }

size_t netplus::ssl::recvData(buffer &data, int flags) {
    return 0;
    buffer encBuf((const char*)ciphertext.data(), ciphertext.size());
    return tcp::sendData(encBuf, flags);
}

void netplus::ssl::connect(std::unique_ptr<socket> &ssock){
size_t netplus::ssl::recvData(buffer &data, int flags) {
    if (!_handshakeDone) return tcp::recvData(data, flags);

    buffer rawData(data.size);
    size_t len = tcp::recvData(rawData, flags);

    std::vector<uint8_t> ciphertext(rawData.data.buf, rawData.data.buf + len);
    std::vector<uint8_t> plaintext;

    for(size_t i=0; i < ciphertext.size(); i+=16) {
        std::vector<uint8_t> block(ciphertext.begin()+i, ciphertext.begin()+i+16);
        std::vector<uint8_t> decBlock = _aes->decrypt(block);
        plaintext.insert(plaintext.end(), decBlock.begin(), decBlock.end());
    }

    size_t copyLen = (plaintext.size() > data.size) ? data.size : plaintext.size();
    std::memcpy(data.data.buf, plaintext.data(), copyLen);
    return copyLen;
}