LocalAttestationTrusted.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 "EnclaveMessageExchange.h"
  34. #include "sgx_eid.h"
  35. #include "error_codes.h"
  36. #include "sgx_ecp_types.h"
  37. #include "sgx_thread.h"
  38. #include "dh_session_protocol.h"
  39. #include "sgx_dh.h"
  40. #include "sgx_tcrypto.h"
  41. #include "LocalAttestationCode_t.h"
  42. #include "sgx_tseal.h"
  43. static class LocalAttestationTrusted{
  44. dh_session_t global_session_info;
  45. uint32_t global_session_id=0;
  46. uint32_t one_successful_la_done;
  47. sgx_ecc_state_handle_t ecc_state;
  48. uint8_t verifier_mr_enclave[32];
  49. SymmetricEncryptionBox symmetricEncryptionBoxApache;
  50. SymmetricEncryptionBox symmetricEncryptionBoxVerifier;
  51. extern "C" uint32_t verify_peer_enclave_trust(sgx_dh_session_enclave_identity_t* peer_enclave_identity)
  52. {
  53. uint32_t count; sgx_measurement_t given_mr_signer ;
  54. if(!peer_enclave_identity)
  55. {
  56. return INVALID_PARAMETER_ERROR;
  57. }
  58. if(one_successful_la_done==0)
  59. {
  60. // TODO: Set this attribute in the decryptor object.
  61. verifier_mr_enclave = peer_enclave_identity->mr_enclave;
  62. memset(&(apache_mr_signer.m),0x0,SGX_HASH_SIZE); // "initialization"
  63. one_successful_la_done=1;
  64. }
  65. else // apache enclave
  66. {
  67. given_mr_signer = peer_enclave_identity->mr_signer;
  68. int count;
  69. for(count=0; count<SGX_HASH_SIZE; count++)
  70. {
  71. if( given_mr_signer.m[count] != apache_mr_signer.m[count] )
  72. return ENCLAVE_TRUST_ERROR;
  73. }
  74. one_successful_la_done ++;
  75. }
  76. return SGX_SUCCESS;
  77. }
  78. public:
  79. //Handle the request from Source Enclave for a session
  80. ATTESTATION_STATUS session_request(sgx_dh_msg1_t *dh_msg1, uint32_t *session_id)
  81. {
  82. sgx_dh_session_t sgx_dh_session;
  83. sgx_status_t status = SGX_SUCCESS;
  84. if(!session_id || !dh_msg1)
  85. {
  86. return INVALID_PARAMETER_ERROR;
  87. }
  88. //Intialize the session as a session responder
  89. status = sgx_dh_init_session(SGX_DH_SESSION_RESPONDER, &sgx_dh_session);
  90. if(SGX_SUCCESS != status)
  91. {
  92. return status;
  93. }
  94. *session_id=1;
  95. global_session_info.status = IN_PROGRESS;
  96. //Generate Message1 that will be returned to Source Enclave
  97. status = sgx_dh_responder_gen_msg1((sgx_dh_msg1_t*)dh_msg1, &sgx_dh_session);
  98. if(SGX_SUCCESS != status)
  99. return status;
  100. memcpy(&global_session_info.in_progress.dh_session, &sgx_dh_session, sizeof(sgx_dh_session_t));
  101. return 0;
  102. }
  103. // TODO: Hope to edit the sgx_dh_responder_proc_msg2 call to return 32 byte key.
  104. //Verify Message 2, generate Message3 and exchange Message 3 with Source Enclave
  105. ATTESTATION_STATUS exchange_report(sgx_dh_msg2_t *dh_msg2, sgx_dh_msg3_t *dh_msg3, uint32_t* session_id)
  106. {
  107. sgx_key_128bit_t dh_aek;
  108. sgx_status_t status = SUCCESS;
  109. sgx_dh_session_t sgx_dh_session;
  110. sgx_dh_session_enclave_identity_t initiator_identity;
  111. uint32_t verify_return;
  112. memset(&dh_aek,0, sizeof(sgx_key_128bit_t));
  113. if(!dh_msg2 || !dh_msg3)
  114. return INVALID_PARAMETER_ERROR;
  115. if(global_session_info.status != IN_PROGRESS)
  116. return INVALID_SESSION; // end_session(); // TODO: DA FUQ RETURN STH HERE.
  117. memcpy(&sgx_dh_session, &global_session_info.in_progress.dh_session, sizeof(sgx_dh_session_t));
  118. dh_msg3->msg3_body.additional_prop_length = 0;
  119. //Process message 2 from source enclave and obtain message 3
  120. status = sgx_dh_responder_proc_msg2(dh_msg2, dh_msg3, &sgx_dh_session, &dh_aek, &initiator_identity);
  121. if(SGX_SUCCESS != status)
  122. return status;
  123. //Verify source enclave's trust
  124. verify_return = verify_peer_enclave_trust(&initiator_identity);
  125. if(verify_return != 0)
  126. return verify_return;
  127. if(one_successful_la_done == 1)
  128. symmetricEncryptionBoxVerifier.set_symmetric_key(dh_aek);
  129. else
  130. symmetricEncryptionBoxApache.set_symmetric_key(dh_aek);
  131. /*
  132. //save the session ID, status and initialize the session nonce
  133. global_session_info.session_id = *session_id;
  134. global_session_info.status = ACTIVE; // This means that you can't keep calling exchange_report over and over again.
  135. global_session_info.active.counter = 0;
  136. memcpy(&global_session_info.active.AEK, &dh_aek, sizeof(sgx_key_128bit_t));
  137. memset(&dh_aek,0, sizeof(sgx_key_128bit_t));
  138. */
  139. return status;
  140. }
  141. public:
  142. LocalAttestationTrusted(): symmetricEncryptionBoxApache(), symmetricEncryptionBoxVerifier();
  143. };