fetchCertificate

suspend fun fetchCertificate(host: String, port: Int = 443, timeout: Long = TrustPinConstants.DEFAULT_OPERATION_TIMEOUT_MS): String

Opens an ephemeral TLS connection to host:port, performs OS-level certificate chain validation and RFC 2818/6125 hostname verification (the presented certificate must actually match host, not merely be CA-valid), and returns the server's leaf certificate as a PEM string.

The returned PEM string can be passed directly to verify.

Return

PEM-encoded leaf certificate string

Parameters

host

Hostname to connect to (e.g. "api.example.com")

port

TCP port — defaults to 443

timeout

End-to-end deadline in milliseconds. Default 30s. Clamped to [MIN_OPERATION_TIMEOUT_MS, MAX_OPERATION_TIMEOUT_MS] (currently 10s–120s) to bound both pathologically short timeouts that race with TLS handshakes and unbounded blocking.

Throws

if the TLS handshake fails or no leaf certificate is present

if the supplied timeout elapses before completion