From 46532ccbcb6a5084efb9c1ac12a1850c9c34cb5d Mon Sep 17 00:00:00 2001 From: Ross Nicoll Date: Thu, 5 Oct 2017 09:14:00 +0100 Subject: [PATCH] Don't repackage well known key types (#1545) * Don't repackage well known key types when converting keys to a well known type * Remove custom key serializers * Remove duplicate serializer registration --- .../kotlin/net/corda/core/crypto/Crypto.kt | 21 ++++++- .../serialization/DefaultKryoCustomizer.kt | 9 ++- .../nodeapi/internal/serialization/Kryo.kt | 60 ------------------- 3 files changed, 23 insertions(+), 67 deletions(-) diff --git a/core/src/main/kotlin/net/corda/core/crypto/Crypto.kt b/core/src/main/kotlin/net/corda/core/crypto/Crypto.kt index 605b95466c..b6df5e2bc9 100644 --- a/core/src/main/kotlin/net/corda/core/crypto/Crypto.kt +++ b/core/src/main/kotlin/net/corda/core/crypto/Crypto.kt @@ -941,7 +941,16 @@ object Crypto { * is inappropriate for a supported key factory to produce a private key. */ @JvmStatic - fun toSupportedPublicKey(key: PublicKey): PublicKey = decodePublicKey(key.encoded) + fun toSupportedPublicKey(key: PublicKey): PublicKey { + return when (key) { + is BCECPublicKey -> key + is BCRSAPublicKey -> key + is BCSphincs256PublicKey -> key + is EdDSAPublicKey -> key + is CompositeKey -> key + else -> decodePublicKey(key.encoded) + } + } /** * Convert a private key to a supported implementation. This can be used to convert a SUN's EC key to an BC key. @@ -952,5 +961,13 @@ object Crypto { * is inappropriate for a supported key factory to produce a private key. */ @JvmStatic - fun toSupportedPrivateKey(key: PrivateKey): PrivateKey = decodePrivateKey(key.encoded) + fun toSupportedPrivateKey(key: PrivateKey): PrivateKey { + return when (key) { + is BCECPrivateKey -> key + is BCRSAPrivateKey -> key + is BCSphincs256PrivateKey -> key + is EdDSAPrivateKey -> key + else -> decodePrivateKey(key.encoded) + } + } } diff --git a/node-api/src/main/kotlin/net/corda/nodeapi/internal/serialization/DefaultKryoCustomizer.kt b/node-api/src/main/kotlin/net/corda/nodeapi/internal/serialization/DefaultKryoCustomizer.kt index d4a6f11ee2..04104533ee 100644 --- a/node-api/src/main/kotlin/net/corda/nodeapi/internal/serialization/DefaultKryoCustomizer.kt +++ b/node-api/src/main/kotlin/net/corda/nodeapi/internal/serialization/DefaultKryoCustomizer.kt @@ -88,10 +88,10 @@ object DefaultKryoCustomizer { register(BufferedInputStream::class.java, InputStreamSerializer) register(Class.forName("sun.net.www.protocol.jar.JarURLConnection\$JarURLInputStream"), InputStreamSerializer) noReferencesWithin() - register(ECPublicKeyImpl::class.java, ECPublicKeyImplSerializer) - register(EdDSAPublicKey::class.java, Ed25519PublicKeySerializer) - register(EdDSAPrivateKey::class.java, Ed25519PrivateKeySerializer) - register(CompositeKey::class.java, CompositeKeySerializer) // Using a custom serializer for compactness + register(ECPublicKeyImpl::class.java, PublicKeySerializer) + register(EdDSAPublicKey::class.java, PublicKeySerializer) + register(EdDSAPrivateKey::class.java, PrivateKeySerializer) + register(CompositeKey::class.java, PublicKeySerializer) // Using a custom serializer for compactness // Exceptions. We don't bother sending the stack traces as the client will fill in its own anyway. register(Array::class, read = { _, _ -> emptyArray() }, write = { _, _, _ -> }) // This ensures a NonEmptySetSerializer is constructed with an initial value. @@ -109,7 +109,6 @@ object DefaultKryoCustomizer { register(BCRSAPublicKey::class.java, PublicKeySerializer) register(BCSphincs256PrivateKey::class.java, PrivateKeySerializer) register(BCSphincs256PublicKey::class.java, PublicKeySerializer) - register(sun.security.ec.ECPublicKeyImpl::class.java, PublicKeySerializer) register(NotaryChangeWireTransaction::class.java, NotaryChangeWireTransactionSerializer) register(PartyAndCertificate::class.java, PartyAndCertificateSerializer) diff --git a/node-api/src/main/kotlin/net/corda/nodeapi/internal/serialization/Kryo.kt b/node-api/src/main/kotlin/net/corda/nodeapi/internal/serialization/Kryo.kt index 82234df40b..2905922460 100644 --- a/node-api/src/main/kotlin/net/corda/nodeapi/internal/serialization/Kryo.kt +++ b/node-api/src/main/kotlin/net/corda/nodeapi/internal/serialization/Kryo.kt @@ -285,66 +285,6 @@ object SignedTransactionSerializer : Serializer() { } } -/** For serialising an ed25519 private key */ -@ThreadSafe -object Ed25519PrivateKeySerializer : Serializer() { - override fun write(kryo: Kryo, output: Output, obj: EdDSAPrivateKey) { - check(obj.params == Crypto.EDDSA_ED25519_SHA512.algSpec) - output.writeBytesWithLength(obj.seed) - } - - override fun read(kryo: Kryo, input: Input, type: Class): EdDSAPrivateKey { - val seed = input.readBytesWithLength() - return EdDSAPrivateKey(EdDSAPrivateKeySpec(seed, Crypto.EDDSA_ED25519_SHA512.algSpec as EdDSANamedCurveSpec)) - } -} - -/** For serialising an ed25519 public key */ -@ThreadSafe -object Ed25519PublicKeySerializer : Serializer() { - override fun write(kryo: Kryo, output: Output, obj: EdDSAPublicKey) { - check(obj.params == Crypto.EDDSA_ED25519_SHA512.algSpec) - output.writeBytesWithLength(obj.abyte) - } - - override fun read(kryo: Kryo, input: Input, type: Class): EdDSAPublicKey { - val A = input.readBytesWithLength() - return EdDSAPublicKey(EdDSAPublicKeySpec(A, Crypto.EDDSA_ED25519_SHA512.algSpec as EdDSANamedCurveSpec)) - } -} - -/** For serialising an ed25519 public key */ -@ThreadSafe -object ECPublicKeyImplSerializer : Serializer() { - override fun write(kryo: Kryo, output: Output, obj: ECPublicKeyImpl) { - output.writeBytesWithLength(obj.encoded) - } - - override fun read(kryo: Kryo, input: Input, type: Class): ECPublicKeyImpl { - val A = input.readBytesWithLength() - val der = DerValue(A) - return ECPublicKeyImpl.parse(der) as ECPublicKeyImpl - } -} - -// TODO Implement standardized serialization of CompositeKeys. See JIRA issue: CORDA-249. -@ThreadSafe -object CompositeKeySerializer : Serializer() { - override fun write(kryo: Kryo, output: Output, obj: CompositeKey) { - output.writeInt(obj.threshold) - output.writeInt(obj.children.size) - obj.children.forEach { kryo.writeClassAndObject(output, it) } - } - - override fun read(kryo: Kryo, input: Input, type: Class): CompositeKey { - val threshold = input.readInt() - val children = readListOfLength(kryo, input, minLen = 2) - val builder = CompositeKey.Builder() - children.forEach { builder.addKey(it.node, it.weight) } - return builder.build(threshold) as CompositeKey - } -} - @ThreadSafe object PrivateKeySerializer : Serializer() { override fun write(kryo: Kryo, output: Output, obj: PrivateKey) {