QEClass.cpp 5.2 KB

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