Commit 4a6b002d authored by jan.koester's avatar jan.koester
Browse files

fixed win build

parent 908b182c
Loading
Loading
Loading
Loading
+100 −12
Changes for src/crypto/x509.cpp: 100 added lines, 12 removed lines.
Original line number Diff line number Diff line
@@ -27,6 +27,10 @@
#include <iomanip>
#include <sstream>
#include <ctime>
#include <chrono>
#include <cctype>
#include <string>
#include <vector>

#include "x509.h"

@@ -51,25 +55,109 @@ netplus::x509cert& netplus::x509cert::operator=(const x509cert &cert) {
    return *this;
}

time_t netplus::x509cert::parseASN1Time(const std::vector<uint8_t>& data, uint8_t tag) {
    std::string timeStr(data.begin(), data.end());
    struct tm t = {0};
    const char* format;

    if (tag == 0x17) { // UTCTime: YYMMDDHHMMSSZ
        format = "%y%m%d%H%M%S";
    } else if (tag == 0x18) { // GeneralizedTime: YYYYMMDDHHMMSSZ
        format = "%Y%m%d%H%M%S";
time_t netplus::x509cert::parseASN1Time(
    const std::vector<uint8_t>& data,
    uint8_t tag
) {

    auto parse2 = [] (const std::string& s, size_t p, int& v) {
        if (p + 2 > s.size()) return false;
        if (!isdigit(s[p]) || !isdigit(s[p+1])) return false;
        v = (s[p]-'0')*10 + (s[p+1]-'0');
        return true;
    };

    auto parse4 = [] (const std::string& s, size_t p, int& v) {
        if (p + 4 > s.size()) return false;
        for (size_t i = 0; i < 4; ++i)
            if (!isdigit(s[p+i])) return false;
        v = (s[p]-'0')*1000 + (s[p+1]-'0')*100 + (s[p+2]-'0')*10 + (s[p+3]-'0');
        return true;
    };

    auto tm_to_utc_time_t = [](std::tm& t) {
#if defined(_WIN32)
        return _mkgmtime(&t);   // MSVC: tm interpreted as UTC
#else
        return timegm(&t);      // POSIX: tm interpreted as UTC
#endif
    };

    if (data.empty()) return 0;

    std::string s(data.begin(), data.end());

    // Trim whitespace (defensive)
    while (!s.empty() && (s.back() == '\r' || s.back() == '\n' || s.back() == ' ' || s.back() == '\t'))
        s.pop_back();

    // Must end in 'Z' for the formats we support (UTC)
    if (s.empty() || s.back() != 'Z') return 0;
    s.pop_back(); // remove trailing 'Z'

    int year = 0, mon = 0, day = 0, hour = 0, min = 0, sec = 0;

    if (tag == 0x17) {
        // UTCTime: YYMMDDHHMM[SS]
        // Length can be 10 (no seconds) or 12 (with seconds)
        if (s.size() != 10 && s.size() != 12) return 0;

        int yy = 0;
        if (!parse2(s, 0, yy)) return 0;
        if (!parse2(s, 2, mon)) return 0;
        if (!parse2(s, 4, day)) return 0;
        if (!parse2(s, 6, hour)) return 0;
        if (!parse2(s, 8, min)) return 0;
        if (s.size() == 12) {
            if (!parse2(s, 10, sec)) return 0;
        } else {
            sec = 0;
        }

        // RFC 5280: UTCTime year mapping
        // 00-49 => 2000-2049, 50-99 => 1950-1999
        year = (yy <= 49) ? (2000 + yy) : (1900 + yy);

    } else if (tag == 0x18) {
        // GeneralizedTime: YYYYMMDDHHMM[SS]
        if (s.size() != 12 && s.size() != 14) return 0;

        if (!parse4(s, 0, year)) return 0;
        if (!parse2(s, 4, mon)) return 0;
        if (!parse2(s, 6, day)) return 0;
        if (!parse2(s, 8, hour)) return 0;
        if (!parse2(s, 10, min)) return 0;
        if (s.size() == 14) {
            if (!parse2(s, 12, sec)) return 0;
        } else {
        return 0;
            sec = 0;
        }

    if (strptime(timeStr.c_str(), format, &t)) {
        return timegm(&t); // Convert to UTC timestamp
    } else {
        return 0; // unsupported tag
    }
    return 0;

    // Basic range checks (defensive)
    if (mon < 1 || mon > 12) return 0;
    if (day < 1 || day > 31) return 0;
    if (hour < 0 || hour > 23) return 0;
    if (min < 0 || min > 59) return 0;
    if (sec < 0 || sec > 60) return 0; // allow leap second

    std::tm t{};
    t.tm_year = year - 1900;
    t.tm_mon  = mon - 1;
    t.tm_mday = day;
    t.tm_hour = hour;
    t.tm_min  = min;
    t.tm_sec  = sec;
    t.tm_isdst = 0;

    return tm_to_utc_time_t(t);
}


bool netplus::x509cert::checkValidity() {
    if (root.children.empty() || root.children[0].children.size() < 5)
        return false;