tkey_exchange.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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 <stdint.h>
  32. #include "sgx_tkey_exchange.h"
  33. #include "sgx_trts.h"
  34. #include "sgx_utils.h"
  35. #include "ecp_interface.h"
  36. #include "util.h"
  37. #include "string.h"
  38. #include "stdlib.h"
  39. #include "sgx_spinlock.h"
  40. #include "sgx_tkey_exchange_t.h"
  41. #include "simple_vector.h"
  42. #include "se_cdefs.h"
  43. // Add a version to tkey_exchange.
  44. SGX_ACCESS_VERSION(tkey_exchange, 1)
  45. #define ERROR_BREAK(sgx_status) if(SGX_SUCCESS!=sgx_status){break;}
  46. #define SAFE_FREE(ptr) {if (NULL != (ptr)) {free(ptr); (ptr)=NULL;}}
  47. #pragma pack(push, 1)
  48. // any call to sgx_ra_init will reset the input pubkey related ra_db_item_t.ra_state to ra_inited
  49. // only sgx_ra_get_ga can change ra_inited to ra_get_gaed
  50. // only sgx_ra_proc_msg2_trusted can change ra_get_gaed to ra_proc_msg2ed
  51. // sgx_ra_get_msg3_trusted and sgx_ra_get_keys will check ra_state whether to be ra_proc_msg2ed
  52. typedef enum _ra_state
  53. {
  54. ra_inited= 0,
  55. ra_get_gaed,
  56. ra_proc_msg2ed
  57. }ra_state;
  58. typedef struct _ra_db_item_t
  59. {
  60. sgx_ec256_public_t g_a;
  61. sgx_ec256_public_t g_b;
  62. sgx_ec_key_128bit_t vk_key;
  63. sgx_ec256_public_t sp_pubkey;
  64. sgx_ec256_private_t a;
  65. sgx_ps_sec_prop_desc_t ps_sec_prop;
  66. sgx_ec_key_128bit_t mk_key;
  67. sgx_ec_key_128bit_t sk_key;
  68. sgx_ec_key_128bit_t smk_key;
  69. sgx_quote_nonce_t quote_nonce; //to verify quote report data
  70. sgx_target_info_t qe_target; //to verify quote report
  71. ra_state state;
  72. sgx_spinlock_t item_lock;
  73. }ra_db_item_t;
  74. #pragma pack(pop)
  75. static simple_vector g_ra_db = {0, 0, NULL};
  76. static sgx_spinlock_t g_ra_db_lock;
  77. extern "C" sgx_status_t sgx_ra_get_ga(
  78. sgx_ra_context_t context,
  79. sgx_ec256_public_t *g_a)
  80. {
  81. sgx_status_t se_ret;
  82. if(vector_size(&g_ra_db) <= context||!g_a)
  83. return SGX_ERROR_INVALID_PARAMETER;
  84. ra_db_item_t* item = NULL;
  85. if(0 != vector_get(&g_ra_db, context, reinterpret_cast<void**>(&item)) || item == NULL )
  86. return SGX_ERROR_INVALID_PARAMETER;
  87. sgx_ecc_state_handle_t ecc_state = NULL;
  88. sgx_ec256_public_t pub_key;
  89. sgx_ec256_private_t priv_key;
  90. memset(&pub_key, 0, sizeof(pub_key));
  91. memset(&priv_key, 0, sizeof(priv_key));
  92. sgx_spin_lock(&item->item_lock);
  93. do
  94. {
  95. //sgx_ra_init must have been called
  96. if (item->state != ra_inited)
  97. {
  98. se_ret = SGX_ERROR_INVALID_STATE;
  99. break;
  100. }
  101. // ecc_state should be closed when exit.
  102. se_ret = sgx_ecc256_open_context(&ecc_state);
  103. if (SGX_SUCCESS != se_ret)
  104. {
  105. if(SGX_ERROR_OUT_OF_MEMORY != se_ret)
  106. se_ret = SGX_ERROR_UNEXPECTED;
  107. break;
  108. }
  109. se_ret = sgx_ecc256_create_key_pair(&priv_key, &pub_key, ecc_state);
  110. if (SGX_SUCCESS != se_ret)
  111. {
  112. if(SGX_ERROR_OUT_OF_MEMORY != se_ret)
  113. se_ret = SGX_ERROR_UNEXPECTED;
  114. break;
  115. }
  116. memcpy(&item->a, &priv_key, sizeof(item->a));
  117. memcpy(&item->g_a, &pub_key, sizeof(item->g_a));
  118. memcpy(g_a, &pub_key, sizeof(sgx_ec256_public_t));
  119. item->state = ra_get_gaed;
  120. //clear local private key to defense in depth
  121. memset_s(&priv_key,sizeof(priv_key),0,sizeof(sgx_ec256_private_t));
  122. }while(0);
  123. sgx_spin_unlock(&item->item_lock);
  124. if(ecc_state!=NULL)
  125. sgx_ecc256_close_context(ecc_state);
  126. return se_ret;
  127. }
  128. extern "C" sgx_status_t sgx_ra_proc_msg2_trusted(
  129. sgx_ra_context_t context,
  130. const sgx_ra_msg2_t *p_msg2, //(g_b||spid||quote_type|| KDF_ID ||sign_gb_ga||cmac||sig_rl_size||sig_rl)
  131. const sgx_target_info_t *p_qe_target,
  132. sgx_report_t *p_report,
  133. sgx_quote_nonce_t* p_nonce)
  134. {
  135. sgx_status_t se_ret = SGX_ERROR_UNEXPECTED;
  136. //p_msg2[in] p_qe_target[in] p_report[out] p_nonce[out] in EDL file
  137. if(vector_size(&g_ra_db) <= context
  138. || !p_msg2
  139. || !p_qe_target
  140. || !p_report
  141. || !p_nonce)
  142. return SGX_ERROR_INVALID_PARAMETER;
  143. ra_db_item_t* item = NULL;
  144. if(0 != vector_get(&g_ra_db, context, reinterpret_cast<void**>(&item)) || item == NULL )
  145. return SGX_ERROR_INVALID_PARAMETER;
  146. sgx_ec256_private_t a;
  147. memset(&a, 0, sizeof(a));
  148. // Create gb_ga
  149. sgx_ec256_public_t gb_ga[2];
  150. sgx_ec256_public_t sp_pubkey;
  151. memset(&gb_ga[0], 0, sizeof(gb_ga));
  152. sgx_spin_lock(&item->item_lock);
  153. //sgx_ra_get_ga must have been called
  154. if (item->state != ra_get_gaed)
  155. {
  156. sgx_spin_unlock(&item->item_lock);
  157. return SGX_ERROR_INVALID_STATE;
  158. }
  159. memcpy(&a, &item->a, sizeof(a));
  160. memcpy(&gb_ga[1], &item->g_a, sizeof(gb_ga[1]));
  161. memcpy(&sp_pubkey, &item->sp_pubkey, sizeof(sp_pubkey));
  162. sgx_spin_unlock(&item->item_lock);
  163. memcpy(&gb_ga[0], &p_msg2->g_b, sizeof(gb_ga[0]));
  164. sgx_ecc_state_handle_t ecc_state = NULL;
  165. // ecc_state need to be freed when exit.
  166. se_ret = sgx_ecc256_open_context(&ecc_state);
  167. if (SGX_SUCCESS != se_ret)
  168. {
  169. if(SGX_ERROR_OUT_OF_MEMORY != se_ret)
  170. se_ret = SGX_ERROR_UNEXPECTED;
  171. return se_ret;
  172. }
  173. sgx_ec256_dh_shared_t dh_key;
  174. memset(&dh_key, 0, sizeof(dh_key));
  175. sgx_ec256_public_t* p_msg2_g_b = const_cast<sgx_ec256_public_t*>(&p_msg2->g_b);
  176. se_ret = sgx_ecc256_compute_shared_dhkey(&a,
  177. (sgx_ec256_public_t*)p_msg2_g_b,
  178. (sgx_ec256_dh_shared_t*)&dh_key, ecc_state);
  179. if(SGX_SUCCESS != se_ret)
  180. {
  181. if (SGX_ERROR_OUT_OF_MEMORY != se_ret)
  182. se_ret = SGX_ERROR_UNEXPECTED;
  183. sgx_ecc256_close_context(ecc_state);
  184. return se_ret;
  185. }
  186. // Verify signature of gb_ga
  187. uint8_t result;
  188. sgx_ec256_signature_t* p_msg2_sign_gb_ga = const_cast<sgx_ec256_signature_t*>(&p_msg2->sign_gb_ga);
  189. se_ret = sgx_ecdsa_verify((uint8_t *)&gb_ga, sizeof(gb_ga),
  190. &sp_pubkey,
  191. p_msg2_sign_gb_ga,
  192. &result, ecc_state);
  193. if(SGX_SUCCESS != se_ret)
  194. {
  195. if (SGX_ERROR_OUT_OF_MEMORY != se_ret)
  196. se_ret = SGX_ERROR_UNEXPECTED;
  197. sgx_ecc256_close_context(ecc_state);
  198. return se_ret;
  199. }
  200. if(SGX_EC_VALID != result)
  201. {
  202. sgx_ecc256_close_context(ecc_state);
  203. return SGX_ERROR_INVALID_SIGNATURE;
  204. }
  205. sgx_ec_key_128bit_t smkey = {0};
  206. sgx_ec_key_128bit_t skey = {0};
  207. sgx_ec_key_128bit_t mkey = {0};
  208. sgx_ec_key_128bit_t vkey = {0};
  209. do
  210. {
  211. if (p_msg2->kdf_id == 0x0001)
  212. {
  213. se_ret = derive_key(&dh_key, "SMK", (uint32_t)(sizeof("SMK") -1), &smkey);
  214. if (SGX_SUCCESS != se_ret)
  215. {
  216. if(SGX_ERROR_OUT_OF_MEMORY != se_ret)
  217. se_ret = SGX_ERROR_UNEXPECTED;
  218. break;
  219. }
  220. sgx_cmac_128bit_tag_t mac;
  221. uint32_t maced_size = offsetof(sgx_ra_msg2_t, mac);
  222. se_ret = sgx_rijndael128_cmac_msg(&smkey, (const uint8_t *)p_msg2, maced_size, &mac);
  223. if (SGX_SUCCESS != se_ret)
  224. {
  225. if(SGX_ERROR_OUT_OF_MEMORY != se_ret)
  226. se_ret = SGX_ERROR_UNEXPECTED;
  227. break;
  228. }
  229. //Check mac
  230. if(0 == consttime_memequal(mac, p_msg2->mac, sizeof(mac)))
  231. {
  232. se_ret = SGX_ERROR_MAC_MISMATCH;
  233. break;
  234. }
  235. se_ret = derive_key((sgx_ec256_dh_shared_t*)&dh_key, "SK", (uint32_t )(sizeof("SK") -1), &skey);
  236. if (SGX_SUCCESS != se_ret)
  237. {
  238. if(SGX_ERROR_OUT_OF_MEMORY != se_ret)
  239. se_ret = SGX_ERROR_UNEXPECTED;
  240. break;
  241. }
  242. se_ret = derive_key((sgx_ec256_dh_shared_t*)&dh_key, "MK", (uint32_t)(sizeof("MK") -1), &mkey);
  243. if (SGX_SUCCESS != se_ret)
  244. {
  245. if(SGX_ERROR_OUT_OF_MEMORY != se_ret)
  246. se_ret = SGX_ERROR_UNEXPECTED;
  247. break;
  248. }
  249. se_ret = derive_key((sgx_ec256_dh_shared_t*)&dh_key, "VK", (uint32_t)(sizeof("VK") -1), &vkey);
  250. if (SGX_SUCCESS != se_ret)
  251. {
  252. if(SGX_ERROR_OUT_OF_MEMORY != se_ret)
  253. break;
  254. }
  255. }
  256. else
  257. {
  258. se_ret = SGX_ERROR_KDF_MISMATCH;
  259. break;
  260. }
  261. //create a random nonce
  262. se_ret =sgx_read_rand((uint8_t*)p_nonce, sizeof(sgx_quote_nonce_t));
  263. if (SGX_SUCCESS != se_ret)
  264. {
  265. if(SGX_ERROR_OUT_OF_MEMORY != se_ret)
  266. se_ret = SGX_ERROR_UNEXPECTED;
  267. break;
  268. }
  269. sgx_spin_lock(&item->item_lock);
  270. //sgx_ra_get_ga must have been called
  271. if (item->state != ra_get_gaed)
  272. {
  273. se_ret = SGX_ERROR_INVALID_STATE;
  274. sgx_spin_unlock(&item->item_lock);
  275. break;
  276. }
  277. memcpy(&item->g_b, &p_msg2->g_b, sizeof(item->g_b));
  278. memcpy(&item->smk_key, smkey, sizeof(item->smk_key));
  279. memcpy(&item->sk_key, skey, sizeof(item->sk_key));
  280. memcpy(&item->mk_key, mkey, sizeof(item->mk_key));
  281. memcpy(&item->vk_key, vkey, sizeof(item->vk_key));
  282. memcpy(&item->qe_target, p_qe_target, sizeof(sgx_target_info_t));
  283. memcpy(&item->quote_nonce, p_nonce, sizeof(sgx_quote_nonce_t));
  284. sgx_report_data_t report_data = {{0}};
  285. se_static_assert(sizeof(sgx_report_data_t)>=sizeof(sgx_sha256_hash_t));
  286. // H = SHA256(ga || gb || VK_CMAC)
  287. uint32_t sha256ed_size = offsetof(ra_db_item_t, sp_pubkey);
  288. //report_data is 512bits, H is 256bits. The H is in the lower 256 bits of report data while the higher 256 bits are all zeros.
  289. se_ret = sgx_sha256_msg((uint8_t *)&item->g_a, sha256ed_size,
  290. (sgx_sha256_hash_t *)&report_data);
  291. if(SGX_SUCCESS != se_ret)
  292. {
  293. if (SGX_ERROR_OUT_OF_MEMORY != se_ret)
  294. se_ret = SGX_ERROR_UNEXPECTED;
  295. sgx_spin_unlock(&item->item_lock);
  296. break;
  297. }
  298. //REPORTDATA = H
  299. se_ret = sgx_create_report(p_qe_target, &report_data, p_report);
  300. if (SGX_SUCCESS != se_ret)
  301. {
  302. if(SGX_ERROR_OUT_OF_MEMORY != se_ret)
  303. se_ret = SGX_ERROR_UNEXPECTED;
  304. sgx_spin_unlock(&item->item_lock);
  305. break;
  306. }
  307. item->state = ra_proc_msg2ed;
  308. sgx_spin_unlock(&item->item_lock);
  309. }while(0);
  310. memset_s(&dh_key, sizeof(dh_key), 0, sizeof(dh_key));
  311. sgx_ecc256_close_context(ecc_state);
  312. memset_s(&a, sizeof(sgx_ec256_private_t),0, sizeof(sgx_ec256_private_t));
  313. memset_s(smkey, sizeof(sgx_ec_key_128bit_t),0, sizeof(sgx_ec_key_128bit_t));
  314. memset_s(skey, sizeof(sgx_ec_key_128bit_t),0, sizeof(sgx_ec_key_128bit_t));
  315. memset_s(mkey, sizeof(sgx_ec_key_128bit_t),0, sizeof(sgx_ec_key_128bit_t));
  316. memset_s(vkey, sizeof(sgx_ec_key_128bit_t),0, sizeof(sgx_ec_key_128bit_t));
  317. return se_ret;
  318. }
  319. /* the caller is supposed to fill the quote field in emp_msg3 before calling
  320. * this function.*/
  321. extern "C" sgx_status_t sgx_ra_get_msg3_trusted(
  322. sgx_ra_context_t context,
  323. uint32_t quote_size,
  324. sgx_report_t* qe_report,
  325. sgx_ra_msg3_t *emp_msg3, //(mac||g_a||ps_sec_prop||quote)
  326. uint32_t msg3_size)
  327. {
  328. if(vector_size(&g_ra_db) <= context ||!quote_size || !qe_report || !emp_msg3)
  329. return SGX_ERROR_INVALID_PARAMETER;
  330. ra_db_item_t* item = NULL;
  331. if(0 != vector_get(&g_ra_db, context, reinterpret_cast<void**>(&item)) || item == NULL )
  332. return SGX_ERROR_INVALID_PARAMETER;
  333. //check integer overflow of msg3_size and quote_size
  334. if (UINTPTR_MAX - reinterpret_cast<uintptr_t>(emp_msg3) < msg3_size ||
  335. UINT32_MAX - quote_size < sizeof(sgx_ra_msg3_t) ||
  336. sizeof(sgx_ra_msg3_t) + quote_size != msg3_size)
  337. return SGX_ERROR_INVALID_PARAMETER;
  338. if (!sgx_is_outside_enclave(emp_msg3, msg3_size))
  339. return SGX_ERROR_INVALID_PARAMETER;
  340. sgx_status_t se_ret = SGX_ERROR_UNEXPECTED;
  341. //verify qe report
  342. se_ret = sgx_verify_report(qe_report);
  343. if(se_ret != SGX_SUCCESS)
  344. {
  345. if (SGX_ERROR_MAC_MISMATCH != se_ret &&
  346. SGX_ERROR_OUT_OF_MEMORY != se_ret)
  347. se_ret = SGX_ERROR_UNEXPECTED;
  348. return se_ret;
  349. }
  350. sgx_spin_lock(&item->item_lock);
  351. //sgx_ra_proc_msg2_trusted must have been called
  352. if (item->state != ra_proc_msg2ed)
  353. {
  354. sgx_spin_unlock(&item->item_lock);
  355. return SGX_ERROR_INVALID_STATE;
  356. }
  357. //verify qe_report attributes and mr_enclave same as quoting enclave
  358. if( memcmp( &qe_report->body.attributes, &item->qe_target.attributes, sizeof(sgx_attributes_t)) ||
  359. memcmp( &qe_report->body.mr_enclave, &item->qe_target.mr_enclave, sizeof(sgx_measurement_t)) )
  360. {
  361. sgx_spin_unlock(&item->item_lock);
  362. return SGX_ERROR_INVALID_PARAMETER;
  363. }
  364. sgx_ra_msg3_t msg3_except_quote_in;
  365. sgx_cmac_128bit_key_t smk_key;
  366. memcpy(&msg3_except_quote_in.g_a, &item->g_a, sizeof(msg3_except_quote_in.g_a));
  367. memcpy(&msg3_except_quote_in.ps_sec_prop, &item->ps_sec_prop,
  368. sizeof(msg3_except_quote_in.ps_sec_prop));
  369. memcpy(&smk_key, &item->smk_key, sizeof(smk_key));
  370. sgx_spin_unlock(&item->item_lock);
  371. sgx_sha_state_handle_t sha_handle = NULL;
  372. sgx_cmac_state_handle_t cmac_handle = NULL;
  373. //SHA256(NONCE || emp_quote)
  374. sgx_sha256_hash_t hash = {0};
  375. se_ret = sgx_sha256_init(&sha_handle);
  376. if (SGX_SUCCESS != se_ret)
  377. {
  378. if(SGX_ERROR_OUT_OF_MEMORY != se_ret)
  379. se_ret = SGX_ERROR_UNEXPECTED;
  380. return se_ret;
  381. }
  382. if (NULL == sha_handle)
  383. {
  384. return SGX_ERROR_UNEXPECTED;
  385. }
  386. do
  387. {
  388. se_ret = sgx_sha256_update((uint8_t *)&item->quote_nonce,
  389. sizeof(item->quote_nonce),
  390. sha_handle);
  391. if (SGX_SUCCESS != se_ret)
  392. {
  393. if(SGX_ERROR_OUT_OF_MEMORY != se_ret)
  394. se_ret = SGX_ERROR_UNEXPECTED;
  395. break;
  396. }
  397. //cmac M := ga || PS_SEC_PROP_DESC(all zero if unused) ||emp_quote
  398. sgx_cmac_128bit_tag_t mac;
  399. se_ret = sgx_cmac128_init(&smk_key, &cmac_handle);
  400. if (SGX_SUCCESS != se_ret)
  401. {
  402. if(SGX_ERROR_OUT_OF_MEMORY != se_ret)
  403. se_ret = SGX_ERROR_UNEXPECTED;
  404. break;
  405. }
  406. if (NULL == cmac_handle)
  407. {
  408. se_ret = SGX_ERROR_UNEXPECTED;
  409. break;
  410. }
  411. se_ret = sgx_cmac128_update((uint8_t*)&msg3_except_quote_in.g_a,
  412. sizeof(msg3_except_quote_in.g_a), cmac_handle);
  413. if (SGX_SUCCESS != se_ret)
  414. {
  415. if(SGX_ERROR_OUT_OF_MEMORY != se_ret)
  416. se_ret = SGX_ERROR_UNEXPECTED;
  417. break;
  418. }
  419. se_ret = sgx_cmac128_update((uint8_t*)&msg3_except_quote_in.ps_sec_prop,
  420. sizeof(msg3_except_quote_in.ps_sec_prop), cmac_handle);
  421. if (SGX_SUCCESS != se_ret)
  422. {
  423. if(SGX_ERROR_OUT_OF_MEMORY != se_ret)
  424. se_ret = SGX_ERROR_UNEXPECTED;
  425. break;
  426. }
  427. // sha256 and cmac quote
  428. uint8_t quote_piece[32];
  429. const uint8_t* emp_quote_piecemeal = emp_msg3->quote;
  430. uint32_t quote_piece_size = static_cast<uint32_t>(sizeof(quote_piece));
  431. while (emp_quote_piecemeal < emp_msg3->quote + quote_size)
  432. {
  433. //caculate size of one piece, the size of them are sizeof(quote_piece) except for the last one.
  434. if (static_cast<uint32_t>(emp_msg3->quote + quote_size - emp_quote_piecemeal) < quote_piece_size)
  435. quote_piece_size = static_cast<uint32_t>(emp_msg3->quote - emp_quote_piecemeal) + quote_size ;
  436. memcpy(quote_piece, emp_quote_piecemeal, quote_piece_size);
  437. se_ret = sgx_sha256_update(quote_piece,
  438. quote_piece_size,
  439. sha_handle);
  440. if (SGX_SUCCESS != se_ret)
  441. {
  442. if(SGX_ERROR_OUT_OF_MEMORY != se_ret)
  443. se_ret = SGX_ERROR_UNEXPECTED;
  444. break;
  445. }
  446. se_ret = sgx_cmac128_update(quote_piece,
  447. quote_piece_size,
  448. cmac_handle);
  449. if (SGX_SUCCESS != se_ret)
  450. {
  451. if(SGX_ERROR_OUT_OF_MEMORY != se_ret)
  452. se_ret = SGX_ERROR_UNEXPECTED;
  453. break;
  454. }
  455. emp_quote_piecemeal += sizeof(quote_piece);
  456. }
  457. ERROR_BREAK(se_ret);
  458. //get sha256 hash value
  459. se_ret = sgx_sha256_get_hash(sha_handle, &hash);
  460. if (SGX_SUCCESS != se_ret)
  461. {
  462. if(SGX_ERROR_OUT_OF_MEMORY != se_ret)
  463. se_ret = SGX_ERROR_UNEXPECTED;
  464. break;
  465. }
  466. //get cmac value
  467. se_ret = sgx_cmac128_final(cmac_handle, &mac);
  468. if (SGX_SUCCESS != se_ret)
  469. {
  470. if(SGX_ERROR_OUT_OF_MEMORY != se_ret)
  471. se_ret = SGX_ERROR_UNEXPECTED;
  472. break;
  473. }
  474. //verify qe_report->body.report_data == SHA256(NONCE || emp_quote)
  475. if(0 != memcmp(&qe_report->body.report_data, &hash, sizeof(hash)))
  476. {
  477. se_ret = SGX_ERROR_MAC_MISMATCH;
  478. break;
  479. }
  480. memcpy(&msg3_except_quote_in.mac, mac, sizeof(mac));
  481. memcpy(emp_msg3, &msg3_except_quote_in, offsetof(sgx_ra_msg3_t, quote));
  482. se_ret = SGX_SUCCESS;
  483. }while(0);
  484. memset_s(&smk_key, sizeof(smk_key), 0, sizeof(smk_key));
  485. (void)sgx_sha256_close(sha_handle);
  486. if(cmac_handle != NULL)
  487. sgx_cmac128_close(cmac_handle);
  488. return se_ret;
  489. }
  490. // TKE interface for isv enclaves
  491. sgx_status_t sgx_ra_init(
  492. const sgx_ec256_public_t *p_pub_key,
  493. int b_pse,
  494. sgx_ra_context_t *p_context)
  495. {
  496. int valid = 0;
  497. sgx_status_t ret = SGX_SUCCESS;
  498. sgx_ecc_state_handle_t ecc_state = NULL;
  499. if(!p_pub_key || !p_context)
  500. return SGX_ERROR_INVALID_PARAMETER;
  501. if(!sgx_is_within_enclave(p_pub_key, sizeof(sgx_ec256_public_t)))
  502. return SGX_ERROR_INVALID_PARAMETER;
  503. ret = sgx_ecc256_open_context(&ecc_state);
  504. if(SGX_SUCCESS != ret)
  505. {
  506. if(SGX_ERROR_OUT_OF_MEMORY != ret)
  507. ret = SGX_ERROR_UNEXPECTED;
  508. return ret;
  509. }
  510. ret = sgx_ecc256_check_point((const sgx_ec256_public_t *)p_pub_key,
  511. ecc_state, &valid);
  512. if(SGX_SUCCESS != ret)
  513. {
  514. if(SGX_ERROR_OUT_OF_MEMORY != ret)
  515. ret = SGX_ERROR_UNEXPECTED;
  516. sgx_ecc256_close_context(ecc_state);
  517. return ret;
  518. }
  519. if(!valid)
  520. {
  521. sgx_ecc256_close_context(ecc_state);
  522. return SGX_ERROR_INVALID_PARAMETER;
  523. }
  524. sgx_ecc256_close_context(ecc_state);
  525. //add new item to g_ra_db
  526. ra_db_item_t* new_item = (ra_db_item_t*)malloc(sizeof(ra_db_item_t));
  527. if (!new_item)
  528. {
  529. return SGX_ERROR_OUT_OF_MEMORY;
  530. }
  531. memset(new_item,0, sizeof(ra_db_item_t));
  532. memcpy(&new_item->sp_pubkey, p_pub_key, sizeof(new_item->sp_pubkey));
  533. if(b_pse)
  534. {
  535. //sgx_create_pse_session() must have been called
  536. ret = sgx_get_ps_sec_prop(&new_item->ps_sec_prop);
  537. if (ret!=SGX_SUCCESS)
  538. {
  539. SAFE_FREE(new_item);
  540. return ret;
  541. }
  542. }
  543. new_item->state = ra_inited;
  544. //find first empty slot in g_ra_db
  545. int first_empty = -1;
  546. ra_db_item_t* item = NULL;
  547. sgx_spin_lock(&g_ra_db_lock);
  548. uint32_t size = vector_size(&g_ra_db);
  549. for (uint32_t i = 0; i < size; i++)
  550. {
  551. if(0 != vector_get(&g_ra_db, i, reinterpret_cast<void**>(&item)))
  552. {
  553. sgx_spin_unlock(&g_ra_db_lock);
  554. SAFE_FREE(new_item);
  555. return SGX_ERROR_UNEXPECTED;
  556. }
  557. if(item == NULL)
  558. {
  559. first_empty = i;
  560. break;
  561. }
  562. }
  563. //if there is a empty slot, use it
  564. if (first_empty >= 0)
  565. {
  566. errno_t vret = vector_set(&g_ra_db, first_empty, new_item);
  567. UNUSED(vret);
  568. assert(vret == 0);
  569. *p_context = first_empty;
  570. }
  571. //if there are no empty slots, add a new item to g_ra_db
  572. else
  573. {
  574. if(size >= INT32_MAX)
  575. {
  576. //overflow
  577. sgx_spin_unlock(&g_ra_db_lock);
  578. SAFE_FREE(new_item);
  579. return SGX_ERROR_OUT_OF_MEMORY;
  580. }
  581. if(0 != vector_push_back(&g_ra_db, new_item))
  582. {
  583. sgx_spin_unlock(&g_ra_db_lock);
  584. SAFE_FREE(new_item);
  585. return SGX_ERROR_OUT_OF_MEMORY;
  586. }
  587. *p_context = size;
  588. }
  589. sgx_spin_unlock(&g_ra_db_lock);
  590. return SGX_SUCCESS;
  591. }
  592. // TKE interface for isv enclaves
  593. sgx_status_t sgx_ra_get_keys(
  594. sgx_ra_context_t context,
  595. sgx_ra_key_type_t type,
  596. sgx_ra_key_128_t *p_key)
  597. {
  598. if(vector_size(&g_ra_db) <= context || !p_key)
  599. return SGX_ERROR_INVALID_PARAMETER;
  600. ra_db_item_t* item = NULL;
  601. if(0 != vector_get(&g_ra_db, context, reinterpret_cast<void**>(&item)) || item == NULL )
  602. return SGX_ERROR_INVALID_PARAMETER;
  603. if(!sgx_is_within_enclave(p_key, sizeof(sgx_ra_key_128_t)))
  604. return SGX_ERROR_INVALID_PARAMETER;
  605. sgx_status_t ret = SGX_SUCCESS;
  606. sgx_spin_lock(&item->item_lock);
  607. //sgx_ra_proc_msg2_trusted fill the keys, so keys are available after it's called.
  608. if (item->state != ra_proc_msg2ed)
  609. ret = SGX_ERROR_INVALID_STATE;
  610. else if(SGX_RA_KEY_MK == type)
  611. memcpy(p_key, item->mk_key, sizeof(sgx_ra_key_128_t));
  612. else if(SGX_RA_KEY_SK == type)
  613. memcpy(p_key, item->sk_key, sizeof(sgx_ra_key_128_t));
  614. else
  615. ret = SGX_ERROR_INVALID_PARAMETER;
  616. sgx_spin_unlock(&item->item_lock);
  617. return ret;
  618. }
  619. // TKE interface for isv enclaves
  620. sgx_status_t SGXAPI sgx_ra_close(
  621. sgx_ra_context_t context)
  622. {
  623. if(vector_size(&g_ra_db) <= context)
  624. return SGX_ERROR_INVALID_PARAMETER;
  625. ra_db_item_t* item = NULL;
  626. if(0 != vector_get(&g_ra_db, context, reinterpret_cast<void**>(&item)) || item == NULL )
  627. return SGX_ERROR_INVALID_PARAMETER;
  628. sgx_spin_lock(&g_ra_db_lock);
  629. //safe clear private key and RA key before free memory to defense in depth
  630. memset_s(&item->a,sizeof(item->a),0,sizeof(sgx_ec256_private_t));
  631. memset_s(&item->vk_key,sizeof(item->vk_key),0,sizeof(sgx_ec_key_128bit_t));
  632. memset_s(&item->mk_key,sizeof(item->mk_key),0,sizeof(sgx_ec_key_128bit_t));
  633. memset_s(&item->sk_key,sizeof(item->sk_key),0,sizeof(sgx_ec_key_128bit_t));
  634. memset_s(&item->smk_key,sizeof(item->smk_key),0,sizeof(sgx_ec_key_128bit_t));
  635. SAFE_FREE(item);
  636. vector_set(&g_ra_db, context, NULL);
  637. sgx_spin_unlock(&g_ra_db_lock);
  638. return SGX_SUCCESS;
  639. }