Commit c1f73b94 authored by jan.koester's avatar jan.koester
Browse files

x509 initial support

parent 20839611
Loading
Loading
Loading
Loading
+2 −0
Changes for src/CMakeLists.txt: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ list(APPEND netplussrc
    crypto/aes.cpp
    crypto/rc4.cpp
    crypto/rsa.cpp
    crypto/x509.cpp
)

if(${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Windows")
@@ -49,6 +50,7 @@ set(headers
    crypto/aes.h
    crypto/rc4.h
    crypto/rsa.h
    crypto/x509.h
)

add_library(netplus SHARED ${netplussrc} ${headers})

src/crypto/x509.cpp

0 → 100644
+80 −0
Changes for src/crypto/x509.cpp: 80 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*******************************************************************************
 * Copyright (c) 2025, Jan Koester jan.koester@gmx.net
 * All rights reserved.
 * * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the <organization> nor the
 * names of its contributors may be used to endorse or promote products
 * derived from this software without specific prior written permission.
 * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/

#include <fstream>
#include <iomanip>
#include <sstream>

#include "x509.h"

netplus::x509cert::x509cert() {}

bool netplus::x509cert::loadFromFile(const std::string& filePath) {
    std::ifstream file(filePath, std::ios::binary | std::ios::ate);
    if (!file.is_open()) return false;

    std::streamsize size = file.tellg();
    file.seekg(0, std::ios::beg);

    derData.resize(size);
    if (!file.read((char*)derData.data(), size)) return false;

    // A very basic manual parse would begin here.
    // X.509 starts with a Sequence tag (0x30).
    if (derData[0] != 0x30) return false;

    // In a real implementation, you would recursively traverse the TLV
    // to find the 'Subject' (OID 2.5.4.3) and 'Issuer' fields.
    return true;
}

size_t netplus::x509cert::parseTLV(const uint8_t* data, size_t size, uint8_t& tag, std::vector<uint8_t>& value) {
    if (size < 2) return 0;

    tag = data[0];
    size_t length = data[1];
    size_t headerSize = 2;

    // Handle "Long Form" length (if length byte has bit 8 set)
    if (length & 0x80) {
        int numOctets = length & 0x7F;
        length = 0;
        for (int i = 0; i < numOctets; ++i) {
            length = (length << 8) | data[2 + i];
        }
        headerSize += numOctets;
    }

    value.assign(data + headerSize, data + headerSize + length);
    return headerSize + length;
}

std::string netplus::x509cert::bytesToHex(const std::vector<uint8_t>& bytes) {
    std::stringstream ss;
    for (uint8_t b : bytes) {
        ss << std::hex << std::setw(2) << std::setfill('0') << (int)b;
    }
    return ss.str();
}

src/crypto/x509.h

0 → 100644
+55 −0
Changes for src/crypto/x509.h: 55 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*******************************************************************************
 * Copyright (c) 2025, Jan Koester jan.koester@gmx.net
 * All rights reserved.
 * * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the <organization> nor the
 * names of its contributors may be used to endorse or promote products
 * derived from this software without specific prior written permission.
 * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/

#pragma once

#include <string>
#include <vector>
#include <cstdint>

namespace netplus {
    class x509cert {
    public:
        x509cert();

        // Load binary DER data
        bool loadFromFile(const std::string& filePath);

        // Simple getters for parsed data
        std::string getSerialNumber() const { return serialNumber; }
        std::string getIssuer() const { return issuer; }
        std::string getSubject() const { return subject; }

    private:
        std::vector<uint8_t> derData;
        std::string serialNumber;
        std::string issuer;
        std::string subject;

        // Internal ASN.1 parsing helpers
        size_t parseTLV(const uint8_t* data, size_t size, uint8_t& tag, std::vector<uint8_t>& value);
        std::string bytesToHex(const std::vector<uint8_t>& bytes);
    };
};
+9 −0
Changes for test/CMakeLists.txt: 9 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -20,6 +20,10 @@ add_executable(rsa rsa.cpp)

target_link_libraries(rsa netplus-static)

add_executable(x509 x509.cpp)

target_link_libraries(x509 netplus-static)

add_test(
    NAME dest_test
    COMMAND des
@@ -39,3 +43,8 @@ add_test(
    NAME rsa_test
    COMMAND rsa
)

add_test(
    NAME x509_test
    COMMAND x509
)
+25 −0
Changes for test/rsa.cpp: 25 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*******************************************************************************
 * Copyright (c) 2025, Jan Koester jan.koester@gmx.net
 * All rights reserved.
 * * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the <organization> nor the
 * names of its contributors may be used to endorse or promote products
 * derived from this software without specific prior written permission.
 * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/

#include <iostream>
#include <cassert>
#include "../src/crypto/rsa.h"
Loading