Skip to content

Commit

Permalink
Refactor Test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
n0900 committed Dec 14, 2023
1 parent 8c57d91 commit b8afabb
Show file tree
Hide file tree
Showing 18 changed files with 270 additions and 140 deletions.
2 changes: 1 addition & 1 deletion conventions-vclib/src/main/kotlin/VcLibVersions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ object VcLibVersions {
const val resultlib = "1.5.3"
const val encoding = "1.2.3"
const val okio = "3.5.0"
const val kmpcrypto = "1.0-SNAPSHOT"
const val kmpcrypto = "2.2.0-SNAPSHOT"

object Jvm {
const val `jose-jwt` = "9.31"
Expand Down
2 changes: 1 addition & 1 deletion kmp-crypto
8 changes: 6 additions & 2 deletions vclib-aries/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ val artifactVersion: String by extra
group = "at.asitplus.wallet"
version = artifactVersion

val iosDisabled: String? by extra


kotlin {
jvm()
iosArm64()
if (iosDisabled != "true") {
iosArm64()
iosSimulatorArm64()
iosX64()
}
sourceSets {
commonMain {
dependencies {
Expand All @@ -41,7 +45,7 @@ kotlin {
}
}
}
exportIosFramework("VcLibAriesKmm", *commonIosExports(), project(":vclib"))
if (iosDisabled != "true") exportIosFramework("VcLibAriesKmm", *commonIosExports(), project(":vclib"))

val javadocJar = setupDokka(baseUrl = "https://github.com/a-sit-plus/kmm-vc-library/tree/main/", multiModuleDoc = true)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package at.asitplus.wallet.lib.aries

import at.asitplus.crypto.datatypes.JwsAlgorithm
import at.asitplus.crypto.datatypes.jws.JwsAlgorithm
import at.asitplus.crypto.datatypes.jws.JsonWebKey
import at.asitplus.wallet.lib.agent.Holder
import at.asitplus.wallet.lib.agent.Verifier
Expand Down
15 changes: 11 additions & 4 deletions vclib-openid/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@ group = "at.asitplus.wallet"
version = artifactVersion


val iosDisabled: String? by extra


kotlin {
jvm()
iosArm64()
iosSimulatorArm64()
iosX64()
if (iosDisabled != "true") {
iosArm64()
iosSimulatorArm64()
iosX64()
}
sourceSets {
commonMain {
dependencies {
commonImplementationDependencies()
api("at.asitplus.crypto:datatypes-jws:${VcLibVersions.kmpcrypto}")
api(project(":vclib"))
}
}
Expand All @@ -41,7 +47,8 @@ kotlin {
}
}

exportIosFramework("VcLibOpenIdKmm", *commonIosExports(), project(":vclib"))
if (iosDisabled != "true") exportIosFramework("VcLibOpenIdKmm", *commonIosExports(), project(":vclib"))

val javadocJar = setupDokka(baseUrl = "https://github.com/a-sit-plus/kmm-vc-library/tree/main/", multiModuleDoc = true)

publishing {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package at.asitplus.wallet.lib.oidc

import at.asitplus.crypto.datatypes.CryptoPublicKey
import at.asitplus.crypto.datatypes.JwsAlgorithm
import at.asitplus.crypto.datatypes.jws.JwsAlgorithm
import at.asitplus.crypto.datatypes.jws.JwsHeader
import at.asitplus.crypto.datatypes.jws.JwsSigned
import at.asitplus.crypto.datatypes.jws.toJsonWebKey
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package at.asitplus.wallet.lib.oidc

import at.asitplus.KmmResult
import at.asitplus.crypto.datatypes.CryptoPublicKey
import at.asitplus.crypto.datatypes.JwsAlgorithm
import at.asitplus.crypto.datatypes.jws.JwsAlgorithm
import at.asitplus.crypto.datatypes.jws.JwsHeader
import at.asitplus.crypto.datatypes.jws.JwsSigned
import at.asitplus.crypto.datatypes.jws.toJsonWebKey
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package at.asitplus.wallet.lib.oidvci

import at.asitplus.crypto.datatypes.JwsAlgorithm
import at.asitplus.crypto.datatypes.jws.JwsAlgorithm
import at.asitplus.crypto.datatypes.cose.toCoseKey
import at.asitplus.crypto.datatypes.io.Base64UrlStrict
import at.asitplus.crypto.datatypes.jws.JsonWebToken
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package at.asitplus.wallet.lib.oidvci

import at.asitplus.crypto.datatypes.jws.JsonWebToken
import at.asitplus.crypto.datatypes.jws.JwsHeader
import at.asitplus.crypto.datatypes.jws.toJwsAlgorithm
import at.asitplus.wallet.lib.agent.CryptoService
import at.asitplus.wallet.lib.agent.DefaultCryptoService
import at.asitplus.wallet.lib.data.ConstantIndex
Expand Down Expand Up @@ -91,7 +92,7 @@ class WalletService(
proofType = OpenIdConstants.ProofTypes.JWT,
jwt = jwsService.createSignedJwsAddingParams(
header = JwsHeader(
algorithm = cryptoService.algorithm,
algorithm = cryptoService.algorithm.toJwsAlgorithm(),
type = OpenIdConstants.ProofTypes.JWT_HEADER_TYPE,
),
payload = JsonWebToken(
Expand Down
15 changes: 10 additions & 5 deletions vclib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@ val artifactVersion: String by extra
group = "at.asitplus.wallet"
version = artifactVersion


val iosDisabled: String? by extra


kotlin {
jvm()
iosArm64()
iosSimulatorArm64()
iosX64()
if (iosDisabled != "true") {
iosArm64()
iosSimulatorArm64()
iosX64()
}
sourceSets {

commonMain {
Expand Down Expand Up @@ -48,8 +54,7 @@ kotlin {
}
}
}

exportIosFramework("VcLibKmm", *commonIosExports())
if (iosDisabled != "true") exportIosFramework("VcLibKmm", *commonIosExports())

val javadocJar = setupDokka(baseUrl = "https://github.com/a-sit-plus/kmm-vc-library/tree/main/", multiModuleDoc = true)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ interface CryptoService {

fun messageDigest(input: ByteArray, digest: Digest): KmmResult<ByteArray>

val algorithm: JwsAlgorithm
val algorithm: CryptoAlgorithm

val publicKey: CryptoPublicKey

Expand All @@ -63,7 +63,7 @@ interface VerifierCryptoService {
fun verify(
input: ByteArray,
signature: CryptoSignature,
algorithm: JwsAlgorithm,
algorithm: CryptoAlgorithm,
publicKey: CryptoPublicKey,
): KmmResult<Boolean>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class DefaultVerifierCoseService(
val verified = cryptoService.verify(
input = signatureInput,
signature = coseSigned.signature,
algorithm = algorithm.toJwsAlgorithm(),
algorithm = algorithm.toCryptoAlgorithm(),
publicKey = publicKey
)
val result = verified.getOrElse {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class DefaultJwsService(private val cryptoService: CryptoService) : JwsService {
contentType: String?
): JwsSigned? {
val jwsHeader = JwsHeader(
algorithm = cryptoService.algorithm,
algorithm = cryptoService.algorithm.toJwsAlgorithm(),
keyId = cryptoService.publicKey.keyId,
type = type,
contentType = contentType
Expand All @@ -77,7 +77,7 @@ class DefaultJwsService(private val cryptoService: CryptoService) : JwsService {
}

override suspend fun createSignedJws(header: JwsHeader, payload: ByteArray): JwsSigned? {
if (header.algorithm != cryptoService.algorithm
if (header.algorithm != cryptoService.algorithm.toJwsAlgorithm()
|| header.keyId?.let { it != cryptoService.publicKey.keyId } == true
|| header.jsonWebKey?.let { it != cryptoService.jsonWebKey } == true
) {
Expand All @@ -98,7 +98,7 @@ class DefaultJwsService(private val cryptoService: CryptoService) : JwsService {
addKeyId: Boolean,
addJsonWebKey: Boolean
): JwsSigned? {
var copy = header.copy(algorithm = cryptoService.algorithm)
var copy = header.copy(algorithm = cryptoService.algorithm.toJwsAlgorithm())
if (addKeyId)
copy = copy.copy(keyId = cryptoService.publicKey.keyId)
if (addJsonWebKey)
Expand Down Expand Up @@ -219,15 +219,14 @@ class DefaultVerifierJwsService(
val verified = cryptoService.verify(
input = jwsObject.plainSignatureInput.encodeToByteArray(),
signature = jwsObject.signature,
algorithm = header.algorithm,
algorithm = header.algorithm.toCryptoAlgorithm(),
publicKey = publicKey
)
// val falseVar = false //workaround kotlin bug for linking xcframework
// return verified.getOrElse {
// Napier.w("No verification from native code", it)
// falseVar
// }
return verified.getOrThrow()
val falseVar = false //workaround kotlin bug for linking xcframework
return verified.getOrElse {
Napier.w("No verification from native code", it)
falseVar
}
}

/**
Expand All @@ -240,7 +239,7 @@ class DefaultVerifierJwsService(
val verified = cryptoService.verify(
jwsObject.plainSignatureInput.encodeToByteArray(),
jwsObject.signature,
jwsObject.header.algorithm,
jwsObject.header.algorithm.toCryptoAlgorithm(),
publicKey,
)
val falseVar = false //workaround kotlin bug for linking xcframework
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package at.asitplus.wallet.lib.agent

import at.asitplus.crypto.datatypes.JwsAlgorithm
import at.asitplus.crypto.datatypes.jws.JwsAlgorithm
import at.asitplus.crypto.datatypes.jws.JwsContentTypeConstants
import at.asitplus.crypto.datatypes.jws.JwsHeader
import at.asitplus.crypto.datatypes.jws.JwsSigned
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package at.asitplus.wallet.lib.jws


import at.asitplus.crypto.datatypes.JwsAlgorithm
import at.asitplus.crypto.datatypes.jws.JwsAlgorithm
import at.asitplus.crypto.datatypes.io.Base64Strict
import at.asitplus.crypto.datatypes.jws.JwsContentTypeConstants
import at.asitplus.crypto.datatypes.jws.JwsHeader
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package at.asitplus.wallet.lib.jws

import at.asitplus.crypto.datatypes.JwsAlgorithm
import at.asitplus.crypto.datatypes.jws.JwsAlgorithm
import at.asitplus.crypto.datatypes.jws.*
import at.asitplus.wallet.lib.agent.CryptoService
import at.asitplus.wallet.lib.agent.DefaultCryptoService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ actual open class DefaultCryptoService : CryptoService {

private val privateKey: PrivateKey

final override val algorithm: JwsAlgorithm
final override val algorithm: CryptoAlgorithm

final override val publicKey: CryptoPublicKey

Expand All @@ -57,7 +57,7 @@ actual open class DefaultCryptoService : CryptoService {
val keyPair =
KeyPairGenerator.getInstance("EC").also { it.initialize(SECP_256_R_1.keyLengthBits.toInt()) }.genKeyPair()
this.privateKey = keyPair.private
this.algorithm = JwsAlgorithm.ES256
this.algorithm = CryptoAlgorithm.ES256
this.publicKey = CryptoPublicKey.fromJcaPublicKey(keyPair.public).getOrThrow()
this.jsonWebKey = publicKey.toJsonWebKey().getOrThrow()
this.coseKey = publicKey.toCoseKey(algorithm.toCoseAlgorithm()).getOrThrow()
Expand All @@ -70,7 +70,7 @@ actual open class DefaultCryptoService : CryptoService {
* it's mandatory
* Also used for custom certificates
*/
constructor(keyPair: KeyPair, algorithm: JwsAlgorithm, certificate: Certificate? = null) {
constructor(keyPair: KeyPair, algorithm: CryptoAlgorithm, certificate: Certificate? = null) {
this.privateKey = keyPair.private
this.algorithm = algorithm
this.publicKey = CryptoPublicKey.fromJcaPublicKey(keyPair.public).getOrThrow()
Expand Down Expand Up @@ -105,29 +105,15 @@ actual open class DefaultCryptoService : CryptoService {
return X509Certificate(tbsCertificate, algorithm, signature)
}

// override suspend fun sign(input: ByteArray): KmmResult<CryptoSignature> =
// runCatching {
// Signature.getInstance(algorithm.jcaName).apply {
// initSign(privateKey)
// update(input)
// }.sign()
// }.wrap().mapCatching {
//// when (algorithm) {
//// JwsAlgorithm.ES256, JwsAlgorithm.ES384, JwsAlgorithm.ES512 -> CryptoSignature.EC(it)
//// else -> CryptoSignature.RSAorHMAC(it)
//// }
// CryptoSignature.decodeFromDer(it)
// }
override suspend fun sign(input: ByteArray): KmmResult<CryptoSignature> =
runCatching {
val sig = Signature.getInstance(algorithm.jcaName).apply {
initSign(privateKey)
update(input)
}.sign()
val test = sig.encodeToString(Base64UrlStrict)
println(test)
CryptoSignature.decodeFromDer(sig)
}.wrap().also { it.getOrThrow() }
val res = if (algorithm.name.take(2) == "ES") CryptoSignature.decodeFromDer(sig) else CryptoSignature.RSAorHMAC(sig) //In Java EC signatures are returned as DER-encoded, RSA signatures however are raw bytearrays
res
}.wrap()

override fun encrypt(
key: ByteArray, iv: ByteArray, aad: ByteArray, input: ByteArray, algorithm: JweEncryption
Expand Down Expand Up @@ -207,7 +193,7 @@ actual open class DefaultVerifierCryptoService : VerifierCryptoService {
override fun verify(
input: ByteArray,
signature: CryptoSignature,
algorithm: JwsAlgorithm,
algorithm: CryptoAlgorithm,
publicKey: CryptoPublicKey,
): KmmResult<Boolean> =
runCatching {
Expand Down
Loading

0 comments on commit b8afabb

Please sign in to comment.