Skip to content

Commit

Permalink
fix: pr requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianIOHK committed Nov 14, 2023
1 parent 7cbaafd commit 45c16df
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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<String, String>) {
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 }
}
}
}
Original file line number Diff line number Diff line change
@@ -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-----"))
}
}

0 comments on commit 45c16df

Please sign in to comment.