uae_api.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * Copyright (C) 2011-2017 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 <AEServicesProvider.h>
  32. #include <AEServices.h>
  33. #include <stdlib.h>
  34. #include <AEInitQuoteRequest.h>
  35. #include <AEInitQuoteResponse.h>
  36. #include <AEGetQuoteRequest.h>
  37. #include <AEGetQuoteResponse.h>
  38. #include <AEGetLaunchTokenRequest.h>
  39. #include <AEGetLaunchTokenResponse.h>
  40. #include <AEGetPsCapRequest.h>
  41. #include <AEGetPsCapResponse.h>
  42. #include <AEReportAttestationRequest.h>
  43. #include <AEReportAttestationResponse.h>
  44. #include <AEGetWhiteListSizeRequest.h>
  45. #include <AEGetWhiteListSizeResponse.h>
  46. #include <AEGetWhiteListRequest.h>
  47. #include <AEGetWhiteListResponse.h>
  48. #include <AESGXGetExtendedEpidGroupIdRequest.h>
  49. #include <AESGXGetExtendedEpidGroupIdResponse.h>
  50. #include <AESGXSwitchExtendedEpidGroupRequest.h>
  51. #include <AESGXSwitchExtendedEpidGroupResponse.h>
  52. ////////THE COMMON STUFF aka INTEGRATION with Linux API
  53. #include <sgx_report.h>
  54. #include <arch.h>
  55. #include <sgx_urts.h>
  56. #include <sgx_uae_service.h>
  57. #include <oal/uae_oal_api.h>
  58. #include <aesm_error.h>
  59. #include <new>
  60. #define TRY_CATCH_BAD_ALLOC(block) \
  61. try{ \
  62. block; \
  63. } \
  64. catch(std::bad_alloc& e) \
  65. { \
  66. *result = AESM_OUT_OF_MEMORY_ERROR; \
  67. return UAE_OAL_SUCCESS; \
  68. }
  69. ///////////////////////////////////////////////////////
  70. // NOTE -> uAE works internally with milliseconds and cannot obtain a better resolution for timeout because
  71. // epoll_wait will get the timeout parameter in milliseconds
  72. extern "C"
  73. uae_oal_status_t oal_get_launch_token(const enclave_css_t* signature, const sgx_attributes_t* attribute, sgx_launch_token_t* launchToken, uint32_t timeout_usec, aesm_error_t *result)
  74. {
  75. TRY_CATCH_BAD_ALLOC({
  76. AEServices* servicesProvider = AEServicesProvider::GetServicesProvider();
  77. if (servicesProvider == NULL)
  78. return UAE_OAL_ERROR_UNEXPECTED;
  79. AEGetLaunchTokenRequest getLaunchTokenRequest(sizeof(sgx_measurement_t),
  80. (const uint8_t*)signature->body.enclave_hash.m,
  81. sizeof(signature->key.modulus),
  82. (const uint8_t*)signature->key.modulus,
  83. sizeof(sgx_attributes_t),
  84. (const uint8_t*)attribute,
  85. timeout_usec/1000);
  86. AEGetLaunchTokenResponse getLaunchTokenResponse;
  87. uae_oal_status_t ret = servicesProvider->InternalInterface(&getLaunchTokenRequest, &getLaunchTokenResponse, timeout_usec / 1000);
  88. if (ret == UAE_OAL_SUCCESS)
  89. {
  90. bool valid = getLaunchTokenResponse.GetValues((uint32_t*)result, (uint8_t*)launchToken, sizeof(sgx_launch_token_t));
  91. if (!valid)
  92. ret = UAE_OAL_ERROR_UNEXPECTED;
  93. }
  94. return ret;
  95. });
  96. }
  97. /*
  98. QUOTING
  99. */
  100. extern "C"
  101. uae_oal_status_t SGXAPI oal_init_quote(sgx_target_info_t *p_target_info, sgx_epid_group_id_t *p_gid, uint32_t timeout_usec, aesm_error_t *result)
  102. {
  103. TRY_CATCH_BAD_ALLOC({
  104. AEServices *servicesProvider = AEServicesProvider::GetServicesProvider();
  105. if (servicesProvider == NULL)
  106. return UAE_OAL_ERROR_UNEXPECTED;
  107. AEInitQuoteRequest initQuoteRequest(timeout_usec / 1000);
  108. AEInitQuoteResponse initQuoteResponse;
  109. uae_oal_status_t ret = servicesProvider->InternalInterface(&initQuoteRequest, &initQuoteResponse, timeout_usec / 1000);
  110. if (ret == UAE_OAL_SUCCESS)
  111. {
  112. bool valid = initQuoteResponse.GetValues((uint32_t*)result, sizeof(sgx_epid_group_id_t), (uint8_t*)p_gid, sizeof(sgx_target_info_t), (uint8_t*)p_target_info);
  113. if (!valid)
  114. ret = UAE_OAL_ERROR_UNEXPECTED;
  115. }
  116. return ret;
  117. });
  118. }
  119. extern "C"
  120. uae_oal_status_t SGXAPI oal_get_quote(
  121. const sgx_report_t *p_report,
  122. sgx_quote_sign_type_t quote_type,
  123. const sgx_spid_t *p_spid,
  124. const sgx_quote_nonce_t *p_nonce,
  125. const uint8_t *p_sig_rl,
  126. uint32_t sig_rl_size,
  127. sgx_report_t *p_qe_report,
  128. sgx_quote_t *p_quote,
  129. uint32_t quote_size,
  130. uint32_t timeout_usec,
  131. aesm_error_t *result)
  132. {
  133. TRY_CATCH_BAD_ALLOC({
  134. AEServices *servicesProvider = AEServicesProvider::GetServicesProvider();
  135. if (servicesProvider == NULL)
  136. return UAE_OAL_ERROR_UNEXPECTED;
  137. AEGetQuoteRequest getQuoteRequest(sizeof(sgx_report_t), (const uint8_t*)p_report,
  138. (uint32_t)quote_type,
  139. sizeof(sgx_spid_t), (const uint8_t*)p_spid,
  140. sizeof(sgx_quote_nonce_t), (const uint8_t*)p_nonce,
  141. sig_rl_size, (const uint8_t*)p_sig_rl,
  142. quote_size,
  143. p_qe_report != NULL,
  144. timeout_usec / 1000);
  145. AEGetQuoteResponse getQuoteResponse;
  146. uae_oal_status_t ret = servicesProvider->InternalInterface(&getQuoteRequest, &getQuoteResponse, timeout_usec / 1000);
  147. if (ret == UAE_OAL_SUCCESS)
  148. {
  149. bool valid = getQuoteResponse.GetValues((uint32_t*)result, quote_size, (uint8_t*)p_quote, sizeof(sgx_report_t), (uint8_t*)p_qe_report);
  150. if (!valid)
  151. ret = UAE_OAL_ERROR_UNEXPECTED;
  152. }
  153. return ret;
  154. });
  155. }
  156. extern "C"
  157. uae_oal_status_t SGXAPI oal_get_ps_cap(uint64_t* ps_cap, uint32_t timeout_usec, aesm_error_t *result)
  158. {
  159. TRY_CATCH_BAD_ALLOC({
  160. AEServices* servicesProvider = AEServicesProvider::GetServicesProvider();
  161. if (servicesProvider == NULL)
  162. return UAE_OAL_ERROR_UNEXPECTED;
  163. AEGetPsCapRequest getPsCapRequest(timeout_usec/1000);
  164. AEGetPsCapResponse getPsCapResponse;
  165. uae_oal_status_t ret = servicesProvider->InternalInterface(&getPsCapRequest, &getPsCapResponse, timeout_usec / 1000);
  166. if (ret == UAE_OAL_SUCCESS)
  167. {
  168. bool valid = getPsCapResponse.GetValues((uint32_t*)result, ps_cap);
  169. if (!valid)
  170. ret = UAE_OAL_ERROR_UNEXPECTED;
  171. }
  172. return ret;
  173. });
  174. }
  175. extern "C"
  176. uae_oal_status_t SGXAPI oal_report_attestation_status(
  177. const sgx_platform_info_t* platform_info,
  178. int attestation_error_code,
  179. sgx_update_info_bit_t* platform_update_info,
  180. uint32_t timeout_usec,
  181. aesm_error_t *result)
  182. {
  183. TRY_CATCH_BAD_ALLOC({
  184. AEServices* servicesProvider = AEServicesProvider::GetServicesProvider();
  185. if (servicesProvider == NULL)
  186. return UAE_OAL_ERROR_UNEXPECTED;
  187. AEReportAttestationRequest reportAttestationRequest(sizeof(sgx_platform_info_t), (const uint8_t*)platform_info, (uint32_t)attestation_error_code, sizeof(sgx_update_info_bit_t), timeout_usec / 1000);
  188. AEReportAttestationResponse reportAttestationResponse;
  189. uae_oal_status_t ret = servicesProvider->InternalInterface(&reportAttestationRequest, &reportAttestationResponse, timeout_usec / 1000);
  190. if (ret == UAE_OAL_SUCCESS)
  191. {
  192. bool valid = reportAttestationResponse.GetValues((uint32_t*)result, sizeof(sgx_update_info_bit_t), (uint8_t*)platform_update_info);
  193. if (!valid)
  194. ret = UAE_OAL_ERROR_UNEXPECTED;
  195. }
  196. return ret;
  197. });
  198. }
  199. extern "C"
  200. uae_oal_status_t oal_get_whitelist_size(uint32_t* white_list_size, uint32_t timeout_usec, aesm_error_t *result)
  201. {
  202. TRY_CATCH_BAD_ALLOC({
  203. AEServices* servicesProvider = AEServicesProvider::GetServicesProvider();
  204. if (servicesProvider == NULL)
  205. return UAE_OAL_ERROR_UNEXPECTED;
  206. AEGetWhiteListSizeRequest getWhiteListSizeRequest(timeout_usec / 1000);
  207. AEGetWhiteListSizeResponse getWhiteListSizeResponse;
  208. uae_oal_status_t ret = servicesProvider->InternalInterface(&getWhiteListSizeRequest, &getWhiteListSizeResponse, timeout_usec / 1000);
  209. if (ret == UAE_OAL_SUCCESS)
  210. {
  211. bool valid = getWhiteListSizeResponse.GetValues((uint32_t*)result, white_list_size);
  212. if (!valid)
  213. ret = UAE_OAL_ERROR_UNEXPECTED;
  214. }
  215. return ret;
  216. });
  217. }
  218. extern "C"
  219. uae_oal_status_t oal_get_whitelist(uint8_t *white_list, uint32_t white_list_size, uint32_t timeout_usec, aesm_error_t *result)
  220. {
  221. TRY_CATCH_BAD_ALLOC({
  222. AEServices* servicesProvider = AEServicesProvider::GetServicesProvider();
  223. if (servicesProvider == NULL)
  224. return UAE_OAL_ERROR_UNEXPECTED;
  225. AEGetWhiteListRequest getWhiteListRequest(white_list_size, timeout_usec / 1000);
  226. AEGetWhiteListResponse getWhiteListResponse;
  227. uae_oal_status_t ret = servicesProvider->InternalInterface(&getWhiteListRequest, &getWhiteListResponse, timeout_usec / 1000);
  228. if (ret == UAE_OAL_SUCCESS)
  229. {
  230. bool valid = getWhiteListResponse.GetValues((uint32_t*)result, white_list_size, white_list);
  231. if (!valid)
  232. ret = UAE_OAL_ERROR_UNEXPECTED;
  233. }
  234. return ret;
  235. });
  236. }
  237. extern "C"
  238. uae_oal_status_t oal_get_extended_epid_group_id(uint32_t* extended_group_id, uint32_t timeout_usec, aesm_error_t *result)
  239. {
  240. TRY_CATCH_BAD_ALLOC({
  241. AEServices* servicesProvider = AEServicesProvider::GetServicesProvider();
  242. if (servicesProvider == NULL)
  243. return UAE_OAL_ERROR_UNEXPECTED;
  244. AESGXGetExtendedEpidGroupIdRequest getExtendedEpidGroupIdRequest(timeout_usec / 1000);
  245. AESGXGetExtendedEpidGroupIdResponse getExtendedEpidGroupIdResponse;
  246. uae_oal_status_t ret = servicesProvider->InternalInterface(&getExtendedEpidGroupIdRequest, &getExtendedEpidGroupIdResponse, timeout_usec / 1000);
  247. if (ret == UAE_OAL_SUCCESS)
  248. {
  249. bool valid = getExtendedEpidGroupIdResponse.GetValues((uint32_t*)result, extended_group_id);
  250. if (!valid)
  251. ret = UAE_OAL_ERROR_UNEXPECTED;
  252. }
  253. return ret;
  254. });
  255. }
  256. extern "C"
  257. uae_oal_status_t oal_switch_extended_epid_group(uint32_t x_group_id, uint32_t timeout_usec, aesm_error_t *result)
  258. {
  259. TRY_CATCH_BAD_ALLOC({
  260. AEServices* servicesProvider = AEServicesProvider::GetServicesProvider();
  261. if (servicesProvider == NULL)
  262. return UAE_OAL_ERROR_UNEXPECTED;
  263. AESGXSwitchExtendedEpidGroupRequest switchExtendedEpidGroupRequest(x_group_id, timeout_usec / 1000);
  264. AESGXSwitchExtendedEpidGroupResponse switchExtendedEpidGroupResponse;
  265. uae_oal_status_t ret = servicesProvider->InternalInterface(&switchExtendedEpidGroupRequest, &switchExtendedEpidGroupResponse, timeout_usec / 1000);
  266. if (ret == UAE_OAL_SUCCESS)
  267. {
  268. bool valid = switchExtendedEpidGroupResponse.GetValues((uint32_t*)result);
  269. if (!valid)
  270. ret = UAE_OAL_ERROR_UNEXPECTED;
  271. }
  272. return ret;
  273. });
  274. }