SgxLAInitiator.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (C) 2011-2017 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include "sgx_trts.h"
  32. #include "sgx_utils.h"
  33. #include "sgx_eid.h"
  34. #include "error_codes.h"
  35. #include "sgx_ecp_types.h"
  36. #include "sgx_thread.h"
  37. #include <map>
  38. #include "sgx_dh.h"
  39. #include "dh_session_protocol.h"
  40. #include "sgx_tcrypto.h"
  41. #include "datatypes.h"
  42. #include "ProtobufSgxLATransforms_Inititator.h"
  43. #define MAX_SESSION_COUNT 16
  44. #define SGX_CAST(type, item) ((type)(item))
  45. #include <string.h>
  46. // uint32_t session_ids[MAX_SESSION_COUNT] ;
  47. // Our enclave will not be doing LA with more than 1 decryptor enclave at a time.
  48. // We should not need this.
  49. //std::map<int, dh_session_t>g_dest_session_info_map;
  50. dh_session_t global_session_info;
  51. // TODO: May be we need to store all previously assigned session IDs instead of just the counter; to prevent replay attacks -
  52. uint32_t global_session_id=0;
  53. 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)
  54. {
  55. int count=0;
  56. if(!peer_enclave_identity)
  57. return INVALID_PARAMETER_ERROR;
  58. sgx_measurement_t actual_mr_enclave = peer_enclave_identity->mr_enclave;
  59. sgx_measurement_t actual_mr_signer = peer_enclave_identity->mr_signer;
  60. /* for(count=0; count<SGX_HASH_SIZE; count++)
  61. {
  62. if( actual_mr_enclave.m[count] != expected_mr_enclave[count] )
  63. {
  64. return ENCLAVE_TRUST_ERROR;
  65. }
  66. if( actual_mr_signer.m[count] != expected_mr_signer[count] )
  67. {
  68. return ENCLAVE_TRUST_ERROR; // TODO: Different error here.
  69. }
  70. }*/
  71. return SUCCESS;
  72. }
  73. //Create a session with the destination enclave
  74. int create_session(int fd, uint8_t* expected_mr_enclave, uint8_t* expected_mr_signer, uint8_t* send_mr_signer)
  75. {
  76. sgx_dh_msg1_t dh_msg1; //Diffie-Hellman Message 1
  77. sgx_key_128bit_t dh_aek; // Session Key
  78. sgx_dh_msg2_t dh_msg2; //Diffie-Hellman Message 2
  79. sgx_dh_msg3_t dh_msg3; //Diffie-Hellman Message 3
  80. int retstatus;
  81. sgx_status_t status = SGX_SUCCESS;
  82. sgx_dh_session_t sgx_dh_session;
  83. sgx_dh_session_enclave_identity_t responder_identity;
  84. memset(&dh_aek,0, sizeof(sgx_key_128bit_t));
  85. memset(&dh_msg1, 0, sizeof(sgx_dh_msg1_t));
  86. memset(&dh_msg2, 0, sizeof(sgx_dh_msg2_t));
  87. memset(&dh_msg3, 0, sizeof(sgx_dh_msg3_t));
  88. memset(&global_session_info, 0, sizeof(dh_session_t));
  89. //Intialize the session as a session initiator
  90. status = sgx_dh_init_session(SGX_DH_SESSION_INITIATOR, &sgx_dh_session);
  91. if(status != SGX_SUCCESS)
  92. return status;
  93. retstatus = session_request_call(fd, &dh_msg1);//, &session_id);
  94. if(retstatus!=0)
  95. return retstatus;
  96. //Process the message 1 obtained from desination enclave and generate message 2
  97. status = sgx_dh_initiator_proc_msg1(&dh_msg1, &dh_msg2, &sgx_dh_session);
  98. if(SGX_SUCCESS != status)
  99. return status;
  100. //Send Message 2 to Destination Enclave and get Message 3 in return
  101. retstatus = exchange_report_call( fd, &dh_msg2, &dh_msg3); //, &session_id);
  102. if(retstatus!=0)
  103. return retstatus;
  104. //Process Message 3 obtained from the destination enclave
  105. status = sgx_dh_initiator_proc_msg3(&dh_msg3, &sgx_dh_session, &dh_aek, &responder_identity);
  106. if(SGX_SUCCESS != status)
  107. return status;
  108. // Verify the identity of the destination enclave
  109. retstatus = verify_peer_enclave_trust(&responder_identity, expected_mr_enclave, expected_mr_signer);
  110. if(retstatus != 0)
  111. return retstatus;
  112. memcpy(global_session_info.active.AEK, &dh_aek, sizeof(sgx_key_128bit_t));
  113. global_session_info.session_id = 1; // TODO: session_id;
  114. global_session_info.active.counter = 0;
  115. global_session_info.status = ACTIVE;
  116. memset(&dh_aek,0, sizeof(sgx_key_128bit_t));
  117. return 0;
  118. }