QEClass.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 "arch.h"
  33. #include "QEClass.h"
  34. #include "PVEClass.h"
  35. #include "PCEClass.h"
  36. #include "se_memcpy.h"
  37. #include "prof_fun.h"
  38. #include "quoting_enclave_u.h"
  39. #include "quoting_enclave_u.c"
  40. void CQEClass::before_enclave_load() {
  41. // always unload qe enclave before loading pve enclave
  42. CPVEClass::instance().unload_enclave();
  43. CPCEClass::instance().unload_enclave();
  44. }
  45. uint32_t CQEClass::get_qe_target(
  46. sgx_target_info_t *p_qe_target)
  47. {
  48. token_t *p_launch =
  49. reinterpret_cast<token_t *>(&m_launch_token);
  50. /* We need to make sure the QE is successfully loaded and then we can use
  51. the cached attributes and launch token. */
  52. assert(m_enclave_id);
  53. memset(p_qe_target, 0, sizeof(sgx_target_info_t));
  54. memcpy_s(&p_qe_target->attributes, sizeof(p_qe_target->attributes),
  55. &m_attributes.secs_attr, sizeof(m_attributes.secs_attr));
  56. memcpy_s(&p_qe_target->misc_select, sizeof(p_qe_target->misc_select),
  57. &m_attributes.misc_select, sizeof(m_attributes.misc_select));
  58. memcpy_s(&p_qe_target->mr_enclave, sizeof(p_qe_target->mr_enclave),
  59. &p_launch->body.mr_enclave,
  60. sizeof(p_launch->body.mr_enclave));
  61. return AE_SUCCESS;
  62. }
  63. uint32_t CQEClass::verify_blob(
  64. uint8_t *p_epid_blob,
  65. uint32_t blob_size,
  66. bool *p_is_resealed)
  67. {
  68. uint32_t ret = AE_SUCCESS;
  69. sgx_status_t status = SGX_SUCCESS;
  70. uint8_t is_resealed = 0;
  71. int retry = 0;
  72. AESM_PROFILE_FUN;
  73. assert(m_enclave_id);
  74. status = ::verify_blob(m_enclave_id, &ret, p_epid_blob, blob_size,
  75. &is_resealed);
  76. for(; status == SGX_ERROR_ENCLAVE_LOST && retry < AESM_RETRY_COUNT; retry++)
  77. {
  78. unload_enclave();
  79. // Reload an AE will not fail because of out of EPC, so AESM_AE_OUT_OF_EPC is not checked here
  80. if(AE_SUCCESS != load_enclave())
  81. return AE_FAILURE;
  82. status = ::verify_blob(m_enclave_id, &ret, p_epid_blob, blob_size,
  83. &is_resealed);
  84. }
  85. if(status != SGX_SUCCESS)
  86. return AE_FAILURE;
  87. if(ret == AE_SUCCESS)
  88. {
  89. *p_is_resealed = is_resealed != 0;
  90. }
  91. if(ret == QE_EPIDBLOB_ERROR)
  92. {
  93. AESM_LOG_FATAL("%s", g_event_string_table[SGX_EVENT_EPID_INTEGRITY_ERROR]);
  94. }
  95. return ret;
  96. }
  97. uint32_t CQEClass::get_quote(
  98. uint8_t *p_epid_blob,
  99. uint32_t blob_size,
  100. const sgx_report_t *p_report,
  101. sgx_quote_sign_type_t quote_type,
  102. const sgx_spid_t *p_spid,
  103. const sgx_quote_nonce_t *p_nonce,
  104. const uint8_t *p_sigrl,
  105. uint32_t sigrl_size,
  106. sgx_report_t *p_qe_report,
  107. uint8_t *p_quote,
  108. uint32_t quote_size,
  109. uint16_t pce_isv_svn)
  110. {
  111. uint32_t ret = AE_SUCCESS;
  112. sgx_status_t status = SGX_SUCCESS;
  113. int retry = 0;
  114. AESM_PROFILE_FUN;
  115. assert(m_enclave_id);
  116. status = ::get_quote(
  117. m_enclave_id,
  118. &ret,
  119. p_epid_blob,
  120. blob_size,
  121. p_report,
  122. quote_type,
  123. p_spid,
  124. p_nonce,
  125. p_sigrl,
  126. sigrl_size,
  127. p_qe_report,
  128. p_quote,
  129. quote_size,
  130. pce_isv_svn);
  131. for(; status == SGX_ERROR_ENCLAVE_LOST && retry < AESM_RETRY_COUNT; retry++)
  132. {
  133. unload_enclave();
  134. // Reload an AE will not fail because of out of EPC, so AESM_AE_OUT_OF_EPC is not checked here
  135. if(AE_SUCCESS != load_enclave())
  136. return AE_FAILURE;
  137. status = ::get_quote(
  138. m_enclave_id,
  139. &ret,
  140. p_epid_blob,
  141. blob_size,
  142. p_report,
  143. quote_type,
  144. p_spid,
  145. p_nonce,
  146. p_sigrl,
  147. sigrl_size,
  148. p_qe_report,
  149. p_quote,
  150. quote_size,
  151. pce_isv_svn);
  152. }
  153. if(status != SGX_SUCCESS)
  154. return AE_FAILURE;
  155. if(ret == QE_REVOKED_ERROR)
  156. {
  157. AESM_LOG_FATAL("%s", g_event_string_table[SGX_EVENT_EPID_REVOCATION]);
  158. }
  159. else if(ret == QE_SIGRL_ERROR)
  160. {
  161. AESM_LOG_FATAL("%s", g_event_string_table[SGX_EVENT_EPID20_SIGRL_INTEGRITY_ERROR]);
  162. }
  163. return ret;
  164. }