quoting_enclave.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  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. #ifndef __linux__
  32. #include "targetver.h"
  33. #endif
  34. // Exclude rarely-used stuff from Windows headers
  35. //#define WIN32_LEAN_AND_MEAN
  36. // Windows Header Files:
  37. //#include <windows.h>
  38. #include "se_types.h"
  39. #include "sgx_quote.h"
  40. #include "aeerror.h"
  41. #include "sgx_tseal.h"
  42. #include "epid_pve_type.h"
  43. #include "sgx_utils.h"
  44. #include "ipp_wrapper.h"
  45. #include "epid/common/errors.h"
  46. #include "ae_ipp.h"
  47. #include "epid/member/api.h"
  48. #include "quoting_enclave_t.c"
  49. #include "sgx_tcrypto.h"
  50. #include "se_sig_rl.h"
  51. #include "se_ecdsa_verify_internal.h"
  52. #include "se_quote_internal.h"
  53. #include "pve_qe_common.h"
  54. #include "byte_order.h"
  55. #include "util.h"
  56. #include "qsdk_pub.hh"
  57. #include "isk_pub.hh"
  58. #if !defined(SWAP_4BYTES)
  59. #define SWAP_4BYTES(u32) \
  60. ((uint32_t)(((((unsigned char*)&(u32))[0]) << 24) \
  61. + ((((unsigned char*)&(u32))[1]) << 16) \
  62. + ((((unsigned char*)&(u32))[2]) << 8) \
  63. + (((unsigned char*)&(u32))[3])))
  64. #endif
  65. // Start from 1, and it's little endian.
  66. #define QE_QUOTE_VERSION 2
  67. #define QE_AES_IV_SIZE 12
  68. #define QE_AES_KEY_SIZE 16
  69. #define QE_OAEP_SEED_SIZE 32
  70. /* One field in sgx_quote_t(signature_len) is not part of the quote_body need
  71. to be signed by EPID. So we need to minus sizeof(uint32_t). */
  72. #define QE_QUOTE_BODY_SIZE (sizeof(sgx_quote_t) - sizeof(uint32_t))
  73. /*
  74. * An internal function used to verify EPID Blob, get EPID Group Cert
  75. * and get EPID context, at the same time, you can check whether EPID blob has
  76. * been resealed.
  77. *
  78. * @param p_blob[in, out] Pointer to EPID Blob.
  79. * @param p_is_resealed[out] Whether the EPID Blob has been resealed.
  80. * @param create_context[in] Flag indicates create EPID context or not.
  81. * @param plaintext_epid_data[out] Used to get the plaintext part of epid blob
  82. * @param pp_epid_context[out] Used to get the pointer of the EPID context.
  83. * @return ae_error_t AE_SUCCESS or other error cases.
  84. */
  85. static ae_error_t verify_blob_internal(
  86. uint8_t *p_blob,
  87. uint32_t blob_size,
  88. uint8_t *p_is_resealed,
  89. uint32_t create_context,
  90. se_plaintext_epid_data_sdk_t& plaintext_epid_data,
  91. MemberCtx **pp_epid_context)
  92. {
  93. sgx_status_t se_ret = SGX_SUCCESS;
  94. uint8_t resealed= FALSE;
  95. se_secret_epid_data_sdk_t secret_epid_data;
  96. se_plaintext_epid_data_sik_t plaintext_old_format;
  97. uint32_t plaintext_length;
  98. int is_old_format = 0;
  99. uint32_t decryptedtext_length = sizeof(secret_epid_data);
  100. sgx_sealed_data_t *p_epid_blob = (sgx_sealed_data_t *)p_blob;
  101. uint8_t local_epid_blob[sizeof(*p_epid_blob)
  102. + sizeof(secret_epid_data)
  103. + sizeof(plaintext_epid_data)]
  104. = {0};
  105. // We will use plaintext_old_format as buffer to hold the output of sgx_unseal_data.
  106. // It can be se_plaintext_epid_data_sik_t or se_plaintext_epid_data_sdk_t.
  107. // We use the static assert to reassure plaintext_old_format is big enough.
  108. // If someone changed the definition of these 2 structures and break current assumption,
  109. // it will report error in compile time.
  110. se_static_assert(sizeof(plaintext_old_format)>=sizeof(plaintext_epid_data));
  111. if (sgx_get_encrypt_txt_len(p_epid_blob) != sizeof(se_secret_epid_data_sdk_t)&&
  112. sgx_get_encrypt_txt_len(p_epid_blob) != sizeof(se_secret_epid_data_sik_t)){
  113. return QE_EPIDBLOB_ERROR;
  114. }
  115. plaintext_length = sgx_get_add_mac_txt_len(p_epid_blob);
  116. if(plaintext_length != sizeof(se_plaintext_epid_data_sik_t)&&
  117. plaintext_length != sizeof(se_plaintext_epid_data_sdk_t))
  118. {
  119. return QE_EPIDBLOB_ERROR;
  120. }
  121. memset(&secret_epid_data, 0, sizeof(secret_epid_data));
  122. memset(&plaintext_epid_data, 0, sizeof(plaintext_epid_data));
  123. memset(&plaintext_old_format, 0, sizeof(plaintext_old_format));
  124. se_ret = sgx_unseal_data(p_epid_blob,
  125. (uint8_t *)&plaintext_old_format, // The unsealed plaintext can be old or new format, the buffer is defined as old format because it is bigger
  126. &plaintext_length,
  127. (uint8_t *)&secret_epid_data,
  128. &decryptedtext_length);
  129. if(SGX_SUCCESS != se_ret)
  130. {
  131. memset_s(&secret_epid_data, sizeof(secret_epid_data), 0,
  132. sizeof(secret_epid_data));
  133. return QE_EPIDBLOB_ERROR;
  134. }
  135. //QE will support both epid blob with/without member precomputation
  136. //If the epid blob without member precomputation is used, QE will generate member precomputation and reseal epid blob
  137. if((plaintext_old_format.seal_blob_type != PVE_SEAL_EPID_KEY_BLOB)
  138. || (plaintext_old_format.epid_key_version != EPID_KEY_BLOB_VERSION_SDK&&
  139. plaintext_old_format.epid_key_version != EPID_KEY_BLOB_VERSION_SIK )) //blob_type and key_version are always first two fields of plaintext in both format
  140. {
  141. memset_s(&secret_epid_data, sizeof(secret_epid_data), 0,
  142. sizeof(secret_epid_data));
  143. return QE_EPIDBLOB_ERROR;
  144. }
  145. // Only 2 combinations are legitimate for the tuple epid_key_version|decryptedtext_length|plaintext_length:
  146. // EPID_KEY_BLOB_VERSION_SIK|sizeof(se_secret_epid_data_sik_t)|sizeof(se_plaintext_epid_data_sik_t)
  147. // EPID_KEY_BLOB_VERSION_SDK|sizeof(se_secret_epid_data_sdk_t)|sizeof(se_plaintext_epid_data_sdk_t)
  148. if((plaintext_old_format.epid_key_version == EPID_KEY_BLOB_VERSION_SIK &&
  149. (decryptedtext_length!=sizeof(se_secret_epid_data_sik_t)||plaintext_length!=sizeof(se_plaintext_epid_data_sik_t)))||
  150. (plaintext_old_format.epid_key_version == EPID_KEY_BLOB_VERSION_SDK &&
  151. (decryptedtext_length!=sizeof(se_secret_epid_data_sdk_t)||plaintext_length!=sizeof(se_plaintext_epid_data_sdk_t)))){
  152. memset_s(&secret_epid_data, sizeof(secret_epid_data), 0,
  153. sizeof(secret_epid_data));
  154. return QE_EPIDBLOB_ERROR;
  155. }
  156. // If the input epid blob is in sik format, we will upgrade it to sdk version
  157. if(plaintext_old_format.epid_key_version == EPID_KEY_BLOB_VERSION_SIK){
  158. plaintext_epid_data.seal_blob_type = PVE_SEAL_EPID_KEY_BLOB;
  159. plaintext_epid_data.epid_key_version = EPID_KEY_BLOB_VERSION_SDK;
  160. memcpy(&plaintext_epid_data.equiv_cpu_svn, &plaintext_old_format.equiv_cpu_svn, sizeof(plaintext_old_format.equiv_cpu_svn));
  161. memcpy(&plaintext_epid_data.equiv_pve_isv_svn, &plaintext_old_format.equiv_pve_isv_svn, sizeof(plaintext_old_format.equiv_pve_isv_svn));
  162. memcpy(&plaintext_epid_data.epid_group_cert, &plaintext_old_format.epid_group_cert, sizeof(plaintext_old_format.epid_group_cert));
  163. memcpy(&plaintext_epid_data.qsdk_exp, &plaintext_old_format.qsdk_exp, sizeof(plaintext_old_format.qsdk_exp));
  164. memcpy(&plaintext_epid_data.qsdk_mod, &plaintext_old_format.qsdk_mod, sizeof(plaintext_old_format.qsdk_mod));
  165. memcpy(&plaintext_epid_data.epid_sk, &plaintext_old_format.epid_sk, sizeof(plaintext_old_format.epid_sk));
  166. plaintext_epid_data.xeid = plaintext_old_format.xeid;
  167. memset(&secret_epid_data.member_precomp_data, 0, sizeof(secret_epid_data.member_precomp_data));
  168. is_old_format = 1;
  169. //PrivKey of secret_epid_data are both in offset 0 so that we need not move it
  170. }else{//SDK version format
  171. memcpy(&plaintext_epid_data, &plaintext_old_format, sizeof(plaintext_epid_data));
  172. }
  173. /* Create report to get current cpu_svn and isv_svn. */
  174. sgx_report_t report;
  175. memset(&report, 0, sizeof(report));
  176. se_ret = sgx_create_report(NULL, NULL, &report);
  177. if(SGX_SUCCESS != se_ret)
  178. {
  179. memset_s(&secret_epid_data, sizeof(secret_epid_data), 0,
  180. sizeof(secret_epid_data));
  181. return QE_UNEXPECTED_ERROR;
  182. }
  183. /* Get the random function pointer. */
  184. BitSupplier rand_func = epid_random_func;
  185. /* Create EPID member context if required. PvE is responsible for verifying
  186. the Cert signature before storing them in the EPID blob. */
  187. if(create_context)
  188. {
  189. EpidStatus epid_ret = kEpidNoErr;
  190. epid_ret = EpidMemberCreate(
  191. &(plaintext_epid_data.epid_group_cert),
  192. (PrivKey*)&(secret_epid_data.epid_private_key),
  193. is_old_format?NULL:&secret_epid_data.member_precomp_data,
  194. rand_func,
  195. NULL,
  196. pp_epid_context);
  197. if(kEpidNoErr != epid_ret)
  198. {
  199. memset_s(&secret_epid_data, sizeof(secret_epid_data), 0,
  200. sizeof(secret_epid_data));
  201. // Make sure the pointer is not pointered to garbage. And according
  202. // to EPID SDK 2.0 API document of EpidMemberCreate, it will not return
  203. // error with memory allocated. So set the pointer to NULL will not
  204. // cause memory leak here.
  205. *pp_epid_context = NULL;
  206. return QE_UNEXPECTED_ERROR;
  207. }
  208. epid_ret = EpidMemberSetHashAlg(*pp_epid_context, kSha256);
  209. if(kEpidNoErr != epid_ret)
  210. {
  211. EpidMemberDelete(pp_epid_context);
  212. memset_s(&secret_epid_data, sizeof(secret_epid_data), 0 ,
  213. sizeof(secret_epid_data));
  214. *pp_epid_context = NULL;
  215. return QE_UNEXPECTED_ERROR;
  216. }
  217. if(is_old_format)
  218. {
  219. epid_ret = EpidMemberWritePrecomp(*pp_epid_context, &secret_epid_data.member_precomp_data);
  220. if(kEpidNoErr != epid_ret)
  221. {
  222. EpidMemberDelete(pp_epid_context);
  223. memset_s(&secret_epid_data, sizeof(secret_epid_data), 0,
  224. sizeof(secret_epid_data));
  225. *pp_epid_context = NULL;
  226. return QE_UNEXPECTED_ERROR;
  227. }
  228. }
  229. }
  230. /* Update the Key Blob using the SEAL Key for the current TCB if the TCB is
  231. upgraded after the Key Blob is generated. Here memcmp cpu_svns might be
  232. different even though they're actually same, but for defense in depth we
  233. will keep this comparison here. And we will also upgrade old format EPID
  234. blob to new format here. */
  235. if((memcmp(&report.body.cpu_svn, &p_epid_blob->key_request.cpu_svn,
  236. sizeof(report.body.cpu_svn)))
  237. || (report.body.isv_svn != p_epid_blob->key_request.isv_svn)
  238. || plaintext_old_format.epid_key_version == EPID_KEY_BLOB_VERSION_SIK)
  239. {
  240. se_ret = sgx_seal_data(sizeof(plaintext_epid_data),
  241. (uint8_t *)&plaintext_epid_data,
  242. sizeof(secret_epid_data),
  243. (uint8_t *)&secret_epid_data,
  244. SGX_TRUSTED_EPID_BLOB_SIZE_SDK,
  245. (sgx_sealed_data_t *)local_epid_blob);
  246. if(SGX_SUCCESS != se_ret)
  247. {
  248. // Clear the output buffer to make sure nothing leaks.
  249. memset_s(&secret_epid_data, sizeof(secret_epid_data), 0,
  250. sizeof(secret_epid_data));
  251. // *pp_epid_context contains pointers, so we cannot simply clear it.
  252. if(pp_epid_context)
  253. {
  254. EpidMemberDelete(pp_epid_context);
  255. *pp_epid_context = NULL;
  256. }
  257. return QE_UNEXPECTED_ERROR;
  258. }
  259. memcpy(p_epid_blob, local_epid_blob, blob_size);
  260. resealed = TRUE;
  261. }
  262. memset_s(&secret_epid_data, sizeof(secret_epid_data), 0,
  263. sizeof(secret_epid_data));
  264. *p_is_resealed = resealed;
  265. return AE_SUCCESS;
  266. }
  267. /*
  268. * External function used to verify EPID Blob and check whether QE has
  269. * been updated.
  270. *
  271. * @param p_blob[in, out] Pointer to EPID Blob.
  272. * @param blob_size[in] The size of EPID Blob, in bytes.
  273. * @param p_is_resealed[out] Whether the EPID Blob is resealed within this function call.
  274. * @return uint32_t AE_SUCCESS or other error cases.
  275. */
  276. uint32_t verify_blob(
  277. uint8_t *p_blob,
  278. uint32_t blob_size,
  279. uint8_t *p_is_resealed)
  280. {
  281. se_plaintext_epid_data_sdk_t plain_text;
  282. /* Actually, some cases here will be checked with code generated by
  283. edger8r. Here we just want to defend in depth. */
  284. if(NULL == p_blob || NULL == p_is_resealed)
  285. return QE_PARAMETER_ERROR;
  286. if(SGX_TRUSTED_EPID_BLOB_SIZE_SDK != blob_size)
  287. return QE_PARAMETER_ERROR;
  288. if(!sgx_is_within_enclave(p_blob, blob_size))
  289. return QE_PARAMETER_ERROR;
  290. return verify_blob_internal(p_blob, blob_size,
  291. p_is_resealed, FALSE, plain_text, NULL);
  292. }
  293. /*
  294. * An internal function used to sign the EPID signature on the quote body.
  295. * Prefix "emp_" means it is a pointer points memory outside enclave.
  296. *
  297. * For quote with SIG-RL
  298. * |--------------------------------------------------------------------|
  299. * |sgx_quote_t|wrap_key_t|iv|payload_size|basic_sig|rl_ver|n2|nrp..|mac|
  300. * |--------------------------------------------------------------------|
  301. * For quote without SIG-RL
  302. * |--------------------------------------------------------------|
  303. * |sgx_quote_t|wrap_key_t|iv|payload_size|basic_sig|rl_ver|n2|mac|
  304. * |--------------------------------------------------------------|
  305. *
  306. * @param p_epid_context[in] Pointer to the EPID context.
  307. * @param plaintext[in] Reference to the plain text part of EPID blob.
  308. * @param p_basename[in] The pointer to basename.
  309. * @param emp_sig_rl_entries[in] The pointer to SIG-RL entries.
  310. * @param p_sig_rl_header[in] The header of SIG-RL, within EPC.
  311. * @param p_sig_rl_signature[in] The ecdsa signature of SIG-RL, within EPC.
  312. * @param p_enclave_report[in] The input isv report.
  313. * @param p_nonce[in] The input nonce.
  314. * @param p_qe_report[out] The output buffer for qe_report.
  315. * @param emp_quote[out] The output buffer for quote.
  316. * @param p_quote_body[in] The quote body in EPC.
  317. * @param sign_size[in] size of the signature.
  318. * @return ae_error_t AE_SUCCESS for success, otherwise for errors.
  319. */
  320. static ae_error_t qe_epid_sign(
  321. MemberCtx *p_epid_context,
  322. const se_plaintext_epid_data_sdk_t& plaintext,
  323. const sgx_basename_t *p_basename,
  324. const SigRlEntry *emp_sig_rl_entries,
  325. se_sig_rl_t *p_sig_rl_header,
  326. sgx_ec256_signature_t *p_sig_rl_signature,
  327. const sgx_report_t *p_enclave_report,
  328. const sgx_quote_nonce_t *p_nonce,
  329. sgx_report_t *p_qe_report,
  330. uint8_t *emp_quote,
  331. const sgx_quote_t *p_quote_body,
  332. uint32_t sign_size)
  333. {
  334. ae_error_t ret = AE_SUCCESS;
  335. IppStatus ipp_ret = ippStsNoErr;
  336. sgx_status_t se_ret = SGX_SUCCESS;
  337. EpidStatus epid_ret = kEpidNoErr;
  338. se_wrap_key_t wrap_key;
  339. BasicSignature basic_sig;
  340. BasicSignature encrypted_basic_sig;
  341. uint8_t aes_iv[QUOTE_IV_SIZE] = {0};
  342. uint8_t aes_key[QE_AES_KEY_SIZE] = {0};
  343. uint8_t aes_tag[SGX_SEAL_TAG_SIZE] = {0};
  344. Ipp8u seeds[QE_OAEP_SEED_SIZE] = {0};
  345. sgx_report_data_t qe_report_data = {{0}};
  346. sgx_target_info_t report_target;
  347. sgx_ec256_public_t ec_pub_key;
  348. se_ae_ecdsa_hash_t sig_rl_hash = {{0}};
  349. IppECResult ec_result = ippECValid ;
  350. int aes_context_size = 0;
  351. sgx_sha_state_handle_t sha_context = NULL;
  352. sgx_sha_state_handle_t sha_quote_context = NULL;
  353. IppsAES_GCMState *aes_context = NULL;
  354. IppsRSAPublicKeyState *pub_key = NULL;
  355. int pub_key_size = 0;
  356. uint8_t* pub_key_buffer = NULL;
  357. IppsECCPState *p_ecp = NULL;
  358. memset(&wrap_key, 0, sizeof(wrap_key));
  359. memset(&basic_sig, 0, sizeof(basic_sig));
  360. memset(&encrypted_basic_sig, 0, sizeof(encrypted_basic_sig));
  361. memset(&report_target, 0, sizeof(report_target));
  362. memset(&ec_pub_key, 0, sizeof(ec_pub_key));
  363. se_encrypted_sign_t *emp_p = (se_encrypted_sign_t *)
  364. (((sgx_quote_t *)emp_quote)->signature);
  365. uint8_t* emp_nr = NULL;
  366. uint32_t match = FALSE;
  367. /* Sign the quote body and get the basic signature*/
  368. epid_ret = EpidSignBasic(p_epid_context,
  369. (uint8_t *)const_cast<sgx_quote_t *>(p_quote_body),
  370. (uint32_t)QE_QUOTE_BODY_SIZE,
  371. (uint8_t *)const_cast<sgx_basename_t *>(p_basename),
  372. sizeof(*p_basename),
  373. &basic_sig);
  374. if(kEpidNoErr != epid_ret)
  375. {
  376. ret = QE_UNEXPECTED_ERROR;
  377. goto CLEANUP;
  378. }
  379. /* Prepare the context for SHA256 of quote. */
  380. if(p_qe_report)
  381. {
  382. se_ret = sgx_sha256_init(&sha_quote_context);
  383. if(SGX_SUCCESS != se_ret)
  384. {
  385. ret = QE_UNEXPECTED_ERROR;
  386. goto CLEANUP;
  387. }
  388. // Update hash for nonce.
  389. se_ret = sgx_sha256_update((uint8_t *)const_cast<sgx_quote_nonce_t *>(p_nonce),
  390. sizeof(*p_nonce),
  391. sha_quote_context);
  392. if(SGX_SUCCESS != se_ret)
  393. {
  394. ret = QE_UNEXPECTED_ERROR;
  395. goto CLEANUP;
  396. }
  397. // Update hash for the first part of quote.
  398. se_ret = sgx_sha256_update((uint8_t *)const_cast<sgx_quote_t *>(p_quote_body),
  399. sizeof(*p_quote_body),
  400. sha_quote_context);
  401. if(SGX_SUCCESS != se_ret)
  402. {
  403. ret = QE_UNEXPECTED_ERROR;
  404. goto CLEANUP;
  405. }
  406. }
  407. /* Prepare the context for SHA256 and start calculate the hash of header
  408. * of SIG-RL. */
  409. if(emp_sig_rl_entries)
  410. {
  411. se_ret = sgx_sha256_init(&sha_context);
  412. if(SGX_SUCCESS != se_ret)
  413. {
  414. ret = QE_UNEXPECTED_ERROR;
  415. goto CLEANUP;
  416. }
  417. /* Calculate the hash of SIG-RL header. */
  418. se_ret = sgx_sha256_update((Ipp8u *)p_sig_rl_header,
  419. (uint32_t)(sizeof(se_sig_rl_t) - sizeof(SigRlEntry)),
  420. sha_context);
  421. if(SGX_SUCCESS != se_ret)
  422. {
  423. ret = QE_UNEXPECTED_ERROR;
  424. goto CLEANUP;
  425. }
  426. }
  427. // Start encrypt the signature.
  428. ipp_ret = ippsAES_GCMGetSize(&aes_context_size);
  429. if(ipp_ret != ippStsNoErr){
  430. ret = QE_UNEXPECTED_ERROR;
  431. goto CLEANUP;
  432. }
  433. aes_context = (IppsAES_GCMState *)malloc(aes_context_size);
  434. if(NULL == aes_context)
  435. {
  436. ret = QE_UNEXPECTED_ERROR;
  437. goto CLEANUP;
  438. }
  439. /* Get the random wrap key */
  440. se_ret = sgx_read_rand(aes_key, sizeof(aes_key));
  441. if(SGX_SUCCESS != se_ret)
  442. {
  443. ret = QE_UNEXPECTED_ERROR;
  444. goto CLEANUP;
  445. }
  446. /* Copy the hash of wrap key into output buffer. */
  447. se_static_assert(sizeof(wrap_key.key_hash) == sizeof(sgx_sha256_hash_t));
  448. se_ret = sgx_sha256_msg(aes_key, sizeof(aes_key),
  449. (sgx_sha256_hash_t *)wrap_key.key_hash);
  450. if(SGX_SUCCESS != se_ret)
  451. {
  452. ret = QE_UNEXPECTED_ERROR;
  453. goto CLEANUP;
  454. }
  455. //Start encrypt the wrap key by RSA IPP algorithm.
  456. ipp_ret = create_rsa_pub_key(sizeof(g_qsdk_pub_key_n),
  457. sizeof(g_qsdk_pub_key_e),
  458. g_qsdk_pub_key_n,
  459. g_qsdk_pub_key_e,
  460. &pub_key);
  461. if(ipp_ret != ippStsNoErr)
  462. {
  463. ret = QE_UNEXPECTED_ERROR;
  464. goto CLEANUP;
  465. }
  466. se_ret = sgx_read_rand(seeds, sizeof(seeds));
  467. if(SGX_SUCCESS != se_ret)
  468. {
  469. ret = QE_UNEXPECTED_ERROR;
  470. goto CLEANUP;
  471. }
  472. ipp_ret = ippsRSA_GetBufferSizePublicKey(&pub_key_size, pub_key);
  473. if (ipp_ret != ippStsNoErr)
  474. {
  475. ret = QE_UNEXPECTED_ERROR;
  476. goto CLEANUP;
  477. }
  478. pub_key_buffer = (uint8_t*)malloc(pub_key_size);
  479. if (pub_key_buffer == NULL)
  480. {
  481. ret = QE_UNEXPECTED_ERROR;
  482. goto CLEANUP;
  483. }
  484. ipp_ret = ippsRSAEncrypt_OAEP(aes_key, sizeof(aes_key),
  485. NULL, 0, seeds,
  486. wrap_key.encrypted_key,
  487. pub_key, IPP_ALG_HASH_SHA256,
  488. pub_key_buffer);
  489. if(ipp_ret != ippStsNoErr)
  490. {
  491. ret = QE_UNEXPECTED_ERROR;
  492. goto CLEANUP;
  493. }
  494. ipp_ret = ippsAES_GCMInit(aes_key,
  495. sizeof(aes_key),
  496. aes_context,
  497. aes_context_size);
  498. memset_s(aes_key, sizeof(aes_key), 0, sizeof(aes_key));
  499. if(ipp_ret != ippStsNoErr)
  500. {
  501. ret = QE_UNEXPECTED_ERROR;
  502. goto CLEANUP;
  503. }
  504. /* Create the random AES IV. */
  505. se_ret = sgx_read_rand(aes_iv, sizeof(aes_iv));
  506. if(SGX_SUCCESS != se_ret)
  507. {
  508. ret = QE_UNEXPECTED_ERROR;
  509. goto CLEANUP;
  510. }
  511. /* Copy the wrap_key_t into output buffer. */
  512. memcpy(&emp_p->wrap_key, &wrap_key, sizeof(wrap_key));
  513. /* Copy the AES IV into output buffer. */
  514. memcpy(&emp_p->iv, aes_iv, sizeof(aes_iv));
  515. /* Copy the AES Blob payload size into output buffer. */
  516. memcpy(&emp_p->payload_size, &sign_size, sizeof(sign_size));
  517. ipp_ret = ippsAES_GCMStart(aes_iv, sizeof(aes_iv), NULL, 0,
  518. aes_context);
  519. if(ipp_ret != ippStsNoErr)
  520. {
  521. ret = QE_UNEXPECTED_ERROR;
  522. goto CLEANUP;
  523. }
  524. /* Encrypt the basic signature. */
  525. ipp_ret = ippsAES_GCMEncrypt((Ipp8u *)&basic_sig,
  526. (uint8_t *)&encrypted_basic_sig,
  527. sizeof(encrypted_basic_sig),
  528. aes_context);
  529. if(ipp_ret != ippStsNoErr)
  530. {
  531. ret = QE_UNEXPECTED_ERROR;
  532. goto CLEANUP;
  533. }
  534. /* Copy the encrypted basic signature into output buffer. */
  535. memcpy(&emp_p->basic_sign, &encrypted_basic_sig,
  536. sizeof(encrypted_basic_sig));
  537. if(p_qe_report)
  538. {
  539. se_ret = sgx_sha256_update((uint8_t *)&wrap_key,
  540. sizeof(wrap_key),
  541. sha_quote_context);
  542. if(SGX_SUCCESS != se_ret)
  543. {
  544. ret = QE_UNEXPECTED_ERROR;
  545. goto CLEANUP;
  546. }
  547. se_ret = sgx_sha256_update(aes_iv,
  548. sizeof(aes_iv),
  549. sha_quote_context);
  550. if(SGX_SUCCESS != se_ret)
  551. {
  552. ret = QE_UNEXPECTED_ERROR;
  553. goto CLEANUP;
  554. }
  555. se_ret = sgx_sha256_update((uint8_t *)&sign_size,
  556. sizeof(sign_size),
  557. sha_quote_context);
  558. if(SGX_SUCCESS != se_ret)
  559. {
  560. ret = QE_UNEXPECTED_ERROR;
  561. goto CLEANUP;
  562. }
  563. se_ret = sgx_sha256_update((uint8_t *)&encrypted_basic_sig,
  564. sizeof(encrypted_basic_sig),
  565. sha_quote_context);
  566. if(SGX_SUCCESS != se_ret)
  567. {
  568. ret = QE_UNEXPECTED_ERROR;
  569. goto CLEANUP;
  570. }
  571. }
  572. /* Start process the SIG-RL. */
  573. if(emp_sig_rl_entries)
  574. {
  575. unsigned int entry_count = 0;
  576. unsigned int i = 0;
  577. RLver_t encrypted_rl_ver = {{0}};
  578. RLCount encrypted_n2 = {{0}};
  579. entry_count = lv_ntohl(p_sig_rl_header->sig_rl.n2);//entry count for big endian to little endian
  580. // Continue encrypt the output
  581. ipp_ret = ippsAES_GCMEncrypt((Ipp8u *)&(p_sig_rl_header->sig_rl.version),
  582. (Ipp8u *)&encrypted_rl_ver,
  583. sizeof(encrypted_rl_ver),
  584. aes_context);
  585. if(ipp_ret != ippStsNoErr)
  586. {
  587. ret = QE_UNEXPECTED_ERROR;
  588. goto CLEANUP;
  589. }
  590. ipp_ret = ippsAES_GCMEncrypt((Ipp8u *)&(p_sig_rl_header->sig_rl.n2),
  591. (Ipp8u *)&encrypted_n2,
  592. sizeof(encrypted_n2),
  593. aes_context);
  594. if(ipp_ret != ippStsNoErr)
  595. {
  596. ret = QE_UNEXPECTED_ERROR;
  597. goto CLEANUP;
  598. }
  599. memcpy(&(emp_p->rl_ver), &encrypted_rl_ver,
  600. sizeof(encrypted_rl_ver));
  601. memcpy(&(emp_p->rl_num), &encrypted_n2,
  602. sizeof(encrypted_n2));
  603. if(p_qe_report)
  604. {
  605. se_ret = sgx_sha256_update((uint8_t *)&encrypted_rl_ver,
  606. sizeof(encrypted_rl_ver),
  607. sha_quote_context);
  608. if(SGX_SUCCESS != se_ret)
  609. {
  610. ret = QE_UNEXPECTED_ERROR;
  611. goto CLEANUP;
  612. }
  613. se_ret = sgx_sha256_update((uint8_t *)&encrypted_n2,
  614. sizeof(encrypted_n2),
  615. sha_quote_context);
  616. if(SGX_SUCCESS != se_ret)
  617. {
  618. ret = QE_UNEXPECTED_ERROR;
  619. goto CLEANUP;
  620. }
  621. }
  622. /* Start process the SIG-RL entries one by one. */
  623. emp_nr = emp_p->nrp_mac;
  624. for (i = 0; i < entry_count; i++, emp_nr += sizeof(NrProof))
  625. {
  626. /* Generate non-revoke prove one by one. */
  627. SigRlEntry entry;
  628. NrProof temp_nr;
  629. NrProof encrypted_temp_nr;
  630. memcpy(&entry, emp_sig_rl_entries + i, sizeof(entry));
  631. memset_s(&temp_nr, sizeof(temp_nr), 0, sizeof(temp_nr));
  632. memset_s(&encrypted_temp_nr, sizeof(encrypted_temp_nr), 0, sizeof(encrypted_temp_nr));
  633. epid_ret = EpidNrProve(p_epid_context,
  634. (uint8_t *)const_cast<sgx_quote_t *>(p_quote_body),
  635. (uint32_t)QE_QUOTE_BODY_SIZE,
  636. &basic_sig, // Basic signature with 'b' and 'k' in it
  637. &entry, //Single entry in SigRl composed of 'b' and 'k'
  638. &temp_nr); // The generated non-revoked proof
  639. if(kEpidNoErr != epid_ret)
  640. {
  641. if(kEpidSigRevokedinSigRl == epid_ret)
  642. match = TRUE;
  643. else
  644. {
  645. ret = QE_UNEXPECTED_ERROR;
  646. goto CLEANUP;
  647. }
  648. }
  649. /* Update the hash of SIG-RL */
  650. se_ret = sgx_sha256_update((Ipp8u *)&entry,
  651. sizeof(entry), sha_context);
  652. if(SGX_SUCCESS != se_ret)
  653. {
  654. ret = QE_UNEXPECTED_ERROR;
  655. goto CLEANUP;
  656. }
  657. ipp_ret = ippsAES_GCMEncrypt((Ipp8u *)&temp_nr,
  658. (Ipp8u *)&encrypted_temp_nr,
  659. sizeof(NrProof),
  660. aes_context);
  661. if(ipp_ret != ippStsNoErr)
  662. {
  663. ret = QE_UNEXPECTED_ERROR;
  664. goto CLEANUP;
  665. }
  666. memcpy(emp_nr, &encrypted_temp_nr, sizeof(encrypted_temp_nr));
  667. if(p_qe_report)
  668. {
  669. se_ret = sgx_sha256_update((uint8_t *)&encrypted_temp_nr,
  670. sizeof(encrypted_temp_nr),
  671. sha_quote_context);
  672. if(SGX_SUCCESS != se_ret)
  673. {
  674. ret = QE_UNEXPECTED_ERROR;
  675. goto CLEANUP;
  676. }
  677. }
  678. }
  679. /* Get the final hash of the whole SIG-RL. */
  680. se_ret = sgx_sha256_get_hash(sha_context,
  681. (sgx_sha256_hash_t *)&sig_rl_hash.hash);
  682. if(SGX_SUCCESS != se_ret)
  683. {
  684. ret = QE_UNEXPECTED_ERROR;
  685. goto CLEANUP;
  686. }
  687. /* Verify the integraty of SIG-RL by check ECDSA signature. */
  688. ipp_ret = new_std_256_ecp(&p_ecp);
  689. if(ipp_ret != ippStsNoErr)
  690. {
  691. ret = QE_UNEXPECTED_ERROR;
  692. goto CLEANUP;
  693. }
  694. se_static_assert(sizeof(ec_pub_key) == sizeof(plaintext.epid_sk));
  695. // Both plaintext.epid_sk and ec_pub_key are little endian
  696. memcpy(&ec_pub_key, plaintext.epid_sk, sizeof(ec_pub_key));
  697. // se_ecdsa_verify_internal will take ec_pub_key as little endian
  698. se_ret = se_ecdsa_verify_internal(p_ecp,
  699. &ec_pub_key,
  700. p_sig_rl_signature,
  701. &sig_rl_hash,
  702. &ec_result);
  703. if(SGX_SUCCESS != se_ret)
  704. {
  705. ret = QE_UNEXPECTED_ERROR;
  706. goto CLEANUP;
  707. }
  708. else if(ippECValid != ec_result)
  709. {
  710. ret = QE_SIGRL_ERROR;
  711. goto CLEANUP;
  712. }
  713. else if(match)
  714. {
  715. ret = QE_REVOKED_ERROR;
  716. goto CLEANUP;
  717. }
  718. }
  719. else
  720. {
  721. se_static_assert(sizeof(emp_p->rl_ver) == sizeof(RLver_t));
  722. se_static_assert(sizeof(emp_p->rl_num) == sizeof(RLCount));
  723. uint8_t temp_buf[sizeof(RLver_t) + sizeof(RLCount)] = {0};
  724. uint8_t encrypted_temp_buf[sizeof(temp_buf)] = {0};
  725. ipp_ret = ippsAES_GCMEncrypt(temp_buf,
  726. (Ipp8u *)&encrypted_temp_buf,
  727. sizeof(encrypted_temp_buf),
  728. aes_context);
  729. if(ipp_ret != ippStsNoErr)
  730. {
  731. ret = QE_UNEXPECTED_ERROR;
  732. goto CLEANUP;
  733. }
  734. /* This will copy both encrypted rl_ver and encrypted rl_num into
  735. Output buffer. */
  736. memcpy(&emp_p->rl_ver, &encrypted_temp_buf,
  737. sizeof(encrypted_temp_buf));
  738. if(p_qe_report)
  739. {
  740. se_ret = sgx_sha256_update((uint8_t *)&encrypted_temp_buf,
  741. sizeof(encrypted_temp_buf),
  742. sha_quote_context);
  743. if(SGX_SUCCESS != se_ret)
  744. {
  745. ret = QE_UNEXPECTED_ERROR;
  746. goto CLEANUP;
  747. }
  748. }
  749. }
  750. ipp_ret = ippsAES_GCMGetTag(aes_tag, sizeof(aes_tag), aes_context);
  751. if(ipp_ret != ippStsNoErr)
  752. {
  753. ret = QE_UNEXPECTED_ERROR;
  754. goto CLEANUP;
  755. }
  756. memcpy((uint8_t *)&(emp_p->basic_sign) + sign_size, &aes_tag,
  757. sizeof(aes_tag));
  758. if(p_qe_report)
  759. {
  760. se_ret = sgx_sha256_update(aes_tag, sizeof(aes_tag),
  761. sha_quote_context);
  762. if(SGX_SUCCESS != se_ret)
  763. {
  764. ret = QE_UNEXPECTED_ERROR;
  765. goto CLEANUP;
  766. }
  767. se_ret = sgx_sha256_get_hash(sha_quote_context,
  768. (sgx_sha256_hash_t *)&qe_report_data);
  769. if(SGX_SUCCESS != se_ret)
  770. {
  771. ret = QE_UNEXPECTED_ERROR;
  772. goto CLEANUP;
  773. }
  774. memcpy(&(report_target.attributes),
  775. &(((const sgx_report_t *)p_enclave_report)->body.attributes),
  776. sizeof(report_target.attributes));
  777. memcpy(&(report_target.mr_enclave),
  778. &(((const sgx_report_t *)p_enclave_report)->body.mr_enclave),
  779. sizeof(report_target.mr_enclave));
  780. memcpy(&(report_target.misc_select),
  781. &(((const sgx_report_t *)p_enclave_report)->body.misc_select),
  782. sizeof(report_target.misc_select));
  783. se_ret = sgx_create_report(&report_target, &qe_report_data, p_qe_report);
  784. if(SGX_SUCCESS != se_ret)
  785. {
  786. ret = QE_PARAMETER_ERROR;
  787. goto CLEANUP;
  788. }
  789. }
  790. CLEANUP:
  791. memset_s(aes_key, sizeof(aes_key), 0, sizeof(aes_key));
  792. sgx_sha256_close(sha_context);
  793. sgx_sha256_close(sha_quote_context);
  794. if(aes_context)
  795. free(aes_context);
  796. if(pub_key)
  797. secure_free_rsa_pub_key(sizeof(g_qsdk_pub_key_n), sizeof(g_qsdk_pub_key_e), pub_key);
  798. if(pub_key_buffer)
  799. free(pub_key_buffer);
  800. secure_free_std_256_ecp(p_ecp);
  801. return ret;
  802. }
  803. /*
  804. * External function used to get quote. Prefix "emp_" means it is a pointer
  805. * points memory outside enclave.
  806. *
  807. * @param p_blob[in, out] Pointer to the EPID Blob.
  808. * @param blob_size[in] The size of EPID Blob, in bytes.
  809. * @param p_enclave_report[in] The application enclave's report.
  810. * @param quote_type[in] The type of quote, random based or name based.
  811. * @param p_spid[in] Pointer to SPID.
  812. * @param p_nonce[in] Pointer to nonce.
  813. * @param emp_sig_rl[in] Pointer to SIG-RL.
  814. * @param sig_rl_size[in] The size of SIG-RL, in bytes.
  815. * @param p_qe_report[out] Pointer to QE report, which reportdata is
  816. * sha256(nonce || quote)
  817. * @param emp_quote[out] Pointer to the output buffer for quote.
  818. * @param quote_size[in] The size of emp_quote, in bytes.
  819. * @param pce_isvsvn[in] The ISVSVN of PCE.
  820. * @return ae_error_t AE_SUCCESS for success, otherwise for errors.
  821. */
  822. uint32_t get_quote(
  823. uint8_t *p_blob,
  824. uint32_t blob_size,
  825. const sgx_report_t *p_enclave_report,
  826. sgx_quote_sign_type_t quote_type,
  827. const sgx_spid_t *p_spid,
  828. const sgx_quote_nonce_t *p_nonce,
  829. const uint8_t *emp_sig_rl,
  830. uint32_t sig_rl_size,
  831. sgx_report_t *p_qe_report,
  832. uint8_t *emp_quote,
  833. uint32_t quote_size,
  834. sgx_isv_svn_t pce_isvsvn)
  835. {
  836. ae_error_t ret = AE_SUCCESS;
  837. EpidStatus epid_ret = kEpidNoErr;
  838. MemberCtx *p_epid_context = NULL;
  839. sgx_quote_t quote_body;
  840. uint8_t is_resealed = 0;
  841. sgx_basename_t basename = {{0}};
  842. uint64_t sign_size = 0;
  843. sgx_status_t se_ret = SGX_SUCCESS;
  844. sgx_report_t qe_report;
  845. uint64_t required_buffer_size = 0;
  846. se_sig_rl_t sig_rl_header;
  847. se_plaintext_epid_data_sdk_t plaintext;
  848. sgx_ec256_signature_t ec_signature;
  849. memset(&quote_body, 0, sizeof(quote_body));
  850. memset(&sig_rl_header, 0, sizeof(sig_rl_header));
  851. memset(&plaintext, 0, sizeof(plaintext));
  852. memset(&ec_signature, 0, sizeof(ec_signature));
  853. /* Actually, some cases here will be checked with code generated by
  854. edger8r. Here we just want to defend in depth. */
  855. if((NULL == p_blob)
  856. || (NULL == p_enclave_report)
  857. || (NULL == p_spid)
  858. || (NULL == emp_quote)
  859. || (!quote_size)
  860. || ((NULL != emp_sig_rl) && (sig_rl_size < sizeof(se_sig_rl_t)
  861. + 2 * SE_ECDSA_SIGN_SIZE))
  862. || ((NULL == emp_sig_rl) && (sig_rl_size != 0)))
  863. return QE_PARAMETER_ERROR;
  864. if(SGX_TRUSTED_EPID_BLOB_SIZE_SDK != blob_size)
  865. return QE_PARAMETER_ERROR;
  866. if(SGX_LINKABLE_SIGNATURE != quote_type
  867. && SGX_UNLINKABLE_SIGNATURE != quote_type)
  868. return QE_PARAMETER_ERROR;
  869. if(!p_nonce && p_qe_report)
  870. return QE_PARAMETER_ERROR;
  871. if(p_nonce && !p_qe_report)
  872. return QE_PARAMETER_ERROR;
  873. /* To reduce the memory footprint of QE, we should leave sig_rl and
  874. quote buffer outside enclave. */
  875. if(!sgx_is_outside_enclave(emp_sig_rl, sig_rl_size))
  876. return QE_PARAMETER_ERROR;
  877. if(!sgx_is_outside_enclave(emp_quote, quote_size))
  878. return QE_PARAMETER_ERROR;
  879. /* Check whether p_blob is copied into EPC. If we want to reduce the
  880. memory usage, maybe we can leave the p_blob outside EPC. */
  881. if(!sgx_is_within_enclave(p_blob, blob_size))
  882. return QE_PARAMETER_ERROR;
  883. if(!sgx_is_within_enclave(p_enclave_report, sizeof(*p_enclave_report)))
  884. return QE_PARAMETER_ERROR;
  885. if(!sgx_is_within_enclave(p_spid, sizeof(*p_spid)))
  886. return QE_PARAMETER_ERROR;
  887. /* If the code reach here, if p_nonce is NULL, then p_qe_report will be
  888. NULL also. So we only check p_nonce here.*/
  889. if(p_nonce)
  890. {
  891. /* Actually Edger8r will alloc the buffer within EPC, this is just kind
  892. of defense in depth. */
  893. if(!sgx_is_within_enclave(p_nonce, sizeof(*p_nonce)))
  894. return QE_PARAMETER_ERROR;
  895. if(!sgx_is_within_enclave(p_qe_report, sizeof(*p_qe_report)))
  896. return QE_PARAMETER_ERROR;
  897. }
  898. /* Verify the input report. */
  899. if(SGX_SUCCESS != sgx_verify_report(p_enclave_report))
  900. return QE_PARAMETER_ERROR;
  901. /* Verify EPID p_blob and create the context */
  902. ret = verify_blob_internal(p_blob,
  903. blob_size,
  904. &is_resealed,
  905. TRUE,
  906. plaintext,
  907. &p_epid_context);
  908. if(AE_SUCCESS != ret)
  909. goto CLEANUP;
  910. /* If SIG-RL is provided, we should check its size. */
  911. if(emp_sig_rl)
  912. {
  913. uint64_t temp_size = 0;
  914. uint64_t n2 = 0;
  915. memcpy(&sig_rl_header, emp_sig_rl, sizeof(sig_rl_header));
  916. if(sig_rl_header.protocol_version != SE_EPID_SIG_RL_VERSION)
  917. {
  918. ret = QE_PARAMETER_ERROR;
  919. goto CLEANUP;
  920. }
  921. if(sig_rl_header.epid_identifier != SE_EPID_SIG_RL_ID)
  922. {
  923. ret = QE_PARAMETER_ERROR;
  924. goto CLEANUP;
  925. }
  926. if(memcmp(&sig_rl_header.sig_rl.gid, &plaintext.epid_group_cert.gid,
  927. sizeof(sig_rl_header.sig_rl.gid)))
  928. {
  929. ret = QE_PARAMETER_ERROR;
  930. goto CLEANUP;
  931. }
  932. temp_size = se_get_sig_rl_size(&sig_rl_header);
  933. if(temp_size != sig_rl_size)
  934. {
  935. ret = QE_PARAMETER_ERROR;
  936. goto CLEANUP;
  937. }
  938. se_static_assert(sizeof(ec_signature.x) == SE_ECDSA_SIGN_SIZE);
  939. se_static_assert(sizeof(ec_signature.y) == SE_ECDSA_SIGN_SIZE);
  940. memcpy(ec_signature.x,
  941. emp_sig_rl + sig_rl_size - (SE_ECDSA_SIGN_SIZE * 2),
  942. sizeof(ec_signature.x));
  943. SWAP_ENDIAN_32B(ec_signature.x);
  944. memcpy(ec_signature.y,
  945. emp_sig_rl + sig_rl_size - (SE_ECDSA_SIGN_SIZE * 1),
  946. sizeof(ec_signature.y));
  947. SWAP_ENDIAN_32B(ec_signature.y);
  948. n2 = SWAP_4BYTES(sig_rl_header.sig_rl.n2);
  949. temp_size = sizeof(EpidSignature) - sizeof(NrProof)
  950. + n2 * sizeof(NrProof);
  951. if(temp_size > UINT32_MAX)
  952. {
  953. ret = QE_PARAMETER_ERROR;
  954. goto CLEANUP;
  955. }
  956. sign_size = temp_size;
  957. }
  958. else
  959. {
  960. sign_size = sizeof(BasicSignature)
  961. + sizeof(uint32_t) // rl_ver
  962. + sizeof(uint32_t); // rl_num
  963. }
  964. /* Verify sizeof basename is large enough and it should always be true*/
  965. se_static_assert(sizeof(basename) > sizeof(*p_spid));
  966. /* Because basename has already been zeroed,
  967. so we don't need to concatenating with 0s.*/
  968. memcpy(&basename, p_spid, sizeof(*p_spid));
  969. if(SGX_UNLINKABLE_SIGNATURE == quote_type)
  970. {
  971. uint8_t *p = (uint8_t *)&basename + sizeof(*p_spid);
  972. se_ret = sgx_read_rand(p, sizeof(basename) - sizeof(*p_spid));
  973. if(SGX_SUCCESS != se_ret)
  974. {
  975. ret = QE_UNEXPECTED_ERROR;
  976. goto CLEANUP;
  977. }
  978. }
  979. epid_ret = EpidRegisterBaseName(p_epid_context, (uint8_t *)&basename,
  980. sizeof(basename));
  981. if(kEpidNoErr != epid_ret)
  982. {
  983. ret = QE_UNEXPECTED_ERROR;
  984. goto CLEANUP;
  985. }
  986. required_buffer_size = SE_QUOTE_LENGTH_WITHOUT_SIG + sign_size;
  987. /* We should make sure the buffer size is big enough. */
  988. if(quote_size < required_buffer_size)
  989. {
  990. ret = QE_PARAMETER_ERROR;
  991. goto CLEANUP;
  992. }
  993. /* Copy the data in the report into quote body. */
  994. memset(emp_quote, 0, quote_size);
  995. quote_body.version = QE_QUOTE_VERSION;
  996. quote_body.sign_type = (uint16_t)quote_type;
  997. quote_body.pce_svn = pce_isvsvn; // Both are little endian
  998. quote_body.xeid = plaintext.xeid; // Both are little endian
  999. se_static_assert(sizeof(plaintext.epid_group_cert.gid) == sizeof(uint32_t));
  1000. se_static_assert(sizeof(quote_body.epid_group_id) == sizeof(uint32_t));
  1001. ((uint8_t *)(&quote_body.epid_group_id))[0] = plaintext.epid_group_cert.gid.data[3];
  1002. ((uint8_t *)(&quote_body.epid_group_id))[1] = plaintext.epid_group_cert.gid.data[2];
  1003. ((uint8_t *)(&quote_body.epid_group_id))[2] = plaintext.epid_group_cert.gid.data[1];
  1004. ((uint8_t *)(&quote_body.epid_group_id))[3] = plaintext.epid_group_cert.gid.data[0];
  1005. memcpy(&quote_body.basename, &basename, sizeof(quote_body.basename));
  1006. // Get the QE's report.
  1007. se_ret = sgx_create_report(NULL, NULL, &qe_report);
  1008. if(SGX_SUCCESS != se_ret)
  1009. {
  1010. ret = QE_PARAMETER_ERROR;
  1011. goto CLEANUP;
  1012. }
  1013. // Copy QE's security version in to Quote body.
  1014. quote_body.qe_svn = qe_report.body.isv_svn;
  1015. // Copy the incoming report into Quote body.
  1016. memcpy(&quote_body.report_body, &(p_enclave_report->body),
  1017. sizeof(quote_body.report_body));
  1018. /* Because required_buffer_size is larger than signature_len, so if we
  1019. get here, then no integer overflow will ocur. */
  1020. quote_body.signature_len = (uint32_t)(sizeof(se_wrap_key_t)
  1021. + QUOTE_IV_SIZE
  1022. + sizeof(uint32_t)
  1023. + sign_size
  1024. + sizeof(sgx_mac_t));
  1025. /* Make the signature. */
  1026. ret = qe_epid_sign(p_epid_context,
  1027. plaintext,
  1028. &basename,
  1029. emp_sig_rl ? ((const se_sig_rl_t *)emp_sig_rl)->sig_rl.bk
  1030. : NULL,
  1031. &sig_rl_header,
  1032. &ec_signature,
  1033. p_enclave_report,
  1034. p_nonce,
  1035. p_qe_report,
  1036. emp_quote,
  1037. &quote_body,
  1038. (uint32_t)sign_size);
  1039. if(AE_SUCCESS != ret)
  1040. {
  1041. // Only need to clean the buffer after the fixed length part.
  1042. memset_s(emp_quote + sizeof(sgx_quote_t), quote_size - sizeof(sgx_quote_t),
  1043. 0, quote_size - sizeof(sgx_quote_t));
  1044. goto CLEANUP;
  1045. }
  1046. memcpy(emp_quote, &quote_body, sizeof(sgx_quote_t));
  1047. CLEANUP:
  1048. if(p_epid_context)
  1049. EpidMemberDelete(&p_epid_context);
  1050. return ret;
  1051. }