provision_msg4.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 "provision_msg.h"
  32. #include <sgx_trts.h>
  33. #include "protocol.h"
  34. #include "cipher.h"
  35. #include "sgx_tcrypto.h"
  36. #include "epid/common/errors.h"
  37. #include "epid/member/api.h"
  38. #include "pve_hardcoded_tlv_data.h"
  39. #include "byte_order.h"
  40. #include "pek_pub_key.h"
  41. #include "pve_qe_common.h"
  42. #include <string.h>
  43. #include <stdlib.h>
  44. #include "util.h"
  45. /**
  46. * File: provision_msg4.cpp
  47. * Description: Provide the implementation of code to process data from ProvMsg4 and generate EPID Data Blob
  48. *
  49. * Core Code of Provision Enclave
  50. */
  51. //Function to verify and process ProvMsg4.field1_2 and decrypt mce to get f part of EPID Private Key by PSK
  52. // to generate PrivKey
  53. //@mce: input pointer to decrypted membership credential and escrow data
  54. //@msg4_input: input data decoded from ProvMsg4
  55. //@prv_key: output the EPID Private Key on success
  56. //@return PVEC_SUCCESS on success and error code otherwise
  57. static pve_status_t proc_prov_msg4_membercredential(const membership_credential_with_escrow_t *mce,
  58. const proc_prov_msg4_input_t * msg4_input,
  59. PrivKey& prv_key)
  60. {
  61. pve_status_t ret = PVEC_SUCCESS;
  62. sgx_status_t sgx_status = SGX_SUCCESS;
  63. sgx_key_128bit_t psk;
  64. ret = get_pve_psk(&msg4_input->equivalent_psvn, &psk);//generate Provisioning Seal Key used to unseal f
  65. if(PVEC_SUCCESS != ret){
  66. goto ret_point;
  67. }
  68. if(mce->escrow.version!=0){//only support escrow version 0 now
  69. ret = PVEC_MSG_ERROR;
  70. goto ret_point;
  71. }
  72. //Decrypt private key f (which had been sealed in ProvMsg3)
  73. sgx_status = sgx_rijndael128GCM_decrypt(reinterpret_cast<const sgx_aes_gcm_128bit_key_t *>(&psk),
  74. reinterpret_cast<const uint8_t *>(&mce->escrow.f), sizeof(FpElemStr), reinterpret_cast<uint8_t *>(&prv_key.f),
  75. mce->escrow.iv, IV_SIZE, NULL, 0,
  76. reinterpret_cast<const sgx_aes_gcm_128bit_tag_t *>(mce->escrow.mac));
  77. if(SGX_ERROR_MAC_MISMATCH == sgx_status){
  78. ret = PVEC_MSG_ERROR;
  79. goto ret_point;
  80. }else if(SGX_SUCCESS != sgx_status){
  81. ret = sgx_error_to_pve_error(sgx_status);
  82. goto ret_point;
  83. }
  84. //copy A, x and gid
  85. memcpy(&prv_key.A, &mce->A,sizeof(prv_key.A));
  86. memcpy(&prv_key.x, &mce->x, sizeof(prv_key.x));
  87. memcpy(&prv_key.gid, &msg4_input->group_cert.key.gid, sizeof(GroupId));
  88. ret_point:
  89. if(PVEC_SUCCESS != ret){
  90. (void)memset_s(&prv_key, sizeof(prv_key),0, sizeof(prv_key));
  91. }
  92. (void)memset_s(&psk,sizeof(psk),0,sizeof(psk));
  93. return ret;
  94. }
  95. //check whether prv key is valid and generate epid blob if PrivKey is valid
  96. //@prv_key: input the EPID Private Key
  97. //@psvn: the equivalent psvn
  98. //@pub_key: input the EPID group public Key
  99. //@epid_blob: output the EPID_DATA_BLOB on success, the size of the buffer has been checked before calling to the function
  100. //@return PVEC_SUCCESS on success or error code on failure
  101. static pve_status_t gen_epid_blob(const extended_epid_group_blob_t* pxegb,
  102. const PrivKey *prv_key,
  103. const psvn_t *psvn,
  104. const GroupPubKey *pub_key,
  105. sgx_sealed_data_t *epid_blob)
  106. {
  107. uint8_t *tmp_buffer = NULL;
  108. pve_status_t ret = PVEC_SUCCESS;
  109. sgx_status_t sgx_status = SGX_SUCCESS;
  110. EpidStatus epid_ret = kEpidNoErr;
  111. MemberCtx *p_epid_context = NULL;
  112. se_secret_epid_data_sdk_t *epid_data = NULL;
  113. se_plaintext_epid_data_sdk_t *plaintext = NULL;
  114. uint32_t tmp_buffer_size = static_cast<uint32_t>(sizeof(se_plaintext_epid_data_sdk_t) + sizeof(se_secret_epid_data_sdk_t));
  115. if(!EpidIsPrivKeyInGroup(pub_key, prv_key)){
  116. ret = PVEC_MSG_ERROR;
  117. goto ret_point;
  118. }
  119. //alloc one temp buffer to hold both plaintext and secret data before encryption
  120. //all data before encryption will be copied into the buffer inorder
  121. tmp_buffer = reinterpret_cast<uint8_t *>(malloc(tmp_buffer_size));
  122. if(!tmp_buffer)
  123. {
  124. ret =PVEC_MALLOC_ERROR;
  125. goto ret_point;
  126. }
  127. memset(tmp_buffer,0,tmp_buffer_size);
  128. plaintext = reinterpret_cast<se_plaintext_epid_data_sdk_t *>(tmp_buffer);//plaintext in the beginning of the buffer
  129. //group cert as part of aad
  130. memcpy(&plaintext->epid_group_cert, pub_key, sizeof(*pub_key));
  131. //PVE ISVSVN, CPUSVN and some public key from xegb in aad too
  132. epid_data = reinterpret_cast<se_secret_epid_data_sdk_t*>(plaintext + 1);//secret data in same buffer
  133. memcpy(&plaintext->equiv_cpu_svn, &psvn->cpu_svn, SGX_CPUSVN_SIZE); //equivalent CPUSVN and ISVSVN from PSVN of ProvMsgs used in AAD
  134. memcpy(&plaintext->equiv_pve_isv_svn, &psvn->isv_svn, sizeof(plaintext->equiv_pve_isv_svn));
  135. plaintext->seal_blob_type = PVE_SEAL_EPID_KEY_BLOB;
  136. plaintext->epid_key_version = EPID_KEY_BLOB_VERSION_SDK;
  137. plaintext->xeid = pxegb->xeid;
  138. memcpy(&plaintext->qsdk_exp, pxegb->qsdk_exp, sizeof(plaintext->qsdk_exp));
  139. memcpy(&plaintext->qsdk_mod, pxegb->qsdk_mod, sizeof(plaintext->qsdk_mod));
  140. memcpy(&plaintext->epid_sk, pxegb->epid_sk, sizeof(plaintext->epid_sk));
  141. memcpy(&epid_data->epid_private_key,prv_key,sizeof(PrivKey)); //EPID Private key is sealed in EPID_DATA_BLOB together with Member Precomputation
  142. epid_ret = EpidMemberCreate(&(plaintext->epid_group_cert),
  143. (PrivKey*)&(epid_data->epid_private_key),
  144. NULL,
  145. epid_random_func,
  146. NULL,
  147. &p_epid_context);
  148. if(kEpidNoErr!=epid_ret){
  149. ret = epid_error_to_pve_error(epid_ret);
  150. goto ret_point;
  151. }
  152. epid_ret = EpidMemberWritePrecomp(p_epid_context, &epid_data->member_precomp_data);//Create Member Precomputation
  153. if(kEpidNoErr!=epid_ret){
  154. ret = epid_error_to_pve_error(epid_ret);
  155. goto ret_point;
  156. }
  157. EpidMemberDelete(&p_epid_context);
  158. //call sgx_seal_data to generate EPID_DATA_BLOB
  159. if((sgx_status=sgx_seal_data(
  160. sizeof(se_plaintext_epid_data_sdk_t), reinterpret_cast<uint8_t*>(plaintext),//plaintext as AAD
  161. sizeof(se_secret_epid_data_sdk_t), reinterpret_cast<uint8_t*>(epid_data), //secret data to SEAL
  162. SGX_TRUSTED_EPID_BLOB_SIZE_SDK,
  163. epid_blob))!=SGX_SUCCESS){//generate EPID_DATA_BLOB in epid_blob
  164. ret = sgx_error_to_pve_error(sgx_status);
  165. goto ret_point;
  166. }
  167. ret_point:
  168. if(tmp_buffer){
  169. //reset memory to 0 of the temp buffer to defense in depth
  170. (void)memset_s(tmp_buffer,tmp_buffer_size, 0, tmp_buffer_size);
  171. free(tmp_buffer);
  172. }
  173. return ret;
  174. }
  175. //Function to process the decoded data from ProvMsg4 and generate EPID data blob
  176. //It will create EPIDDataBlob in output parameter epid_blob on success
  177. //@msg_input: input data decoded from ProvMsg4
  178. //@epid_blob: output buffer to hold the generated EPID_DATA_BLOB on success
  179. //@return PVEC_SUCCESS on success and error code otherwise
  180. pve_status_t proc_prov_msg4_data(const proc_prov_msg4_input_t *msg4_input,
  181. sgx_sealed_data_t *epid_blob)
  182. {
  183. uint8_t member_escrow_tlv_buf[MEMBERSHIP_CREDENTIAL_TLV_TOTAL_SIZE];
  184. pve_status_t ret = PVEC_SUCCESS;
  185. sgx_status_t sgx_status = SGX_SUCCESS;
  186. uint8_t pek_result = SGX_EC_INVALID_SIGNATURE;
  187. sgx_key_128bit_t pwk2;
  188. PrivKey prv_key;
  189. uint8_t aad_buf[sizeof(device_id_t)+sizeof(GroupId)];
  190. extended_epid_group_blob_t local_xegb;
  191. device_id_t *device_id_in_aad = reinterpret_cast<device_id_t *>(aad_buf+sizeof(GroupId));
  192. membership_credential_with_escrow_t *mce = reinterpret_cast<membership_credential_with_escrow_t *>(member_escrow_tlv_buf+MEMBERSHIP_CREDENTIAL_TLV_HEADER_SIZE);
  193. memset(member_escrow_tlv_buf, 0, sizeof(member_escrow_tlv_buf));
  194. memset(aad_buf, 0 ,sizeof(aad_buf));
  195. memset(&pwk2, 0, sizeof(pwk2));
  196. memset(&prv_key, 0, sizeof(prv_key));
  197. sgx_status = verify_xegb_with_default(msg4_input->xegb, &pek_result,local_xegb);
  198. if(SGX_SUCCESS != sgx_status){
  199. ret = sgx_error_to_pve_error(sgx_status);
  200. goto ret_point;
  201. }else if(pek_result != SGX_EC_VALID){
  202. ret = PVEC_XEGDSK_SIGN_ERROR;
  203. goto ret_point;
  204. }
  205. //Verify it is signed by EPID Signing key
  206. ret = check_signature_of_group_pub_cert(&msg4_input->group_cert, local_xegb.epid_sk);
  207. if (PVEC_SUCCESS != ret){
  208. goto ret_point;
  209. }
  210. //create PWK2
  211. ret = get_pwk2(&msg4_input->equivalent_psvn, msg4_input->n2, &pwk2);
  212. if (PVEC_SUCCESS != ret){
  213. goto ret_point;
  214. }
  215. //preparing device id for AAD
  216. memcpy(aad_buf, &msg4_input->group_cert.key.gid, sizeof(GroupId));
  217. memcpy(&device_id_in_aad->fmsp, &msg4_input->fmsp, sizeof(fmsp_t));
  218. memcpy(&device_id_in_aad->psvn, &msg4_input->equivalent_psvn, sizeof(psvn_t));
  219. memset(&device_id_in_aad->ppid, 0 ,sizeof(ppid_t));
  220. se_static_assert(sizeof(sgx_aes_gcm_128bit_key_t)==sizeof(pwk2)); /*SK_SIZE should be same as that of sgx_aes_gcm_128bit_key_t*/
  221. se_static_assert(sizeof(sgx_aes_gcm_128bit_tag_t)==sizeof(msg4_input->member_credential_mac)); /*member_credential_mac size should be same as that of sgx_aes_gcm_128bit_tag_t*/
  222. se_static_assert(HARD_CODED_EPID_MEMBER_WITH_ESCROW_TLV_SIZE == MEMBERSHIP_CREDENTIAL_TLV_TOTAL_SIZE); /*hardcoded size should be matched*/
  223. sgx_status = sgx_rijndael128GCM_decrypt(reinterpret_cast<sgx_aes_gcm_128bit_key_t *>(&pwk2),
  224. msg4_input->encrypted_member_credential,static_cast<uint32_t>(HARD_CODED_EPID_MEMBER_WITH_ESCROW_TLV_SIZE), member_escrow_tlv_buf,
  225. msg4_input->member_credential_iv, IV_SIZE, aad_buf, sizeof(aad_buf),
  226. reinterpret_cast<const sgx_aes_gcm_128bit_tag_t *>(msg4_input->member_credential_mac));//decrypt membership credential and escrow data TLV
  227. if(sgx_status == SGX_ERROR_MAC_MISMATCH){
  228. ret = PVEC_MSG_ERROR;
  229. goto ret_point;
  230. }else if(sgx_status != SGX_SUCCESS){
  231. ret = sgx_error_to_pve_error(sgx_status);
  232. goto ret_point;
  233. }
  234. if(memcmp(member_escrow_tlv_buf, MEMBERSHIP_CREDENTIAL_TLV_HEADER, MEMBERSHIP_CREDENTIAL_TLV_HEADER_SIZE)!=0){//checking TLV header matches hard-coded value
  235. ret = PVEC_MSG_ERROR;
  236. goto ret_point;
  237. }
  238. se_static_assert(sizeof(membership_credential_with_escrow_t)+MEMBERSHIP_CREDENTIAL_TLV_HEADER_SIZE==MEMBERSHIP_CREDENTIAL_TLV_TOTAL_SIZE); /*invalid hard-coded value*/
  239. ret = proc_prov_msg4_membercredential(mce, msg4_input, prv_key);//decrypt and generate epid private key
  240. if(PVEC_SUCCESS!=ret){
  241. goto ret_point;
  242. }
  243. ret = gen_epid_blob(&local_xegb, &prv_key, &msg4_input->equivalent_psvn, &msg4_input->group_cert.key, epid_blob);//seal epid private key to generate EPID data blob
  244. ret_point:
  245. //now clear secret data from memory to defense in depth
  246. (void)memset_s(&pwk2,sizeof(pwk2), 0, sizeof(pwk2));
  247. (void)memset_s(&prv_key, sizeof(prv_key), 0, sizeof(prv_key));
  248. (void)memset_s(member_escrow_tlv_buf, sizeof(member_escrow_tlv_buf), 0, sizeof(member_escrow_tlv_buf));
  249. (void)memset_s(aad_buf, sizeof(aad_buf), 0, sizeof(aad_buf));
  250. return ret;
  251. }