TrustPin

class TrustPin

TrustPin SSL certificate pinning SDK for JVM and Android.

TrustPin provides SSL certificate pinning functionality to prevent man-in-the-middle (MITM) attacks by validating server certificates against pre-configured public key pins. The library supports both Android and JVM platforms with a unified API and advanced security features.

Single-Instance Usage (simplest)

For most applications a single, shared pinning context is sufficient. Use the static convenience methods on the companion object — they all delegate to default:

// Setup
TrustPin.setup(
TrustPinConfiguration(
organizationId = "my-org",
projectId = "my-project",
publicKey = "MFkwEwYH...",
mode = TrustPinMode.STRICT
)
)

// Verify
TrustPin.verify("api.example.com", certificate)

// OkHttp integration
val client = OkHttpClient.Builder()
.sslSocketFactory(TrustPin.makeSSLSocketFactory(), TrustPin.makeTrustManager())
.build()

Multi-Instance Usage

Libraries or multi-tenant apps that need isolated pinning contexts can create named instances via instance. Each instance has its own configuration, state, and log output tagged with the instance id:

val payments = TrustPin.instance("payments")
payments.setup(
TrustPinConfiguration(
organizationId = "payments-org",
projectId = "payments-api",
publicKey = "MFkwEwYH..."
)
)
payments.verify("pay.example.com", certificate)

val analytics = TrustPin.instance("analytics")
analytics.setup(/* ... */)

Java Usage

From Java, access the default instance via TrustPin.getDefault():

TrustPin.getDefault().setupBlocking(
new TrustPinConfiguration("my-org", "my-project", "MFkwEwYH...", TrustPinMode.STRICT, null)
);
SSLSocketFactory factory = TrustPin.getDefault().makeSSLSocketFactory();
X509TrustManager trustManager = TrustPin.getDefault().makeTrustManager();

Pinning Modes

See also

Types

Link copied to clipboard
object Companion
Link copied to clipboard
class TlsPair

An SSLSocketFactory together with the exact X509TrustManager that backs it — the two arguments OkHttp's sslSocketFactory(factory, trustManager) expects, guaranteed to belong to the same validation pipeline. Obtain via makeTlsPair.

Properties

Link copied to clipboard

true when a validated pinning payload is cached and usable by verify without a new fetch.

Functions

Link copied to clipboard
suspend fun awaitConfiguration(timeout: Long = TrustPinConstants.DEFAULT_OPERATION_TIMEOUT_MS)

Waits until the pinning configuration has been fetched, signature-verified, and accepted by the SDK's integrity check — the explicit fail-closed gate.

Link copied to clipboard
fun awaitConfigurationBlocking(timeout: Long = TrustPinConstants.DEFAULT_OPERATION_TIMEOUT_MS)

Blocking version of awaitConfiguration. Must not be called from the Android main thread.

Link copied to clipboard
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.

Link copied to clipboard
fun fetchCertificateBlocking(host: String, port: Int = 443, timeout: Long = TrustPinConstants.DEFAULT_OPERATION_TIMEOUT_MS): String

Blocking version of fetchCertificate. Must not be called from the Android main thread.

Link copied to clipboard

Creates an SSLSocketFactory bound to this TrustPin instance.

Link copied to clipboard

Creates a literally matched TLS pair: an SSLSocketFactory and the exact X509TrustManager instance its SSLContext was initialized with.

Link copied to clipboard

Creates an X509TrustManager bound to this TrustPin instance.

Link copied to clipboard

Sets the log level for this TrustPin instance.

Link copied to clipboard
suspend fun setup(configuration: TrustPinConfiguration)

Initializes this TrustPin instance with the given configuration.

Link copied to clipboard

Blocking version of setup. Must not be called from the Android main thread.

Link copied to clipboard
suspend fun verify(domain: String, certificate: X509Certificate, timeout: Long = TrustPinConstants.DEFAULT_OPERATION_TIMEOUT_MS)

Verifies an X.509 certificate against the configured pins for domain.

suspend fun verify(domain: String, certificate: String, timeout: Long = TrustPinConstants.DEFAULT_OPERATION_TIMEOUT_MS)

Verifies a PEM-encoded certificate against the configured pins for domain.

suspend fun verify(domain: String, chain: List<X509Certificate>, timeout: Long = TrustPinConstants.DEFAULT_OPERATION_TIMEOUT_MS)

Verifies the leaf of a server-presented certificate chain against the configured pins for domain.

Link copied to clipboard
fun verifyBlocking(domain: String, certificate: X509Certificate, timeout: Long = TrustPinConstants.DEFAULT_OPERATION_TIMEOUT_MS)

Blocking version of verify with X.509 certificate. Must not be called from the Android main thread.

fun verifyBlocking(domain: String, certificate: String, timeout: Long = TrustPinConstants.DEFAULT_OPERATION_TIMEOUT_MS)

Blocking version of verify with PEM string. Must not be called from the Android main thread.

fun verifyBlocking(domain: String, chain: List<X509Certificate>, timeout: Long = TrustPinConstants.DEFAULT_OPERATION_TIMEOUT_MS)

Blocking version of verify with a certificate chain. Must not be called from the Android main thread.