Skip to content

Commit

Permalink
update dependencies and kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
JesusMcCloud committed Sep 16, 2024
1 parent 221824c commit 26403fe
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 39 deletions.
2 changes: 2 additions & 0 deletions conventions-vclib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ repositories {
maven("https://maven.pkg.jetbrains.space/kotlin/p/dokka/dev")
mavenCentral()
gradlePluginPortal()

maven("https://s01.oss.sonatype.org/content/repositories/snapshots") //KOTEST snapshot
}

gradlePlugin {
Expand Down
6 changes: 6 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
pluginManagement {
repositories {
maven("https://s01.oss.sonatype.org/content/repositories/snapshots") //Kotest snapshot for Kotlin 2.0.20 until new Kotest stable is released
google()
gradlePluginPortal()
mavenCentral()
}
includeBuild("conventions-vclib")
}
rootProject.name = "vclibrary"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ interface ZlibService {

}

expect class DefaultZlibService() : ZlibService
expect class DefaultZlibService() : ZlibService{
override fun compress(input: ByteArray): ByteArray?
override fun decompress(input: ByteArray): ByteArray?
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package at.asitplus.wallet.lib.agent

import at.asitplus.KmmResult
import at.asitplus.wallet.lib.jws.EcCurve
import at.asitplus.wallet.lib.jws.JsonWebKey
import at.asitplus.wallet.lib.jws.JweAlgorithm
import at.asitplus.wallet.lib.jws.JweEncryption
import at.asitplus.wallet.lib.jws.JwsAlgorithm
import at.asitplus.wallet.lib.jws.*

interface CryptoService {

Expand Down Expand Up @@ -85,6 +81,51 @@ interface EphemeralKeyHolder {
fun toPublicJsonWebKey(): JsonWebKey
}

expect class DefaultCryptoService() : CryptoService
expect class DefaultCryptoService() : CryptoService {
override suspend fun sign(input: ByteArray): KmmResult<ByteArray>
override fun encrypt(
key: ByteArray,
iv: ByteArray,
aad: ByteArray,
input: ByteArray,
algorithm: JweEncryption
): KmmResult<AuthenticatedCiphertext>

expect class DefaultVerifierCryptoService() : VerifierCryptoService
override suspend fun decrypt(
key: ByteArray,
iv: ByteArray,
aad: ByteArray,
input: ByteArray,
authTag: ByteArray,
algorithm: JweEncryption
): KmmResult<ByteArray>

override fun generateEphemeralKeyPair(ecCurve: EcCurve): KmmResult<EphemeralKeyHolder>

override fun performKeyAgreement(
ephemeralKey: EphemeralKeyHolder,
recipientKey: JsonWebKey,
algorithm: JweAlgorithm
): KmmResult<ByteArray>

override fun performKeyAgreement(ephemeralKey: JsonWebKey, algorithm: JweAlgorithm): KmmResult<ByteArray>

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

override val keyId: String

override val jwsAlgorithm: JwsAlgorithm

override fun toJsonWebKey(): JsonWebKey
}

expect class DefaultVerifierCryptoService() : VerifierCryptoService {
override fun verify(
input: ByteArray,
signature: ByteArray,
algorithm: JwsAlgorithm,
publicKey: JsonWebKey
): KmmResult<Boolean>

override fun extractPublicKeyFromX509Cert(it: ByteArray): JsonWebKey?
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import platform.Foundation.decompressedDataUsingAlgorithm

actual class DefaultZlibService actual constructor() : ZlibService {

override fun compress(input: ByteArray): ByteArray? {
actual override fun compress(input: ByteArray): ByteArray? {
memScoped {
val data = toData(input)
val errorPointer = alloc<ObjCObjectVar<NSError?>>()
Expand Down Expand Up @@ -54,7 +54,7 @@ actual class DefaultZlibService actual constructor() : ZlibService {
private fun UInt.toByteArray(size: Int = 4): ByteArray =
ByteArray(size) { i -> (this.toLong() shr (i * 8)).toByte() }.reversedArray()

override fun decompress(input: ByteArray): ByteArray? {
actual override fun decompress(input: ByteArray): ByteArray? {
memScoped {
var data = toData(input)
if (input.size > 1 && input[0] == 0x78.toByte() && input[1] == 0x9C.toByte()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ import platform.CoreFoundation.CFDictionaryAddValue as CFDictionaryAddValue1
@Suppress("UNCHECKED_CAST")
actual class DefaultCryptoService : CryptoService {

override val keyId: String
override val jwsAlgorithm = JwsAlgorithm.ES256
actual override val keyId: String
actual override val jwsAlgorithm = JwsAlgorithm.ES256
private val privateKey: SecKeyRef
private val publicKey: SecKeyRef
private val jsonWebKey: JsonWebKey
Expand All @@ -72,7 +72,7 @@ actual class DefaultCryptoService : CryptoService {
this.keyId = jsonWebKey.keyId!!
}

override suspend fun sign(input: ByteArray): KmmResult<ByteArray> {
actual override suspend fun sign(input: ByteArray): KmmResult<ByteArray> {
memScoped {
val inputData = CFBridgingRetain(toData(input)) as CFDataRef
val signature =
Expand All @@ -82,7 +82,7 @@ actual class DefaultCryptoService : CryptoService {
}
}

override fun encrypt(
actual override fun encrypt(
key: ByteArray,
iv: ByteArray,
aad: ByteArray,
Expand All @@ -97,7 +97,7 @@ actual class DefaultCryptoService : CryptoService {
)
}

override suspend fun decrypt(
actual override suspend fun decrypt(
key: ByteArray,
iv: ByteArray,
aad: ByteArray,
Expand All @@ -111,7 +111,7 @@ actual class DefaultCryptoService : CryptoService {
KmmResult.failure(IllegalArgumentException())
}

override fun generateEphemeralKeyPair(ecCurve: EcCurve): KmmResult<EphemeralKeyHolder> {
actual override fun generateEphemeralKeyPair(ecCurve: EcCurve): KmmResult<EphemeralKeyHolder> {
val query = CFDictionaryCreateMutable(null, 2, null, null).apply {
CFDictionaryAddValue1(this, kSecAttrKeyType, kSecAttrKeyTypeEC)
CFDictionaryAddValue1(this, kSecAttrKeySizeInBits, CFBridgingRetain(NSNumber(256)))
Expand All @@ -123,30 +123,30 @@ actual class DefaultCryptoService : CryptoService {
return KmmResult.success(DefaultEphemeralKeyHolder(publicKey, privateKey))
}

override fun performKeyAgreement(
actual override fun performKeyAgreement(
ephemeralKey: EphemeralKeyHolder,
recipientKey: JsonWebKey,
algorithm: JweAlgorithm
): KmmResult<ByteArray> {
return KmmResult.success("sharedSecret-${algorithm.text}".encodeToByteArray())
}

override fun performKeyAgreement(ephemeralKey: JsonWebKey, algorithm: JweAlgorithm): KmmResult<ByteArray> {
actual override fun performKeyAgreement(ephemeralKey: JsonWebKey, algorithm: JweAlgorithm): KmmResult<ByteArray> {
return KmmResult.success("sharedSecret-${algorithm.text}".encodeToByteArray())
}

override fun messageDigest(input: ByteArray, digest: Digest): KmmResult<ByteArray> {
actual override fun messageDigest(input: ByteArray, digest: Digest): KmmResult<ByteArray> {
return KmmResult.success(input)
}

override fun toJsonWebKey() = jsonWebKey
actual override fun toJsonWebKey() = jsonWebKey

}

@Suppress("UNCHECKED_CAST")
actual class DefaultVerifierCryptoService : VerifierCryptoService {

override fun verify(
actual override fun verify(
input: ByteArray,
signature: ByteArray,
algorithm: JwsAlgorithm,
Expand Down Expand Up @@ -177,7 +177,7 @@ actual class DefaultVerifierCryptoService : VerifierCryptoService {
}
}

override fun extractPublicKeyFromX509Cert(it: ByteArray): JsonWebKey? {
actual override fun extractPublicKeyFromX509Cert(it: ByteArray): JsonWebKey? {
memScoped {
val certData = CFBridgingRetain(toData(it)) as CFDataRef
val certificate = SecCertificateCreateWithData(null, certData)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ actual class DefaultZlibService actual constructor() : ZlibService {
*/
private val MAX_DECOMPRESSED_SIZE = 5 * 1024 * 1024

override fun compress(input: ByteArray): ByteArray {
actual override fun compress(input: ByteArray): ByteArray? {
return DeflaterInputStream(input.inputStream(), Deflater(Deflater.DEFAULT_COMPRESSION)).readBytes()
}

/**
* Safely decompresses ZLIB encoded bytes, with max size [MAX_DECOMPRESSED_SIZE]
*/
override fun decompress(input: ByteArray): ByteArray {
actual override fun decompress(input: ByteArray): ByteArray? {
return InflaterInputStream(input.inputStream()).readBytes().also {
val inflaterStream = InflaterInputStream(input.inputStream())
val outputStream = ByteArrayOutputStream(DEFAULT_BUFFER_SIZE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ actual open class DefaultCryptoService : CryptoService {
private val ecCurve: EcCurve = EcCurve.SECP_256_R_1
private val keyPair: KeyPair
private val jsonWebKey: JsonWebKey
override val keyId: String
actual override val keyId: String get() = jsonWebKey.keyId!!

actual constructor() {
this.keyPair = KeyPairGenerator.getInstance("EC").also { it.initialize(ecCurve.keyLengthBits) }.genKeyPair()
Expand All @@ -44,7 +44,7 @@ actual open class DefaultCryptoService : CryptoService {
(keyPair.public as ECPublicKey).w.affineX.toByteArray().ensureSize(ecCurve.coordinateLengthBytes),
(keyPair.public as ECPublicKey).w.affineY.toByteArray().ensureSize(ecCurve.coordinateLengthBytes)
)!!
this.keyId = jsonWebKey.keyId!!

}

constructor(keyPair: KeyPair) {
Expand All @@ -55,14 +55,14 @@ actual open class DefaultCryptoService : CryptoService {
(keyPair.public as ECPublicKey).w.affineX.toByteArray().ensureSize(ecCurve.coordinateLengthBytes),
(keyPair.public as ECPublicKey).w.affineY.toByteArray().ensureSize(ecCurve.coordinateLengthBytes)
)!!
this.keyId = jsonWebKey.keyId!!

}

override val jwsAlgorithm = JwsAlgorithm.ES256
actual override val jwsAlgorithm = JwsAlgorithm.ES256

override fun toJsonWebKey() = jsonWebKey
actual override fun toJsonWebKey() = jsonWebKey

override suspend fun sign(input: ByteArray): KmmResult<ByteArray> =
actual override suspend fun sign(input: ByteArray): KmmResult<ByteArray> =
try {
val signed = Signature.getInstance(jwsAlgorithm.jcaName).apply {
initSign(keyPair.private)
Expand All @@ -73,7 +73,7 @@ actual open class DefaultCryptoService : CryptoService {
KmmResult.failure(e)
}

override fun encrypt(
actual override fun encrypt(
key: ByteArray,
iv: ByteArray,
aad: ByteArray,
Expand All @@ -95,7 +95,7 @@ actual open class DefaultCryptoService : CryptoService {
KmmResult.failure(e)
}

override suspend fun decrypt(
actual override suspend fun decrypt(
key: ByteArray,
iv: ByteArray,
aad: ByteArray,
Expand All @@ -116,7 +116,7 @@ actual open class DefaultCryptoService : CryptoService {
KmmResult.failure(e)
}

override fun performKeyAgreement(
actual override fun performKeyAgreement(
ephemeralKey: EphemeralKeyHolder,
recipientKey: JsonWebKey,
algorithm: JweAlgorithm
Expand All @@ -133,7 +133,7 @@ actual open class DefaultCryptoService : CryptoService {
}
}

override fun performKeyAgreement(ephemeralKey: JsonWebKey, algorithm: JweAlgorithm): KmmResult<ByteArray> = try {
actual override fun performKeyAgreement(ephemeralKey: JsonWebKey, algorithm: JweAlgorithm): KmmResult<ByteArray> = try {
val parameterSpec = ECNamedCurveTable.getParameterSpec(ephemeralKey.curve?.jcaName)
val ecPoint = parameterSpec.curve.validatePoint(BigInteger(1, ephemeralKey.x), BigInteger(1, ephemeralKey.y))
val ecPublicKeySpec = ECPublicKeySpec(ecPoint, parameterSpec)
Expand All @@ -147,10 +147,10 @@ actual open class DefaultCryptoService : CryptoService {
KmmResult.failure(e)
}

override fun generateEphemeralKeyPair(ecCurve: EcCurve): KmmResult<EphemeralKeyHolder> =
actual override fun generateEphemeralKeyPair(ecCurve: EcCurve): KmmResult<EphemeralKeyHolder> =
KmmResult.success(JvmEphemeralKeyHolder(ecCurve))

override fun messageDigest(input: ByteArray, digest: Digest): KmmResult<ByteArray> = try {
actual override fun messageDigest(input: ByteArray, digest: Digest): KmmResult<ByteArray> = try {
KmmResult.success(MessageDigest.getInstance(digest.jcaName).digest(input))
} catch (e: Throwable) {
KmmResult.failure(e)
Expand All @@ -160,7 +160,7 @@ actual open class DefaultCryptoService : CryptoService {

actual open class DefaultVerifierCryptoService : VerifierCryptoService {

override fun verify(
actual override fun verify(
input: ByteArray,
signature: ByteArray,
algorithm: JwsAlgorithm,
Expand All @@ -178,7 +178,7 @@ actual open class DefaultVerifierCryptoService : VerifierCryptoService {
}
}

override fun extractPublicKeyFromX509Cert(it: ByteArray): JsonWebKey? = try {
actual override fun extractPublicKeyFromX509Cert(it: ByteArray): JsonWebKey? = try {
val pubKey = CertificateFactory.getInstance("X.509").generateCertificate(it.inputStream()).publicKey
if (pubKey is ECPublicKey) JsonWebKey.fromJcaKey(pubKey, EcCurve.SECP_256_R_1) else null
} catch (e: Throwable) {
Expand Down

0 comments on commit 26403fe

Please sign in to comment.