|
@@ -104,6 +104,9 @@ struct NodeCommState {
|
|
|
void message_start(uint32_t plaintext_len);
|
|
|
|
|
|
void message_data(uint8_t *data, uint32_t len);
|
|
|
+
|
|
|
+ // Start the handshake (as the client)
|
|
|
+ void handshake_start();
|
|
|
};
|
|
|
|
|
|
// A typical default in_msg_get_buf handler. It computes the maximum
|
|
@@ -132,14 +135,42 @@ static uint8_t* default_in_msg_get_buf(NodeCommState &commst,
|
|
|
|
|
|
// Receive (at the server) the first handshake message
|
|
|
static void handshake_1_msg_received(NodeCommState &nodest,
|
|
|
- uint8_t *data, uint32_t plaintext_len, uint32_t message_len)
|
|
|
+ uint8_t *data, uint32_t plaintext_len, uint32_t)
|
|
|
{
|
|
|
+ /*
|
|
|
printf("Received handshake_1 message of %u bytes:\n", plaintext_len);
|
|
|
for (uint32_t i=0;i<plaintext_len;++i) {
|
|
|
printf("%02x", data[i]);
|
|
|
}
|
|
|
printf("\n");
|
|
|
+ */
|
|
|
+
|
|
|
+ if (plaintext_len != sizeof(sgx_ec256_public_t)) {
|
|
|
+ printf("Received handshake_1 message of incorrect size %u\n",
|
|
|
+ plaintext_len);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ sgx_ecc_state_handle_t ecc_handle;
|
|
|
+ sgx_ec256_public_t pubkey;
|
|
|
+ memmove(&pubkey, data, sizeof(pubkey));
|
|
|
+ sgx_ecc256_open_context(&ecc_handle);
|
|
|
+ int valid;
|
|
|
+ if (sgx_ecc256_check_point(&pubkey, ecc_handle, &valid) || !valid) {
|
|
|
+ printf("Invalid public key received from node %hu\n",
|
|
|
+ nodest.node_num);
|
|
|
+ sgx_ecc256_close_context(ecc_handle);
|
|
|
+ return;
|
|
|
+ }
|
|
|
delete[] data;
|
|
|
+
|
|
|
+ printf("Valid public key received from node %hu\n", nodest.node_num);
|
|
|
+ memmove(&nodest.handshake_peer_pubkey, &pubkey, sizeof(pubkey));
|
|
|
+
|
|
|
+ // Create our own DH key pair
|
|
|
+ sgx_ecc256_create_key_pair(&nodest.handshake_privkey,
|
|
|
+ &nodest.handshake_pubkey, ecc_handle);
|
|
|
+
|
|
|
+ sgx_ecc256_close_context(ecc_handle);
|
|
|
}
|
|
|
|
|
|
// Start a new outgoing message. Pass the number of _plaintext_ bytes
|
|
@@ -241,7 +272,7 @@ void NodeCommState::message_data(uint8_t *data, uint32_t len)
|
|
|
// The communication states for all the nodes. There's an entry for
|
|
|
// ourselves in here, but it is unused.
|
|
|
static std::vector<NodeCommState> commstates;
|
|
|
-static nodenum_t tot_nodes;
|
|
|
+static nodenum_t tot_nodes, my_node_num;
|
|
|
|
|
|
// Generate a new identity signature key. Output the public key and the
|
|
|
// sealed private key. outsealedpriv must point to SEALEDPRIVKEY_SIZE =
|
|
@@ -307,7 +338,7 @@ bool ecall_identity_key_load(sgx_ec256_public_t *outpub,
|
|
|
}
|
|
|
|
|
|
bool comms_init_nodestate(const EnclaveAPINodeConfig *apinodeconfigs,
|
|
|
- nodenum_t num_nodes, nodenum_t my_node_num)
|
|
|
+ nodenum_t num_nodes, nodenum_t me)
|
|
|
{
|
|
|
sgx_ecc_state_handle_t ecc_handle;
|
|
|
sgx_ecc256_open_context(&ecc_handle);
|
|
@@ -330,6 +361,8 @@ bool comms_init_nodestate(const EnclaveAPINodeConfig *apinodeconfigs,
|
|
|
}
|
|
|
sgx_ecc256_close_context(ecc_handle);
|
|
|
|
|
|
+ my_node_num = me;
|
|
|
+
|
|
|
// Check that no one other than us has our pubkey (deals with
|
|
|
// reflection attacks)
|
|
|
for (nodenum_t i=0; i<num_nodes; ++i) {
|
|
@@ -441,3 +474,31 @@ bool ecall_chunk(nodenum_t node_num, const uint8_t *chunkdata,
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
+
|
|
|
+// Start the handshake (as the client)
|
|
|
+void NodeCommState::handshake_start()
|
|
|
+{
|
|
|
+ sgx_ecc_state_handle_t ecc_handle;
|
|
|
+
|
|
|
+ sgx_ecc256_open_context(&ecc_handle);
|
|
|
+
|
|
|
+ // Create a DH keypair
|
|
|
+ sgx_ecc256_create_key_pair(&handshake_privkey, &handshake_pubkey,
|
|
|
+ ecc_handle);
|
|
|
+
|
|
|
+ sgx_ecc256_close_context(ecc_handle);
|
|
|
+
|
|
|
+ // Send the public key as the first message
|
|
|
+ message_start(sizeof(handshake_pubkey));
|
|
|
+
|
|
|
+ message_data((uint8_t*)&handshake_pubkey, sizeof(handshake_pubkey));
|
|
|
+}
|
|
|
+
|
|
|
+// Start all handshakes for which we are the client
|
|
|
+bool ecall_comms_start()
|
|
|
+{
|
|
|
+ for (nodenum_t t = my_node_num+1; t<tot_nodes; ++t) {
|
|
|
+ commstates[t].handshake_start();
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+}
|