LocalAttestationTrusted.cpp 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #include "Decryptor.h"
  34. #include "error_codes.h"
  35. #include "datatypes.h"
  36. #include "sgx_tcrypto.h"
  37. #include "sgx_dh.h"
  38. dh_session_t LocalAttestationTrusted::global_session_info;
  39. //Handle the request from Source Enclave for a session
  40. uint32_t LocalAttestationTrusted::session_request(sgx_dh_msg1_t *dh_msg1)
  41. {
  42. sgx_dh_session_t sgx_dh_session;
  43. sgx_status_t status = SGX_SUCCESS;
  44. //Intialize the session as a session responder
  45. status = sgx_dh_init_session(SGX_DH_SESSION_RESPONDER, &sgx_dh_session);
  46. if(SGX_SUCCESS != status)
  47. {
  48. return status;
  49. }
  50. global_session_info.status = IN_PROGRESS;
  51. //Generate Message1 that will be returned to Source Enclave
  52. status = sgx_dh_responder_gen_msg1((sgx_dh_msg1_t*)dh_msg1, &sgx_dh_session);
  53. if(SGX_SUCCESS != status)
  54. return status;
  55. memcpy(&global_session_info.in_progress.dh_session, &sgx_dh_session, sizeof(sgx_dh_session_t));
  56. return 0;
  57. }
  58. // TODO: Hope to edit the sgx_dh_responder_proc_msg2 call to return 32 byte key.
  59. //Verify Message 2, generate Message3 and exchange Message 3 with Source Enclave
  60. uint32_t LocalAttestationTrusted::exchange_report(sgx_dh_msg2_t *dh_msg2, sgx_dh_msg3_t *dh_msg3)
  61. {
  62. sgx_key_128bit_t dh_aek;
  63. uint32_t status = 0;
  64. sgx_dh_session_t sgx_dh_session;
  65. sgx_dh_session_enclave_identity_t initiator_identity;
  66. uint32_t verify_return;
  67. memset(&dh_aek,0, sizeof(sgx_key_128bit_t));
  68. if(global_session_info.status != IN_PROGRESS)
  69. return INVALID_SESSION;
  70. memcpy(&sgx_dh_session, &global_session_info.in_progress.dh_session, sizeof(sgx_dh_session_t));
  71. dh_msg3->msg3_body.additional_prop_length = 0;
  72. //Process message 2 from source enclave and obtain message 3
  73. status = sgx_dh_responder_proc_msg2(dh_msg2, dh_msg3, &sgx_dh_session, &dh_aek, &initiator_identity);
  74. if(SGX_SUCCESS != status)
  75. return status;
  76. //Verify source enclave's trust
  77. verify_return = Decryptor::verify_peer_enclave_trust(initiator_identity.mr_enclave.m, initiator_identity.mr_signer.m, dh_aek);
  78. if(verify_return != 0)
  79. return verify_return;
  80. return 0;
  81. }