platform_info_logic.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 "util.h"
  32. #include "platform_info_logic.h"
  33. #include "sgx_quote.h"
  34. #include "aesm_encode.h"
  35. #include "pve_logic.h"
  36. #include "aesm_logic.h"
  37. #include "le2be_macros.h"
  38. #include "pibsk_pub.hh"
  39. #include "sgx_sha256_128.h"
  40. #include <assert.h>
  41. #include "sgx_profile.h"
  42. #include "aesm_long_lived_thread.h"
  43. ae_error_t pib_verify_signature(platform_info_blob_wrapper_t& piBlobWrapper)
  44. {
  45. ae_error_t ae_err = AE_FAILURE;
  46. sgx_ecc_state_handle_t ecc_handle = NULL;
  47. uint8_t result = SGX_EC_INVALID_SIGNATURE;
  48. const uint32_t data_size = static_cast<uint32_t>(sizeof(piBlobWrapper.platform_info_blob) - sizeof(piBlobWrapper.platform_info_blob.signature));
  49. piBlobWrapper.valid_info_blob = false;
  50. do
  51. {
  52. sgx_ec256_public_t publicKey;
  53. sgx_ec256_signature_t signature;
  54. sgx_status_t sgx_status;
  55. se_static_assert(sizeof(publicKey) == sizeof(s_pib_pub_key_big_endian));
  56. se_static_assert(sizeof(signature) == sizeof(piBlobWrapper.platform_info_blob.signature));
  57. // convert the public key to little endian
  58. if(0!=memcpy_s(&publicKey, sizeof(publicKey), s_pib_pub_key_big_endian, sizeof(s_pib_pub_key_big_endian))){
  59. ae_err = AE_FAILURE;
  60. break;
  61. }
  62. SwapEndian_32B(((uint8_t*)&publicKey) + 0);
  63. SwapEndian_32B(((uint8_t*)&publicKey) + 32);
  64. // convert the signature to little endian
  65. if(0!=memcpy_s(&signature, sizeof(signature), &piBlobWrapper.platform_info_blob.signature, sizeof(piBlobWrapper.platform_info_blob.signature))){
  66. ae_err = AE_FAILURE;
  67. break;
  68. }
  69. SwapEndian_32B(((uint8_t*)&signature) + 0);
  70. SwapEndian_32B(((uint8_t*)&signature) + 32);
  71. sgx_status = sgx_ecc256_open_context(&ecc_handle);
  72. BREAK_IF_TRUE((SGX_SUCCESS != sgx_status), ae_err, AE_FAILURE);
  73. sgx_status = sgx_ecdsa_verify((uint8_t*)&piBlobWrapper.platform_info_blob, data_size, &publicKey, &signature, &result, ecc_handle);
  74. BREAK_IF_TRUE((SGX_SUCCESS != sgx_status), ae_err, AE_FAILURE);
  75. if (SGX_EC_VALID != result)
  76. {
  77. AESM_LOG_WARN(g_event_string_table[SGX_EVENT_PID_SIGNATURE_FAILURE]);
  78. break;
  79. }
  80. piBlobWrapper.valid_info_blob = true;
  81. ae_err = AE_SUCCESS;
  82. } while (0);
  83. if (ecc_handle != NULL) {
  84. sgx_ecc256_close_context(ecc_handle);
  85. }
  86. return ae_err;
  87. }
  88. aesm_error_t PlatformInfoLogic::report_attestation_status(
  89. uint8_t* platform_info, uint32_t platform_info_size,
  90. uint32_t attestation_status,
  91. uint8_t* update_info, uint32_t update_info_size)
  92. {
  93. AESM_DBG_TRACE("enter fun");
  94. //
  95. // we don't do anything without platform info
  96. //
  97. if (NULL == platform_info) {
  98. return AESM_PARAMETER_ERROR;
  99. }
  100. platform_info_blob_wrapper_t pibw;
  101. //
  102. // presence of platform info is conditional, on whether we're up to date
  103. // if we're up to date, no platform info and no need for update info
  104. //
  105. if (((sizeof(pibw.platform_info_blob) > platform_info_size)) || ((NULL != update_info) && (sizeof(sgx_update_info_bit_t) > update_info_size))) {
  106. return AESM_PARAMETER_ERROR;
  107. }
  108. pibw.valid_info_blob = false;
  109. memcpy_s(&pibw.platform_info_blob, sizeof(pibw.platform_info_blob), platform_info, platform_info_size);
  110. aesm_error_t status = AESM_SUCCESS; // status only tells app to look at updateInfo
  111. //
  112. // contents of input platform info can get stale, but not by virtue of anything we do
  113. // (the latest/current versions can change)
  114. // therefore, we'll use the same platform info the whole time
  115. //
  116. bool pibSigGood = (AE_SUCCESS == pib_verify_signature(pibw));
  117. //
  118. // invalid pib is an error whenever it's provided
  119. //
  120. if (!pibSigGood) {
  121. AESM_DBG_ERROR("pib verify signature failed");
  122. return AESM_PLATFORM_INFO_BLOB_INVALID_SIG;
  123. }
  124. if(pibw.platform_info_blob.xeid != AESMLogic::get_active_extended_epid_group_id()){
  125. return AESM_UNEXPECTED_ERROR;
  126. }
  127. uint32_t gid_mt_result = AESMLogic::is_gid_matching_result_in_epid_blob( pibw.platform_info_blob.gid);
  128. if(AESMLogic::GIDMT_UNMATCHED == gid_mt_result||
  129. AESMLogic::GIDMT_UNEXPECTED_ERROR == gid_mt_result){
  130. return AESM_UNEXPECTED_ERROR;
  131. }
  132. else if (AESMLogic::GIDMT_NOT_AVAILABLE == gid_mt_result) {
  133. return AESM_EPIDBLOB_ERROR;
  134. }
  135. ae_error_t nepStatus = need_epid_provisioning(&pibw);
  136. AESM_DBG_TRACE("need_epid_provisioning return %d",nepStatus);
  137. switch (nepStatus)
  138. {
  139. case AESM_NEP_DONT_NEED_EPID_PROVISIONING:
  140. {
  141. break;
  142. }
  143. case AESM_NEP_DONT_NEED_UPDATE_PVEQE: // sure thing
  144. {
  145. AESMLogicLock lock(AESMLogic::_qe_pve_mutex);
  146. if(!query_pve_thread_status()){//If another thread is busy on Epid Provisioning
  147. status = AESM_SUCCESS;
  148. break;
  149. }
  150. bool perfRekey = false;
  151. status = PvEAESMLogic::provision(perfRekey, THREAD_TIMEOUT);
  152. if (AESM_BUSY == status || //thread timeout
  153. AESM_PROXY_SETTING_ASSIST == status || //uae service need to set up proxy info and retry
  154. AESM_UPDATE_AVAILABLE == status || //PSW need be updated
  155. AESM_OUT_OF_EPC == status) // out of EPC
  156. {
  157. return status;//We should return to uae serivce directly
  158. }
  159. if (AESM_SUCCESS != status &&
  160. AESM_OUT_OF_MEMORY_ERROR != status &&
  161. AESM_BACKEND_SERVER_BUSY != status &&
  162. AESM_NETWORK_ERROR != status &&
  163. AESM_NETWORK_BUSY_ERROR != status)
  164. {
  165. status = AESM_SGX_PROVISION_FAILED;
  166. }
  167. break;
  168. }
  169. case AESM_NEP_PERFORMANCE_REKEY:
  170. {
  171. if (0 == attestation_status) // pr only if we succeeded (also we'll never get pr unless gid up-to-date)
  172. {
  173. bool perfRekey = true;
  174. AESMLogicLock lock(AESMLogic::_qe_pve_mutex);
  175. if(!query_pve_thread_status()){//If another thread is busy on Epid Provisioning
  176. status = AESM_SUCCESS;
  177. break;
  178. }
  179. status = PvEAESMLogic::provision(perfRekey, THREAD_TIMEOUT);
  180. if (AESM_BUSY == status ||//thread timeout
  181. AESM_PROXY_SETTING_ASSIST == status ||//uae service need to set up proxy info and retry
  182. AESM_UPDATE_AVAILABLE == status ||
  183. AESM_OUT_OF_EPC == status)
  184. {
  185. return status;//We should return to uae serivce directly
  186. }
  187. if (AESM_SUCCESS != status &&
  188. AESM_OUT_OF_MEMORY_ERROR != status &&
  189. AESM_BACKEND_SERVER_BUSY != status &&
  190. AESM_NETWORK_ERROR != status &&
  191. AESM_NETWORK_BUSY_ERROR != status)
  192. {
  193. status = AESM_SGX_PROVISION_FAILED;
  194. }
  195. }
  196. break;
  197. }
  198. default:
  199. {
  200. status = AESM_UNEXPECTED_ERROR;
  201. break;
  202. }
  203. }
  204. //
  205. // don't nag happy app about updates
  206. //
  207. if ((0 != attestation_status) && (NULL != update_info))
  208. {
  209. sgx_update_info_bit_t* p_update_info = (sgx_update_info_bit_t*)update_info;
  210. memset(p_update_info, 0, sizeof(*p_update_info));
  211. //
  212. // here, we treat values that get reported live - cpusvn, qe.isvsvn,
  213. // in normal flow, live values reported to attestation server will be the same as current values now so
  214. // we just look at out-of-date bits corresponding to these values.
  215. // the alternative would be to compare current with latest as reported by IAS. this
  216. // isn't an option for cpusvn since what we get from IAS is equivalent cpusvn.
  217. //
  218. if (cpu_svn_out_of_date(&pibw))
  219. {
  220. p_update_info->ucodeUpdate = 1;
  221. status = AESM_UPDATE_AVAILABLE;
  222. }
  223. if (qe_svn_out_of_date(&pibw) ||
  224. pce_svn_out_of_date(&pibw)
  225. )
  226. {
  227. p_update_info->pswUpdate = 1;
  228. status = AESM_UPDATE_AVAILABLE;
  229. }
  230. }
  231. return status;
  232. }