PVEClass.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. #include <assert.h>
  32. #include "PVEClass.h"
  33. #include "QEClass.h"
  34. #include "PCEClass.h"
  35. #include "util.h"
  36. #include "prof_fun.h"
  37. #include "sgx_report.h"
  38. #include "sgx_tseal.h"
  39. #include "epid_pve_type.h"
  40. #include "aesm_xegd_blob.h"
  41. #include "provision_enclave_u.h"
  42. #include "provision_enclave_u.c"
  43. void CPVEClass::before_enclave_load() {
  44. // always unload qe enclave before loading pve enclave
  45. CQEClass::instance().unload_enclave();
  46. CPCEClass::instance().unload_enclave();
  47. }
  48. uint32_t CPVEClass::gen_prov_msg1_data(
  49. const signed_pek_t *pek,
  50. const sgx_target_info_t *pce_target_info,
  51. sgx_report_t *pek_report)
  52. {
  53. uint32_t ret = AE_SUCCESS;
  54. sgx_status_t status = SGX_SUCCESS;
  55. extended_epid_group_blob_t xegb;
  56. int retry = 0;
  57. AESM_PROFILE_FUN;
  58. memset(&xegb, 0, sizeof(xegb));
  59. if(m_enclave_id==0){
  60. AESM_DBG_ERROR("call gen_prov_msg1_data without loading PvE");
  61. return AE_FAILURE;
  62. }
  63. if (AE_SUCCESS != (ret = XEGDBlob::instance().read(xegb))){
  64. return ret;
  65. }
  66. status = gen_prov_msg1_data_wrapper(
  67. m_enclave_id, &ret,
  68. &xegb,
  69. pek,
  70. pce_target_info,
  71. pek_report);
  72. for(; status == SGX_ERROR_ENCLAVE_LOST && retry < AESM_RETRY_COUNT; retry++)
  73. {
  74. unload_enclave();
  75. // Reload an AE will not fail because of out of EPC, so AESM_AE_OUT_OF_EPC is not checked here
  76. if(AE_SUCCESS != load_enclave())
  77. return AE_FAILURE;
  78. status = gen_prov_msg1_data_wrapper(
  79. m_enclave_id, &ret,
  80. &xegb,
  81. pek,
  82. pce_target_info,
  83. pek_report);
  84. }
  85. if (PVE_XEGDSK_SIGN_ERROR == ret) {
  86. AESM_DBG_ERROR("XEGD signature mismatch in gen_prov_msg1_data");
  87. }
  88. if(status != SGX_SUCCESS)
  89. ret = AE_FAILURE;
  90. return ret;
  91. }
  92. uint32_t CPVEClass::proc_prov_msg2_data(
  93. const proc_prov_msg2_blob_input_t* input,
  94. bool performance_rekey_used,
  95. const uint8_t* sigrl,
  96. uint32_t sigrl_size,
  97. gen_prov_msg3_output_t* msg3_fixed_output,
  98. uint8_t* epid_sig,
  99. uint32_t epid_sig_buffer_size)
  100. {
  101. uint32_t ret = AE_SUCCESS;
  102. int retry = 0;
  103. sgx_status_t status = SGX_SUCCESS;
  104. uint8_t b_performance_rekey_used = performance_rekey_used?1:0;
  105. AESM_PROFILE_FUN;
  106. if(m_enclave_id==0){
  107. AESM_DBG_ERROR("call proc_prov_msg2_data without loading PvE");
  108. return AE_FAILURE;
  109. }
  110. status = proc_prov_msg2_data_wrapper(
  111. m_enclave_id, &ret,
  112. input,
  113. b_performance_rekey_used,
  114. sigrl, sigrl_size,
  115. msg3_fixed_output,
  116. epid_sig, epid_sig_buffer_size);
  117. for(; status == SGX_ERROR_ENCLAVE_LOST && retry < AESM_RETRY_COUNT; retry++)
  118. {
  119. unload_enclave();
  120. // Reload an AE will not fail because of out of EPC, so AESM_AE_OUT_OF_EPC is not checked here
  121. if(AE_SUCCESS != load_enclave())
  122. return AE_FAILURE;
  123. status = proc_prov_msg2_data_wrapper(
  124. m_enclave_id, &ret,
  125. input,
  126. b_performance_rekey_used,
  127. sigrl, sigrl_size,
  128. msg3_fixed_output,
  129. epid_sig, epid_sig_buffer_size);
  130. }
  131. if (PVE_XEGDSK_SIGN_ERROR == ret) {
  132. AESM_DBG_ERROR("XEGD signature mismatch in proc_prov_msg2_data");
  133. }
  134. if(status != SGX_SUCCESS)
  135. ret = AE_FAILURE;
  136. return ret;
  137. }
  138. uint32_t CPVEClass::proc_prov_msg4_data(
  139. const proc_prov_msg4_input_t* msg4_input,
  140. proc_prov_msg4_output_t* data_blob)
  141. {
  142. uint32_t ret = AE_SUCCESS;
  143. sgx_status_t status = SGX_SUCCESS;
  144. int retry = 0;
  145. AESM_PROFILE_FUN;
  146. if(m_enclave_id==0){
  147. AESM_DBG_ERROR("call proc_prov_msg4_data without loading PvE");
  148. return AE_FAILURE;
  149. }
  150. status = proc_prov_msg4_data_wrapper(
  151. m_enclave_id, &ret,
  152. msg4_input,
  153. data_blob);
  154. for(; status == SGX_ERROR_ENCLAVE_LOST && retry < AESM_RETRY_COUNT; retry++)
  155. {
  156. unload_enclave();
  157. // Reload an AE will not fail because of out of EPC, so AESM_AE_OUT_OF_EPC is not checked here
  158. if(AE_SUCCESS != load_enclave())
  159. return AE_FAILURE;
  160. status = proc_prov_msg4_data_wrapper(
  161. m_enclave_id, &ret,
  162. msg4_input,
  163. data_blob);
  164. }
  165. if (PVE_XEGDSK_SIGN_ERROR == ret) {
  166. AESM_DBG_ERROR("XEGD signature mismatch in proc_prov_msg4_data");
  167. }
  168. if(status != SGX_SUCCESS)
  169. ret = AE_FAILURE;
  170. return ret;
  171. }
  172. uint32_t CPVEClass::gen_es_msg1_data(
  173. gen_endpoint_selection_output_t* es_output)
  174. {
  175. uint32_t ret = AE_SUCCESS;
  176. sgx_status_t status = SGX_SUCCESS;
  177. int retry = 0;
  178. AESM_PROFILE_FUN;
  179. if(m_enclave_id==0){
  180. AESM_DBG_ERROR("call gen_es_msg1_data without loading PvE");
  181. return AE_FAILURE;
  182. }
  183. status = gen_es_msg1_data_wrapper(
  184. m_enclave_id, &ret,
  185. es_output);
  186. for(; status == SGX_ERROR_ENCLAVE_LOST && retry < AESM_RETRY_COUNT; retry++)
  187. {
  188. unload_enclave();
  189. // Reload an AE will not fail because of out of EPC, so AESM_AE_OUT_OF_EPC is not checked here
  190. if(AE_SUCCESS != load_enclave())
  191. return AE_FAILURE;
  192. status = gen_es_msg1_data_wrapper(
  193. m_enclave_id, &ret,
  194. es_output);
  195. }
  196. if(status != SGX_SUCCESS)
  197. ret = AE_FAILURE;
  198. return ret;
  199. }