From 2a31187f79387fd8ee53c72c50c97ea09aa0556f Mon Sep 17 00:00:00 2001 From: Simon Mueller Date: Thu, 19 Oct 2023 11:38:23 +0200 Subject: [PATCH] Integrate Pull request changes --- .../crypto/datatypes/jws/JsonWebKey.kt | 43 ++++++------------- .../src/jvmTest/kotlin/JsonWebKeyJvmTest.kt | 24 ----------- .../asitplus/crypto/datatypes/io/Encoding.kt | 5 +-- datatypes/src/jvmTest/kotlin/PublicKeyTest.kt | 4 +- 4 files changed, 17 insertions(+), 59 deletions(-) diff --git a/datatypes-jws/src/commonMain/kotlin/at/asitplus/crypto/datatypes/jws/JsonWebKey.kt b/datatypes-jws/src/commonMain/kotlin/at/asitplus/crypto/datatypes/jws/JsonWebKey.kt index 0caaf3b1..da01bce6 100644 --- a/datatypes-jws/src/commonMain/kotlin/at/asitplus/crypto/datatypes/jws/JsonWebKey.kt +++ b/datatypes-jws/src/commonMain/kotlin/at/asitplus/crypto/datatypes/jws/JsonWebKey.kt @@ -113,32 +113,15 @@ data class JsonWebKey( } override fun toString() = - when (type) { - JwkType.EC -> "JsonWebKey(" + - "type=$type, " + - "curve=$curve, " + - "keyId=$keyId," + - "x=${x?.encodeToString(Base64Strict)}," + - "y=${y?.encodeToString(Base64Strict)}" + - ")" - - JwkType.RSA -> "JsonWebKey(" + - "type=$type, " + - "keyId=$keyId," + - "n=${n?.encodeToString(Base64Strict)})" + - "e=${e?.encodeToString(Base64Strict)}" + - ")" - - null -> "JsonWebKey(" + - "type=$type, " + - "curve=$curve, " + - "keyId=$keyId," + - "x=${x?.encodeToString(Base64Strict)}," + - "y=${y?.encodeToString(Base64Strict)}" + - "n=${n?.encodeToString(Base64Strict)})" + - "e=${e?.encodeToString(Base64Strict)}" + - ")" - } + "JsonWebKey(" + + "type=$type, " + + "curve=$curve, " + + "keyId=$keyId," + + "x=${x?.encodeToString(Base64Strict)}," + + "y=${y?.encodeToString(Base64Strict)}" + + "n=${n?.encodeToString(Base64Strict)})" + + "e=${e?.encodeToString(Base64Strict)}" + + ")" fun toCryptoPublicKey(): CryptoPublicKey? = when (type) { @@ -153,9 +136,9 @@ data class JsonWebKey( } JwkType.RSA -> { - this.n?.let { + this.let { CryptoPublicKey.Rsa( - n = it, + n = n ?: return null, e = e?.let { bytes -> Int.decodeFromDer(bytes) } ?: return null ) } @@ -175,7 +158,7 @@ fun CryptoPublicKey.toJsonWebKey(): JsonWebKey = curve = curve, x = x, y = y - ) + ).apply { jwkId = identifier } is CryptoPublicKey.Rsa -> JsonWebKey( @@ -183,7 +166,7 @@ fun CryptoPublicKey.toJsonWebKey(): JsonWebKey = keyId = jwkId, n = n, e = e.encodeToByteArray() - ) + ).apply { jwkId = identifier } } private const val JWK_ID = "jwkIdentifier" diff --git a/datatypes-jws/src/jvmTest/kotlin/JsonWebKeyJvmTest.kt b/datatypes-jws/src/jvmTest/kotlin/JsonWebKeyJvmTest.kt index 1cab73d6..84ee770b 100644 --- a/datatypes-jws/src/jvmTest/kotlin/JsonWebKeyJvmTest.kt +++ b/datatypes-jws/src/jvmTest/kotlin/JsonWebKeyJvmTest.kt @@ -53,30 +53,6 @@ class JsonWebKeyJvmTest : FreeSpec({ } } - //Todo move to CryptoPublicKeyTest -// "JWK can be created from ANSI X962" - { -// val xFromBc = (keyPair.public as ECPublicKey).w.affineX.toByteArray().ensureSize(ecCurve.coordinateLengthBytes) -// val yFromBc = (keyPair.public as ECPublicKey).w.affineY.toByteArray().ensureSize(ecCurve.coordinateLengthBytes) -// val ansiX962 = byteArrayOf(0x04) + xFromBc + yFromBc -// val jsonWebKey = CryptoPublicKey.Ec.fromAnsiX963Bytes(ansiX962)!!.toJsonWebKey() -// -// jsonWebKey.shouldNotBeNull() -// jsonWebKey.x shouldBe xFromBc -// jsonWebKey.y shouldBe yFromBc -// jsonWebKey.keyId.shouldNotBeNull() -// jsonWebKey.keyId shouldHaveMinLength 32 -// jsonWebKey.toAnsiX963ByteArray().getOrThrow() shouldBe ansiX962 -// -// "it can be recreated" { -// val recreatedJwk = JsonWebKey.fromKeyId(jsonWebKey.keyId!!) -// recreatedJwk.shouldNotBeNull() -// recreatedJwk.keyId shouldBe jsonWebKey.keyId -// recreatedJwk.x shouldBe jsonWebKey.x -// recreatedJwk.y shouldBe jsonWebKey.y -// jsonWebKey.toAnsiX963ByteArray().getOrThrow() shouldBe ansiX962 -// } -// } - "JWK can be created from n and e" - { val nFromBc = (keyPairRSA.public as RSAPublicKey).modulus.toByteArray() val eFromBc = (keyPairRSA.public as RSAPublicKey).publicExponent.toInt() diff --git a/datatypes/src/commonMain/kotlin/at/asitplus/crypto/datatypes/io/Encoding.kt b/datatypes/src/commonMain/kotlin/at/asitplus/crypto/datatypes/io/Encoding.kt index 58f794a9..e5f53a17 100644 --- a/datatypes/src/commonMain/kotlin/at/asitplus/crypto/datatypes/io/Encoding.kt +++ b/datatypes/src/commonMain/kotlin/at/asitplus/crypto/datatypes/io/Encoding.kt @@ -79,11 +79,10 @@ object MultibaseHelper { private fun multibaseWrapBase64(it: ByteArray) = "m${it.encodeToString(Base64Strict)}" - // 0x1200 would be with compression, so we'll use 0x1290 - private fun multicodecWrapEC(it: ByteArray) = byteArrayOf(0x12.toByte(), 0x90.toByte()) + it + private fun multicodecWrapRSA(it: ByteArray) = byteArrayOf(0x12.toByte(), 0x05.toByte()) + it // 0x1200 would be with compression, so we'll use 0x1290 - private fun multicodecWrapRSA(it: ByteArray) = byteArrayOf(0x12.toByte(), 0x05.toByte()) + it + private fun multicodecWrapEC(it: ByteArray) = byteArrayOf(0x12.toByte(), 0x90.toByte()) + it // No compression, because decompression would need some EC math private fun encodeEcKey(x: ByteArray, y: ByteArray, curve: EcCurve) = diff --git a/datatypes/src/jvmTest/kotlin/PublicKeyTest.kt b/datatypes/src/jvmTest/kotlin/PublicKeyTest.kt index 81472556..623a0e2d 100644 --- a/datatypes/src/jvmTest/kotlin/PublicKeyTest.kt +++ b/datatypes/src/jvmTest/kotlin/PublicKeyTest.kt @@ -51,6 +51,7 @@ class PublicKeyTest : FreeSpec({ } } } + "RSA" - { withData(512, 1024, 2048, 3072, 4096) { bits -> val keys = List(13000 / bits) { @@ -70,7 +71,7 @@ class PublicKeyTest : FreeSpec({ val own = CryptoPublicKey.Rsa(pubKey.modulus.toByteArray(), pubKey.publicExponent.toInt()) val own1 = CryptoPublicKey.Rsa( - byteArrayOf(0, 0, 0) + pubKey.modulus.toByteArray(), + ByteArray((0..10).random()) { 0 } + pubKey.modulus.toByteArray(), pubKey.publicExponent.toInt() ) @@ -90,5 +91,4 @@ class PublicKeyTest : FreeSpec({ } } } - })