Skip to content
moyaverPublic

About

Hardware-backed RFC 9449 DPoP & ECDSA P-256 device-bound signing for Kotlin Multiplatform (Android Keystore, iOS Secure Enclave, JVM, and Ktor).

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Kothless

CI License

Hardware-backed RFC 9449 DPoP & ECDSA P-256 device-bound signing for Kotlin Multiplatform.

Kothless gives Android, iOS, and JVM backend targets a focused, zero-bloat Kotlin API for generating non-extractable NIST P-256 (secp256r1) keys inside hardware security modules (Android Keystore StrongBox/TEE and Apple Secure Enclave), issuing RFC 9449 DPoP (dpop+jwt, ES256) proofs or challenge signatures, and verifying them identically on your JVM server.

How Kothless compares

  • vs. Passkeys (CredentialManager / AuthenticationServices): Passkeys sync across devices via iCloud Keychain or Google Password Manager and present OS sheets. Kothless keys stay bound to one physical device (ThisDeviceOnly / AndroidKeyStore) and sign HTTP requests silently without UI prompts (or via BiometricPrompt.CryptoObject when step-up auth is required).
  • vs. a-sit-plus/signum: Use Signum if you need a full PKI/eID suite (COSE, X.509 certificate builders, Verifiable Credentials, key agreement). Use Kothless if you want a minimal, zero-dependency ECDSA P-256 hardware signer + RFC 9449 DPoP with an optional drop-in Ktor Client plugin.

Modules

Module Purpose Dependencies
:library (io.yavero:kothless) DeviceKeyStore (StrongBox/TEE & Secure Enclave), P-256 SPKI/X9.63/JWK/DER/P1363 codecs, pure-Kotlin SHA-256 + RFC 7638 jkt, RFC 9449 DpopProofGenerator & DpopProofVerifier, RequestSigner, ChallengeAuthenticator kotlinx-coroutines-core only
:kothless-ktor (io.yavero:kothless-ktor) KothlessDpop Ktor 3 ClientPlugin with automatic ath token binding, per-origin DPoP-Nonce caching, and transparent 401/400 nonce challenge-retry :library + ktor-client-core

Wire Formats

Artifact Android Keystore Apple Secure Enclave Normalized by Kothless
Public Key X.509 SPKI DER (91 B) Uncompressed X9.63 (65 B) spkiDer(), x963Raw(), toPem(), toJwkJson(), jwkThumbprint() (RFC 7638 jkt)
Signature ASN.1 DER SHA256withECDSA ASN.1 DER kSecKeyAlgorithmECDSASignatureMessageX962SHA256 der(), p1363() (64 B r || s for JWS ES256), derBase64Url(), p1363Base64Url()

Quick Start

1. Generate a hardware-bound key (Android / iOS)

// Android: AndroidKeyStoreDeviceKeyStore(context.packageManager)
// iOS:     SecureEnclaveDeviceKeyStore()
val keyStore: DeviceKeyStore = /* platform instance */
val keyId = KeyId("device-dpop-v1")

val generated = keyStore.generateKey(
    keyId = keyId,
    config = KeyConfig(
        hardwarePolicy = HardwarePolicy.REQUIRE_HARDWARE, // fails closed if no TEE/StrongBox/Secure Enclave
        userPresence = UserPresence.NONE,
        attestationChallenge = serverChallengeBytes, // optional Android Key Attestation
    ),
)

// Register generated.publicKey.jwkThumbprint() or generated.publicKey.toJwkJson() on your auth server.

2. Automatic RFC 9449 DPoP with Ktor Client (:kothless-ktor)

val client = HttpClient {
    install(KothlessDpop) {
        this.keyStore = keyStore
        this.keyId = keyId
    }
}

// Automatically attaches DPoP JWT (with ath = base64url(SHA-256(accessToken))),
// caches DPoP-Nonce per origin, and retries once on a fresh 401/400 DPoP-Nonce challenge.
val response = client.post("https://api.example.com/v1/transfers") {
    header(HttpHeaders.Authorization, "DPoP $accessToken")
    setBody(payload)
}

3. Manual DPoP generation & JVM server verification (:library)

// Client
val generator = DpopProofGenerator(keyStore)
val proof = generator.generate(
    keyId,
    DpopProofRequest(
        httpMethod = "POST",
        httpTargetUri = "https://api.example.com/v1/transfers?dryRun=true",
        accessToken = accessToken,
        nonce = serverNonce,
    ),
)

// Server (JVM / Ktor / Spring)
val verifier = DpopProofVerifier(JvmSignatureVerifier())
when (val res = verifier.verify(
    proof.jwt,
    DpopVerificationPolicy(
        expectedHttpMethod = "POST",
        expectedHttpTargetUri = "https://api.example.com/v1/transfers",
        expectedAccessToken = accessToken,
        expectedNonce = serverNonce,
        expectedJkt = tokenBoundJkt,
        isJtiFresh = { jti -> replayCache.markIfAbsent(jti) },
    ),
)) {
    is DpopVerificationResult.Valid -> { /* authenticated & proof-of-possession verified */ }
    is DpopVerificationResult.Invalid -> { /* reject with res.reason */ }
}

4. Android BiometricPrompt step-up signing

val androidStore = AndroidKeyStoreDeviceKeyStore(context.packageManager)
val sig = androidStore.createBiometricSignature(keyId)
// Pass BiometricPrompt.CryptoObject(sig) to BiometricPrompt.authenticate(...)
// Inside onAuthenticationSucceeded(result):
val signatureBytes = androidStore.completeSignature(
    requireNotNull(result.cryptoObject?.signature),
    payload,
)

Security & Threat Model Notes

  • Fail-closed hardware enforcement: Under HardwarePolicy.REQUIRE_HARDWARE, Android attempts StrongBox (API 28+) and falls back to TEE if unavailable, then inspects KeyInfo — if software-backed, Kothless deletes the key and throws KothlessException.HardwareUnavailable. Physical iOS devices use kSecAttrTokenIDSecureEnclave with kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly; the iOS Simulator has no Secure Enclave and fails closed unless ALLOW_SOFTWARE_FALLBACK is explicitly passed for tests.
  • RFC 9449 Section 4.3 enforcement: DpopProofVerifier validates typ: "dpop+jwt", alg: "ES256", rejects any "d" private key parameter in jwk, verifies ES256 signatures in constant time where applicable, normalizes htu without query/fragment, and enforces iat skew, ath, nonce, cnf.jkt, and jti replay hooks.
  • Server policy: Your backend must store per-token cnf.jkt bindings, enforce jti uniqueness across the skew window, rotate DPoP-Nonce values, and verify Android Key Attestation chains against Google's hardware root CA when attestation is required.

Building & Testing

# Run all unit, DPoP, and real localhost TCP Ktor HTTP end-to-end tests + Android & iOS compilation
./gradlew :library:jvmTest :library:testAndroidHostTest :library:compileAndroidMain \
  :library:compileKotlinIosArm64 :library:compileTestKotlinIosSimulatorArm64 \
  :kothless-ktor:jvmTest :kothless-ktor:testAndroidHostTest :kothless-ktor:compileAndroidMain \
  :kothless-ktor:compileKotlinIosArm64

# On macOS: execute Security.framework & DPoP tests on iOS Simulator
./gradlew :library:iosSimulatorArm64Test :kothless-ktor:compileTestKotlinIosSimulatorArm64

License

Apache License 2.0. See LICENSE.

About

Hardware-backed RFC 9449 DPoP & ECDSA P-256 device-bound signing for Kotlin Multiplatform (Android Keystore, iOS Secure Enclave, JVM, and Ktor).

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages