-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: 340 cant generate a did with an existing key (#344)
- Loading branch information
1 parent
fcd2bf1
commit adf5bfe
Showing
27 changed files
with
456 additions
and
249 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/kotlin/id/walt/services/key/AsymmetricPublicKeyConverter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package id.walt.services.key | ||
|
||
import org.bouncycastle.asn1.edec.EdECObjectIdentifiers | ||
import org.bouncycastle.asn1.x509.AlgorithmIdentifier | ||
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo | ||
import org.bouncycastle.crypto.params.AsymmetricKeyParameter | ||
import org.bouncycastle.crypto.params.ECPublicKeyParameters | ||
import org.bouncycastle.crypto.params.Ed25519PublicKeyParameters | ||
import org.bouncycastle.crypto.params.RSAKeyParameters | ||
import org.bouncycastle.jcajce.provider.asymmetric.util.EC5Util | ||
import java.security.KeyFactory | ||
import java.security.PublicKey | ||
import java.security.spec.ECPoint | ||
import java.security.spec.ECPublicKeySpec | ||
import java.security.spec.RSAPublicKeySpec | ||
import java.security.spec.X509EncodedKeySpec | ||
|
||
class AsymmetricPublicKeyConverter { | ||
|
||
fun convert(key: AsymmetricKeyParameter) = when (key) { | ||
is ECPublicKeyParameters -> ecAsymmetricKeyParameterToPublicKey(key) | ||
is Ed25519PublicKeyParameters -> edAsymmetricKeyParameterToPublicKey(key) | ||
is RSAKeyParameters -> rsaAsymmetricKeyParameterToPublicKey(key) | ||
else -> null | ||
} | ||
|
||
private fun ecAsymmetricKeyParameterToPublicKey(key: ECPublicKeyParameters): PublicKey = let { | ||
val ecParameterSpec = EC5Util.convertToSpec(key.parameters) | ||
val ecPoint: ECPoint = EC5Util.convertPoint(key.q) | ||
val ecPublicKeySpec = ECPublicKeySpec(ecPoint, ecParameterSpec) | ||
KeyFactory.getInstance("ECDSA").generatePublic(ecPublicKeySpec) | ||
} | ||
|
||
private fun edAsymmetricKeyParameterToPublicKey(key: Ed25519PublicKeyParameters): PublicKey = let { | ||
val pubKeyInfo = SubjectPublicKeyInfo(AlgorithmIdentifier(EdECObjectIdentifiers.id_Ed25519), key.encoded) | ||
KeyFactory.getInstance("Ed25519").generatePublic(X509EncodedKeySpec(pubKeyInfo.encoded)) | ||
} | ||
|
||
private fun rsaAsymmetricKeyParameterToPublicKey(key: RSAKeyParameters): PublicKey = let { | ||
val rsaPublicKeySpec = RSAPublicKeySpec(key.modulus, key.exponent) | ||
KeyFactory.getInstance("RSA").generatePublic(rsaPublicKeySpec) | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/kotlin/id/walt/services/key/deriver/AsymmetricPublicKeyDeriver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package id.walt.services.key.deriver | ||
|
||
import id.walt.services.key.AsymmetricPublicKeyConverter | ||
import org.bouncycastle.crypto.params.* | ||
import java.security.PublicKey | ||
|
||
class AsymmetricPublicKeyDeriver( | ||
private val keyConverter: AsymmetricPublicKeyConverter | ||
) : PublicKeyDeriver<AsymmetricKeyParameter> { | ||
|
||
override fun derive(key: AsymmetricKeyParameter): PublicKey? = when (key) { | ||
is RSAPrivateCrtKeyParameters -> { | ||
RSAKeyParameters(false, key.modulus, key.publicExponent) | ||
} | ||
is Ed25519PrivateKeyParameters -> { | ||
key.generatePublicKey() | ||
} | ||
is ECPrivateKeyParameters -> { | ||
val q = key.parameters.g.multiply(key.d) | ||
ECPublicKeyParameters(q, key.parameters) | ||
} | ||
else -> null | ||
}?.let{ | ||
keyConverter.convert(it) | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/kotlin/id/walt/services/key/deriver/DefaultPublicKeyDeriver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package id.walt.services.key.deriver | ||
|
||
import id.walt.crypto.KeyAlgorithm | ||
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey | ||
import java.security.KeyFactory | ||
import java.security.PrivateKey | ||
import java.security.PublicKey | ||
import java.security.interfaces.RSAPrivateCrtKey | ||
import java.security.spec.RSAPublicKeySpec | ||
|
||
|
||
class DefaultPublicKeyDeriver: PublicKeyDeriver<PrivateKey> { | ||
override fun derive(key: PrivateKey): PublicKey? = when (KeyAlgorithm.fromString(key.algorithm)) { | ||
KeyAlgorithm.RSA -> (key as? RSAPrivateCrtKey)?.let { | ||
KeyFactory.getInstance("RSA").generatePublic(RSAPublicKeySpec(it.modulus, it.publicExponent)) | ||
} | ||
KeyAlgorithm.EdDSA_Ed25519 -> { | ||
|
||
null | ||
} | ||
//TODO: remove BC dependency, rely purely on java.security | ||
KeyAlgorithm.ECDSA_Secp256k1, KeyAlgorithm.ECDSA_Secp256r1 -> { | ||
val definingKey = key as BCECPrivateKey | ||
val d = definingKey.d | ||
val ecSpec = definingKey.parameters | ||
val q = definingKey.parameters.g.multiply(d) | ||
val pubSpec = org.bouncycastle.jce.spec.ECPublicKeySpec(q, ecSpec) | ||
KeyFactory.getInstance("ECDSA").generatePublic(pubSpec) | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/kotlin/id/walt/services/key/deriver/PublicKeyDeriver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package id.walt.services.key.deriver | ||
|
||
import java.security.PublicKey | ||
|
||
interface PublicKeyDeriver<T> { | ||
fun derive(key: T): PublicKey? | ||
} |
63 changes: 63 additions & 0 deletions
63
src/main/kotlin/id/walt/services/key/import/JwkKeyImport.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package id.walt.services.key.import | ||
|
||
import com.nimbusds.jose.jwk.Curve | ||
import com.nimbusds.jose.jwk.JWK | ||
import com.nimbusds.jose.jwk.KeyType | ||
import id.walt.crypto.* | ||
import id.walt.services.CryptoProvider | ||
import id.walt.services.keystore.KeyStoreService | ||
|
||
class JwkKeyImport(private val keyString: String) : KeyImportStrategy { | ||
|
||
override fun import(keyStore: KeyStoreService): KeyId { | ||
val key = parseJwkKey(keyString) | ||
keyStore.store(key) | ||
return key.keyId | ||
} | ||
|
||
private fun parseJwkKey(jwkKeyStr: String): Key { | ||
val jwk = JWK.parse(jwkKeyStr) | ||
|
||
val key = when (jwk.keyType) { | ||
KeyType.RSA -> Key( | ||
keyId = KeyId(jwk.keyID ?: newKeyId().id), | ||
algorithm = KeyAlgorithm.RSA, | ||
cryptoProvider = CryptoProvider.SUN, | ||
keyPair = jwk.toRSAKey().toKeyPair() | ||
) | ||
|
||
KeyType.EC -> { | ||
val alg = when (jwk.toECKey().curve) { | ||
Curve.P_256 -> KeyAlgorithm.ECDSA_Secp256r1 | ||
Curve.SECP256K1 -> KeyAlgorithm.ECDSA_Secp256k1 | ||
else -> throw IllegalArgumentException("EC key with curve ${jwk.toECKey().curve} not suppoerted") | ||
} | ||
Key( | ||
keyId = KeyId(jwk.keyID ?: newKeyId().id), | ||
algorithm = alg, | ||
cryptoProvider = CryptoProvider.SUN, | ||
keyPair = jwk.toECKey().toKeyPair() | ||
) | ||
} | ||
|
||
KeyType.OKP -> { | ||
val alg = when (jwk.toOctetKeyPair().curve) { | ||
Curve.Ed25519 -> KeyAlgorithm.EdDSA_Ed25519 | ||
else -> throw IllegalArgumentException("OKP key with curve ${jwk.toOctetKeyPair().curve} not supported") | ||
} | ||
buildKey( | ||
keyId = jwk.keyID ?: newKeyId().id, | ||
algorithm = alg.name, | ||
provider = CryptoProvider.SUN.name, | ||
publicPart = jwk.toOctetKeyPair().x.toString(), | ||
privatePart = jwk.toOctetKeyPair().d?.let { jwk.toOctetKeyPair().d.toString() }, | ||
format = KeyFormat.BASE64_RAW | ||
) | ||
} | ||
|
||
else -> throw IllegalArgumentException("KeyType ${jwk.keyType} / Algorithm ${jwk.algorithm} not supported") | ||
} | ||
return key | ||
} | ||
|
||
} |
Oops, something went wrong.