From 3ae53683ea9aeec46689afaed9442fb98a9ed52a Mon Sep 17 00:00:00 2001 From: Chris Rankin Date: Mon, 14 Aug 2017 13:51:25 +0100 Subject: [PATCH] Register Kryo serializer for use inside the SGX enclave. (#23) --- sgx-jvm/Makefile | 1 + sgx-jvm/jvm-enclave/enclave/enclave.cpp | 13 +++++---- .../txverify/KryoSerializationScheme.kt | 28 +++++++++++++++++++ 3 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 verify-enclave/src/main/kotlin/com/r3/enclaves/txverify/KryoSerializationScheme.kt diff --git a/sgx-jvm/Makefile b/sgx-jvm/Makefile index a68e28a7ae..88be20fc5b 100644 --- a/sgx-jvm/Makefile +++ b/sgx-jvm/Makefile @@ -46,6 +46,7 @@ clean: $(MAKE) -C linux-sgx clean $(MAKE) -C linux-sgx-driver clean [ ! -d jdk8u ] || $(MAKE) -C jdk8u clean + ../gradlew -p .. verify-enclave:clean $(MAKE) -C avian clean .PHONY: distclean diff --git a/sgx-jvm/jvm-enclave/enclave/enclave.cpp b/sgx-jvm/jvm-enclave/enclave/enclave.cpp index 8380ddd69b..decbd55124 100644 --- a/sgx-jvm/jvm-enclave/enclave/enclave.cpp +++ b/sgx-jvm/jvm-enclave/enclave/enclave.cpp @@ -53,13 +53,16 @@ void check_transaction(void *reqbuf, size_t buflen, char *error) { JNI_CreateJavaVM(&vm, &env_void, &vmArgs); JNIEnv* env = static_cast(env_void); - jclass c = env->FindClass("com/r3/enclaves/txverify/Enclavelet"); + env->FindClass("com/r3/enclaves/txverify/KryoVerifierSerializationScheme"); if (!env->ExceptionCheck()) { - jmethodID m = env->GetStaticMethodID(c, "verifyInEnclave", "([B)V"); + jclass c = env->FindClass("com/r3/enclaves/txverify/Enclavelet"); if (!env->ExceptionCheck()) { - jbyteArray reqbits = env->NewByteArray((jsize) buflen); - env->SetByteArrayRegion(reqbits, 0, buflen, (const jbyte *)reqbuf); - jobject result = env->CallStaticObjectMethod(c, m, reqbits); + jmethodID m = env->GetStaticMethodID(c, "verifyInEnclave", "([B)V"); + if (!env->ExceptionCheck()) { + jbyteArray reqbits = env->NewByteArray((jsize) buflen); + env->SetByteArrayRegion(reqbits, 0, buflen, (const jbyte *)reqbuf); + jobject result = env->CallStaticObjectMethod(c, m, reqbits); + } } } diff --git a/verify-enclave/src/main/kotlin/com/r3/enclaves/txverify/KryoSerializationScheme.kt b/verify-enclave/src/main/kotlin/com/r3/enclaves/txverify/KryoSerializationScheme.kt new file mode 100644 index 0000000000..d01157d904 --- /dev/null +++ b/verify-enclave/src/main/kotlin/com/r3/enclaves/txverify/KryoSerializationScheme.kt @@ -0,0 +1,28 @@ +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.AbstractKryoSerializationScheme +import net.corda.nodeapi.internal.serialization.KRYO_P2P_CONTEXT +import net.corda.nodeapi.internal.serialization.KryoHeaderV0_1 +import net.corda.nodeapi.internal.serialization.SerializationFactoryImpl + +@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() + + private companion object { + init { + SerializationDefaults.SERIALIZATION_FACTORY = SerializationFactoryImpl().apply { + registerScheme(KryoVerifierSerializationScheme()) + } + SerializationDefaults.P2P_CONTEXT = KRYO_P2P_CONTEXT + } + } +}