From 0223716280da3def903e45d60125ef13b3bab39d Mon Sep 17 00:00:00 2001 From: Christian Kollmann Date: Fri, 23 Aug 2024 07:52:53 +0200 Subject: [PATCH] Extract extension functions --- warden/src/main/kotlin/AttestationService.kt | 39 +++++++++----------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/warden/src/main/kotlin/AttestationService.kt b/warden/src/main/kotlin/AttestationService.kt index f3a39e0..bda1033 100644 --- a/warden/src/main/kotlin/AttestationService.kt +++ b/warden/src/main/kotlin/AttestationService.kt @@ -1,5 +1,6 @@ package at.asitplus.attestation +import at.asitplus.KmmResult import at.asitplus.attestation.AttestationException import at.asitplus.attestation.IOSAttestationConfiguration.AppData import at.asitplus.attestation.android.* @@ -295,30 +296,24 @@ abstract class AttestationService { keyToBeAttested: T, firstTry: AttestationResult.Android ): KeyAttestation = - if (CryptoPublicKey.fromJcaPublicKey(keyToBeAttested) == CryptoPublicKey.fromJcaPublicKey( - firstTry.attestationCertificate.publicKey - ) - ) KeyAttestation(keyToBeAttested, firstTry) - else { - ("Android attestation failed: keyToBeAttested (${keyToBeAttested.encoded.encodeBase64()}) does not match " + - "key from attestation certificate: ${firstTry.attestationCertificate.publicKey.encoded.encodeBase64()}").let { - KeyAttestation( - null, - AttestationResult.Error( - explanation = it, - cause = AttException.Content.Android( - it, - AttestationValueException( - it, - cause = null, - reason = AttestationValueException.Reason.APP_UNEXPECTED - ) - ) - ) - ) - } + if (keyToBeAttested.toCryptoPublicKey() == firstTry.attestationCertificate.publicKey.toCryptoPublicKey()) { + KeyAttestation(keyToBeAttested, firstTry) + } else { + val reason = "Android attestation failed: keyToBeAttested (${keyToBeAttested.toLogString()}) does not " + + "match key from attestation certificate: ${firstTry.attestationCertificate.publicKey.toLogString()}" + AttException.Content.Android( + reason, AttestationValueException(reason, null, AttestationValueException.Reason.APP_UNEXPECTED) + ).toAttestationError(reason) } + private fun T.toLogString(): String? = encoded.encodeBase64() + + private fun AttestationException.Content.toAttestationError(it: String): KeyAttestation = + KeyAttestation(null, AttestationResult.Error(it, this)) + + private fun T.toCryptoPublicKey(): KmmResult = + CryptoPublicKey.fromJcaPublicKey(this) + /** Same as [verifyKeyAttestation], but taking an encoded (either ANSI X9.63 or DER) publix key as a byte array * @see verifyKeyAttestation