epid_provision_msg2.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*
  2. * Copyright (C) 2011-2016 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include "type_length_value.h"
  32. #include "sgx_tcrypto_internal.h"
  33. #include "epid_utility.h"
  34. #include "aeerror.h"
  35. #include "PVEClass.h"
  36. #include "aesm_rand.h"
  37. #include "se_wrapper.h"
  38. #include <assert.h>
  39. /**
  40. * File: epid_provision_msg2.cpp
  41. * Description: Provide the untrusted implementation of code to process ProvMsg2
  42. *
  43. * Untrusted Code for EPID Provision
  44. */
  45. #define MSG2_TOP_FIELDS_COUNT_WITH_SIGRL 4
  46. #define MSG2_TOP_FIELDS_COUNT_WITHOUT_SIGRL 3
  47. #define MSG2_TOP_FIELD_NONCE tlvs_msg2[0]
  48. #define MSG2_TOP_FIELD_DATA tlvs_msg2[1]
  49. #define MSG2_TOP_FIELD_MAC tlvs_msg2[2]
  50. #define MSG2_TOP_FIELD_SIGRL tlvs_msg2[3]
  51. #define MSG2_FIELD1_MAX_COUNT 8
  52. #define MSG2_FIELD1_MIN_COUNT 6
  53. #define MSG2_FIELD1_GROUP_CERT tlvs_field1[0]
  54. #define MSG2_FIELD1_NONCE tlvs_field1[1]
  55. #define MSG2_FIELD1_PREV_PSVN tlvs_field1[2]//optional field
  56. #define MSG2_FIELD1_PSID tlvs_field1[psid_index]//psid_index is 2 if optional PREV_PSVN is not present or 3 if it is present
  57. #define MSG2_FIELD1_ENC_TCB tlvs_field1[psid_index+1]
  58. #define MSG2_FIELD1_MAC_TCB tlvs_field1[psid_index+2]
  59. #define MSG2_FIELD1_DEVICE_ID tlvs_field1[psid_index+3]
  60. #define PREV_GID_INDEX (psid_index+4)
  61. #define MSG2_FIELD1_PREV_GID tlvs_field1[PREV_GID_INDEX]
  62. //Function to verify that EPID SigRL type and version is correct for sigrl
  63. static ae_error_t verify_sigrl_cert_type_version(const se_sig_rl_t *sigrl_cert)
  64. {
  65. if(sigrl_cert->epid_identifier!=SE_EPID_SIG_RL_ID||
  66. sigrl_cert->protocol_version!=SE_EPID_SIG_RL_VERSION)
  67. return PVE_INTEGRITY_CHECK_ERROR;
  68. return AE_SUCCESS;
  69. }
  70. static ae_error_t msg2_integrity_checking(const TLVsMsg& tlvs_msg2)
  71. {
  72. uint32_t tlv_count = tlvs_msg2.get_tlv_count();
  73. if(tlv_count != MSG2_TOP_FIELDS_COUNT_WITH_SIGRL && tlv_count != MSG2_TOP_FIELDS_COUNT_WITHOUT_SIGRL)
  74. return PVE_INTEGRITY_CHECK_ERROR;
  75. if(MSG2_TOP_FIELD_NONCE.type != TLV_NONCE || MSG2_TOP_FIELD_NONCE.size != NONCE_SIZE || MSG2_TOP_FIELD_NONCE.version != TLV_VERSION_1)
  76. return PVE_INTEGRITY_CHECK_ERROR;
  77. if(MSG2_TOP_FIELD_NONCE.header_size != SMALL_TLV_HEADER_SIZE)//NONCE TLV has small header size
  78. return PVE_INTEGRITY_CHECK_ERROR;
  79. if(MSG2_TOP_FIELD_DATA.type != TLV_BLOCK_CIPHER_TEXT || MSG2_TOP_FIELD_DATA.version != TLV_VERSION_1)
  80. return PVE_INTEGRITY_CHECK_ERROR;
  81. 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)
  82. return PVE_INTEGRITY_CHECK_ERROR;
  83. if(MSG2_TOP_FIELD_MAC.header_size != SMALL_TLV_HEADER_SIZE)
  84. return PVE_INTEGRITY_CHECK_ERROR;
  85. if(tlv_count == MSG2_TOP_FIELDS_COUNT_WITH_SIGRL){
  86. if(MSG2_TOP_FIELD_SIGRL.type != TLV_EPID_SIG_RL || MSG2_TOP_FIELD_SIGRL.version != TLV_VERSION_1)
  87. return PVE_INTEGRITY_CHECK_ERROR;
  88. if(MSG2_TOP_FIELD_SIGRL.size < 2*SE_ECDSA_SIGN_SIZE + sizeof(se_sig_rl_t))
  89. return PVE_INTEGRITY_CHECK_ERROR;
  90. if(MSG2_TOP_FIELD_SIGRL.header_size != LARGE_TLV_HEADER_SIZE)
  91. return PVE_INTEGRITY_CHECK_ERROR;
  92. return verify_sigrl_cert_type_version(reinterpret_cast<const se_sig_rl_t *>(MSG2_TOP_FIELD_SIGRL.payload));
  93. }
  94. return AE_SUCCESS;
  95. }
  96. //check the format of msg2_field1 and copy correspondent data input msg2_blob_input
  97. 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)
  98. {
  99. uint32_t tlv_count = tlvs_field1.get_tlv_count();
  100. uint32_t psid_index = 2;
  101. msg2_blob_input.is_previous_psvn_provided = false;
  102. if(tlv_count<MSG2_FIELD1_MIN_COUNT||tlv_count>MSG2_FIELD1_MAX_COUNT){
  103. return PVE_MSG_ERROR;
  104. }
  105. uint32_t i;
  106. for(i=0;i<tlv_count;++i)
  107. if(tlvs_field1[i].version != TLV_VERSION_1)
  108. return PVE_MSG_ERROR;
  109. if(MSG2_FIELD1_PREV_PSVN.type == TLV_EPID_PSVN){//EPID_PSVN TLV is available
  110. psid_index++;
  111. msg2_blob_input.is_previous_psvn_provided = true;
  112. if(tlv_count!=MSG2_FIELD1_MAX_COUNT){
  113. return PVE_MSG_ERROR;//make sure the number of TLVs are correct
  114. }
  115. if(MSG2_FIELD1_PREV_PSVN.size != sizeof(psvn_t)){
  116. return PVE_MSG_ERROR;
  117. }
  118. if(MSG2_FIELD1_PREV_PSVN.header_size != SMALL_TLV_HEADER_SIZE){
  119. return PVE_MSG_ERROR;
  120. }
  121. if(MSG2_FIELD1_PREV_GID.type != TLV_EPID_GID ||
  122. MSG2_FIELD1_PREV_GID.size != sizeof(GroupID)){
  123. return PVE_MSG_ERROR;
  124. }
  125. if(MSG2_FIELD1_PREV_GID.header_size != SMALL_TLV_HEADER_SIZE){
  126. return PVE_MSG_ERROR;
  127. }
  128. if(0!=memcpy_s(&msg2_blob_input.previous_gid, sizeof(GroupID), MSG2_FIELD1_PREV_GID.payload, MSG2_FIELD1_PREV_GID.size)){
  129. return PVE_UNEXPECTED_ERROR;
  130. }
  131. if(0!=memcpy_s(&msg2_blob_input.previous_psvn, sizeof(psvn_t), MSG2_FIELD1_PREV_PSVN.payload, MSG2_FIELD1_PREV_PSVN.size)){
  132. return PVE_UNEXPECTED_ERROR;
  133. }
  134. }else if(tlv_count!=MSG2_FIELD1_MIN_COUNT){
  135. return PVE_MSG_ERROR;//make sure number of TLVs are correct
  136. }
  137. if(MSG2_FIELD1_GROUP_CERT.type != TLV_EPID_GROUP_CERT||
  138. MSG2_FIELD1_GROUP_CERT.size != sizeof(signed_epid_group_cert_t)||
  139. MSG2_FIELD1_GROUP_CERT.header_size != SMALL_TLV_HEADER_SIZE)
  140. return PVE_MSG_ERROR;
  141. if(MSG2_FIELD1_NONCE.type != TLV_NONCE||
  142. MSG2_FIELD1_NONCE.size != CHALLENGE_NONCE_SIZE||
  143. MSG2_FIELD1_NONCE.header_size != SMALL_TLV_HEADER_SIZE)
  144. return PVE_MSG_ERROR;
  145. if(MSG2_FIELD1_DEVICE_ID.type != TLV_DEVICE_ID ||
  146. MSG2_FIELD1_DEVICE_ID.size != sizeof(device_id_t)||
  147. MSG2_FIELD1_DEVICE_ID.header_size != SMALL_TLV_HEADER_SIZE)
  148. return PVE_MSG_ERROR;
  149. if(MSG2_FIELD1_ENC_TCB.type != TLV_BLOCK_CIPHER_TEXT||
  150. MSG2_FIELD1_ENC_TCB.size != BLOCK_CIPHER_TEXT_TLV_PAYLOAD_SIZE(SK_SIZE))
  151. return PVE_MSG_ERROR;
  152. if(MSG2_FIELD1_MAC_TCB.type != TLV_MESSAGE_AUTHENTICATION_CODE||
  153. MSG2_FIELD1_MAC_TCB.size != MAC_SIZE||
  154. MSG2_FIELD1_MAC_TCB.header_size != SMALL_TLV_HEADER_SIZE)
  155. return PVE_MSG_ERROR;
  156. if(MSG2_FIELD1_PSID.type != TLV_PS_ID ||
  157. MSG2_FIELD1_PSID.size != sizeof(psid_t)||
  158. MSG2_FIELD1_PSID.header_size != SMALL_TLV_HEADER_SIZE)
  159. return PVE_MSG_ERROR;
  160. sgx_sha256_hash_t psid_hash;
  161. 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));
  162. if(AE_SUCCESS != ret)
  163. return ret;
  164. if(0!=memcmp(&psid_hash, MSG2_FIELD1_PSID.payload, sizeof(psid_hash)))//PSID does not match
  165. return PVE_MSG_ERROR;
  166. tlv_msg_t tcb_data = block_cipher_tlv_get_encrypted_text(MSG2_FIELD1_ENC_TCB);
  167. 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)||
  168. 0!=memcpy_s(&msg2_blob_input.challenge_nonce, CHALLENGE_NONCE_SIZE, MSG2_FIELD1_NONCE.payload, MSG2_FIELD1_NONCE.size)||
  169. 0!=memcpy_s(&msg2_blob_input.equivalent_psvn, sizeof(psvn_t), device_id_tlv_get_psvn(MSG2_FIELD1_DEVICE_ID),sizeof(psvn_t))||
  170. 0!=memcpy_s(&msg2_blob_input.fmsp, sizeof(fmsp_t), device_id_tlv_get_fmsp(MSG2_FIELD1_DEVICE_ID), sizeof(fmsp_t))||
  171. 0!=memcpy_s(&msg2_blob_input.tcb_iv, IV_SIZE, block_cipher_tlv_get_iv(MSG2_FIELD1_ENC_TCB), IV_SIZE)||
  172. 0!=memcpy_s(&msg2_blob_input.encrypted_tcb, SK_SIZE, tcb_data.msg_buf, tcb_data.msg_size)||
  173. 0!=memcpy_s(&msg2_blob_input.tcb_mac, MAC_SIZE, MSG2_FIELD1_MAC_TCB.payload, MSG2_FIELD1_MAC_TCB.size)){
  174. return PVE_UNEXPECTED_ERROR;
  175. }
  176. return AE_SUCCESS;
  177. }
  178. //Function to check message header of ProvMsg2 to determine whether it is valid
  179. //@msg2_header, input the message header of ProvMsg2
  180. //@return AE_SUCCESS if the message header is valid ProvMsg2 or error code if there're any problems
  181. static ae_error_t check_prov_msg2_header(const provision_response_header_t *msg2_header, uint32_t msg2_size)
  182. {
  183. if(msg2_header->protocol != SE_EPID_PROVISIONING || msg2_header->type != TYPE_PROV_MSG2 ||
  184. msg2_header->version != TLV_VERSION_1){
  185. return PVE_INTEGRITY_CHECK_ERROR;
  186. }
  187. uint32_t size_in_header = lv_ntohl(msg2_header->size);
  188. if(size_in_header + PROVISION_RESPONSE_HEADER_SIZE != msg2_size)
  189. return PVE_INTEGRITY_CHECK_ERROR;
  190. return AE_SUCCESS;
  191. }
  192. static uint32_t estimate_epid_sig_size(uint32_t sigrl_size)
  193. {
  194. uint32_t sigrl_body_size = 0;
  195. uint32_t sigrl_extra_size = static_cast<uint32_t>(sizeof(se_sig_rl_t)-sizeof(SigRLEntry)+2*ECDSA_SIGN_SIZE);
  196. if(sigrl_size<=sigrl_extra_size){
  197. return static_cast<uint32_t>(sizeof(EPIDSignature)-sizeof(NRProof));
  198. }else{
  199. sigrl_body_size = sigrl_size - sigrl_extra_size;
  200. size_t entry_count = sigrl_body_size/sizeof(SigRLEntry);
  201. return static_cast<uint32_t>(sizeof(EPIDSignature)-sizeof(NRProof)+sizeof(NRProof)*entry_count);
  202. }
  203. }
  204. 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)
  205. {
  206. msg3_header->protocol = SE_EPID_PROVISIONING;
  207. msg3_header->version = TLV_VERSION_1;
  208. msg3_header->type = TYPE_PROV_MSG3;
  209. size_t field1_size = 0;
  210. if(msg3_output.is_join_proof_generated){
  211. field1_size = BLOCK_CIPHER_TEXT_TLV_SIZE(HARD_CODED_JOIN_PROOF_WITH_ESCROW_TLV_SIZE)+MAC_TLV_SIZE(MAC_SIZE);
  212. }else{
  213. field1_size = BLOCK_CIPHER_TEXT_TLV_SIZE(0)+MAC_TLV_SIZE(MAC_SIZE);
  214. }
  215. size_t total_body_size = NONCE_TLV_SIZE(NONCE_SIZE) + BLOCK_CIPHER_TEXT_TLV_SIZE(field1_size)+MAC_TLV_SIZE(MAC_SIZE);
  216. if(msg3_output.is_epid_sig_generated){
  217. total_body_size += BLOCK_CIPHER_TEXT_TLV_SIZE(msg3_output.epid_sig_output_size)+MAC_TLV_SIZE(MAC_SIZE);
  218. }
  219. uint32_t size_in_net = _htonl(total_body_size);
  220. if(0!=memcpy_s(&msg3_header->size, sizeof(msg3_header->size), &size_in_net, sizeof(size_in_net)))
  221. return PVE_UNEXPECTED_ERROR;//size in Big Endian in message header of ProvMsg3
  222. if(total_body_size>msg3_size - PROVISION_REQUEST_HEADER_SIZE ){//make sure buffer size provided by user is large enough
  223. return PVE_INSUFFICIENT_MEMORY_ERROR;
  224. }
  225. if(0!=memcpy_s(msg3_header->xid, XID_SIZE, xid, XID_SIZE))
  226. return PVE_UNEXPECTED_ERROR; //copy transaction id of ProvMsg2
  227. msg3_size = static_cast<uint32_t>(total_body_size + PROVISION_REQUEST_HEADER_SIZE);
  228. return AE_SUCCESS;
  229. }
  230. //Function to decode ProvMsg2 and generate ProvMsg3 on success
  231. //ProvMsg2 Format:
  232. //R, E+MAC(**), [SigRL with ECDSA Sig]
  233. //@msg2, the input buffer for ProvMsg2 in TLV encoded format
  234. //@msg2_size, size of buffer msg2
  235. //@pek, the input pek got from endpoint selection protocol
  236. //@epid_blob, input an optional old epid data blob used to generate non-revoke proof if required
  237. //@blob_size, the buffer size of epid_blob
  238. //@ek2, an output parameter for EK2 used in provision protocol which could be reused in proc_prov_msg4
  239. //@previous_psvn, an optional output buffer for Previous SigRL PSVN if old epid blob is missing or invalid
  240. // so that caller could repeat ProvMsg1 and ProvMsg4 to retrieve backuped old epid data blob
  241. //@msg3, output the ProvMsg3 in TLV encoded format
  242. //@msg3_size: input the size of buffer msg3
  243. //@return AE_SUCCESS on success and error code on failure
  244. // PVE_EPID_BLOB_ERROR is returned if old_epid_blob is required but it is invalid or not provided and
  245. // previous_psvn will be filled in by a Previous SigRL PSVN
  246. uint32_t CPVEClass::proc_prov_msg2(
  247. const uint8_t* msg2,
  248. uint32_t msg2_size,
  249. const signed_pek_t& pek,
  250. const uint8_t* epid_blob,
  251. uint32_t blob_size,
  252. uint8_t ek2[SK_SIZE],
  253. psvn_t* previous_psvn,
  254. uint8_t* msg3,
  255. uint32_t msg3_size)
  256. {
  257. ae_error_t ret = AE_SUCCESS;
  258. const se_sig_rl_t *sigrl = NULL;
  259. uint32_t sigrl_size = 0;
  260. uint8_t *epid_sig = NULL;
  261. uint8_t *decoded_msg2 = NULL;
  262. uint8_t *encrypted_field1 = NULL;
  263. uint8_t aad[PROVISION_RESPONSE_HEADER_SIZE+sizeof(RLver_t)+sizeof(GroupID)];
  264. size_t aad_size = PROVISION_RESPONSE_HEADER_SIZE;
  265. const provision_response_header_t *msg2_header = reinterpret_cast<const provision_response_header_t *>(msg2);
  266. provision_request_header_t *msg3_header = reinterpret_cast<provision_request_header_t *>(msg3);
  267. if(msg2_size < PROVISION_RESPONSE_HEADER_SIZE){
  268. AESM_DBG_ERROR("ProvMsg2 size too small");
  269. return PVE_MSG_ERROR;
  270. }
  271. if(msg3_size <PROVISION_REQUEST_HEADER_SIZE){
  272. AESM_DBG_ERROR("Input ProvMsg3 buffer too small");
  273. return PVE_INSUFFICIENT_MEMORY_ERROR;
  274. }
  275. ret = check_prov_msg2_header(msg2_header, msg2_size);//process message header
  276. if( AE_SUCCESS != ret){
  277. AESM_DBG_ERROR("Fail to decode ProvMsg2:%d",ret);
  278. return ret;
  279. }
  280. ret = check_epid_pve_pg_status_before_mac_verification(msg2_header);
  281. if( AE_SUCCESS != ret){
  282. AESM_DBG_ERROR("Backend server reported error in ProvMsg2:%d",ret);
  283. return ret;
  284. }
  285. if( 0!=memcpy_s(aad, sizeof(aad), msg2_header, PROVISION_RESPONSE_HEADER_SIZE)){
  286. AESM_DBG_FATAL("memcpy error");
  287. return PVE_UNEXPECTED_ERROR;
  288. }
  289. do{
  290. TLVsMsg tlvs_msg2;
  291. tlv_status_t tlv_status;
  292. //decode TLV structure of message body
  293. 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));
  294. ret = tlv_error_2_pve_error(tlv_status);
  295. if(AE_SUCCESS!=ret){
  296. AESM_DBG_ERROR("Fail to decode ProvMsg2:%d",ret);
  297. break;
  298. }
  299. ret = msg2_integrity_checking(tlvs_msg2);//checking and verifying that TLV structure is correct and version is supported
  300. if(AE_SUCCESS!=ret){
  301. AESM_DBG_ERROR("ProvMsg2 integrity checking error:%d",ret);
  302. break;
  303. }
  304. prov_get_ek2_input_t ek2_input;
  305. if(memcpy_s(ek2_input.nonce, NONCE_SIZE, MSG2_TOP_FIELD_NONCE.payload, NONCE_SIZE)!=0){
  306. AESM_DBG_FATAL("memcpy error");
  307. ret = PVE_UNEXPECTED_ERROR;
  308. break;
  309. }
  310. if(memcpy_s(ek2_input.xid, XID_SIZE, msg2_header->xid, XID_SIZE)!=0){
  311. AESM_DBG_FATAL("memcpy error");
  312. ret = PVE_UNEXPECTED_ERROR;
  313. break;
  314. }
  315. //call PvE to get EK2
  316. se_static_assert(SK_SIZE == sizeof(prov_get_ek2_output_t));
  317. ret = static_cast<ae_error_t>(get_ek2(&ek2_input, reinterpret_cast<prov_get_ek2_output_t *>(ek2)));
  318. if(AE_SUCCESS != ret){
  319. AESM_DBG_ERROR("Fail to get EK2:%d",ret);
  320. break;
  321. }
  322. if(tlvs_msg2.get_tlv_count() == MSG2_TOP_FIELDS_COUNT_WITH_SIGRL){//RLver and gid is added as part of AAD if available
  323. sigrl = reinterpret_cast<const se_sig_rl_t *>(MSG2_TOP_FIELD_SIGRL.payload);
  324. if(0!=memcpy_s(aad+PROVISION_RESPONSE_HEADER_SIZE, sizeof(RLver_t), &sigrl->sig_rl.RLver, sizeof(RLver_t))){
  325. AESM_DBG_FATAL("memcpy error");
  326. ret = PVE_UNEXPECTED_ERROR;
  327. break;
  328. }
  329. if(0!=memcpy_s(aad+PROVISION_RESPONSE_HEADER_SIZE+sizeof(RLver_t), sizeof(GroupID), &sigrl->sig_rl.gid, sizeof(GroupID))){
  330. AESM_DBG_FATAL("memcpy error");
  331. ret = PVE_UNEXPECTED_ERROR;
  332. break;
  333. }
  334. aad_size += sizeof(RLver_t)+sizeof(GroupID);
  335. sigrl_size = MSG2_TOP_FIELD_SIGRL.size;
  336. }
  337. se_static_assert(SK_SIZE==sizeof(sgx_aes_gcm_128bit_key_t));
  338. tlv_msg_t field1 = block_cipher_tlv_get_encrypted_text(MSG2_TOP_FIELD_DATA);
  339. decoded_msg2 = reinterpret_cast<uint8_t *>(malloc(field1.msg_size));
  340. if(NULL == decoded_msg2){
  341. AESM_DBG_ERROR("malloc error");
  342. ret= AE_OUT_OF_MEMORY_ERROR;
  343. break;
  344. }
  345. //decrypt ProvMsg2 by EK2
  346. sgx_status_t sgx_status = sgx_rijndael128GCM_decrypt(reinterpret_cast<const sgx_aes_gcm_128bit_key_t *>(ek2),
  347. field1.msg_buf, field1.msg_size, decoded_msg2,
  348. reinterpret_cast<uint8_t *>(block_cipher_tlv_get_iv(MSG2_TOP_FIELD_DATA)), IV_SIZE,
  349. aad, static_cast<uint32_t>(aad_size), reinterpret_cast<const sgx_aes_gcm_128bit_tag_t *>(MSG2_TOP_FIELD_MAC.payload));
  350. if(SGX_ERROR_MAC_MISMATCH == sgx_status){
  351. AESM_DBG_ERROR("Fail to decrypt ProvMsg2 body by EK2");
  352. ret = PVE_INTEGRITY_CHECK_ERROR;
  353. break;
  354. }
  355. if( AE_SUCCESS != (ret = sgx_error_to_ae_error(sgx_status))){
  356. AESM_DBG_ERROR("error in decrypting ProvMsg2 body:%d",sgx_status);
  357. break;
  358. }
  359. ret = check_epid_pve_pg_status_after_mac_verification(msg2_header);
  360. if(AE_SUCCESS != ret){
  361. AESM_DBG_ERROR("Backend server reported error in ProvMsg2 passed MAC verification:%d",ret);
  362. break;
  363. }
  364. TLVsMsg tlvs_field1;
  365. tlv_status = tlvs_field1.init_from_buffer(decoded_msg2, field1.msg_size);//decode TLV structure of field1 of ProvMsg2
  366. ret = tlv_error_2_pve_error(tlv_status);
  367. if(AE_SUCCESS!=ret){
  368. AESM_DBG_ERROR("Fail to decode field1 of ProvMsg2:%d",tlv_status);
  369. break;
  370. }
  371. proc_prov_msg2_blob_input_t msg2_blob_input;
  372. ret = msg2_field1_msg_check_copy(tlvs_field1, msg2_blob_input, pek);//version/type checking to verify message format
  373. if( AE_SUCCESS != ret){
  374. AESM_DBG_ERROR("field1 of ProvMsg2 checking error:%d",ret);
  375. break;
  376. }
  377. gen_prov_msg3_output_t msg3_fixed_output;
  378. memset(&msg3_fixed_output, 0, sizeof(msg3_fixed_output));
  379. //collect old epid blob
  380. if(epid_blob==NULL){
  381. memset(msg2_blob_input.old_epid_data_blob, 0, HARD_CODED_EPID_BLOB_SIZE);
  382. }else if(blob_size!=HARD_CODED_EPID_BLOB_SIZE){
  383. AESM_DBG_FATAL("epid blob internal size error");
  384. ret = PVE_UNEXPECTED_ERROR;
  385. break;
  386. }else{
  387. #ifdef DBG_LOG
  388. {
  389. char dbg_str[256];
  390. aesm_dbg_format_hex(reinterpret_cast<const uint8_t *>(&epid_blob), blob_size, dbg_str, 256);
  391. AESM_DBG_TRACE("old epid blob=%s",dbg_str);
  392. }
  393. #endif
  394. if(0!=memcpy_s(msg2_blob_input.old_epid_data_blob, HARD_CODED_EPID_BLOB_SIZE, epid_blob, blob_size)){
  395. AESM_DBG_FATAL("memcpy error");
  396. ret = PVE_UNEXPECTED_ERROR;
  397. break;
  398. }
  399. }
  400. uint32_t epid_sig_output_size = estimate_epid_sig_size(sigrl_size) + MAX_TLV_HEADER_SIZE;
  401. epid_sig = reinterpret_cast<uint8_t *>(malloc(epid_sig_output_size));
  402. if(NULL == epid_sig){
  403. AESM_DBG_ERROR("malloc error");
  404. ret = AE_OUT_OF_MEMORY_ERROR;
  405. break;
  406. }
  407. ret = (ae_error_t)proc_prov_msg2_data(&msg2_blob_input, reinterpret_cast<const uint8_t *>(sigrl), sigrl_size,
  408. &msg3_fixed_output, epid_sig, epid_sig_output_size);//ecall to process msg2 data and generate msg3 data in PvE
  409. if( PVE_EPIDBLOB_ERROR == ret){
  410. if(previous_psvn == NULL){
  411. AESM_DBG_ERROR("PvE requires previous PSVN but it is not provided");
  412. ret = PVE_PARAMETER_ERROR;
  413. break;
  414. }else{//output previous svn in correspondent to sigrl in epid blob error
  415. if(0!=memcpy_s(previous_psvn, sizeof(psvn_t), &msg2_blob_input.previous_psvn, sizeof(psvn_t))){
  416. AESM_DBG_FATAL("memcpy error");
  417. ret = PVE_UNEXPECTED_ERROR;
  418. break;
  419. }
  420. }
  421. }
  422. if(AE_SUCCESS != ret){
  423. AESM_DBG_ERROR("PvE report error %d in processing ProvMsg2",ret);
  424. break;
  425. }
  426. uint8_t iv[IV_SIZE];
  427. uint8_t mac[MAC_SIZE];
  428. uint8_t *payload_data=NULL;
  429. uint32_t payload_size = 0;
  430. ret = aesm_read_rand(iv, IV_SIZE);
  431. if(AE_SUCCESS != ret){
  432. AESM_DBG_ERROR("fail to generate random number:%d",ret);
  433. break;
  434. }
  435. //Now start to generate ProvMsg3
  436. ret = gen_msg3_header(msg3_fixed_output, ek2_input.xid, msg3_header,msg3_size);//first generate header
  437. if( AE_SUCCESS != ret){
  438. AESM_DBG_ERROR("Fail to generate ProvMsg3 Header:%d",ret);
  439. break;
  440. }
  441. TLVsMsg tlvs_msg3;
  442. tlv_status = tlvs_msg3.add_nonce(ek2_input.nonce, NONCE_SIZE);
  443. ret = tlv_error_2_pve_error(tlv_status);
  444. if(AE_SUCCESS!=ret){
  445. AESM_DBG_ERROR("Fail to generate Nonce TLV in ProvMsg3:%d",tlv_status);
  446. break;
  447. }
  448. if(msg3_fixed_output.is_join_proof_generated){
  449. payload_data = msg3_fixed_output.field1_data;
  450. payload_size = static_cast<uint32_t>(HARD_CODED_JOIN_PROOF_WITH_ESCROW_TLV_SIZE);
  451. }
  452. TLVsMsg tlvs_m3field1;
  453. tlv_status = tlvs_m3field1.add_block_cipher_text(msg3_fixed_output.field1_iv, payload_data, payload_size);
  454. ret = tlv_error_2_pve_error(tlv_status);
  455. if(AE_SUCCESS != ret){
  456. AESM_DBG_ERROR("Fail to generate Field3.1 TLV in ProvMsg3:%d",tlv_status);
  457. break;
  458. }
  459. tlv_status = tlvs_m3field1.add_mac(msg3_fixed_output.field1_mac);
  460. ret = tlv_error_2_pve_error(tlv_status);
  461. if(AE_SUCCESS != ret){
  462. AESM_DBG_ERROR("Fail to generate Field3.2 TLV in ProvMsg3:%d",tlv_status);
  463. break;
  464. }
  465. encrypted_field1 = reinterpret_cast<uint8_t *>(malloc(tlvs_m3field1.get_tlv_msg_size()));
  466. if( NULL == encrypted_field1){
  467. AESM_DBG_ERROR("malloc error");
  468. ret = AE_OUT_OF_MEMORY_ERROR;
  469. break;
  470. }
  471. //encrypt field1 using ek2 as key
  472. sgx_status = sgx_rijndael128GCM_encrypt(reinterpret_cast<const sgx_aes_gcm_128bit_key_t *>(ek2),
  473. tlvs_m3field1.get_tlv_msg(), tlvs_m3field1.get_tlv_msg_size(), encrypted_field1,
  474. iv, IV_SIZE, reinterpret_cast<uint8_t *>(msg3_header), PROVISION_REQUEST_HEADER_SIZE,
  475. reinterpret_cast<sgx_aes_gcm_128bit_tag_t *>(mac));
  476. if(AE_SUCCESS != (ret = sgx_error_to_ae_error(sgx_status))){
  477. AESM_DBG_ERROR("fail to encrypting ProvMsg3 body by ek2:%d",sgx_status);
  478. break;
  479. }
  480. tlv_status = tlvs_msg3.add_block_cipher_text(iv, encrypted_field1, tlvs_m3field1.get_tlv_msg_size());
  481. ret = tlv_error_2_pve_error(tlv_status);
  482. if(AE_SUCCESS != ret){
  483. AESM_DBG_ERROR("Fail to create Field1 TLV of ProvMsg3:%d",tlv_status);
  484. break;
  485. }
  486. ret = tlv_error_2_pve_error(tlvs_msg3.add_mac(mac));
  487. if(AE_SUCCESS != ret){
  488. AESM_DBG_ERROR("Fail to create Field2 TLV of ProvMsg3:%d",ret);
  489. break;
  490. }
  491. if(msg3_fixed_output.is_epid_sig_generated){
  492. tlv_status = tlvs_msg3.add_block_cipher_text(msg3_fixed_output.epid_sig_iv, epid_sig, msg3_fixed_output.epid_sig_output_size);
  493. ret = tlv_error_2_pve_error(tlv_status);
  494. if(AE_SUCCESS != ret){
  495. AESM_DBG_ERROR("Fail to create Field3 TLV of ProvMsg3:%d",tlv_status);
  496. break;
  497. }
  498. tlv_status = tlvs_msg3.add_mac(msg3_fixed_output.epid_sig_mac);
  499. ret = tlv_error_2_pve_error(tlv_status);
  500. if(AE_SUCCESS != ret){
  501. AESM_DBG_ERROR("Fail to create Field4 TLV of ProvMsg3:%d",tlv_status);
  502. break;
  503. }
  504. }
  505. assert( tlvs_msg3.get_tlv_msg_size() <= msg3_size - PROVISION_REQUEST_HEADER_SIZE);//The checking should have been done in header generation
  506. if(0!=memcpy_s(msg3+PROVISION_REQUEST_HEADER_SIZE, msg3_size-PROVISION_REQUEST_HEADER_SIZE,
  507. tlvs_msg3.get_tlv_msg(), tlvs_msg3.get_tlv_msg_size())){
  508. AESM_DBG_FATAL("memcpy error");
  509. ret = PVE_UNEXPECTED_ERROR;
  510. break;
  511. }
  512. AESM_DBG_TRACE("ProvMsg3 generated successfully");
  513. ret = AE_SUCCESS;
  514. }while(0);
  515. if(decoded_msg2)free(decoded_msg2);
  516. if(encrypted_field1) free(encrypted_field1);
  517. if(epid_sig)free(epid_sig);
  518. return ret;
  519. }