epid_provision_msg2.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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 "type_length_value.h"
  32. #include "epid_utility.h"
  33. #include "aesm_xegd_blob.h"
  34. #include "aeerror.h"
  35. #include "PVEClass.h"
  36. #include "PCEClass.h"
  37. #include "aesm_rand.h"
  38. /**
  39. * File: epid_provision_msg2.cpp
  40. * Description: Provide the untrusted implementation of code to process ProvMsg2
  41. *
  42. * Untrusted Code for EPID Provision
  43. */
  44. #define MSG2_TOP_FIELDS_COUNT_WITH_SIGRL 4
  45. #define MSG2_TOP_FIELDS_COUNT_WITHOUT_SIGRL 3
  46. #define MSG2_TOP_FIELD_NONCE tlvs_msg2[0]
  47. #define MSG2_TOP_FIELD_DATA tlvs_msg2[1]
  48. #define MSG2_TOP_FIELD_MAC tlvs_msg2[2]
  49. #define MSG2_TOP_FIELD_SIGRL tlvs_msg2[3]
  50. #define MSG2_FIELD1_MAX_COUNT 6
  51. #define MSG2_FIELD1_MIN_COUNT 4
  52. #define MSG2_FIELD1_GROUP_CERT tlvs_field1[0]
  53. #define MSG2_FIELD1_NONCE tlvs_field1[2]
  54. #define MSG2_FIELD1_PSID tlvs_field1[1]
  55. #define MSG2_FIELD1_PREV_PI tlvs_field1[alt_index+1]
  56. #define PREV_GID_INDEX (alt_index+2)
  57. #define MSG2_FIELD1_PREV_GID tlvs_field1[PREV_GID_INDEX]
  58. #define MSG2_FIELD1_PLAT_INFO tlvs_field1[alt_index+3]
  59. //Function to verify that EPID SigRl type and version is correct for sigrl
  60. static ae_error_t verify_sigrl_cert_type_version(const se_sig_rl_t *sigrl_cert)
  61. {
  62. if(sigrl_cert->epid_identifier!=SE_EPID_SIG_RL_ID||
  63. sigrl_cert->protocol_version!=SE_EPID_SIG_RL_VERSION)
  64. return PVE_INTEGRITY_CHECK_ERROR;
  65. return AE_SUCCESS;
  66. }
  67. static ae_error_t msg2_integrity_checking(const TLVsMsg& tlvs_msg2)
  68. {
  69. uint32_t tlv_count = tlvs_msg2.get_tlv_count();
  70. if(tlv_count != MSG2_TOP_FIELDS_COUNT_WITH_SIGRL && tlv_count != MSG2_TOP_FIELDS_COUNT_WITHOUT_SIGRL)
  71. return PVE_INTEGRITY_CHECK_ERROR;
  72. if(MSG2_TOP_FIELD_NONCE.type != TLV_NONCE || MSG2_TOP_FIELD_NONCE.size != NONCE_SIZE || MSG2_TOP_FIELD_NONCE.version != TLV_VERSION_1)
  73. return PVE_INTEGRITY_CHECK_ERROR;
  74. if(MSG2_TOP_FIELD_NONCE.header_size != SMALL_TLV_HEADER_SIZE)//Requires NONCE to be small header size
  75. return PVE_INTEGRITY_CHECK_ERROR;
  76. if(MSG2_TOP_FIELD_DATA.type != TLV_BLOCK_CIPHER_TEXT || MSG2_TOP_FIELD_DATA.version != TLV_VERSION_1)
  77. return PVE_INTEGRITY_CHECK_ERROR;
  78. if(MSG2_TOP_FIELD_MAC.type != TLV_MESSAGE_AUTHENTICATION_CODE || MSG2_TOP_FIELD_MAC.version != TLV_VERSION_1 || MSG2_TOP_FIELD_MAC.size != MAC_SIZE)
  79. return PVE_INTEGRITY_CHECK_ERROR;
  80. if(MSG2_TOP_FIELD_MAC.header_size != SMALL_TLV_HEADER_SIZE)
  81. return PVE_INTEGRITY_CHECK_ERROR;
  82. if(tlv_count == MSG2_TOP_FIELDS_COUNT_WITH_SIGRL){
  83. if(MSG2_TOP_FIELD_SIGRL.type != TLV_EPID_SIG_RL || MSG2_TOP_FIELD_SIGRL.version != TLV_VERSION_1)
  84. return PVE_INTEGRITY_CHECK_ERROR;
  85. if(MSG2_TOP_FIELD_SIGRL.size < 2*SE_ECDSA_SIGN_SIZE + sizeof(se_sig_rl_t))
  86. return PVE_INTEGRITY_CHECK_ERROR;
  87. if(MSG2_TOP_FIELD_SIGRL.header_size != LARGE_TLV_HEADER_SIZE)
  88. return PVE_INTEGRITY_CHECK_ERROR;
  89. return verify_sigrl_cert_type_version(reinterpret_cast<const se_sig_rl_t *>(MSG2_TOP_FIELD_SIGRL.payload));
  90. }
  91. return AE_SUCCESS;
  92. }
  93. //check the format of msg2_field1 and copy correspondent data to msg2_blob_input
  94. static ae_error_t msg2_field1_msg_check_copy(const TLVsMsg& tlvs_field1, proc_prov_msg2_blob_input_t& msg2_blob_input, const signed_pek_t& pek)
  95. {
  96. uint32_t tlv_count = tlvs_field1.get_tlv_count();
  97. uint32_t alt_index = 2;
  98. msg2_blob_input.is_previous_pi_provided = false;
  99. if(tlv_count == MSG2_FIELD1_MAX_COUNT){//EPID_PSVN TLV is available
  100. msg2_blob_input.is_previous_pi_provided = true;
  101. if(MSG2_FIELD1_PREV_PI.type != TLV_PLATFORM_INFO ||
  102. MSG2_FIELD1_PREV_PI.size != sizeof(bk_platform_info_t)){
  103. return PVE_MSG_ERROR;
  104. }
  105. if(MSG2_FIELD1_PREV_PI.version != TLV_VERSION_1){
  106. return PVE_MSG_ERROR;
  107. }
  108. if(MSG2_FIELD1_PREV_PI.header_size != SMALL_TLV_HEADER_SIZE){
  109. return PVE_MSG_ERROR;
  110. }
  111. if(MSG2_FIELD1_PREV_GID.type != TLV_EPID_GID ||
  112. MSG2_FIELD1_PREV_GID.size != sizeof(GroupId)){
  113. return PVE_MSG_ERROR;
  114. }
  115. if(MSG2_FIELD1_PREV_GID.version != TLV_VERSION_1){
  116. return PVE_MSG_ERROR;
  117. }
  118. if(MSG2_FIELD1_PREV_GID.header_size != SMALL_TLV_HEADER_SIZE){
  119. return PVE_MSG_ERROR;
  120. }
  121. if(0!=memcpy_s(&msg2_blob_input.previous_gid, sizeof(msg2_blob_input.previous_gid), MSG2_FIELD1_PREV_GID.payload, MSG2_FIELD1_PREV_GID.size)){
  122. return PVE_UNEXPECTED_ERROR;
  123. }
  124. if(0!=memcpy_s(&msg2_blob_input.previous_pi, sizeof(msg2_blob_input.previous_pi), MSG2_FIELD1_PREV_PI.payload, MSG2_FIELD1_PREV_PI.size)){
  125. return PVE_UNEXPECTED_ERROR;
  126. }
  127. }else if(tlv_count!=MSG2_FIELD1_MIN_COUNT){
  128. return PVE_MSG_ERROR;//make sure number of TLVs are correct
  129. }else{
  130. alt_index=0;
  131. }
  132. if(MSG2_FIELD1_GROUP_CERT.type != TLV_EPID_GROUP_CERT||
  133. MSG2_FIELD1_GROUP_CERT.version != TLV_VERSION_1 ||
  134. MSG2_FIELD1_GROUP_CERT.size != sizeof(signed_epid_group_cert_t)||
  135. MSG2_FIELD1_GROUP_CERT.header_size != SMALL_TLV_HEADER_SIZE)
  136. return PVE_MSG_ERROR;
  137. if(MSG2_FIELD1_PSID.type != TLV_PS_ID ||
  138. MSG2_FIELD1_PSID.version != TLV_VERSION_1 ||
  139. MSG2_FIELD1_PSID.size != sizeof(psid_t)||
  140. MSG2_FIELD1_PSID.header_size != SMALL_TLV_HEADER_SIZE)
  141. return PVE_MSG_ERROR;
  142. if(MSG2_FIELD1_NONCE.type != TLV_NONCE||
  143. MSG2_FIELD1_NONCE.version != TLV_VERSION_1 ||
  144. MSG2_FIELD1_NONCE.size != CHALLENGE_NONCE_SIZE||
  145. MSG2_FIELD1_NONCE.header_size != SMALL_TLV_HEADER_SIZE)
  146. return PVE_MSG_ERROR;
  147. if(MSG2_FIELD1_PLAT_INFO.type != TLV_PLATFORM_INFO ||
  148. MSG2_FIELD1_PLAT_INFO.version != TLV_VERSION_1 ||
  149. MSG2_FIELD1_PLAT_INFO.size != sizeof(bk_platform_info_t)||
  150. MSG2_FIELD1_PLAT_INFO.header_size != SMALL_TLV_HEADER_SIZE)
  151. return PVE_MSG_ERROR;
  152. sgx_sha256_hash_t psid_hash;
  153. ae_error_t ret = sgx_error_to_ae_error(sgx_sha256_msg(reinterpret_cast<const uint8_t *>(&pek.n), static_cast<uint32_t>(sizeof(pek.n)+sizeof(pek.e)), &psid_hash));
  154. if(AE_SUCCESS != ret)
  155. return ret;
  156. if(0!=memcmp(&psid_hash, MSG2_FIELD1_PSID.payload, sizeof(psid_hash)))//PSID does not match
  157. return PVE_MSG_ERROR;
  158. bk_platform_info_t *d2 = (bk_platform_info_t *)MSG2_FIELD1_PLAT_INFO.payload;
  159. if(0!=memcpy_s(&msg2_blob_input.group_cert, sizeof(msg2_blob_input.group_cert), MSG2_FIELD1_GROUP_CERT.payload, MSG2_FIELD1_GROUP_CERT.size)||
  160. 0!=memcpy_s(&msg2_blob_input.challenge_nonce, sizeof(msg2_blob_input.challenge_nonce), MSG2_FIELD1_NONCE.payload, MSG2_FIELD1_NONCE.size)||
  161. 0!=memcpy_s(&msg2_blob_input.equiv_pi, sizeof(msg2_blob_input.equiv_pi), d2,sizeof(*d2))){
  162. return PVE_UNEXPECTED_ERROR;
  163. }
  164. return AE_SUCCESS;
  165. }
  166. //Function to check message header of ProvMsg2 to determine whether it is valid
  167. //@msg2_header, input the message header of ProvMsg2
  168. //@msg2_size, size of ProvMsg2, in bytes
  169. //@return AE_SUCCESS if the message header is valid ProvMsg2 or error code if there're any problems
  170. static ae_error_t check_prov_msg2_header(const provision_response_header_t *msg2_header, uint32_t msg2_size)
  171. {
  172. if(msg2_header->protocol != SE_EPID_PROVISIONING || msg2_header->type != TYPE_PROV_MSG2 ||
  173. msg2_header->version != TLV_VERSION_2){
  174. return PVE_INTEGRITY_CHECK_ERROR;
  175. }
  176. uint32_t size_in_header = lv_ntohl(msg2_header->size);
  177. if(size_in_header + PROVISION_RESPONSE_HEADER_SIZE != msg2_size)
  178. return PVE_INTEGRITY_CHECK_ERROR;
  179. return AE_SUCCESS;
  180. }
  181. static uint32_t estimate_epid_sig_size(uint32_t sigrl_size)
  182. {
  183. uint32_t sigrl_body_size = 0;
  184. uint32_t sigrl_extra_size = static_cast<uint32_t>(sizeof(se_sig_rl_t)-sizeof(SigRlEntry)+2*ECDSA_SIGN_SIZE);
  185. if(sigrl_size ==sigrl_extra_size || sigrl_size == 0){//sigrl_size==0 is special cases that no sigrl provided
  186. //Add the TLV Header size
  187. return static_cast<uint32_t>(sizeof(EpidSignature)-sizeof(NrProof)+MAX_TLV_HEADER_SIZE);
  188. }else if (sigrl_size < sigrl_extra_size){
  189. //Invalid sigrl size
  190. return 0;
  191. }else{
  192. sigrl_body_size = sigrl_size - sigrl_extra_size;
  193. uint64_t entry_count = sigrl_body_size/sizeof(SigRlEntry);
  194. uint64_t total_size = sizeof(EpidSignature)-sizeof(NrProof)+sizeof(NrProof)*entry_count+MAX_TLV_HEADER_SIZE;
  195. if(total_size > UINT32_MAX){
  196. return 0;
  197. }
  198. return static_cast<uint32_t>(total_size);
  199. }
  200. }
  201. static ae_error_t gen_msg3_header(const gen_prov_msg3_output_t& msg3_output, const uint8_t xid[XID_SIZE], provision_request_header_t *msg3_header, uint32_t& msg3_size)
  202. {
  203. msg3_header->protocol = SE_EPID_PROVISIONING;
  204. msg3_header->version = TLV_VERSION_2;
  205. msg3_header->type = TYPE_PROV_MSG3;
  206. size_t field1_size = 0;
  207. if(msg3_output.is_join_proof_generated){
  208. field1_size = BLOCK_CIPHER_TEXT_TLV_SIZE(HARD_CODED_JOIN_PROOF_WITH_ESCROW_TLV_SIZE)+MAC_TLV_SIZE(MAC_SIZE);
  209. }else{
  210. // BLOCK_CIPHER_TEXT_TLV_SIZE(0) is needed because IV need to be included for the following MAC tlv
  211. field1_size = BLOCK_CIPHER_TEXT_TLV_SIZE(0)+MAC_TLV_SIZE(MAC_SIZE);
  212. }
  213. field1_size+=NONCE_TLV_SIZE(NONCE_2_SIZE)+CIPHER_TEXT_TLV_SIZE(RSA_3072_KEY_BYTES)+SE_REPORT_TLV_SIZE();
  214. size_t total_body_size = NONCE_TLV_SIZE(NONCE_SIZE) + BLOCK_CIPHER_TEXT_TLV_SIZE(field1_size)+MAC_TLV_SIZE(MAC_SIZE);
  215. if(msg3_output.is_epid_sig_generated){
  216. total_body_size += BLOCK_CIPHER_TEXT_TLV_SIZE(msg3_output.epid_sig_output_size)+MAC_TLV_SIZE(MAC_SIZE);
  217. }
  218. uint32_t size_in_net = _htonl(total_body_size);
  219. if(0!=memcpy_s(&msg3_header->size, sizeof(msg3_header->size), &size_in_net, sizeof(size_in_net)))
  220. return PVE_UNEXPECTED_ERROR;//size in Big Endian in message header of ProvMsg3
  221. if(total_body_size>msg3_size - PROVISION_REQUEST_HEADER_SIZE ){//make sure buffer size provided by user is large enough
  222. return PVE_INSUFFICIENT_MEMORY_ERROR;
  223. }
  224. if(0!=memcpy_s(msg3_header->xid, sizeof(msg3_header->xid), xid, XID_SIZE))
  225. return PVE_UNEXPECTED_ERROR; //copy transaction id of ProvMsg2
  226. msg3_size = static_cast<uint32_t>(total_body_size + PROVISION_REQUEST_HEADER_SIZE);
  227. return AE_SUCCESS;
  228. }
  229. //Function to decode ProvMsg2 and generate ProvMsg3
  230. //@data: global structure used to store pve relative data
  231. //@msg2: ProvMsg2
  232. //@msg2_size: size of ProvMsg2
  233. //@epid_blob: input an optional old epid data blob used to generate non-revoke proof if required
  234. //@blob_size: size of the epid blob
  235. //@msg3: output buffer for ProvMsg3
  236. //@msg3_size: input the size of buffer msg3, in byte
  237. //@return AE_SUCCESS on success and error code on failure
  238. uint32_t CPVEClass::proc_prov_msg2(
  239. pve_data_t &data,
  240. const uint8_t *msg2,
  241. uint32_t msg2_size,
  242. const uint8_t *epid_blob,
  243. uint32_t blob_size,
  244. uint8_t *msg3,
  245. uint32_t msg3_size)
  246. {
  247. ae_error_t ret = AE_SUCCESS;
  248. const se_sig_rl_t *sigrl = NULL;
  249. uint32_t sigrl_size = 0;
  250. uint8_t *epid_sig = NULL;
  251. uint8_t *decoded_msg2 = NULL;
  252. uint8_t *encrypted_field1 = NULL;
  253. sgx_status_t sgx_status;
  254. uint8_t aad[PROVISION_RESPONSE_HEADER_SIZE+sizeof(RLver_t)+sizeof(GroupId)];
  255. size_t aad_size = PROVISION_RESPONSE_HEADER_SIZE;
  256. const provision_response_header_t *msg2_header = reinterpret_cast<const provision_response_header_t *>(msg2);
  257. provision_request_header_t *msg3_header = reinterpret_cast<provision_request_header_t *>(msg3);
  258. if(msg2_size < PROVISION_RESPONSE_HEADER_SIZE){
  259. AESM_DBG_ERROR("ProvMsg2 size too small");
  260. return PVE_MSG_ERROR;
  261. }
  262. if (epid_blob != NULL && blob_size != SGX_TRUSTED_EPID_BLOB_SIZE_SDK){
  263. AESM_DBG_FATAL("epid blob size error");
  264. return PVE_UNEXPECTED_ERROR;
  265. }
  266. if(msg3_size <PROVISION_REQUEST_HEADER_SIZE){
  267. AESM_DBG_ERROR("Input ProvMsg3 buffer too small");
  268. return PVE_INSUFFICIENT_MEMORY_ERROR;
  269. }
  270. ret = check_prov_msg2_header(msg2_header, msg2_size);//process message header
  271. if( AE_SUCCESS != ret){
  272. AESM_DBG_ERROR("Fail to decode ProvMsg2:(ae%d)",ret);
  273. return ret;
  274. }
  275. if ( 0!=memcmp(msg2_header->xid, data.xid, XID_SIZE) ){
  276. AESM_DBG_ERROR("unmatched xid in ProvMsg2 header");
  277. return AE_FAILURE;
  278. }
  279. ret = check_epid_pve_pg_status_before_mac_verification(msg2_header);
  280. if( AE_SUCCESS != ret){
  281. AESM_DBG_ERROR("Backend server reported error in ProvMsg2:(ae%d)",ret);
  282. return ret;
  283. }
  284. if( 0!=memcpy_s(aad, sizeof(aad), msg2_header, PROVISION_RESPONSE_HEADER_SIZE)){
  285. AESM_DBG_FATAL("memcpy error");
  286. return PVE_UNEXPECTED_ERROR;
  287. }
  288. do{
  289. TLVsMsg tlvs_msg2;
  290. tlv_status_t tlv_status;
  291. //decode TLV structure of message body
  292. tlv_status= tlvs_msg2.init_from_buffer(msg2+static_cast<uint32_t>(PROVISION_RESPONSE_HEADER_SIZE), msg2_size - static_cast<uint32_t>(PROVISION_RESPONSE_HEADER_SIZE));
  293. ret = tlv_error_2_pve_error(tlv_status);
  294. if(AE_SUCCESS!=ret){
  295. AESM_DBG_ERROR("Fail to decode ProvMsg2:(ae%d)",ret);
  296. break;
  297. }
  298. ret = msg2_integrity_checking(tlvs_msg2);//checking and verifying that TLV structure is correct and version is supported
  299. if(AE_SUCCESS!=ret){
  300. AESM_DBG_ERROR("ProvMsg2 integrity checking error:(ae%d)",ret);
  301. break;
  302. }
  303. sgx_aes_gcm_128bit_key_t ek2;
  304. uint8_t temp[NONCE_SIZE+XID_SIZE];
  305. if(0!=memcpy_s(temp, sizeof(temp), data.xid, sizeof(data.xid))||
  306. 0!=memcpy_s(temp+XID_SIZE, sizeof(temp)-XID_SIZE, MSG2_TOP_FIELD_NONCE.payload, MSG2_TOP_FIELD_NONCE.size)){
  307. AESM_DBG_ERROR("memcpy error");
  308. ret = AE_FAILURE;
  309. break;
  310. }
  311. se_static_assert(sizeof(sgx_cmac_128bit_key_t)==SK_SIZE);
  312. if((sgx_status=sgx_rijndael128_cmac_msg(reinterpret_cast<const sgx_cmac_128bit_key_t *>(data.sk),
  313. temp, XID_SIZE+NONCE_SIZE, &ek2))!=SGX_SUCCESS){
  314. AESM_DBG_ERROR("Fail to generate ek2:(sgx 0x%x)",sgx_status);
  315. ret = AE_FAILURE;
  316. break;
  317. }
  318. if(tlvs_msg2.get_tlv_count() == MSG2_TOP_FIELDS_COUNT_WITH_SIGRL){//sigrl version and gid is added as part of AAD if available
  319. sigrl = reinterpret_cast<const se_sig_rl_t *>(MSG2_TOP_FIELD_SIGRL.payload);
  320. if(0!=memcpy_s(aad+PROVISION_RESPONSE_HEADER_SIZE, sizeof(RLver_t), &sigrl->sig_rl.version, sizeof(RLver_t))){
  321. AESM_DBG_FATAL("memcpy error");
  322. ret = PVE_UNEXPECTED_ERROR;
  323. break;
  324. }
  325. if(0!=memcpy_s(aad+PROVISION_RESPONSE_HEADER_SIZE+sizeof(RLver_t), sizeof(aad)- PROVISION_RESPONSE_HEADER_SIZE-sizeof(RLver_t), &sigrl->sig_rl.gid, sizeof(sigrl->sig_rl.gid))){
  326. AESM_DBG_FATAL("memcpy error");
  327. ret = PVE_UNEXPECTED_ERROR;
  328. break;
  329. }
  330. aad_size += sizeof(RLver_t)+sizeof(GroupId);
  331. sigrl_size = MSG2_TOP_FIELD_SIGRL.size;
  332. }
  333. se_static_assert(SK_SIZE==sizeof(sgx_aes_gcm_128bit_key_t));
  334. tlv_msg_t field1 = block_cipher_tlv_get_encrypted_text(MSG2_TOP_FIELD_DATA);
  335. decoded_msg2 = static_cast<uint8_t *>(malloc(field1.msg_size));
  336. if(NULL == decoded_msg2){
  337. AESM_DBG_ERROR("malloc error");
  338. ret= AE_OUT_OF_MEMORY_ERROR;
  339. break;
  340. }
  341. //decrypt ProvMsg2 by EK2
  342. sgx_status = sgx_rijndael128GCM_decrypt(&ek2,
  343. field1.msg_buf, field1.msg_size, decoded_msg2,
  344. reinterpret_cast<uint8_t *>(block_cipher_tlv_get_iv(MSG2_TOP_FIELD_DATA)), IV_SIZE,
  345. aad, static_cast<uint32_t>(aad_size), reinterpret_cast<const sgx_aes_gcm_128bit_tag_t *>(MSG2_TOP_FIELD_MAC.payload));
  346. if(SGX_ERROR_MAC_MISMATCH == sgx_status){
  347. AESM_DBG_ERROR("Fail to decrypt ProvMsg2 body by EK2 (sgx0x%x)",sgx_status);
  348. ret = PVE_INTEGRITY_CHECK_ERROR;
  349. break;
  350. }
  351. if( AE_SUCCESS != (ret = sgx_error_to_ae_error(sgx_status))){
  352. AESM_DBG_ERROR("error in decrypting ProvMsg2 body:(sgx0x%x)",sgx_status);
  353. break;
  354. }
  355. ret = check_epid_pve_pg_status_after_mac_verification(msg2_header);
  356. if(AE_SUCCESS != ret){
  357. AESM_DBG_ERROR("Backend server reported error in ProvMsg2 passed MAC verification:(ae%d)",ret);
  358. break;
  359. }
  360. TLVsMsg tlvs_field1;
  361. tlv_status = tlvs_field1.init_from_buffer(decoded_msg2, field1.msg_size);//decode TLV structure of field1 of ProvMsg2
  362. ret = tlv_error_2_pve_error(tlv_status);
  363. if(AE_SUCCESS!=ret){
  364. AESM_DBG_ERROR("Fail to decode field1 of ProvMsg2:(ae%d)",ret);
  365. break;
  366. }
  367. proc_prov_msg2_blob_input_t msg2_blob_input;
  368. memset(&msg2_blob_input, 0, sizeof(msg2_blob_input));
  369. ret = CPCEClass::instance().load_enclave();//Load PCE enclave now
  370. if( ret != AE_SUCCESS){
  371. AESM_DBG_ERROR("Fail to load PCE enclave:(ae%d)\n",ret);
  372. break;
  373. }
  374. ret = (ae_error_t)CPCEClass::instance().get_pce_target(&msg2_blob_input.pce_target_info);
  375. if(AE_SUCCESS != ret){
  376. AESM_DBG_ERROR("fail to get PCE target info:(ae%d)\n",ret);
  377. break;
  378. }
  379. ret = msg2_field1_msg_check_copy(tlvs_field1, msg2_blob_input, data.pek);//version/type checking to verify message format
  380. if( AE_SUCCESS != ret){
  381. AESM_DBG_ERROR("field1 of ProvMsg2 checking error:( ae%d)",ret);
  382. break;
  383. }
  384. gen_prov_msg3_output_t msg3_fixed_output;
  385. memset(&msg3_fixed_output, 0, sizeof(msg3_fixed_output));
  386. //collect old epid blob
  387. if(epid_blob==NULL){
  388. memset(msg2_blob_input.old_epid_data_blob, 0, SGX_TRUSTED_EPID_BLOB_SIZE_SDK);
  389. }else{
  390. #ifdef DBG_LOG
  391. {
  392. char dbg_str[256];
  393. aesm_dbg_format_hex(epid_blob, blob_size, dbg_str, 256);
  394. AESM_DBG_TRACE("old epid blob=%s",dbg_str);
  395. }
  396. #endif
  397. if (0 != memcpy_s(msg2_blob_input.old_epid_data_blob, sizeof(msg2_blob_input.old_epid_data_blob), epid_blob, blob_size)){
  398. AESM_DBG_FATAL("memcpy error");
  399. ret = PVE_UNEXPECTED_ERROR;
  400. break;
  401. }
  402. }
  403. if(0!=memcpy_s(&msg2_blob_input.pek,sizeof(msg2_blob_input.pek), &data.pek, sizeof(data.pek))){
  404. AESM_DBG_ERROR("memcpy error");
  405. ret = AE_FAILURE;
  406. break;
  407. }
  408. if (AE_SUCCESS != (ret = XEGDBlob::instance().read(msg2_blob_input.xegb))){
  409. AESM_DBG_ERROR("Fail to read extend epid group blob info ");
  410. return ret;
  411. }
  412. uint32_t epid_sig_output_size = estimate_epid_sig_size(sigrl_size);
  413. //estimate_epid_sig_size(sigrl_size)=0, which means the sigrl is invalid.
  414. if(epid_sig_output_size == 0){
  415. AESM_DBG_ERROR("Invalid SIGRL size %d", sigrl_size);
  416. ret = PVE_MSG_ERROR;
  417. break;
  418. }
  419. epid_sig = static_cast<uint8_t *>(malloc(epid_sig_output_size));
  420. if(NULL == epid_sig){
  421. AESM_DBG_ERROR("malloc error");
  422. ret = AE_OUT_OF_MEMORY_ERROR;
  423. break;
  424. }
  425. ret = CPVEClass::instance().load_enclave();//Load PvE enclave now
  426. if( ret != AE_SUCCESS){
  427. AESM_DBG_ERROR("Fail to load PvE enclave:(ae%d)\n",ret);
  428. break;
  429. }
  430. ret = (ae_error_t)proc_prov_msg2_data(&msg2_blob_input, data.is_performance_rekey, reinterpret_cast<const uint8_t *>(sigrl), sigrl_size,
  431. &msg3_fixed_output, epid_sig, epid_sig_output_size);//ecall to process msg2 data and generate msg3 data in PvE
  432. if( PVE_EPIDBLOB_ERROR == ret){
  433. if(0!=memcpy_s(&data.bpi, sizeof(data.bpi), &msg2_blob_input.previous_pi, sizeof(msg2_blob_input.previous_pi))){
  434. AESM_DBG_FATAL("memcpy error");
  435. ret = PVE_UNEXPECTED_ERROR;
  436. break;
  437. }
  438. }
  439. if(AE_SUCCESS != ret){
  440. AESM_DBG_ERROR("PvE report error (ae%d) in processing ProvMsg2",ret);
  441. break;
  442. }
  443. uint8_t ecdsa_sign[64];
  444. psvn_t psvn;
  445. memset(&psvn, 0, sizeof(psvn));
  446. if(0!=memcpy_s(&psvn.cpu_svn,sizeof(psvn.cpu_svn), &msg2_blob_input.equiv_pi.cpu_svn, sizeof(msg2_blob_input.equiv_pi.cpu_svn))||
  447. 0!=memcpy_s(&psvn.isv_svn, sizeof(psvn.isv_svn), &msg2_blob_input.equiv_pi.pce_svn, sizeof(msg2_blob_input.equiv_pi.pce_svn))){
  448. ret = PVE_UNEXPECTED_ERROR;
  449. break;
  450. }
  451. ret = CPCEClass::instance().load_enclave();//Load PCE enclave now
  452. if( ret != AE_SUCCESS){
  453. AESM_DBG_ERROR("Fail to load PCE enclave:(ae%d)\n",ret);
  454. break;
  455. }
  456. ret = (ae_error_t)CPCEClass::instance().sign_report(psvn, msg3_fixed_output.pwk2_report, ecdsa_sign);
  457. if(AE_SUCCESS != ret){
  458. AESM_DBG_ERROR("PCE report error (ae%d) in sign report",ret);
  459. break;
  460. }
  461. CPCEClass::instance().unload_enclave();
  462. uint8_t iv[IV_SIZE];
  463. uint8_t mac[MAC_SIZE];
  464. uint8_t *payload_data=NULL;
  465. uint32_t payload_size = 0;
  466. ret = aesm_read_rand(iv, IV_SIZE);
  467. if(AE_SUCCESS != ret){
  468. AESM_DBG_ERROR("fail to generate random number:(ae%d)",ret);
  469. break;
  470. }
  471. //Now start to generate ProvMsg3
  472. ret = gen_msg3_header(msg3_fixed_output, data.xid, msg3_header,msg3_size);//first generate header
  473. if( AE_SUCCESS != ret){
  474. AESM_DBG_ERROR("Fail to generate ProvMsg3 Header:(ae%d)",ret);
  475. break;
  476. }
  477. TLVsMsg tlvs_msg3;
  478. tlv_status = tlvs_msg3.add_nonce(MSG2_TOP_FIELD_NONCE.payload, NONCE_SIZE);
  479. ret = tlv_error_2_pve_error(tlv_status);
  480. if(AE_SUCCESS!=ret){
  481. AESM_DBG_ERROR("Fail to generate Nonce TLV in ProvMsg3:(ae%d)",ret);
  482. break;
  483. }
  484. if(msg3_fixed_output.is_join_proof_generated){
  485. payload_data = msg3_fixed_output.field1_data;
  486. payload_size = static_cast<uint32_t>(HARD_CODED_JOIN_PROOF_WITH_ESCROW_TLV_SIZE);
  487. }
  488. TLVsMsg tlvs_m3field1;
  489. tlv_status = tlvs_m3field1.add_block_cipher_text(msg3_fixed_output.field1_iv, payload_data, payload_size);
  490. ret = tlv_error_2_pve_error(tlv_status);
  491. if(AE_SUCCESS != ret){
  492. AESM_DBG_ERROR("Fail to generate Field3.1 TLV in ProvMsg3:(ae%d)", ret);
  493. break;
  494. }
  495. tlv_status = tlvs_m3field1.add_mac(msg3_fixed_output.field1_mac);
  496. ret = tlv_error_2_pve_error(tlv_status);
  497. if(AE_SUCCESS != ret){
  498. AESM_DBG_ERROR("Fail to generate Field3.2 TLV in ProvMsg3:(ae%d)",ret);
  499. break;
  500. }
  501. tlv_status = tlvs_m3field1.add_nonce(msg3_fixed_output.n2, NONCE_2_SIZE);
  502. ret = tlv_error_2_pve_error(tlv_status);
  503. if(AE_SUCCESS != ret){
  504. AESM_DBG_ERROR("Fail to generate Field3.3 NONCE TLV N2 in ProvMsg3:(ae %d)",ret);
  505. break;
  506. }
  507. tlv_status = tlvs_m3field1.add_cipher_text(msg3_fixed_output.encrypted_pwk2, RSA_3072_KEY_BYTES, PEK_3072_PUB);
  508. ret = tlv_error_2_pve_error(tlv_status);
  509. if(AE_SUCCESS != ret){
  510. AESM_DBG_ERROR("Fail to generate Field3.4 SE Report TLV in ProvMsg3:(ae %d)",ret);
  511. break;
  512. }
  513. tlv_status = tlvs_m3field1.add_pce_report_sign(msg3_fixed_output.pwk2_report.body, ecdsa_sign);
  514. ret = tlv_error_2_pve_error(tlv_status);
  515. if(AE_SUCCESS != ret){
  516. AESM_DBG_ERROR("Fail to generate Field3.5 PCE Report Sign TLV in ProvMsg3:(ae %d)",ret);
  517. break;
  518. }
  519. encrypted_field1 = static_cast<uint8_t *>(malloc(tlvs_m3field1.get_tlv_msg_size()));
  520. if( NULL == encrypted_field1){
  521. AESM_DBG_ERROR("malloc error");
  522. ret = AE_OUT_OF_MEMORY_ERROR;
  523. break;
  524. }
  525. //encrypt field1 using ek2 as key
  526. sgx_status = sgx_rijndael128GCM_encrypt(&ek2,
  527. tlvs_m3field1.get_tlv_msg(), tlvs_m3field1.get_tlv_msg_size(), encrypted_field1,
  528. iv, IV_SIZE, reinterpret_cast<uint8_t *>(msg3_header), PROVISION_REQUEST_HEADER_SIZE,
  529. reinterpret_cast<sgx_aes_gcm_128bit_tag_t *>(mac));
  530. if(AE_SUCCESS != (ret = sgx_error_to_ae_error(sgx_status))){
  531. AESM_DBG_ERROR("fail to encrypt ProvMsg3 body by ek2:(sgx0x%x)",sgx_status);
  532. break;
  533. }
  534. tlv_status = tlvs_msg3.add_block_cipher_text(iv, encrypted_field1, tlvs_m3field1.get_tlv_msg_size());
  535. ret = tlv_error_2_pve_error(tlv_status);
  536. if(AE_SUCCESS != ret){
  537. AESM_DBG_ERROR("Fail to create Field1 TLV of ProvMsg3:(ae%d)",ret);
  538. break;
  539. }
  540. ret = tlv_error_2_pve_error(tlvs_msg3.add_mac(mac));
  541. if(AE_SUCCESS != ret){
  542. AESM_DBG_ERROR("Fail to create Field2 TLV of ProvMsg3:(ae%d)",ret);
  543. break;
  544. }
  545. if(msg3_fixed_output.is_epid_sig_generated){
  546. tlv_status = tlvs_msg3.add_block_cipher_text(msg3_fixed_output.epid_sig_iv, epid_sig, msg3_fixed_output.epid_sig_output_size);
  547. ret = tlv_error_2_pve_error(tlv_status);
  548. if(AE_SUCCESS != ret){
  549. AESM_DBG_ERROR("Fail to create Field3 TLV of ProvMsg3:(ae%d)",ret);
  550. break;
  551. }
  552. tlv_status = tlvs_msg3.add_mac(msg3_fixed_output.epid_sig_mac);
  553. ret = tlv_error_2_pve_error(tlv_status);
  554. if(AE_SUCCESS != ret){
  555. AESM_DBG_ERROR("Fail to create Field4 TLV of ProvMsg3:(ae%d)",ret);
  556. break;
  557. }
  558. }
  559. assert( tlvs_msg3.get_tlv_msg_size() <= msg3_size - PROVISION_REQUEST_HEADER_SIZE);//The checking should have been done in header generation
  560. if(0!=memcpy_s(msg3+PROVISION_REQUEST_HEADER_SIZE, msg3_size-PROVISION_REQUEST_HEADER_SIZE,
  561. tlvs_msg3.get_tlv_msg(), tlvs_msg3.get_tlv_msg_size())){
  562. AESM_DBG_FATAL("memcpy error");
  563. ret = PVE_UNEXPECTED_ERROR;
  564. break;
  565. }
  566. AESM_DBG_TRACE("ProvMsg3 generated successfully");
  567. ret = AE_SUCCESS;
  568. }while(0);
  569. if(decoded_msg2)free(decoded_msg2);
  570. if(encrypted_field1) free(encrypted_field1);
  571. if(epid_sig)free(epid_sig);
  572. return ret;
  573. }