LocalAttestationTrusted.cpp 5.8 KB

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