sgx_uae_service.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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 <oal/uae_oal_api.h>
  32. #include <aesm_error.h>
  33. #include "sgx_uae_service.h"
  34. #include "uae_service_internal.h"
  35. #include <Config.h> /*needed to unify default timeout values */
  36. #include "stdint.h"
  37. #include "se_sig_rl.h"
  38. #if !defined(ntohl)
  39. #define ntohl(u32) \
  40. ((uint32_t)(((((const unsigned char*)&(u32))[0]) << 24) \
  41. + ((((const unsigned char*)&(u32))[1]) << 16) \
  42. + ((((const unsigned char*)&(u32))[2]) << 8) \
  43. + (((const unsigned char*)&(u32))[3])))
  44. #endif
  45. #define GET_LAUNCH_TOKEN_TIMEOUT_MSEC (IPC_LATENCY)
  46. #define SE_INIT_QUOTE_TIMEOUT_MSEC (IPC_LATENCY)
  47. //add 3 millisecond per sig_rl entry
  48. #define SE_GET_QUOTE_TIMEOUT_MSEC(p_sig_rl) (IPC_LATENCY + ((p_sig_rl) ? 3*ntohl(((const se_sig_rl_t*)p_sig_rl)->sig_rl.n2) : 0))
  49. #define SE_GET_PS_CAP_TIMEOUT_MSEC (IPC_LATENCY)
  50. #define SE_REPORT_REMOTE_ATTESTATION_FAILURE_TIMEOUT_MSEC (IPC_LATENCY)
  51. extern "C" {
  52. sgx_status_t get_launch_token(
  53. const enclave_css_t* signature,
  54. const sgx_attributes_t* attribute,
  55. sgx_launch_token_t* launch_token)
  56. {
  57. if (signature == NULL || attribute == NULL || launch_token == NULL)
  58. return SGX_ERROR_INVALID_PARAMETER;
  59. aesm_error_t result = AESM_UNEXPECTED_ERROR;
  60. uae_oal_status_t status = oal_get_launch_token(signature, attribute, launch_token, GET_LAUNCH_TOKEN_TIMEOUT_MSEC*1000, &result);
  61. /*common mappings */
  62. sgx_status_t mapped = oal_map_status(status);
  63. if (mapped != SGX_SUCCESS)
  64. return mapped;
  65. mapped = oal_map_result(result);
  66. if (mapped != SGX_SUCCESS)
  67. {
  68. /*operation specific mapping */
  69. if (mapped == SGX_ERROR_UNEXPECTED && result != AESM_UNEXPECTED_ERROR)
  70. {
  71. switch (result)
  72. {
  73. case AESM_NO_DEVICE_ERROR:
  74. mapped = SGX_ERROR_NO_DEVICE;
  75. break;
  76. case AESM_GET_LICENSETOKEN_ERROR:
  77. mapped = SGX_ERROR_SERVICE_INVALID_PRIVILEGE;
  78. break;
  79. default:
  80. mapped = SGX_ERROR_UNEXPECTED;
  81. }
  82. }
  83. }
  84. return mapped;
  85. }
  86. sgx_status_t sgx_init_quote(
  87. sgx_target_info_t *p_target_info,
  88. sgx_epid_group_id_t *p_gid)
  89. {
  90. if (p_target_info == NULL || p_gid == NULL)
  91. return SGX_ERROR_INVALID_PARAMETER;
  92. aesm_error_t result = AESM_UNEXPECTED_ERROR;
  93. uae_oal_status_t status = oal_init_quote(p_target_info, p_gid, SE_INIT_QUOTE_TIMEOUT_MSEC*1000, &result);
  94. sgx_status_t mapped = oal_map_status(status);
  95. if (mapped != SGX_SUCCESS)
  96. return mapped;
  97. mapped = oal_map_result(result);
  98. if (mapped != SGX_SUCCESS)
  99. {
  100. /*operation specific mapping */
  101. if (mapped == SGX_ERROR_UNEXPECTED && result != AESM_UNEXPECTED_ERROR)
  102. {
  103. switch (result)
  104. {
  105. case AESM_EPIDBLOB_ERROR:
  106. mapped = SGX_ERROR_AE_INVALID_EPIDBLOB;
  107. break;
  108. case AESM_EPID_REVOKED_ERROR:
  109. mapped = SGX_ERROR_EPID_MEMBER_REVOKED;
  110. break;
  111. case AESM_BACKEND_SERVER_BUSY:
  112. mapped = SGX_ERROR_BUSY;
  113. break;
  114. case AESM_SGX_PROVISION_FAILED:
  115. mapped = SGX_ERROR_UNEXPECTED;
  116. break;
  117. default:
  118. mapped = SGX_ERROR_UNEXPECTED;
  119. }
  120. }
  121. }
  122. return mapped;
  123. }
  124. sgx_status_t sgx_get_quote(
  125. const sgx_report_t *p_report,
  126. sgx_quote_sign_type_t quote_type,
  127. const sgx_spid_t *p_spid,
  128. const sgx_quote_nonce_t *p_nonce,
  129. const uint8_t *p_sig_rl,
  130. uint32_t sig_rl_size,
  131. sgx_report_t *p_qe_report,
  132. sgx_quote_t *p_quote,
  133. uint32_t quote_size)
  134. {
  135. if (p_report == NULL || p_spid == NULL || p_quote == NULL || quote_size == 0 )
  136. return SGX_ERROR_INVALID_PARAMETER;
  137. if ((p_sig_rl == NULL && sig_rl_size != 0) ||
  138. (p_sig_rl != NULL && sig_rl_size == 0) )
  139. return SGX_ERROR_INVALID_PARAMETER;
  140. aesm_error_t result = AESM_UNEXPECTED_ERROR;
  141. uae_oal_status_t status = oal_get_quote(p_report, quote_type, p_spid, p_nonce, p_sig_rl, sig_rl_size, p_qe_report,
  142. p_quote, quote_size, SE_GET_QUOTE_TIMEOUT_MSEC(p_sig_rl)*1000, &result);
  143. sgx_status_t mapped = oal_map_status(status);
  144. if (mapped != SGX_SUCCESS)
  145. return mapped;
  146. mapped = oal_map_result(result);
  147. if (mapped != SGX_SUCCESS)
  148. {
  149. /*operation specific mapping */
  150. if (mapped == SGX_ERROR_UNEXPECTED && result != AESM_UNEXPECTED_ERROR)
  151. {
  152. switch (result)
  153. {
  154. case AESM_EPIDBLOB_ERROR:
  155. mapped = SGX_ERROR_AE_INVALID_EPIDBLOB;
  156. break;
  157. case AESM_EPID_REVOKED_ERROR:
  158. mapped = SGX_ERROR_EPID_MEMBER_REVOKED;
  159. break;
  160. case AESM_BACKEND_SERVER_BUSY:
  161. mapped = SGX_ERROR_BUSY;
  162. break;
  163. case AESM_SGX_PROVISION_FAILED:
  164. mapped = SGX_ERROR_UNEXPECTED;
  165. break;
  166. default:
  167. mapped = SGX_ERROR_UNEXPECTED;
  168. }
  169. }
  170. }
  171. return mapped;
  172. }
  173. sgx_status_t sgx_get_ps_cap(sgx_ps_cap_t* p_sgx_ps_cap)
  174. {
  175. if (p_sgx_ps_cap == NULL)
  176. return SGX_ERROR_INVALID_PARAMETER;
  177. aesm_error_t result = AESM_UNEXPECTED_ERROR;
  178. uint64_t ps_cap = 0;
  179. uae_oal_status_t status = oal_get_ps_cap(&ps_cap, SE_GET_PS_CAP_TIMEOUT_MSEC*1000, &result);
  180. p_sgx_ps_cap->ps_cap0 = (uint32_t)ps_cap;
  181. p_sgx_ps_cap->ps_cap1 = (uint32_t)(ps_cap >> 32);
  182. sgx_status_t mapped = oal_map_status(status);
  183. if (mapped != SGX_SUCCESS)
  184. return mapped;
  185. mapped = oal_map_result(result);
  186. if (mapped != SGX_SUCCESS)
  187. {
  188. /*operation specific mapping */
  189. if (mapped == SGX_ERROR_UNEXPECTED && result != AESM_UNEXPECTED_ERROR)
  190. {
  191. switch (result)
  192. {
  193. case AESM_LONG_TERM_PAIRING_FAILED:
  194. case AESM_EPH_SESSION_FAILED:
  195. case AESM_PSDA_UNAVAILABLE:
  196. mapped = SGX_ERROR_SERVICE_UNAVAILABLE;
  197. break;
  198. default:
  199. mapped = SGX_ERROR_UNEXPECTED;
  200. }
  201. }
  202. }
  203. return mapped;
  204. }
  205. sgx_status_t sgx_report_attestation_status(
  206. const sgx_platform_info_t* p_platform_info,
  207. int attestation_status,
  208. sgx_update_info_bit_t* p_update_info)
  209. {
  210. if (p_platform_info == NULL || p_update_info == NULL)
  211. return SGX_ERROR_INVALID_PARAMETER;
  212. aesm_error_t result = AESM_UNEXPECTED_ERROR;
  213. uae_oal_status_t status = oal_report_attestation_status(p_platform_info, attestation_status, p_update_info, SE_REPORT_REMOTE_ATTESTATION_FAILURE_TIMEOUT_MSEC*1000, &result);
  214. sgx_status_t mapped = oal_map_status(status);
  215. if (mapped != SGX_SUCCESS)
  216. return mapped;
  217. mapped = oal_map_result(result);
  218. if (mapped != SGX_SUCCESS)
  219. {
  220. /*operation specific mapping */
  221. if (mapped == SGX_ERROR_UNEXPECTED && result != AESM_UNEXPECTED_ERROR)
  222. {
  223. switch (result)
  224. {
  225. case AESM_BACKEND_SERVER_BUSY:
  226. mapped = SGX_ERROR_BUSY;
  227. break;
  228. case AESM_PLATFORM_INFO_BLOB_INVALID_SIG:
  229. mapped = SGX_ERROR_INVALID_PARAMETER;
  230. break;
  231. case AESM_SGX_PROVISION_FAILED:
  232. default:
  233. mapped = SGX_ERROR_UNEXPECTED;
  234. }
  235. }
  236. }
  237. return mapped;
  238. }
  239. sgx_status_t create_session_ocall(
  240. uint32_t *session_id,
  241. uint8_t *se_dh_msg1,
  242. uint32_t dh_msg1_size,
  243. uint32_t timeout)
  244. {
  245. if(!session_id || !se_dh_msg1)
  246. return SGX_ERROR_INVALID_PARAMETER;
  247. aesm_error_t result = AESM_UNEXPECTED_ERROR;
  248. uae_oal_status_t status = oal_create_session(session_id, se_dh_msg1, dh_msg1_size, timeout*1000, &result);
  249. sgx_status_t mapped = oal_map_status(status);
  250. if (mapped != SGX_SUCCESS)
  251. return mapped;
  252. mapped = oal_map_result(result);
  253. if (mapped != SGX_SUCCESS)
  254. {
  255. /*operation specific mapping */
  256. if (mapped == SGX_ERROR_UNEXPECTED && result != AESM_UNEXPECTED_ERROR)
  257. {
  258. switch (result)
  259. {
  260. case AESM_MAX_NUM_SESSION_REACHED:
  261. mapped = SGX_ERROR_BUSY;
  262. break;
  263. case AESM_EPH_SESSION_FAILED:
  264. case AESM_LONG_TERM_PAIRING_FAILED:
  265. case AESM_PSDA_UNAVAILABLE:
  266. case AESM_SERVICE_NOT_AVAILABLE:
  267. mapped = SGX_ERROR_SERVICE_UNAVAILABLE;
  268. break;
  269. case AESM_MSG_ERROR:
  270. default:
  271. mapped = SGX_ERROR_UNEXPECTED;
  272. }
  273. }
  274. }
  275. return mapped;
  276. }
  277. sgx_status_t exchange_report_ocall(
  278. uint32_t session_id,
  279. const uint8_t *se_dh_msg2,
  280. uint32_t dh_msg2_size,
  281. uint8_t *se_dh_msg3,
  282. uint32_t dh_msg3_size,
  283. uint32_t timeout)
  284. {
  285. if (!se_dh_msg2 || !se_dh_msg3)
  286. return SGX_ERROR_INVALID_PARAMETER;
  287. aesm_error_t result = AESM_UNEXPECTED_ERROR;
  288. uae_oal_status_t status = oal_exchange_report(session_id, se_dh_msg2, dh_msg2_size, se_dh_msg3, dh_msg3_size, timeout*1000, &result);
  289. sgx_status_t mapped = oal_map_status(status);
  290. if (mapped != SGX_SUCCESS)
  291. return mapped;
  292. mapped = oal_map_result(result);
  293. if (mapped != SGX_SUCCESS)
  294. {
  295. /*operation specific mapping */
  296. if (mapped == SGX_ERROR_UNEXPECTED && result != AESM_UNEXPECTED_ERROR)
  297. {
  298. switch (result)
  299. {
  300. case AESM_SESSION_INVALID:
  301. mapped = SGX_ERROR_AE_SESSION_INVALID;
  302. break;
  303. case AESM_KDF_MISMATCH:
  304. mapped = SGX_ERROR_KDF_MISMATCH;
  305. break;
  306. case AESM_EPH_SESSION_FAILED:
  307. case AESM_LONG_TERM_PAIRING_FAILED:
  308. case AESM_PSDA_UNAVAILABLE:
  309. case AESM_SERVICE_NOT_AVAILABLE:
  310. mapped = SGX_ERROR_SERVICE_UNAVAILABLE;
  311. break;
  312. default:
  313. mapped = SGX_ERROR_UNEXPECTED;
  314. }
  315. }
  316. }
  317. return mapped;
  318. }
  319. sgx_status_t close_session_ocall(
  320. uint32_t session_id,
  321. uint32_t timeout)
  322. {
  323. aesm_error_t result = AESM_UNEXPECTED_ERROR;
  324. uae_oal_status_t status = oal_close_session(session_id, timeout*1000, &result);
  325. sgx_status_t mapped = oal_map_status(status);
  326. if (mapped != SGX_SUCCESS)
  327. return mapped;
  328. mapped = oal_map_result(result);
  329. if (mapped != SGX_SUCCESS)
  330. {
  331. /*operation specific mapping */
  332. if (mapped == SGX_ERROR_UNEXPECTED && result != AESM_UNEXPECTED_ERROR)
  333. {
  334. switch (result)
  335. {
  336. case AESM_SESSION_INVALID:
  337. mapped = SGX_ERROR_AE_SESSION_INVALID;
  338. break;
  339. case AESM_EPH_SESSION_FAILED:
  340. case AESM_LONG_TERM_PAIRING_FAILED:
  341. case AESM_SERVICE_NOT_AVAILABLE:
  342. mapped = SGX_ERROR_SERVICE_UNAVAILABLE;
  343. break;
  344. default:
  345. mapped = SGX_ERROR_UNEXPECTED;
  346. }
  347. }
  348. }
  349. return mapped;
  350. }
  351. sgx_status_t invoke_service_ocall(
  352. const uint8_t *pse_message_req,
  353. uint32_t pse_message_req_size,
  354. uint8_t *pse_message_resp,
  355. uint32_t pse_message_resp_size,
  356. uint32_t timeout)
  357. {
  358. if (pse_message_req == NULL || pse_message_resp == NULL)
  359. return SGX_ERROR_INVALID_PARAMETER;
  360. aesm_error_t result = AESM_UNEXPECTED_ERROR;
  361. uae_oal_status_t status = oal_invoke_service(pse_message_req, pse_message_req_size, pse_message_resp, pse_message_resp_size, timeout*1000, &result);
  362. sgx_status_t mapped = oal_map_status(status);
  363. if (mapped != SGX_SUCCESS)
  364. return mapped;
  365. mapped = oal_map_result(result);
  366. if (mapped != SGX_SUCCESS)
  367. {
  368. /*operation specific mapping */
  369. if (mapped == SGX_ERROR_UNEXPECTED && result != AESM_UNEXPECTED_ERROR)
  370. {
  371. switch (result)
  372. {
  373. case AESM_SESSION_INVALID:
  374. mapped = SGX_ERROR_AE_SESSION_INVALID;
  375. break;
  376. case AESM_EPH_SESSION_FAILED:
  377. case AESM_LONG_TERM_PAIRING_FAILED:
  378. case AESM_PSDA_UNAVAILABLE:
  379. case AESM_SERVICE_NOT_AVAILABLE:
  380. mapped = SGX_ERROR_SERVICE_UNAVAILABLE;
  381. break;
  382. case AESM_MSG_ERROR:
  383. default:
  384. mapped = SGX_ERROR_UNEXPECTED;
  385. }
  386. }
  387. }
  388. return mapped;
  389. }
  390. // common mapper function for all OAL specific error codes
  391. sgx_status_t oal_map_status(uae_oal_status_t status)
  392. {
  393. sgx_status_t retVal;
  394. switch (status)
  395. {
  396. case UAE_OAL_SUCCESS:
  397. retVal = SGX_SUCCESS;
  398. break;
  399. case UAE_OAL_ERROR_UNEXPECTED:
  400. retVal = SGX_ERROR_UNEXPECTED;
  401. break;
  402. case UAE_OAL_ERROR_AESM_UNAVAILABLE:
  403. retVal = SGX_ERROR_SERVICE_UNAVAILABLE;
  404. break;
  405. case UAE_OAL_ERROR_TIMEOUT:
  406. retVal = SGX_ERROR_SERVICE_TIMEOUT;
  407. break;
  408. default:
  409. retVal = SGX_ERROR_UNEXPECTED;
  410. }
  411. return retVal;
  412. }
  413. sgx_status_t oal_map_result(aesm_error_t result)
  414. {
  415. sgx_status_t retVal = SGX_ERROR_UNEXPECTED;
  416. switch (result)
  417. {
  418. case AESM_SUCCESS:
  419. retVal = SGX_SUCCESS;
  420. break;
  421. case AESM_UPDATE_AVAILABLE:
  422. retVal = SGX_ERROR_UPDATE_NEEDED;
  423. break;
  424. case AESM_UNEXPECTED_ERROR:
  425. retVal = SGX_ERROR_UNEXPECTED;
  426. break;
  427. case AESM_PARAMETER_ERROR:
  428. retVal = SGX_ERROR_INVALID_PARAMETER;
  429. break;
  430. case AESM_SERVICE_STOPPED:
  431. retVal = SGX_ERROR_SERVICE_UNAVAILABLE;
  432. break;
  433. case AESM_OUT_OF_MEMORY_ERROR:
  434. retVal = SGX_ERROR_OUT_OF_MEMORY;
  435. break;
  436. case AESM_BUSY:
  437. retVal = SGX_ERROR_BUSY;
  438. break;
  439. case AESM_NETWORK_ERROR:
  440. case AESM_NETWORK_BUSY_ERROR:
  441. case AESM_PROXY_SETTING_ASSIST:
  442. retVal = SGX_ERROR_NETWORK_FAILURE;
  443. break;
  444. default:
  445. retVal = SGX_ERROR_UNEXPECTED;
  446. }
  447. return retVal;
  448. }
  449. } /* extern "C" */