Commit 6edca0d5 authored by MIDAS NEXT's avatar MIDAS NEXT Committed by jan.koester
Browse files

test: real TLS 1.3 handshake against openssl s_server for P-384/P-256/RSA

Drives a real "openssl s_server" from a small netplus TLS client harness
(no mocked crypto, no in-process shortcut) for a P-384 ECDSA leaf (the new
path), a P-256 ECDSA leaf and an RSA leaf (regression), plus a P-384
server rejected against the wrong CA. Skipped when openssl isn't on PATH.

This openssl build's s_server resets the connection before SSL_accept()
ever runs unless given -www (reproduced against both openssl s_client and
curl, independent of netplus) -- worked around by using it, since the
test only needs the handshake to complete, not the HTTP response.

Co-Authored-By: default avatarClaude Sonnet 5 <noreply@anthropic.com>
parent ea6edf7b
Loading
Loading
Loading
Loading
+13 −0
Changes for test/CMakeLists.txt: 13 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -86,6 +86,15 @@ else()
    message(STATUS "openssl not found - skipping cert_verify_chain_test (can't build its fixtures)")
endif()

# tls_p384_handshake_test: a real TLS 1.3 handshake, netplus as client against a real
# "openssl s_server" (P-384/P-256/RSA leaf, freshly generated), exercising the
# ecdsa_secp384r1_sha384 CertificateVerify support end to end. Needs openssl + nc, both
# already required/available for the other openssl-backed tests above.
if(OPENSSL_EXECUTABLE)
    add_executable(tls_client_handshake_harness tls_client_handshake_harness.cpp)
    target_link_libraries(tls_client_handshake_harness netplus-static)
endif()

# Temporarily disabled: TLS test needs refactoring
# add_executable(tls tls.cpp)
# if(WIN32)
@@ -164,6 +173,10 @@ if(OPENSSL_EXECUTABLE)
        NAME cert_verify_chain_test
        COMMAND cert_verify_chain_test
    )
    add_test(
        NAME tls_p384_handshake_test
        COMMAND sh "${CMAKE_CURRENT_SOURCE_DIR}/tls_p384_handshake.sh" $<TARGET_FILE:tls_client_handshake_harness>
    )
endif()


+41 −0
Changes for test/tls_client_handshake_harness.cpp: 41 added lines, 0 removed lines.
Original line number Diff line number Diff line
// tls_client_handshake_harness: connects out as a real TLS client to whatever is
// listening on <host>:<port> (meant to be a real "openssl s_server"), verifying the
// peer's certificate chain against <caFile> and its SAN/CN against <hostname>. Prints
// HANDSHAKE_OK and exits 0 on a completed handshake, HANDSHAKE_FAIL (with the reason)
// and exits 1 otherwise. Driven by tls_p384_handshake.sh -- see there for the scenarios.

#include <cstdlib>
#include <iostream>
#include <map>
#include <string>

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

int main(int argc, char** argv) {
    if (argc < 5) {
        std::cerr << "usage: " << argv[0] << " <host> <port> <caFile> <hostname>" << std::endl;
        return 2;
    }
    std::string host = argv[1];
    int port = std::atoi(argv[2]);
    std::string caFile = argv[3];
    std::string hostname = argv[4];

    std::map<std::string, netplus::ssl::CertificateBundle> certs; // client: no cert of our own needed
    netplus::ssl client(certs);
    client.getTls().trust_policy.caBundlePath = caFile;
    client.getTls().trust_policy.expectedHostname = hostname;

    try {
        client.connect(host, port);
        std::cout << "HANDSHAKE_OK" << std::endl;
        return 0;
    } catch (netplus::NetException& e) {
        std::cerr << "HANDSHAKE_FAIL: " << e.what() << std::endl;
        return 1;
    } catch (std::exception& e) {
        std::cerr << "HANDSHAKE_FAIL: " << e.what() << std::endl;
        return 1;
    }
}
+128 −0
Changes for test/tls_p384_handshake.sh: 128 added lines, 0 removed lines.
Original line number Diff line number Diff line
#!/bin/sh
# Real TLS 1.3 handshake: netplus as client (H, the compiled harness binary),
# "openssl s_server" as a real server (skipped without openssl). Exercises the
# P-384 CertificateVerify support added alongside this script: ecdsa_secp384r1_sha384
# offered in the ClientHello, verified with tls::verify_ecdsa_sha384.
#
# Each case gets its own small self-contained root CA + leaf (RSA, P-256, P-384),
# SAN = 127.0.0.1 so the harness's hostname check also runs for real.
H="$1"
command -v openssl >/dev/null || { echo "openssl missing: skipped"; exit 0; }
T=$(mktemp -d); cd "$T" || exit 1
FAIL=0

# ----------------------------------------------------------------------------
# Fixtures
# ----------------------------------------------------------------------------
gen_pair() {
    kind="$1" curve="$2" sha="$3"
    if [ "$curve" = rsa ]; then
        openssl genrsa -out "${kind}_root.key" 2048 >/dev/null 2>&1
        openssl genrsa -out "${kind}_leaf.key" 2048 >/dev/null 2>&1
    else
        openssl ecparam -name "$curve" -genkey -noout -out "${kind}_root.key" 2>/dev/null
        openssl ecparam -name "$curve" -genkey -noout -out "${kind}_leaf.key" 2>/dev/null
    fi
    openssl req -x509 -new -key "${kind}_root.key" -"$sha" -days 3650 -subj "/CN=netplus-tls-test-root-$kind" \
        -addext "basicConstraints=critical,CA:TRUE" \
        -addext "keyUsage=critical,keyCertSign,cRLSign" \
        -out "${kind}_root.crt" 2>/dev/null
    openssl req -new -key "${kind}_leaf.key" -subj "/CN=netplus-tls-test-leaf-$kind" -out "${kind}_leaf.csr" 2>/dev/null
    openssl x509 -req -in "${kind}_leaf.csr" -CA "${kind}_root.crt" -CAkey "${kind}_root.key" -CAcreateserial -"$sha" -days 3650 \
        -copy_extensions none \
        -extfile /dev/stdin -extensions v3_leaf -out "${kind}_leaf.crt" 2>/dev/null <<EOF
[v3_leaf]
basicConstraints=critical,CA:FALSE
keyUsage=critical,digitalSignature,keyEncipherment
subjectAltName=IP:127.0.0.1
EOF
}

gen_pair rsa   rsa        sha256
gen_pair ec256 prime256v1 sha256
gen_pair ec384 secp384r1  sha384

# ----------------------------------------------------------------------------
# stop_server: kill + reap, no leftover background process either way
# ----------------------------------------------------------------------------
stop_server() {
    pid="$1"
    [ -z "$pid" ] && return
    kill "$pid" 2>/dev/null
    i=0
    while kill -0 "$pid" 2>/dev/null && [ $i -lt 20 ]; do
        sleep 0.1
        i=$((i + 1))
    done
    kill -9 "$pid" 2>/dev/null
    wait "$pid" 2>/dev/null
}

# ----------------------------------------------------------------------------
# run_case <serverKind> <want: ok|fail> <name> <caFileForClient>
# Starts "openssl s_server" for <serverKind>'s leaf on a free 127.0.0.1 port
# (retried on bind failure -- no fixed port, no race on a shared one), then
# runs the harness against it with <caFileForClient> as the trust anchor.
# ----------------------------------------------------------------------------
run_case() {
    kind="$1" want="$2" name="$3" caFile="$4"
    attempt=0 ready=0 port=0 spid=
    while [ $attempt -lt 8 ]; do
        port=$((25000 + ($$ + attempt * 41) % 500))
        : > "srv_${kind}.log"
        # openssl's "s_server" (without -naccept) serves exactly one connection and then
        # exits -- successful or not, so a plain TCP readiness probe (nc -z, a connect+
        # immediate close) would itself consume that one slot and the real client would
        # then find the port already closed. Instead, poll the server's own log for the
        # "ACCEPT" line it prints right after bind()+listen() succeed, before any
        # connection -- that doesn't touch the socket at all.
        # -www: without it, this openssl build's s_server resets the connection before
        # SSL_accept() ever runs on this host (reproduced against both "openssl s_client"
        # and curl) -- some environment-specific default-mode quirk unrelated to netplus.
        # -www just makes it answer a canned HTTP response after the handshake, which the
        # harness never reads; it only needs the handshake itself to complete.
        openssl s_server -accept "$port" -cert "${kind}_leaf.crt" -key "${kind}_leaf.key" -tls1_3 -www \
            > "srv_${kind}.log" 2>&1 &
        spid=$!
        waited=0
        while [ $waited -lt 30 ]; do
            if grep -q ACCEPT "srv_${kind}.log" 2>/dev/null; then ready=1; break; fi
            kill -0 "$spid" 2>/dev/null || break # died (e.g. bind failed) -> try another port
            sleep 0.1
            waited=$((waited + 1))
        done
        [ $ready -eq 1 ] && break
        stop_server "$spid"; spid=
        attempt=$((attempt + 1))
    done

    if [ $ready -ne 1 ]; then
        echo "FAIL: $name (could not get openssl s_server listening after $attempt attempts)"
        FAIL=1
        return
    fi

    "$H" 127.0.0.1 "$port" "$caFile" 127.0.0.1 > "cli_${kind}.log" 2>&1
    rc=$?
    stop_server "$spid"

    if [ "$want" = ok ]; then
        if [ $rc -eq 0 ] && grep -q HANDSHAKE_OK "cli_${kind}.log"; then r=ok; else r=FAIL; fi
    else
        if [ $rc -ne 0 ] && grep -q HANDSHAKE_FAIL "cli_${kind}.log"; then r=ok; else r=FAIL; fi
    fi
    echo "$r: $name"
    if [ "$r" != ok ]; then
        FAIL=1
        echo "--- server log ($kind) ---"; cat "srv_${kind}.log"
        echo "--- client log ($kind) ---"; cat "cli_${kind}.log"
    fi
}

run_case ec384 ok   "P-384 ECDSA server accepted"                 "ec384_root.crt"
run_case ec256 ok   "P-256 ECDSA server still accepted"           "ec256_root.crt"
run_case rsa   ok   "RSA server still accepted"                   "rsa_root.crt"
run_case ec384 fail "P-384 server rejected against the wrong CA"  "rsa_root.crt"

rm -rf "$T"
exit $FAIL