From 9403e231cc75701bea2df143ac8c6c218639aec7 Mon Sep 17 00:00:00 2001 From: Chris Rankin Date: Mon, 20 Nov 2017 10:49:02 +0000 Subject: [PATCH] Register Kryo and AMQP serialisation schemes for the SGX enclave. --- sgx-jvm/jvm-enclave/enclave/enclave.cpp | 2 +- .../txverify/EnclaveletSerializationScheme.kt | 52 +++++++++++++++++++ .../KryoVerifierSerializationScheme.kt | 32 ------------ 3 files changed, 53 insertions(+), 33 deletions(-) create mode 100644 verify-enclave/src/main/kotlin/com/r3/enclaves/txverify/EnclaveletSerializationScheme.kt delete mode 100644 verify-enclave/src/main/kotlin/com/r3/enclaves/txverify/KryoVerifierSerializationScheme.kt diff --git a/sgx-jvm/jvm-enclave/enclave/enclave.cpp b/sgx-jvm/jvm-enclave/enclave/enclave.cpp index decbd55124..95511b1c43 100644 --- a/sgx-jvm/jvm-enclave/enclave/enclave.cpp +++ b/sgx-jvm/jvm-enclave/enclave/enclave.cpp @@ -53,7 +53,7 @@ void check_transaction(void *reqbuf, size_t buflen, char *error) { JNI_CreateJavaVM(&vm, &env_void, &vmArgs); JNIEnv* env = static_cast(env_void); - env->FindClass("com/r3/enclaves/txverify/KryoVerifierSerializationScheme"); + env->FindClass("com/r3/enclaves/txverify/EnclaveletSerializationScheme"); if (!env->ExceptionCheck()) { jclass c = env->FindClass("com/r3/enclaves/txverify/Enclavelet"); if (!env->ExceptionCheck()) { diff --git a/verify-enclave/src/main/kotlin/com/r3/enclaves/txverify/EnclaveletSerializationScheme.kt b/verify-enclave/src/main/kotlin/com/r3/enclaves/txverify/EnclaveletSerializationScheme.kt new file mode 100644 index 0000000000..82a375dec2 --- /dev/null +++ b/verify-enclave/src/main/kotlin/com/r3/enclaves/txverify/EnclaveletSerializationScheme.kt @@ -0,0 +1,52 @@ +@file:JvmName("EnclaveletSerializationScheme") +package com.r3.enclaves.txverify + +import net.corda.core.serialization.SerializationContext +import net.corda.core.serialization.internal.SerializationEnvironmentImpl +import net.corda.core.serialization.internal.nodeSerializationEnv +import net.corda.core.utilities.ByteSequence +import net.corda.nodeapi.internal.serialization.* +import net.corda.nodeapi.internal.serialization.amqp.AbstractAMQPSerializationScheme +import net.corda.nodeapi.internal.serialization.amqp.AmqpHeaderV1_0 +import net.corda.nodeapi.internal.serialization.amqp.SerializerFactory +import net.corda.nodeapi.internal.serialization.kryo.AbstractKryoSerializationScheme +import net.corda.nodeapi.internal.serialization.kryo.KryoHeaderV0_1 + +@Suppress("UNUSED") +private class EnclaveletSerializationScheme { + /* + * Registers the serialisation schemes as soon as this class is loaded into the JVM. + */ + private companion object { + init { + nodeSerializationEnv = SerializationEnvironmentImpl( + SerializationFactoryImpl().apply { + registerScheme(KryoVerifierSerializationScheme) + registerScheme(AMQPVerifierSerializationScheme) + }, + /** + * Even though default context is set to Kryo P2P, the encoding will be adjusted depending on the + * incoming request received. + */ + KRYO_P2P_CONTEXT) + } + } +} + +private object KryoVerifierSerializationScheme : AbstractKryoSerializationScheme() { + override fun canDeserializeVersion(byteSequence: ByteSequence, target: SerializationContext.UseCase): Boolean { + return byteSequence == KryoHeaderV0_1 && target == SerializationContext.UseCase.P2P + } + + override fun rpcClientKryoPool(context: SerializationContext) = throw UnsupportedOperationException() + override fun rpcServerKryoPool(context: SerializationContext) = throw UnsupportedOperationException() +} + +private object AMQPVerifierSerializationScheme : AbstractAMQPSerializationScheme() { + override fun canDeserializeVersion(byteSequence: ByteSequence, target: SerializationContext.UseCase): Boolean { + return (byteSequence == AmqpHeaderV1_0 && (target == SerializationContext.UseCase.P2P)) + } + + override fun rpcClientSerializerFactory(context: SerializationContext): SerializerFactory = throw UnsupportedOperationException() + override fun rpcServerSerializerFactory(context: SerializationContext): SerializerFactory = throw UnsupportedOperationException() +} diff --git a/verify-enclave/src/main/kotlin/com/r3/enclaves/txverify/KryoVerifierSerializationScheme.kt b/verify-enclave/src/main/kotlin/com/r3/enclaves/txverify/KryoVerifierSerializationScheme.kt deleted file mode 100644 index 33acff962f..0000000000 --- a/verify-enclave/src/main/kotlin/com/r3/enclaves/txverify/KryoVerifierSerializationScheme.kt +++ /dev/null @@ -1,32 +0,0 @@ -package com.r3.enclaves.txverify - -import net.corda.core.serialization.SerializationContext -import net.corda.core.serialization.SerializationDefaults -import net.corda.core.utilities.ByteSequence -import net.corda.nodeapi.internal.serialization.KRYO_P2P_CONTEXT -import net.corda.nodeapi.internal.serialization.SerializationFactoryImpl -import net.corda.nodeapi.internal.serialization.kryo.AbstractKryoSerializationScheme -import net.corda.nodeapi.internal.serialization.kryo.KryoHeaderV0_1 - -@Suppress("UNUSED") -private class KryoVerifierSerializationScheme : AbstractKryoSerializationScheme() { - override fun canDeserializeVersion(byteSequence: ByteSequence, target: SerializationContext.UseCase): Boolean { - return byteSequence == KryoHeaderV0_1 && target == SerializationContext.UseCase.P2P - } - - override fun rpcClientKryoPool(context: SerializationContext) = throw UnsupportedOperationException() - override fun rpcServerKryoPool(context: SerializationContext) = throw UnsupportedOperationException() - - /* - * Registers the serialisation scheme as soon as the class is loaded into the JVM. - */ - private companion object { - init { - // TODO: SerializationDefaults are now global singletons. Revisit this initialisation block. -// SerializationDefaults.SERIALIZATION_FACTORY = SerializationFactoryImpl().apply { -// registerScheme(KryoVerifierSerializationScheme()) -// } -// SerializationDefaults.P2P_CONTEXT = KRYO_P2P_CONTEXT - } - } -}