tkey_exchange.cpp 26 KB

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