AEClass.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #ifndef _AE_CLASS_H_
  32. #define _AE_CLASS_H_
  33. #include "sgx_eid.h"
  34. #include "sgx_key.h"
  35. #include "se_thread.h"
  36. #include "sgx_urts.h"
  37. #include "internal/se_stdio.h"
  38. #include "oal/oal.h"
  39. #include "aeerror.h"
  40. #include <stdlib.h>
  41. #include <exception>
  42. template<class T>
  43. class Singleton
  44. {
  45. CLASS_UNCOPYABLE(Singleton)
  46. public:
  47. static T& instance()
  48. {
  49. if (0 == _instance)
  50. {
  51. _instance = new T();
  52. atexit(destroy);
  53. }
  54. return *_instance;
  55. }
  56. virtual ~Singleton()
  57. {
  58. _instance = NULL;
  59. }
  60. protected:
  61. Singleton(){}
  62. private:
  63. static void destroy()
  64. {
  65. if ( _instance != 0 )
  66. {
  67. delete _instance;
  68. _instance = 0;
  69. }
  70. }
  71. static T * volatile _instance;
  72. };
  73. template<class T>
  74. class SingletonEnclave: public Singleton<T>
  75. {
  76. CLASS_UNCOPYABLE(SingletonEnclave)
  77. public:
  78. virtual ae_error_t load_enclave();
  79. void unload_enclave();
  80. protected:
  81. sgx_enclave_id_t m_enclave_id;
  82. sgx_launch_token_t m_launch_token;
  83. sgx_misc_attribute_t m_attributes;
  84. SingletonEnclave():m_enclave_id(0)
  85. {
  86. memset(&m_launch_token, 0, sizeof(m_launch_token));
  87. memset(&m_attributes, 0, sizeof(m_attributes));
  88. }
  89. ~SingletonEnclave(){}
  90. virtual void before_enclave_load() {}
  91. virtual int get_debug_flag() = 0;
  92. };
  93. template <class T>
  94. ae_error_t SingletonEnclave<T>::load_enclave()
  95. {
  96. before_enclave_load();
  97. if(m_enclave_id)
  98. return AE_SUCCESS;
  99. aesm_enclave_id_t aesm_enclave_id = T::get_enclave_fid();
  100. AESM_DBG_INFO("loading enclave %d",aesm_enclave_id);
  101. sgx_status_t ret;
  102. ae_error_t ae_err;
  103. char enclave_path[MAX_PATH]= {0};
  104. if((ae_err = aesm_get_pathname(FT_ENCLAVE_NAME, aesm_enclave_id, enclave_path,
  105. MAX_PATH))
  106. !=AE_SUCCESS){
  107. AESM_DBG_ERROR("fail to get enclave pathname");
  108. return ae_err;
  109. }
  110. int launch_token_update;
  111. ret = sgx_create_enclave(enclave_path, get_debug_flag(), &m_launch_token,
  112. &launch_token_update, &m_enclave_id,
  113. &m_attributes);
  114. if (ret == SGX_ERROR_NO_DEVICE){
  115. AESM_DBG_ERROR("AE SERVER NOT AVAILABLE in load enclave: %s",enclave_path);
  116. return AE_SERVER_NOT_AVAILABLE;
  117. }
  118. if(ret == SGX_ERROR_OUT_OF_EPC){
  119. AESM_DBG_ERROR("No enough EPC to load AE: %s",enclave_path);
  120. AESM_LOG_ERROR("%s %s", g_event_string_table[SGX_EVENT_OUT_OF_EPC], enclave_path);
  121. return AESM_AE_OUT_OF_EPC;
  122. }
  123. if (ret != SGX_SUCCESS){
  124. AESM_DBG_ERROR("Create Enclave failed:%d",ret);
  125. return AE_SERVER_NOT_AVAILABLE;
  126. }
  127. AESM_DBG_INFO("enclave %d loaded with id 0X%llX",aesm_enclave_id,m_enclave_id);
  128. return AE_SUCCESS;
  129. }
  130. template <class T>
  131. void SingletonEnclave<T>::unload_enclave()
  132. {
  133. if (m_enclave_id)
  134. {
  135. AESM_DBG_INFO("unload enclave 0X%llX",m_enclave_id);
  136. sgx_destroy_enclave(m_enclave_id);
  137. m_enclave_id = 0;
  138. }
  139. return;
  140. }
  141. template<class T>
  142. T * volatile Singleton<T>::_instance = 0;
  143. #define AESM_RETRY_COUNT 3
  144. ae_error_t sgx_error_to_ae_error(sgx_status_t status);
  145. #endif