diff --git a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/Ed25519PrivateKey.kt b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/Ed25519PrivateKey.kt index eaef4a266..8cb5136cf 100644 --- a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/Ed25519PrivateKey.kt +++ b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/Ed25519PrivateKey.kt @@ -8,6 +8,7 @@ import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.ExportableKey import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.JWK import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.KeyTypes import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.PEMKey +import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.PEMKeyType import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.PrivateKey import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.PublicKey import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.SignableKey @@ -37,7 +38,7 @@ class Ed25519PrivateKey(nativeValue: ByteArray) : PrivateKey(), SignableKey, Sto override fun getPem(): String { return PEMKey( - keyType = "EC PRIVATE KEY", + keyType = PEMKeyType.EC_PRIVATE_KEY, keyData = raw ).pemEncoded() } diff --git a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/Ed25519PublicKey.kt b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/Ed25519PublicKey.kt index ff1f97e91..e4ffcd981 100644 --- a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/Ed25519PublicKey.kt +++ b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/Ed25519PublicKey.kt @@ -8,6 +8,7 @@ import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.ExportableKey import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.JWK import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.KeyTypes import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.PEMKey +import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.PEMKeyType import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.PublicKey import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.StorableKey import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.VerifiableKey @@ -30,7 +31,7 @@ class Ed25519PublicKey(nativeValue: ByteArray) : PublicKey(), VerifiableKey, Sto override fun getPem(): String { return PEMKey( - keyType = "EC PUBLIC KEY", + keyType = PEMKeyType.EC_PUBLIC_KEY, keyData = raw ).pemEncoded() } diff --git a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/Secp256k1PrivateKey.kt b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/Secp256k1PrivateKey.kt index df9afa160..c6e9d2451 100644 --- a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/Secp256k1PrivateKey.kt +++ b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/Secp256k1PrivateKey.kt @@ -10,6 +10,7 @@ import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.ExportableKey import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.JWK import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.KeyTypes import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.PEMKey +import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.PEMKeyType import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.PrivateKey import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.PublicKey import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.SignableKey @@ -37,7 +38,7 @@ class Secp256k1PrivateKey(nativeValue: ByteArray) : PrivateKey(), SignableKey, S override fun getPem(): String { return PEMKey( - keyType = "EC PRIVATE KEY", + keyType = PEMKeyType.EC_PRIVATE_KEY, keyData = raw ).pemEncoded() } diff --git a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/Secp256k1PublicKey.kt b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/Secp256k1PublicKey.kt index a2b067a93..87219667e 100644 --- a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/Secp256k1PublicKey.kt +++ b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/Secp256k1PublicKey.kt @@ -12,6 +12,7 @@ import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.ExportableKey import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.JWK import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.KeyTypes import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.PEMKey +import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.PEMKeyType import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.PublicKey import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.StorableKey import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.VerifiableKey @@ -42,7 +43,7 @@ class Secp256k1PublicKey(nativeValue: ByteArray) : PublicKey(), VerifiableKey, S override fun getPem(): String { return PEMKey( - keyType = "EC PUBLIC KEY", + keyType = PEMKeyType.EC_PUBLIC_KEY, keyData = raw ).pemEncoded() } diff --git a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/X25519PrivateKey.kt b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/X25519PrivateKey.kt index 0c9fa8cf5..32af0c0a9 100644 --- a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/X25519PrivateKey.kt +++ b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/X25519PrivateKey.kt @@ -8,6 +8,7 @@ import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.ExportableKey import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.JWK import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.KeyTypes import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.PEMKey +import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.PEMKeyType import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.PrivateKey import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.PublicKey import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.StorableKey @@ -30,7 +31,7 @@ class X25519PrivateKey(nativeValue: ByteArray) : PrivateKey(), StorableKey, Expo override fun getPem(): String { return PEMKey( - keyType = "EC PRIVATE KEY", + keyType = PEMKeyType.EC_PRIVATE_KEY, keyData = raw ).pemEncoded() } diff --git a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/X25519PublicKey.kt b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/X25519PublicKey.kt index c2c58c4ac..789d8c581 100644 --- a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/X25519PublicKey.kt +++ b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/X25519PublicKey.kt @@ -7,6 +7,7 @@ import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.ExportableKey import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.JWK import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.KeyTypes import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.PEMKey +import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.PEMKeyType import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.PublicKey import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.StorableKey @@ -23,7 +24,7 @@ class X25519PublicKey(nativeValue: ByteArray) : PublicKey(), ExportableKey, Stor override fun getPem(): String { return PEMKey( - keyType = "EC PUBLIC KEY", + keyType = PEMKeyType.EC_PUBLIC_KEY, keyData = raw ).pemEncoded() } diff --git a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/domain/models/keyManagement/ExportableImportableKey.kt b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/domain/models/keyManagement/ExportableImportableKey.kt index 8a78871c8..d63d5b8f3 100644 --- a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/domain/models/keyManagement/ExportableImportableKey.kt +++ b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/domain/models/keyManagement/ExportableImportableKey.kt @@ -1,8 +1,7 @@ package io.iohk.atala.prism.walletsdk.domain.models.keyManagement -import io.iohk.atala.prism.apollo.base64.base64UrlDecoded +import io.iohk.atala.prism.apollo.base64.base64PadEncoded import io.iohk.atala.prism.apollo.base64.base64UrlDecodedBytes -import io.iohk.atala.prism.apollo.base64.base64UrlEncoded import kotlinx.serialization.Serializable interface ExportableKey { @@ -40,15 +39,15 @@ data class JWK( val k: String? = null ) -data class PEMKey(val keyType: String, val keyData: ByteArray) { - constructor(keyType: String, keyData: String) : this(keyType, keyData.base64UrlDecodedBytes) +data class PEMKey(val keyType: PEMKeyType, val keyData: ByteArray) { + constructor(keyType: PEMKeyType, keyData: String) : this(keyType, keyData.base64UrlDecodedBytes) fun pemEncoded(): String { - val base64Data = keyData.base64UrlEncoded + val base64Data = keyData.base64PadEncoded val beginMarker = "-----BEGIN $keyType-----" val endMarker = "-----END $keyType-----" - return "$beginMarker\n$base64Data$endMarker" + return "$beginMarker\n$base64Data\n$endMarker" } companion object { @@ -61,17 +60,29 @@ data class PEMKey(val keyType: String, val keyData: ByteArray) { val beginMarker = lines[0] val endMarker = lines[lines.size - 1] - if (!beginMarker.startsWith("-----BEGIN ") || !beginMarker.endsWith("-----") || - !endMarker.startsWith("-----END ") || !endMarker.endsWith("-----") + if (beginMarker.startsWith("-----BEGIN ").not() || beginMarker.endsWith("-----").not() || + endMarker.startsWith("-----END ").not() || endMarker.endsWith("-----").not() ) { return null } - val keyType = beginMarker.substring(11, beginMarker.length - 5) + val keyType = PEMKeyType.fromString(beginMarker) ?: throw Exception("Unknown PEM Key type") + val base64Data = lines.subList(1, lines.size - 1).joinToString("") - val keyData = base64Data.base64UrlDecoded + val keyData = base64Data.base64PadEncoded return PEMKey(keyType = keyType, keyData = keyData) } } } + +enum class PEMKeyType(val value: Pair) { + EC_PRIVATE_KEY(Pair("-----BEGIN EC PRIVATE KEY-----", "-----END EC PRIVATE KEY-----")), + EC_PUBLIC_KEY(Pair("-----BEGIN EC PUBLIC KEY-----", "-----END EC PUBLIC KEY-----")); + + companion object { + fun fromString(value: String): PEMKeyType? { + return values().firstOrNull { it.value.first == value || it.value.second == value } + } + } +} diff --git a/atala-prism-sdk/src/commonTest/kotlin/io/iohk/atala/prism/walletsdk/domain/models/keyManagement/ExportableImportableKeyTest.kt b/atala-prism-sdk/src/commonTest/kotlin/io/iohk/atala/prism/walletsdk/domain/models/keyManagement/ExportableImportableKeyTest.kt new file mode 100644 index 000000000..86d79111d --- /dev/null +++ b/atala-prism-sdk/src/commonTest/kotlin/io/iohk/atala/prism/walletsdk/domain/models/keyManagement/ExportableImportableKeyTest.kt @@ -0,0 +1,24 @@ +package io.iohk.atala.prism.walletsdk.domain.models.keyManagement + +import junit.framework.TestCase.assertNull +import org.junit.Test +import kotlin.test.assertEquals +import kotlin.test.assertNotNull + +class ExportableImportableKeyTest { + + @Test + fun testPEMKeyTypeFromString_whenCorrectStringProvided_thenSuccess() { + val keyTypePrivate = PEMKeyType.fromString("-----BEGIN EC PRIVATE KEY-----") + assertNotNull(keyTypePrivate) + assertEquals(PEMKeyType.EC_PRIVATE_KEY, keyTypePrivate) + val keyTypePublic = PEMKeyType.fromString("-----BEGIN EC PUBLIC KEY-----") + assertNotNull(keyTypePublic) + assertEquals(PEMKeyType.EC_PUBLIC_KEY, keyTypePublic) + } + + @Test + fun testPEMKeyTypeFromString_whenWrongStringProvided_thenReturnNull() { + assertNull(PEMKeyType.fromString("-----BEGIN FAIL KEY-----")) + } +}