123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- /*
- * Copyright (C) 2011-2017 Intel Corporation. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Intel Corporation nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- */
- #include "sgx_trts.h"
- #include "sgx_utils.h"
- #include "sgx_eid.h"
- #include "error_codes.h"
- #include "sgx_ecp_types.h"
- #include "sgx_thread.h"
- #include <map>
- #include "sgx_dh.h"
- #include "dh_session_protocol.h"
- #include "sgx_tcrypto.h"
- #include "datatypes.h"
- #include "ProtobufSgxLATransforms_Inititator.h"
- #define MAX_SESSION_COUNT 16
- #define SGX_CAST(type, item) ((type)(item))
- #include <string.h>
- // uint32_t session_ids[MAX_SESSION_COUNT] ;
- // Our enclave will not be doing LA with more than 1 decryptor enclave at a time.
- // We should not need this.
- //std::map<int, dh_session_t>g_dest_session_info_map;
- dh_session_t global_session_info;
- // TODO: May be we need to store all previously assigned session IDs instead of just the counter; to prevent replay attacks -
- uint32_t global_session_id=0;
- uint32_t verify_peer_enclave_trust(sgx_dh_session_enclave_identity_t* peer_enclave_identity, uint8_t* expected_mr_enclave, uint8_t* expected_mr_signer)
- {
- int count=0;
- if(!peer_enclave_identity)
- return INVALID_PARAMETER_ERROR;
- sgx_measurement_t actual_mr_enclave = peer_enclave_identity->mr_enclave;
- sgx_measurement_t actual_mr_signer = peer_enclave_identity->mr_signer;
- /* for(count=0; count<SGX_HASH_SIZE; count++)
- {
- if( actual_mr_enclave.m[count] != expected_mr_enclave[count] )
- {
- return ENCLAVE_TRUST_ERROR;
- }
- if( actual_mr_signer.m[count] != expected_mr_signer[count] )
- {
- return ENCLAVE_TRUST_ERROR; // TODO: Different error here.
- }
- }*/
- return SUCCESS;
- }
- //Create a session with the destination enclave
- int create_session(int fd, uint8_t* expected_mr_enclave, uint8_t* expected_mr_signer, uint8_t* send_mr_signer)
- {
- sgx_dh_msg1_t dh_msg1; //Diffie-Hellman Message 1
- sgx_key_128bit_t dh_aek; // Session Key
- sgx_dh_msg2_t dh_msg2; //Diffie-Hellman Message 2
- sgx_dh_msg3_t dh_msg3; //Diffie-Hellman Message 3
- int retstatus;
- sgx_status_t status = SGX_SUCCESS;
- sgx_dh_session_t sgx_dh_session;
- sgx_dh_session_enclave_identity_t responder_identity;
- memset(&dh_aek,0, sizeof(sgx_key_128bit_t));
- memset(&dh_msg1, 0, sizeof(sgx_dh_msg1_t));
- memset(&dh_msg2, 0, sizeof(sgx_dh_msg2_t));
- memset(&dh_msg3, 0, sizeof(sgx_dh_msg3_t));
- memset(&global_session_info, 0, sizeof(dh_session_t));
- //Intialize the session as a session initiator
- status = sgx_dh_init_session(SGX_DH_SESSION_INITIATOR, &sgx_dh_session);
- if(status != SGX_SUCCESS)
- return status;
- retstatus = session_request_call(fd, &dh_msg1);//, &session_id);
- if(retstatus!=0)
- return retstatus;
- //Process the message 1 obtained from desination enclave and generate message 2
- status = sgx_dh_initiator_proc_msg1(&dh_msg1, &dh_msg2, &sgx_dh_session);
- if(SGX_SUCCESS != status)
- return status;
- //Send Message 2 to Destination Enclave and get Message 3 in return
- retstatus = exchange_report_call( fd, &dh_msg2, &dh_msg3); //, &session_id);
- if(retstatus!=0)
- return retstatus;
- //Process Message 3 obtained from the destination enclave
- status = sgx_dh_initiator_proc_msg3(&dh_msg3, &sgx_dh_session, &dh_aek, &responder_identity);
- if(SGX_SUCCESS != status)
- return status;
- // Verify the identity of the destination enclave
- retstatus = verify_peer_enclave_trust(&responder_identity, expected_mr_enclave, expected_mr_signer);
- if(retstatus != 0)
- return retstatus;
- memcpy(global_session_info.active.AEK, &dh_aek, sizeof(sgx_key_128bit_t));
- global_session_info.session_id = 1; // TODO: session_id;
- global_session_info.active.counter = 0;
- global_session_info.status = ACTIVE;
- memset(&dh_aek,0, sizeof(sgx_key_128bit_t));
- return 0;
- }
|