provision_msg4.cpp 12 KB

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