TrustPin

TrustPin SSL certificate pinning SDK in Kotlin 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.

Overview

TrustPin uses cryptographically signed configuration to securely deliver pinning configurations to your application. All operations are designed to work with Kotlin Coroutines for seamless integration with modern Android and JVM applications.

Key Features

  • Kotlin Multiplatform: Shared codebase for Android and JVM platforms

  • Flexible Pinning Modes: Strict validation or permissive mode for development

  • Multiple Hash Algorithms: SHA-256 and SHA-512 certificate validation

  • Signed Configuration: Cryptographically signed pinning configurations

  • Android Integrations: OkHttp, TrustManager, and SSLSocketFactory support

  • Intelligent Caching: 10-minute configuration cache with stale fallback

  • Comprehensive Logging: Configurable log levels for debugging and monitoring

  • Thread-Safe: Built with coroutines and concurrent-safe operations

  • Singleton Pattern: One instance per application for consistency

  • Enhanced Security - Advanced signature verification with multiple authentication methods

Basic Usage

import cloud.trustpin.kotlin.sdk.TrustPin
import cloud.trustpin.kotlin.sdk.TrustPinMode

// Configure with your project credentials (suspend function)
suspend fun initialize() {
TrustPin.setup(
organizationId = "your-org-id",
projectId = "your-project-id",
publicKey = "your-base64-public-key",
mode = TrustPinMode.STRICT // Recommended for production
)
}

// Verify a certificate (suspend function)
suspend fun verifyCert() {
val certificate: X509Certificate = // ... obtained from connection
TrustPin.verify(domain = "api.example.com", certificate = certificate)
}

Android Integration

OkHttp Integration

import cloud.trustpin.kotlin.sdk.TrustPinSSLSocketFactory
import okhttp3.OkHttpClient

val sslSocketFactory = TrustPinSSLSocketFactory.create()
val client = OkHttpClient.Builder()
.connectTimeout(30, java.util.concurrent.TimeUnit.SECONDS)
.readTimeout(30, java.util.concurrent.TimeUnit.SECONDS)
.writeTimeout(30, java.util.concurrent.TimeUnit.SECONDS)
.sslSocketFactory(sslSocketFactory, sslSocketFactory.trustManager())
.build()

Manual Certificate Verification

import java.security.cert.X509Certificate

suspend fun verifyManually() {
val certificate: X509Certificate = // ... obtained from connection
try {
TrustPin.verify(domain = "api.example.com", certificate = certificate)
println("Certificate is valid and matches configured pins")
} catch (e: TrustPinError.DomainNotRegistered) {
println("Domain not configured for pinning")
} catch (e: TrustPinError.PinsMismatch) {
println("Certificate doesn't match any configured pins")
}
}

Pinning Modes

Error Handling

All operations can throw specific TrustPinError types for proper error handling:

Thread Safety

All TrustPin operations are thread-safe and can be called from any coroutine context. Internal operations are performed on appropriate background dispatchers.

See also

Functions

Link copied to clipboard

Sets the current log level for TrustPin's internal logging system.

Link copied to clipboard
suspend fun setup(organizationId: String, projectId: String, publicKey: String, mode: TrustPinMode = TrustPinMode.STRICT)

Initializes the TrustPin SDK with the specified configuration.

suspend fun setup(organizationId: String, projectId: String, publicKey: String, configurationURL: URL, mode: TrustPinMode = TrustPinMode.STRICT)

Initializes the TrustPin SDK with a custom configuration URL.

Link copied to clipboard
fun setupBlocking(organizationId: String, projectId: String, publicKey: String, mode: TrustPinMode = TrustPinMode.STRICT)

Initializes the TrustPin SDK with organization credentials (blocking version).

fun setupBlocking(organizationId: String, projectId: String, publicKey: String, configurationURL: URL, mode: TrustPinMode = TrustPinMode.STRICT)

Initializes the TrustPin SDK with a custom configuration URL (blocking version).

Link copied to clipboard
suspend fun verify(domain: String, certificate: X509Certificate)

Verifies a certificate against the specified domain using public key pinning.

Link copied to clipboard
fun verifyBlocking(domain: String, certificate: X509Certificate)

Verifies a certificate against the specified domain using public key pinning (blocking version).