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

test

parent cedbdaff
Loading
Loading
Loading
Loading
+4 −2
Changes for src/CMakeLists.txt: 4 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ if(${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Windows")
        windows/udp.cpp
        windows/tcp.cpp
        windows/random.cpp
        windows/cert_verify.cpp
    )
else()
    list(APPEND netplussrc
@@ -37,6 +38,7 @@ else()
        posix/udp.cpp
        posix/tcp.cpp
        posix/random.cpp
        posix/cert_verify.cpp
    )
endif()

@@ -122,8 +124,8 @@ target_include_directories(netplus-static
)

if(${CMAKE_HOST_SYSTEM_NAME} MATCHES "Windows")
   target_link_libraries(netplus ws2_32)
   target_link_libraries(netplus-static  ws2_32)
   target_link_libraries(netplus ws2_32 crypt32)
   target_link_libraries(netplus-static  ws2_32 crypt32)
else()
   target_link_libraries(netplus pthread) #PUBLIC MbedTLS::mbedcrypto MbedTLS::mbedtls MbedTLS::mbedx509)
   target_link_libraries(netplus-static pthread)
+24 −14
Changes for src/crypto/cert_verify.cpp: 24 added lines, 14 removed lines.
Original line number Diff line number Diff line
#include "cert_verify.h"
#include "cert_verify_platform.h"

#include <fstream>
#include <mutex>
@@ -9,21 +10,11 @@

namespace netplus {

namespace {

// Process-wide cache of parsed root stores, keyed by bundle path -- avoids re-parsing a
// ~150-certificate system bundle on every single connection.
std::vector<x509cert>& systemRootStore(const std::string& path) {
    static std::mutex mtx;
    static std::unordered_map<std::string, std::vector<x509cert>> cache;

    std::lock_guard<std::mutex> lk(mtx);
    auto it = cache.find(path);
    if (it != cache.end()) return it->second;

std::vector<x509cert> loadPemCertBundle(const std::string &path) {
    std::vector<x509cert> store;
    std::ifstream file(path, std::ios::binary);
    if (file) {
    if (!file) return store;

    std::string pem((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
    static const std::string beginMarker = "-----BEGIN CERTIFICATE-----";
    static const std::string endMarker = "-----END CERTIFICATE-----";
@@ -43,9 +34,28 @@ std::vector<x509cert>& systemRootStore(const std::string& path) {
        }
        pos = e;
    }
    return store;
}

    auto inserted = cache.emplace(path, std::move(store));
namespace {

// Process-wide cache of parsed root stores -- avoids re-parsing a ~150-certificate bundle, or
// re-enumerating the Windows store, on every single connection. Keyed by the configured path,
// or a sentinel for "platform default" (empty TlsTrustPolicy::caBundlePath).
std::vector<x509cert> &systemRootStore(const std::string &configuredPath) {
    static std::mutex mtx;
    static std::unordered_map<std::string, std::vector<x509cert>> cache;
    static const std::string kPlatformDefaultKey = "\x01PLATFORM_DEFAULT";

    const std::string &key = configuredPath.empty() ? kPlatformDefaultKey : configuredPath;

    std::lock_guard<std::mutex> lk(mtx);
    auto it = cache.find(key);
    if (it != cache.end()) return it->second;

    std::vector<x509cert> store =
        configuredPath.empty() ? loadPlatformDefaultCaRootStore() : loadPemCertBundle(configuredPath);
    auto inserted = cache.emplace(key, std::move(store));
    return inserted.first->second;
}

+6 −1
Changes for src/crypto/cert_verify.h: 6 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -13,7 +13,12 @@ struct TlsTrustPolicy {
    bool verifyPeer = false;

    // System CA bundle used when the peer's leaf certificate isn't pinned (see below).
    std::string caBundlePath = "/etc/ssl/certs/ca-certificates.crt";
    // Empty (the default) means "use the platform default": on Windows, the system ROOT
    // certificate store (via CryptoAPI); on POSIX, the first Mozilla-CA-derived bundle found
    // among the well-known distro paths (Debian/Ubuntu, Fedora/RHEL, OpenSUSE, Alpine,
    // FreeBSD -- the same list curl and Go's crypto/x509 search). Set explicitly to point at
    // a specific bundle file instead (works on both platforms).
    std::string caBundlePath;

    // SHA-256 digests (raw 32-byte, not hex) of specific leaf certificates to trust outright,
    // regardless of chain-of-trust -- this is what makes a self-signed upstream usable: pin
+22 −0
Changes for src/crypto/cert_verify_platform.h: 22 added lines, 0 removed lines.
Original line number Diff line number Diff line
#pragma once

#include <string>
#include <vector>

#include "x509.h"

namespace netplus {

// Parses every "-----BEGIN CERTIFICATE-----" block out of a PEM bundle file. Implemented in
// cert_verify.cpp (shared -- reading an arbitrary file path is not platform-specific); used
// both for an explicitly-configured TlsTrustPolicy::caBundlePath and by the POSIX default-path
// probing below.
std::vector<x509cert> loadPemCertBundle(const std::string &path);

// Returns the OS's default trusted root store, used only when TlsTrustPolicy::caBundlePath is
// left empty. One implementation per platform: posix/cert_verify.cpp probes the well-known
// Mozilla-CA-derived bundle paths; windows/cert_verify.cpp enumerates the system ROOT store
// via CryptoAPI.
std::vector<x509cert> loadPlatformDefaultCaRootStore();

} // namespace netplus
+26 −0
Changes for src/posix/cert_verify.cpp: 26 added lines, 0 removed lines.
Original line number Diff line number Diff line
#include "../crypto/cert_verify_platform.h"

#include <fstream>

namespace netplus {

std::vector<x509cert> loadPlatformDefaultCaRootStore() {
    // Well-known Mozilla-CA-derived bundle paths across POSIX distros -- the same list curl
    // and Go's crypto/x509 search. First one that actually exists wins.
    static const std::vector<std::string> candidates = {
        "/etc/ssl/certs/ca-certificates.crt",                // Debian/Ubuntu/Gentoo/Arch
        "/etc/pki/tls/certs/ca-bundle.crt",                  // Fedora/RHEL 6
        "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem", // Fedora/RHEL 7+/CentOS/Amazon Linux
        "/etc/ssl/ca-bundle.pem",                            // OpenSUSE
        "/etc/pki/tls/cacert.pem",                           // OpenELEC
        "/etc/ssl/cert.pem",                                 // Alpine w/o ca-certificates, macOS/homebrew, FreeBSD base
        "/usr/local/share/certs/ca-root-nss.crt",            // FreeBSD/DragonFly ca_root_nss
    };
    for (const auto &path : candidates) {
        std::ifstream probe(path, std::ios::binary);
        if (probe) return loadPemCertBundle(path);
    }
    return {};
}

} // namespace netplus
Loading