Created
November 24, 2021 10:56
-
-
Save ashutoshmeher-r3/2f7ea0598bdded9a7fbc7fad731f9616 to your computer and use it in GitHub Desktop.
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
public class Host { | |
public static void main(String[] args) throws EnclaveLoadException, IOException { | |
// Check whether the platform supports hardware enclaves. | |
try { | |
EnclaveHost.checkPlatformSupportsEnclaves(true); | |
System.out.println("This platform supports enclaves in simulation, debug and release mode."); | |
} catch (Exception e) { | |
System.out.println("Platform Support Check Failed" + e.getMessage()); | |
} | |
// Open a TCP socket and implement a trivial protocol that lets a remote client use it. | |
Socket connection = establishConnection(); | |
DataOutputStream output = new DataOutputStream(connection.getOutputStream()); | |
DataInputStream input = new DataInputStream(connection.getInputStream()); | |
// Load the Enclave | |
EnclaveHost enclave = EnclaveHost.load("com.r3.conclave.sample.enclave.ReverseEnclave"); | |
// Start up the enclave with a callback that will deliver the response. | |
enclave.start( new AttestationParameters.DCAP(), (mailCommands -> { | |
for(MailCommand command : mailCommands){ | |
if(command instanceof MailCommand.PostMail){ | |
try{ | |
sendArray(output, ((MailCommand.PostMail) command).getEncryptedBytes()); | |
}catch (IOException e){ | |
e.printStackTrace(); | |
} | |
} | |
} | |
})); | |
// Send Attestation to the Client | |
EnclaveInstanceInfo attestation = enclave.getEnclaveInstanceInfo(); | |
byte[] attestationBytes = attestation.serialize(); | |
sendArray(output, attestationBytes); | |
// Print Attestation | |
System.out.println(EnclaveInstanceInfo.deserialize(attestationBytes)); | |
// Read mail from the client. | |
byte[] mailBytes = new byte[input.readInt()]; | |
input.readFully(mailBytes); | |
// Deliver the mail to the client | |
enclave.deliverMail(1, mailBytes, "routingHint"); | |
// Close the connection. | |
output.close(); | |
connection.close(); | |
} | |
private static Socket establishConnection() throws IOException{ | |
int port = 9999; | |
System.out.println("Listening on port " + port + ". Use the client app to send strings for reversal."); | |
ServerSocket acceptor = new ServerSocket(port); | |
return acceptor.accept(); | |
} | |
private static void sendArray(DataOutputStream stream, byte[] bytes) throws IOException { | |
stream.writeInt(bytes.length); | |
stream.write(bytes); | |
stream.flush(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment