Skip to content

Commit

Permalink
updated pem key import test
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeplotean committed Sep 14, 2023
1 parent 6bf5cc9 commit f2b1a2a
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,29 @@ package id.walt.services.key.deriver

import id.walt.crypto.KeyAlgorithm
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey
import sun.security.ec.ed.EdDSAOperations
import sun.security.ec.ed.EdDSAParameters
import java.math.BigInteger
import java.security.InvalidAlgorithmParameterException
//import sun.security.ec.ed.EdDSAOperations
//import sun.security.ec.ed.EdDSAParameters
//import java.security.InvalidAlgorithmParameterException
import java.security.KeyFactory
import java.security.PrivateKey
import java.security.PublicKey
import java.security.interfaces.RSAPrivateCrtKey
import java.security.spec.EdECPublicKeySpec
import java.security.spec.NamedParameterSpec
import java.security.spec.RSAPrivateKeySpec
import java.security.spec.RSAPublicKeySpec

class DefaultPublicKeyDeriver: PublicKeyDeriver<PrivateKey> {
override fun derive(key: PrivateKey): PublicKey? = when (KeyAlgorithm.fromString(key.algorithm)) {
KeyAlgorithm.RSA -> {
val kf = KeyFactory.getInstance("RSA")
val privateSpec = kf.getKeySpec(key, RSAPrivateKeySpec::class.java)
val publicSpec = RSAPublicKeySpec(privateSpec.modulus, BigInteger.valueOf(65537))
kf.generatePublic(publicSpec)
KeyAlgorithm.RSA -> (key as? RSAPrivateCrtKey)?.let {
KeyFactory.getInstance("RSA").generatePublic(RSAPublicKeySpec(it.modulus, it.publicExponent))
}
KeyAlgorithm.EdDSA_Ed25519 -> {
val edDsaOperations =
EdDSAOperations(EdDSAParameters.get({ InvalidAlgorithmParameterException() }, NamedParameterSpec.ED25519))
val edecPublicKeyPoint = edDsaOperations.computePublic(key.encoded)
val publicSpec = EdECPublicKeySpec(NamedParameterSpec.ED25519, edecPublicKeyPoint)
KeyFactory.getInstance("Ed25519").generatePublic(publicSpec)
// val edDsaOperations =
// EdDSAOperations(EdDSAParameters.get({ InvalidAlgorithmParameterException() }, NamedParameterSpec.ED25519))
// val edecPublicKeyPoint = edDsaOperations.computePublic(key.encoded)
// val publicSpec = EdECPublicKeySpec(NamedParameterSpec.ED25519, edecPublicKeyPoint)
// KeyFactory.getInstance("Ed25519").generatePublic(publicSpec)
null
}
//TODO: remove BC dependency, rely purely on java.security
KeyAlgorithm.ECDSA_Secp256k1, KeyAlgorithm.ECDSA_Secp256r1 -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ open class HKVKeyStoreService : KeyStoreService() {
val algorithm = metaData.substringBefore(delimiter = ";")
val provider = metaData.substringAfter(delimiter = ";")

val publicPart = loadKey(keyId, "enc-pubkey").decodeToString()
val publicPart = if (keyType == KeyType.PUBLIC) loadKey(keyId, "enc-pubkey").decodeToString() else null
val privatePart = if (keyType == KeyType.PRIVATE) loadKey(keyId, "enc-privkey").decodeToString() else null


Expand Down
36 changes: 26 additions & 10 deletions src/test/kotlin/id/walt/services/key/KeyServiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import io.kotest.data.blocking.forAll
import io.kotest.data.row
import io.kotest.matchers.collections.shouldContain
import io.kotest.matchers.collections.shouldNotContain
import io.kotest.matchers.should
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe
import org.bouncycastle.jce.provider.BouncyCastleProvider
Expand Down Expand Up @@ -268,17 +269,32 @@ class KeyServiceTest : AnnotationSpec() {
@Test
fun testImportPEMKey() {
forAll(
// RSA PEM
row(File("src/test/resources/key/rsa.pem").readText()),
// Ed25519 PEM
row(File("src/test/resources/key/ed25519.pem").readText()),
// Secp256k1 PEM
row(File("src/test/resources/key/secp256k1.pem").readText()),
) { keyStr ->
// RSA
row(File("src/test/resources/key/pem/rsa/rsa.pem").readText(), 0x11),
row(File("src/test/resources/key/pem/rsa/rsa.public.pem").readText(), 0x10),
row(File("src/test/resources/key/pem/rsa/rsa.private.pem").readText(), 0x01),
// Ed25519
row(File("src/test/resources/key/pem/ed25519/ed25519.pem").readText(), 0x11),
row(File("src/test/resources/key/pem/ed25519/ed25519.public.pem").readText(), 0x10),
row(File("src/test/resources/key/pem/ed25519/ed25519.private.pem").readText(), 0x01),
// Secp256k1
row(File("src/test/resources/key/pem/ecdsa/secp256k1.pem").readText(), 0x11),
row(File("src/test/resources/key/pem/ecdsa/secp256k1.public.pem").readText(), 0x10),
row(File("src/test/resources/key/pem/ecdsa/secp256k1.private.pem").readText(), 0x01),
) { keyStr, hasBothKeys ->
val kid = keyService.importKey(keyStr)
val privKey = keyService.export(kid.id, KeyFormat.PEM, KeyType.PRIVATE)
val pubKey = keyService.export(kid.id, KeyFormat.PEM, KeyType.PUBLIC)
privKey.plus(System.lineSeparator()).plus(pubKey) shouldBe keyStr
when (hasBothKeys and 0x11) {
0x11 -> {
keyService.export(kid.id, KeyFormat.PEM, KeyType.PRIVATE).plus(System.lineSeparator())
.plus(keyService.export(kid.id, KeyFormat.PEM, KeyType.PUBLIC)) shouldBe keyStr
}
0x01 -> {
keyService.export(kid.id, KeyFormat.PEM, KeyType.PRIVATE) shouldBe keyStr
}
0x10 -> {
keyService.export(kid.id, KeyFormat.PEM, KeyType.PUBLIC) shouldBe keyStr
}
}
}
}

Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions src/test/resources/key/pem/ecdsa/secp256k1.private.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-----BEGIN PRIVATE KEY-----
MEcCAQAwEAYHKoZIzj0CAQYFK4EEAAoEMDAuAgEBBCDhRhzY9+4kOgOm1G2RpZic
YVT21rrpZiGhMcTLFJ1HRaAHBgUrgQQACg==
-----END PRIVATE KEY-----
4 changes: 4 additions & 0 deletions src/test/resources/key/pem/ecdsa/secp256k1.public.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-----BEGIN PUBLIC KEY-----
MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAErWTFQycS7uhkDiKuOv4RLduQkwsTuuoC
PyEMJwLmBFV5mngn1GEkEJxOi0US0SD+/W/ShHyMPzqlbei99XC4mA==
-----END PUBLIC KEY-----
File renamed without changes.
3 changes: 3 additions & 0 deletions src/test/resources/key/pem/ed25519/ed25519.private.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEIJpchqgaS5BrgnM/AfWg9DJp6iE/spWXuQhea3+FIlyH
-----END PRIVATE KEY-----
File renamed without changes.
File renamed without changes.
28 changes: 28 additions & 0 deletions src/test/resources/key/pem/rsa/rsa.private.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCNvHrPDYly8Bvo
vophJHSdJuuJK2U6qmJfOpnLl2gsR9JALsuJ6udJ9NrNlzpniSXnK9To1Y9IDvvq
7iwhPQpfU/Dcydeb/VzC3eJQQDYyojNYCo1OM4dBajouKQ7oRK0WdCTSS3b6gFcj
xeQ41sZqEPqfBBKPeNqLt0zMli7cD6mA9gUxnNW+dsWlE3wp0PlTuKuAyv9qTxeJ
mc5LOcWx47sHwMSmSiYgnIP/H1WB99GjgIlzJj3Y5pRg1Dl89luqnvO8oSL26i+Y
M6drl/jlOY+MWykIYm5Hnsx+e+ZPLd7PTuu5CrWh+J9c7h8HyWtCvv2MthuQx7Uw
7nLxW9gPAgMBAAECggEAUvGJrMFvPiPlMT7QHCkJVS4BiDQs+TfceX4BonLoM4GU
OJsp4Xvl/hGcOdPWCeDonW+5GMhhIPoj0keu64SdHUwyGALGejK6pg7S8cOh89+X
zF3gc3MUI+PWSE0evC30XL97DNW8uruAQx77f4ewdUGaaJQpiGxg+II8Hz2EDqL5
RgODL9xhro4txefXSErMDzdrAJnRUzFcPC6m8zkYN+sTTG5VnPemjxPohJ3tHCFS
aPYxeIUf4FX70ToSoxkFpqSeSgdn8fW3hxFWCaP+c/Mt9f7QNCyaqzkMnj3pR7z1
zCedxvhuLMFyn/OpIVGx+KAXOt4OZKtNnC9vHrBoAQKBgQDjgvu4oE4XRdIfmM2r
2sAPQo1KJg+fViOM0esnu+yCdMUdXKj6GmsNiQGh8ZoMc5H8njQ/jfSn9tEVPeTm
vmWkgwvINnk8wxBqZS26z8sLuYXZRlbTngTA9OSOCxxXu2hvjBOdWM8l8p8QWhH7
TJrKH9cxtAIGzF4MUpu4YQnRwQKBgQCfe+qlFRoUzLIhRKAvd4eKTqnDc0OIXMa5
8Ii3NUTbGhEw5UnCkVYnmyqV2O6ULSqssDpGzoIOh+fP1Epl4Wk/bw/8in8kTDd6
Pz75kKBWNYRHrJEODZPwcr4kqcVk4zBbqEgppWXR3c94XDWvLdAyNXmUWBSsZ8CU
a20IbwB9zwKBgDxpOWHq3xF67rKJJoOtkDX9G/tGY/uRKdtomiJw2fpB+10yj4Pp
o+dyK8bOmnMX9Pm5IHKHxSEcfqQ/TRqgD+lBCs3c+RBTU3aM6zmhl27DknEBJmkG
moiJ/m5hMONy1eZYsZSkUkznQalb4U5zJt7o2YFPJQKdHZP0E3k/+xWBAoGAAvbI
m5zSyUQso1WlLHyqNjXUDd/ch3JB9z6zK8R/rRrE6wW34zfURBQsLHDFrldQjbkQ
vDQmUqLP+hrV7yLLU/R6dZUVW9VWypzWTMkVUcTY2oQohfxWnYdkZUYKdx+JzIpK
HQwH4mie7Hu6O7E8R1KbGdixOasL9zYv5pI5jh0CgYEAySlL4FzuDUMAF7xSGiyU
FWH6m4nGjQSBwo5bYpSCYf73cjuKcjZMq2UIA6T0UBUussh1Qs57DcuRbpbWlEjm
J5amRKJ29RYoXQPlJDC2FKj62l4XaTXnfkKf6Ggpzk7uaAqjB3mjUhfkZluA6Iv+
tPLjdkD7mQBfnf4CjOlb8GM=
-----END PRIVATE KEY-----
9 changes: 9 additions & 0 deletions src/test/resources/key/pem/rsa/rsa.public.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjbx6zw2JcvAb6L6KYSR0
nSbriStlOqpiXzqZy5doLEfSQC7LiernSfTazZc6Z4kl5yvU6NWPSA776u4sIT0K
X1Pw3MnXm/1cwt3iUEA2MqIzWAqNTjOHQWo6LikO6EStFnQk0kt2+oBXI8XkONbG
ahD6nwQSj3jai7dMzJYu3A+pgPYFMZzVvnbFpRN8KdD5U7irgMr/ak8XiZnOSznF
seO7B8DEpkomIJyD/x9VgffRo4CJcyY92OaUYNQ5fPZbqp7zvKEi9uovmDOna5f4
5TmPjFspCGJuR57MfnvmTy3ez07ruQq1ofifXO4fB8lrQr79jLYbkMe1MO5y8VvY
DwIDAQAB
-----END PUBLIC KEY-----

0 comments on commit f2b1a2a

Please sign in to comment.