Register Kryo and AMQP serialisation schemes for the SGX enclave.

This commit is contained in:
Chris Rankin 2017-11-20 10:49:02 +00:00
parent cca532b5c0
commit 9403e231cc
3 changed files with 53 additions and 33 deletions

View File

@ -53,7 +53,7 @@ void check_transaction(void *reqbuf, size_t buflen, char *error) {
JNI_CreateJavaVM(&vm, &env_void, &vmArgs);
JNIEnv* env = static_cast<JNIEnv*>(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()) {

View File

@ -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()
}

View File

@ -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
}
}
}