PVEClass.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (C) 2011-2016 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={0};
  56. int retry = 0;
  57. AESM_PROFILE_FUN;
  58. if(m_enclave_id==0){
  59. AESM_DBG_ERROR("call gen_prov_msg1_data without loading PvE");
  60. return AE_FAILURE;
  61. }
  62. if (AE_SUCCESS != (ret = XEGDBlob::instance().read(xegb))){
  63. return ret;
  64. }
  65. status = gen_prov_msg1_data_wrapper(
  66. m_enclave_id, &ret,
  67. &xegb,
  68. pek,
  69. pce_target_info,
  70. pek_report);
  71. for(; status == SGX_ERROR_ENCLAVE_LOST && retry < AESM_RETRY_COUNT; retry++)
  72. {
  73. unload_enclave();
  74. // Reload an AE will not fail because of out of EPC, so AESM_AE_OUT_OF_EPC is not checked here
  75. if(AE_SUCCESS != load_enclave())
  76. return AE_FAILURE;
  77. status = gen_prov_msg1_data_wrapper(
  78. m_enclave_id, &ret,
  79. &xegb,
  80. pek,
  81. pce_target_info,
  82. pek_report);
  83. }
  84. if (PVE_XEGDSK_SIGN_ERROR == ret) {
  85. AESM_DBG_ERROR("XEGD signature mismatch in gen_prov_msg1_data");
  86. }
  87. if(status != SGX_SUCCESS)
  88. ret = AE_FAILURE;
  89. return ret;
  90. }
  91. uint32_t CPVEClass::proc_prov_msg2_data(
  92. const proc_prov_msg2_blob_input_t* input,
  93. bool performance_rekey_used,
  94. const uint8_t* sigrl,
  95. uint32_t sigrl_size,
  96. gen_prov_msg3_output_t* msg3_fixed_output,
  97. uint8_t* epid_sig,
  98. uint32_t epid_sig_buffer_size)
  99. {
  100. uint32_t ret = AE_SUCCESS;
  101. int retry = 0;
  102. sgx_status_t status = SGX_SUCCESS;
  103. uint8_t b_performance_rekey_used = performance_rekey_used?1:0;
  104. AESM_PROFILE_FUN;
  105. if(m_enclave_id==0){
  106. AESM_DBG_ERROR("call proc_prov_msg2_data without loading PvE");
  107. return AE_FAILURE;
  108. }
  109. status = proc_prov_msg2_data_wrapper(
  110. m_enclave_id, &ret,
  111. input,
  112. b_performance_rekey_used,
  113. sigrl, sigrl_size,
  114. msg3_fixed_output,
  115. epid_sig, epid_sig_buffer_size);
  116. for(; status == SGX_ERROR_ENCLAVE_LOST && retry < AESM_RETRY_COUNT; retry++)
  117. {
  118. unload_enclave();
  119. // Reload an AE will not fail because of out of EPC, so AESM_AE_OUT_OF_EPC is not checked here
  120. if(AE_SUCCESS != load_enclave())
  121. return AE_FAILURE;
  122. status = proc_prov_msg2_data_wrapper(
  123. m_enclave_id, &ret,
  124. input,
  125. b_performance_rekey_used,
  126. sigrl, sigrl_size,
  127. msg3_fixed_output,
  128. epid_sig, epid_sig_buffer_size);
  129. }
  130. if (PVE_XEGDSK_SIGN_ERROR == ret) {
  131. AESM_DBG_ERROR("XEGD signature mismatch in proc_prov_msg2_data");
  132. }
  133. if(status != SGX_SUCCESS)
  134. ret = AE_FAILURE;
  135. return ret;
  136. }
  137. uint32_t CPVEClass::proc_prov_msg4_data(
  138. const proc_prov_msg4_input_t* msg4_input,
  139. proc_prov_msg4_output_t* data_blob)
  140. {
  141. uint32_t ret = AE_SUCCESS;
  142. sgx_status_t status = SGX_SUCCESS;
  143. int retry = 0;
  144. AESM_PROFILE_FUN;
  145. if(m_enclave_id==0){
  146. AESM_DBG_ERROR("call proc_prov_msg4_data without loading PvE");
  147. return AE_FAILURE;
  148. }
  149. status = proc_prov_msg4_data_wrapper(
  150. m_enclave_id, &ret,
  151. msg4_input,
  152. data_blob);
  153. for(; status == SGX_ERROR_ENCLAVE_LOST && retry < AESM_RETRY_COUNT; retry++)
  154. {
  155. unload_enclave();
  156. // Reload an AE will not fail because of out of EPC, so AESM_AE_OUT_OF_EPC is not checked here
  157. if(AE_SUCCESS != load_enclave())
  158. return AE_FAILURE;
  159. status = proc_prov_msg4_data_wrapper(
  160. m_enclave_id, &ret,
  161. msg4_input,
  162. data_blob);
  163. }
  164. if (PVE_XEGDSK_SIGN_ERROR == ret) {
  165. AESM_DBG_ERROR("XEGD signature mismatch in proc_prov_msg4_data");
  166. }
  167. if(status != SGX_SUCCESS)
  168. ret = AE_FAILURE;
  169. return ret;
  170. }
  171. uint32_t CPVEClass::gen_es_msg1_data(
  172. gen_endpoint_selection_output_t* es_output)
  173. {
  174. uint32_t ret = AE_SUCCESS;
  175. sgx_status_t status = SGX_SUCCESS;
  176. int retry = 0;
  177. AESM_PROFILE_FUN;
  178. if(m_enclave_id==0){
  179. AESM_DBG_ERROR("call gen_es_msg1_data without loading PvE");
  180. return AE_FAILURE;
  181. }
  182. status = gen_es_msg1_data_wrapper(
  183. m_enclave_id, &ret,
  184. es_output);
  185. for(; status == SGX_ERROR_ENCLAVE_LOST && retry < AESM_RETRY_COUNT; retry++)
  186. {
  187. unload_enclave();
  188. // Reload an AE will not fail because of out of EPC, so AESM_AE_OUT_OF_EPC is not checked here
  189. if(AE_SUCCESS != load_enclave())
  190. return AE_FAILURE;
  191. status = gen_es_msg1_data_wrapper(
  192. m_enclave_id, &ret,
  193. es_output);
  194. }
  195. if(status != SGX_SUCCESS)
  196. ret = AE_FAILURE;
  197. return ret;
  198. }