pce.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 "pce_cert.h"
  32. #include "pce_t.c"
  33. #include "aeerror.h"
  34. #include "sgx_utils.h"
  35. #include "sgx_lfence.h"
  36. #include "ipp_wrapper.h"
  37. #include "byte_order.h"
  38. #include "pve_qe_common.h"
  39. #include "arch.h"
  40. #include <assert.h>
  41. ae_error_t get_ppid(ppid_t* ppid);
  42. ae_error_t get_pce_priv_key(const psvn_t* psvn, sgx_ec256_private_t* wrap_key);
  43. #define PCE_RSA_SEED_SIZE 32
  44. #define RSA_MOD_SIZE 384 //hardcode n size to be 384
  45. #define RSA_E_SIZE 4 //hardcode e size to be 4
  46. se_static_assert(RSA_MOD_SIZE == PEK_MOD_SIZE);
  47. //Function to generate Current isvsvn from REPORT
  48. static ae_error_t get_isv_svn(sgx_isv_svn_t* isv_svn)
  49. {
  50. sgx_status_t se_ret = SGX_SUCCESS;
  51. sgx_report_t report;
  52. memset(&report, 0, sizeof(report));
  53. se_ret = sgx_create_report(NULL, NULL, &report);
  54. if(SGX_SUCCESS != se_ret){
  55. (void)memset_s(&report,sizeof(report), 0, sizeof(report));
  56. return PCE_UNEXPECTED_ERROR;
  57. }
  58. memcpy(isv_svn, &report.body.isv_svn, sizeof(report.body.isv_svn));
  59. (void)memset_s(&report, sizeof(report), 0, sizeof(report));
  60. return AE_SUCCESS;
  61. }
  62. //always assume the format of public_key is module n of RSA public key followed by 4 bytes e and both n and e are in Big Endian
  63. uint32_t get_pc_info(const sgx_report_t* report,
  64. const uint8_t *public_key, uint32_t key_size,
  65. uint8_t crypto_suite,
  66. uint8_t *encrypted_ppid, uint32_t encrypted_ppid_buf_size,
  67. uint32_t *encrypted_ppid_out_size,
  68. pce_info_t *pce_info,
  69. uint8_t *signature_scheme)
  70. {
  71. if (report == NULL ||
  72. public_key == NULL ||
  73. encrypted_ppid == NULL ||
  74. encrypted_ppid_out_size == NULL ||
  75. pce_info == NULL||
  76. signature_scheme == NULL){
  77. return AE_INVALID_PARAMETER;
  78. }
  79. if(ALG_RSA_OAEP_3072!=crypto_suite){//The only crypto suite supported in RSA 3072 where 384 bytes module n is used
  80. return AE_INVALID_PARAMETER;
  81. }
  82. //RSA public key is mod || e
  83. if (RSA_MOD_SIZE + RSA_E_SIZE != key_size)
  84. {
  85. return AE_INVALID_PARAMETER;
  86. }
  87. //
  88. // if this mispredicts, we might go past end of
  89. // public_key below
  90. //
  91. sgx_lfence();
  92. *encrypted_ppid_out_size = RSA_MOD_SIZE;//output size is same as public key module size
  93. if (encrypted_ppid_buf_size < RSA_MOD_SIZE){
  94. return AE_INSUFFICIENT_DATA_IN_BUFFER;
  95. }
  96. if(SGX_SUCCESS != sgx_verify_report(report)){
  97. return PCE_INVALID_REPORT;
  98. }
  99. if((report->body.attributes.flags & SGX_FLAGS_PROVISION_KEY) != SGX_FLAGS_PROVISION_KEY){
  100. return PCE_INVALID_PRIVILEGE;
  101. }
  102. uint8_t hash_buf[SGX_REPORT_DATA_SIZE];//hash value only use 32 bytes but data in report has 64 bytes size
  103. se_static_assert(sizeof(hash_buf)>=sizeof(sgx_sha256_hash_t));
  104. memset(hash_buf, 0, sizeof(hash_buf));
  105. sgx_sha_state_handle_t sha_handle = NULL;
  106. sgx_status_t sgx_ret = SGX_ERROR_UNEXPECTED;
  107. do
  108. {
  109. sgx_ret = sgx_sha256_init(&sha_handle);
  110. if (SGX_SUCCESS != sgx_ret)
  111. break;
  112. sgx_ret = sgx_sha256_update(&crypto_suite, sizeof(uint8_t), sha_handle);
  113. if (SGX_SUCCESS != sgx_ret)
  114. break;
  115. sgx_ret = sgx_sha256_update(public_key, RSA_MOD_SIZE + RSA_E_SIZE, sha_handle);
  116. if (SGX_SUCCESS != sgx_ret)
  117. break;
  118. sgx_ret = sgx_sha256_get_hash(sha_handle, reinterpret_cast<sgx_sha256_hash_t *>(hash_buf));
  119. } while (0);
  120. if (sha_handle != NULL)
  121. sgx_sha256_close(sha_handle);
  122. if (SGX_ERROR_OUT_OF_MEMORY == sgx_ret){
  123. return AE_OUT_OF_MEMORY_ERROR;
  124. }
  125. else if (SGX_SUCCESS != sgx_ret){
  126. return AE_FAILURE;
  127. }
  128. //verify the report data is SHA256(crypto_suite||public_key)||0-padding
  129. if(memcmp(hash_buf, &report->body.report_data, sizeof(report->body.report_data))!=0){
  130. return AE_INVALID_PARAMETER;
  131. }
  132. ppid_t ppid_buf;
  133. IppsRSAPublicKeyState *pub_key = NULL;
  134. int pub_key_size = 0;
  135. Ipp8u seeds[PCE_RSA_SEED_SIZE] = { 0 };
  136. uint8_t *pub_key_buffer = NULL;
  137. IppStatus ipp_ret;
  138. uint32_t little_endian_e = 0;
  139. uint8_t *le_n = NULL;
  140. ae_error_t ae_ret = get_ppid(&ppid_buf);
  141. if(ae_ret!=AE_SUCCESS){
  142. goto RETURN_POINT;
  143. }
  144. little_endian_e = lv_ntohl(*(public_key + RSA_MOD_SIZE));
  145. le_n = (uint8_t *)malloc(RSA_MOD_SIZE);
  146. if (le_n == NULL){
  147. ae_ret = AE_OUT_OF_MEMORY_ERROR;
  148. goto RETURN_POINT;
  149. }
  150. for (size_t i = 0; i<RSA_MOD_SIZE; i++){
  151. le_n[i] = *(public_key + RSA_MOD_SIZE - 1 - i);//create little endian n
  152. }
  153. ipp_ret = create_rsa_pub_key(RSA_MOD_SIZE, RSA_E_SIZE,
  154. reinterpret_cast<const Ipp32u *>(le_n),
  155. &little_endian_e,
  156. &pub_key);
  157. free(le_n);
  158. if (ippStsMemAllocErr == ipp_ret){
  159. ae_ret = AE_OUT_OF_MEMORY_ERROR;
  160. goto RETURN_POINT;
  161. }
  162. else if(ippStsNoErr != ipp_ret){//possible invalid rsa public key
  163. ae_ret = AE_FAILURE;
  164. goto RETURN_POINT;
  165. }
  166. ipp_ret = ippsRSA_GetBufferSizePublicKey(&pub_key_size, pub_key);
  167. if (ipp_ret != ippStsNoErr){
  168. ae_ret = AE_FAILURE;
  169. goto RETURN_POINT;
  170. }
  171. if (SGX_SUCCESS != sgx_read_rand(seeds, PCE_RSA_SEED_SIZE)){
  172. ae_ret = AE_READ_RAND_ERROR;
  173. goto RETURN_POINT;
  174. }
  175. pub_key_buffer = (uint8_t *)malloc(pub_key_size);
  176. if (pub_key_buffer == NULL){
  177. ae_ret = AE_OUT_OF_MEMORY_ERROR;
  178. goto RETURN_POINT;
  179. }
  180. ipp_ret = ippsRSAEncrypt_OAEP(reinterpret_cast<const Ipp8u *>(&ppid_buf), sizeof(ppid_buf), NULL, 0, seeds,
  181. encrypted_ppid, pub_key, IPP_ALG_HASH_SHA256, pub_key_buffer);
  182. if (ipp_ret != ippStsNoErr){
  183. ae_ret = AE_FAILURE;
  184. goto RETURN_POINT;
  185. }
  186. ae_ret = get_isv_svn(&pce_info->pce_isvn);
  187. if (ae_ret != AE_SUCCESS){
  188. goto RETURN_POINT;
  189. }
  190. pce_info->pce_id = CUR_PCE_ID;
  191. *signature_scheme = NIST_P256_ECDSA_SHA256;
  192. ae_ret = AE_SUCCESS;
  193. RETURN_POINT:
  194. memset_s(&ppid_buf, sizeof(ppid_buf), 0, sizeof(ppid_t));
  195. if(NULL != pub_key)
  196. secure_free_rsa_pub_key(RSA_MOD_SIZE, RSA_E_SIZE, pub_key);
  197. if (NULL != pub_key_buffer)
  198. free(pub_key_buffer);
  199. if (AE_SUCCESS != ae_ret)
  200. memset_s(encrypted_ppid, encrypted_ppid_buf_size, 0, *encrypted_ppid_out_size);
  201. return ae_ret;
  202. }
  203. uint32_t certify_enclave(const psvn_t* cert_psvn,
  204. const sgx_report_t* report,
  205. uint8_t *signature,
  206. uint32_t signature_buf_size,
  207. uint32_t *signature_out_size)
  208. {
  209. if(cert_psvn==NULL||
  210. report==NULL||
  211. signature == NULL||
  212. signature_out_size == NULL){
  213. return AE_INVALID_PARAMETER;
  214. }
  215. if(signature_buf_size < sizeof(sgx_ec256_signature_t)){
  216. *signature_out_size = sizeof(sgx_ec256_signature_t);
  217. return AE_INSUFFICIENT_DATA_IN_BUFFER;
  218. }
  219. ae_error_t ae_ret = AE_FAILURE;
  220. sgx_ecc_state_handle_t handle=NULL;
  221. sgx_ec256_private_t ec_prv_key = {0};
  222. sgx_status_t sgx_status = SGX_SUCCESS;
  223. if(SGX_SUCCESS != sgx_verify_report(report)){
  224. return PCE_INVALID_REPORT;
  225. }
  226. //only PvE could use the interface which has flag SGX_FLAGS_PROVISION_KEY
  227. if((report->body.attributes.flags & SGX_FLAGS_PROVISION_KEY) != SGX_FLAGS_PROVISION_KEY){
  228. return PCE_INVALID_PRIVILEGE;
  229. }
  230. ae_ret = get_pce_priv_key(cert_psvn, &ec_prv_key);
  231. if(AE_SUCCESS!=ae_ret){
  232. goto ret_point;
  233. }
  234. SWAP_ENDIAN_32B(&ec_prv_key);
  235. sgx_status = sgx_ecc256_open_context(&handle);
  236. if (SGX_ERROR_OUT_OF_MEMORY == sgx_status)
  237. {
  238. ae_ret = AE_OUT_OF_MEMORY_ERROR;
  239. goto ret_point;
  240. }
  241. else if (SGX_SUCCESS != sgx_status) {
  242. ae_ret = AE_FAILURE;
  243. goto ret_point;
  244. }
  245. sgx_status = sgx_ecdsa_sign(reinterpret_cast<const uint8_t *>(&report->body), sizeof(report->body),
  246. &ec_prv_key, reinterpret_cast<sgx_ec256_signature_t *>(signature), handle);
  247. if (SGX_ERROR_OUT_OF_MEMORY == sgx_status)
  248. {
  249. ae_ret = AE_OUT_OF_MEMORY_ERROR;
  250. goto ret_point;
  251. }
  252. else if (SGX_SUCCESS != sgx_status) {
  253. ae_ret = AE_FAILURE;
  254. goto ret_point;
  255. }
  256. //swap from little endian used in sgx_crypto to big endian used in network byte order
  257. SWAP_ENDIAN_32B(reinterpret_cast<sgx_ec256_signature_t *>(signature)->x);
  258. SWAP_ENDIAN_32B(reinterpret_cast<sgx_ec256_signature_t *>(signature)->y);
  259. *signature_out_size = sizeof(sgx_ec256_signature_t);
  260. ae_ret = AE_SUCCESS;
  261. ret_point:
  262. (void)memset_s(&ec_prv_key, sizeof(ec_prv_key),0,sizeof(ec_prv_key));
  263. if(handle!=NULL){
  264. sgx_ecc256_close_context(handle);
  265. }
  266. if(AE_SUCCESS != ae_ret){
  267. (void)memset_s(signature, signature_buf_size, 0, sizeof(sgx_ec256_signature_t));
  268. }
  269. return ae_ret;
  270. }