testserver

package
v0.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2026 License: Apache-2.0 Imports: 19 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package testserver provides hermetic servers for testing the TLS prober without touching the network: real crypto/tls servers whose versions, suites and groups are configurable, and a scripted raw-TCP server for replies the standard library cannot produce (SSLv2/SSLv3, garbage, stalls).

Test and demo support only: nothing in the shipped cipherhorizon binary imports it (cmd/demo-targets, a development tool, does).

Index

Constants

This section is empty.

Variables

View Source
var CertValidity = 24 * time.Hour

CertValidity is how long the test certificates are valid. Tests keep the short default (their expiry findings depend on it); the demo targets set a realistic one before the first server starts.

Functions

func Alert

func Alert(desc byte) []byte

Alert is a fatal TLS alert record.

func AnswerAnyTLS13

func AnswerAnyTLS13() func([]byte) []byte

AnswerAnyTLS13 answers every ClientHello with a TLS 1.3 ServerHello (TLS_AES_128_GCM_SHA256, an X25519 share), whatever it offered: a server that does not choose its certificate by the client's signature_algorithms. It cannot complete a handshake.

func Certificates

func Certificates(t testing.TB) (ec, rsaC tls.Certificate)

Certificates returns the test ECDSA and RSA certificates (self-signed).

func Legacy

func Legacy(version uint16, order []uint16, clientPref bool) func([]byte) []byte

Legacy speaks exactly one protocol version. order is the server's preference; clientPref makes it pick the first acceptable suite in the CLIENT's order instead.

func LegacyEcho added in v0.4.0

func LegacyEcho(version uint16, order []uint16, echo []uint16) func([]byte) []byte

LegacyEcho is Legacy that also echoes, in its ServerHello, each extension in echo the client offered.

func LegacyWithCertificate added in v0.4.0

func LegacyWithCertificate(version uint16, order []uint16, chain [][]byte) func([]byte) []byte

LegacyWithCertificate is Legacy (server order), but its flight carries the certificate chain (DER, leaf first) between the ServerHello and the ServerHelloDone: a server whose only suites crypto/tls will not offer (3DES, RC4, NULL), so its certificate is seen only in raw flights.

func LoadCertificates

func LoadCertificates() (ec, rsaC tls.Certificate, err error)

LoadCertificates returns the ECDSA and RSA certificates (self-signed).

func MLDSACertificate

func MLDSACertificate(t testing.TB) tls.Certificate

MLDSACertificate returns the test ML-DSA-65 certificate (self-signed).

func ServerHello

func ServerHello(version, suite uint16) []byte

ServerHello is a ServerHello plus ServerHelloDone record choosing suite.

func ServerHelloWithExtensions added in v0.4.0

func ServerHelloWithExtensions(version, suite uint16, types []uint16) []byte

ServerHelloWithExtensions is ServerHello with empty-bodied extensions of the given types (an echo, the way a server confirms extended_master_secret or heartbeat; heartbeat gets its mode).

Types

type CertKind

type CertKind int

CertKind selects which certificates a server presents.

const (
	CertECDSA CertKind = iota // one ECDSA P-256 certificate
	CertRSA                   // one RSA-2048 certificate
	CertBoth                  // both; the suite chosen decides which is used
	// CertMLDSA is one ML-DSA-65 certificate (FIPS 204), usable only in TLS 1.3.
	CertMLDSA
	// CertECDSAAndMLDSA is an ECDSA P-256 and an ML-DSA-65 certificate; the client's
	// signature_algorithms decide which is used, the ECDSA one when both would do.
	CertECDSAAndMLDSA
)

The certificate kinds.

type Config

type Config struct {
	MinVersion, MaxVersion uint16
	Suites                 []uint16 // TLS 1.2 and earlier; TLS 1.3 suites are fixed by Go
	Curves                 []tls.CurveID
	Cert                   CertKind
	ALPN                   []string
	// ClientAuth configures whether the server asks for (or requires) a client
	// certificate. Zero value (tls.NoClientCert) asks for none.
	ClientAuth tls.ClientAuthType
	// HTTPResponse, when set, makes the server read one request after the TLS
	// handshake completes and write back this function's result (nil closes
	// without answering), mirroring StartScript. Used to test the optional
	// post-handshake HTTP check.
	HTTPResponse func(request []byte) []byte
	// Chain, when set, replaces Cert: the server presents exactly these
	// certificates (with any OCSPStaple and SignedCertificateTimestamps they carry).
	Chain []tls.Certificate
	// NoSNI, when set, is presented to clients that send no server_name.
	NoSNI *tls.Certificate
	// Addr is the listen address; empty means 127.0.0.1:0. Restarting a server on
	// the same address with a different Config simulates an endpoint being changed.
	Addr string
	// StartTLS, when set (smtp, imap, pop3, ftp, xmpp, ldap, postgres), makes every connection
	// begin in that protocol's plain text and switch to TLS when asked. RefuseStartTLS makes the
	// server say no instead.
	StartTLS       string
	RefuseStartTLS bool
	// SelectCertFirst makes the server choose its certificate from the first ClientHello and
	// refuse with an alert when none fits the offered signature_algorithms, before any
	// HelloRetryRequest. OpenSSL works this way; Go's server sends a HelloRetryRequest first.
	SelectCertFirst bool
}

Config configures a TLS server. Zero values mean crypto/tls defaults.

type Server

type Server struct {
	Host string
	Port int
	Addr string // host:port
	// contains filtered or unexported fields
}

Server is a running test server.

func Listen

func Listen(cfg Config) (*Server, error)

Listen is Start for programs that are not tests (the demo targets). The caller closes the server.

func ListenScript

func ListenScript(addr string, reply func(clientBytes []byte) []byte) (*Server, error)

ListenScript is StartScript for programs that are not tests. The caller closes the server.

func Start

func Start(t testing.TB, cfg Config) *Server

Start runs a TLS server on 127.0.0.1 until the test ends. Each connection is handshaken and closed.

func StartScript

func StartScript(t testing.TB, reply func(clientBytes []byte) []byte) *Server

StartScript runs a raw TCP server that reads one chunk from the client and answers with reply(chunk), then closes. A nil reply closes without answering; stall keeps the connection open and silent until the client gives up.

func (*Server) Close

func (s *Server) Close()

Close stops the server early (it is also stopped when the test ends).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL