PSEClass.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (C) 2011-2018 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. #ifndef _PSE_CLASS_H_
  32. #define _PSE_CLASS_H_
  33. #include "AEClass.h"
  34. #include "aeerror.h"
  35. #include "pse_types.h"
  36. #include "sgx_dh.h"
  37. #include "ae_debug_flag.hh"
  38. #include "se_wrapper.h"
  39. #define PS_CAP_NOT_AVAILABLE ((uint64_t)-1)
  40. #define PSDA_CAP_RESULT_MSG_LEN 12
  41. /* defines PSE service status*/
  42. typedef enum _pse_status_t
  43. {
  44. PSE_STATUS_INIT = 0, /* Init status*/
  45. PSE_STATUS_UNAVAILABLE, /* service is unavailable*/
  46. PSE_STATUS_CSE_PROVISIONED, /* CSE is provisioned*/
  47. PSE_STATUS_SERVICE_READY /* Waiting for PS requests (pse_op enclave is loaded)*/
  48. } pse_status_t;
  49. class CPSEClass: public SingletonEnclave<CPSEClass>
  50. {
  51. friend class Singleton<CPSEClass>;
  52. friend class SingletonEnclave<CPSEClass>;
  53. static aesm_enclave_id_t get_enclave_fid(){return PSE_OP_ENCLAVE_FID;}
  54. protected:
  55. CPSEClass(){
  56. m_status = PSE_STATUS_INIT;
  57. m_ps_cap = PS_CAP_NOT_AVAILABLE;
  58. m_freq = se_get_tick_count_freq();
  59. };
  60. ~CPSEClass(){
  61. };
  62. virtual void before_enclave_load();
  63. virtual int get_debug_flag() { return AE_DEBUG_FLAG;}
  64. pse_status_t m_status;
  65. uint64_t m_ps_cap;
  66. uint64_t m_freq;
  67. public:
  68. ae_error_t init_ps(void);
  69. ae_error_t create_session(
  70. uint32_t* session_id,
  71. uint8_t* se_dh_msg1, uint32_t se_dh_msg1_size
  72. );
  73. /*if ok return 0*/
  74. ae_error_t exchange_report(
  75. uint32_t session_id,
  76. uint8_t* se_dh_msg2, uint32_t se_dh_msg2_size,
  77. uint8_t* se_dh_msg3, uint32_t se_dh_msg3_size
  78. );
  79. /*if ok return 0*/
  80. ae_error_t close_session(uint32_t session_id
  81. );
  82. /*if ok return 0*/
  83. ae_error_t invoke_service(
  84. uint8_t* pse_message_req, size_t pse_message_req_size,
  85. uint8_t* pse_message_resp, size_t pse_message_resp_size
  86. );
  87. /* establish ephemeral session between PSE and CSE*/
  88. ae_error_t create_ephemeral_session_pse_cse(bool is_new_pairing, bool redo = false);
  89. pse_status_t get_status() {
  90. return m_status;
  91. }
  92. ae_error_t psda_invoke_service(uint8_t* psda_req_msg, uint32_t psda_req_msg_size,
  93. uint8_t* psda_resp_msg, uint32_t psda_resp_msg_size);
  94. ae_error_t get_ps_cap(uint64_t* ps_cap);
  95. void unload_enclave()
  96. {
  97. if (PSE_STATUS_SERVICE_READY == m_status) {
  98. m_status = PSE_STATUS_CSE_PROVISIONED;
  99. }
  100. SingletonEnclave<CPSEClass>::unload_enclave();
  101. }
  102. private:
  103. ae_error_t psda_start_ephemeral_session(
  104. const uint8_t* pse_instance_id,
  105. pse_cse_msg2_t* cse_msg2
  106. );
  107. ae_error_t psda_finalize_session(
  108. const uint8_t* pse_instance_id,
  109. const pse_cse_msg3_t* cse_msg3,
  110. pse_cse_msg4_t* cse_msg4
  111. );
  112. };
  113. #endif