pce.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. (report->body.attributes.flags & SGX_FLAGS_DEBUG) != 0){
  101. return PCE_INVALID_PRIVILEGE;
  102. }
  103. uint8_t hash_buf[SGX_REPORT_DATA_SIZE];//hash value only use 32 bytes but data in report has 64 bytes size
  104. se_static_assert(sizeof(hash_buf)>=sizeof(sgx_sha256_hash_t));
  105. memset(hash_buf, 0, sizeof(hash_buf));
  106. sgx_sha_state_handle_t sha_handle = NULL;
  107. sgx_status_t sgx_ret = SGX_ERROR_UNEXPECTED;
  108. do
  109. {
  110. sgx_ret = sgx_sha256_init(&sha_handle);
  111. if (SGX_SUCCESS != sgx_ret)
  112. break;
  113. sgx_ret = sgx_sha256_update(&crypto_suite, sizeof(uint8_t), sha_handle);
  114. if (SGX_SUCCESS != sgx_ret)
  115. break;
  116. sgx_ret = sgx_sha256_update(public_key, RSA_MOD_SIZE + RSA_E_SIZE, sha_handle);
  117. if (SGX_SUCCESS != sgx_ret)
  118. break;
  119. sgx_ret = sgx_sha256_get_hash(sha_handle, reinterpret_cast<sgx_sha256_hash_t *>(hash_buf));
  120. } while (0);
  121. if (sha_handle != NULL)
  122. sgx_sha256_close(sha_handle);
  123. if (SGX_ERROR_OUT_OF_MEMORY == sgx_ret){
  124. return AE_OUT_OF_MEMORY_ERROR;
  125. }
  126. else if (SGX_SUCCESS != sgx_ret){
  127. return AE_FAILURE;
  128. }
  129. //verify the report data is SHA256(crypto_suite||public_key)||0-padding
  130. if(memcmp(hash_buf, &report->body.report_data, sizeof(report->body.report_data))!=0){
  131. return AE_INVALID_PARAMETER;
  132. }
  133. ppid_t ppid_buf;
  134. IppsRSAPublicKeyState *pub_key = NULL;
  135. int pub_key_size = 0;
  136. Ipp8u seeds[PCE_RSA_SEED_SIZE] = { 0 };
  137. uint8_t *pub_key_buffer = NULL;
  138. IppStatus ipp_ret;
  139. uint32_t little_endian_e = 0;
  140. uint8_t *le_n = NULL;
  141. ae_error_t ae_ret = get_ppid(&ppid_buf);
  142. if(ae_ret!=AE_SUCCESS){
  143. goto RETURN_POINT;
  144. }
  145. little_endian_e = lv_ntohl(*(public_key + RSA_MOD_SIZE));
  146. le_n = (uint8_t *)malloc(RSA_MOD_SIZE);
  147. if (le_n == NULL){
  148. ae_ret = AE_OUT_OF_MEMORY_ERROR;
  149. goto RETURN_POINT;
  150. }
  151. for (size_t i = 0; i<RSA_MOD_SIZE; i++){
  152. le_n[i] = *(public_key + RSA_MOD_SIZE - 1 - i);//create little endian n
  153. }
  154. ipp_ret = create_rsa_pub_key(RSA_MOD_SIZE, RSA_E_SIZE,
  155. reinterpret_cast<const Ipp32u *>(le_n),
  156. &little_endian_e,
  157. &pub_key);
  158. free(le_n);
  159. if (ippStsMemAllocErr == ipp_ret){
  160. ae_ret = AE_OUT_OF_MEMORY_ERROR;
  161. goto RETURN_POINT;
  162. }
  163. else if(ippStsNoErr != ipp_ret){//possible invalid rsa public key
  164. ae_ret = AE_FAILURE;
  165. goto RETURN_POINT;
  166. }
  167. ipp_ret = ippsRSA_GetBufferSizePublicKey(&pub_key_size, pub_key);
  168. if (ipp_ret != ippStsNoErr){
  169. ae_ret = AE_FAILURE;
  170. goto RETURN_POINT;
  171. }
  172. if (SGX_SUCCESS != sgx_read_rand(seeds, PCE_RSA_SEED_SIZE)){
  173. ae_ret = AE_READ_RAND_ERROR;
  174. goto RETURN_POINT;
  175. }
  176. pub_key_buffer = (uint8_t *)malloc(pub_key_size);
  177. if (pub_key_buffer == NULL){
  178. ae_ret = AE_OUT_OF_MEMORY_ERROR;
  179. goto RETURN_POINT;
  180. }
  181. ipp_ret = ippsRSAEncrypt_OAEP(reinterpret_cast<const Ipp8u *>(&ppid_buf), sizeof(ppid_buf), NULL, 0, seeds,
  182. encrypted_ppid, pub_key, IPP_ALG_HASH_SHA256, pub_key_buffer);
  183. if (ipp_ret != ippStsNoErr){
  184. ae_ret = AE_FAILURE;
  185. goto RETURN_POINT;
  186. }
  187. ae_ret = get_isv_svn(&pce_info->pce_isvn);
  188. if (ae_ret != AE_SUCCESS){
  189. goto RETURN_POINT;
  190. }
  191. pce_info->pce_id = CUR_PCE_ID;
  192. *signature_scheme = NIST_P256_ECDSA_SHA256;
  193. ae_ret = AE_SUCCESS;
  194. RETURN_POINT:
  195. memset_s(&ppid_buf, sizeof(ppid_buf), 0, sizeof(ppid_t));
  196. if(NULL != pub_key)
  197. secure_free_rsa_pub_key(RSA_MOD_SIZE, RSA_E_SIZE, pub_key);
  198. if (NULL != pub_key_buffer)
  199. free(pub_key_buffer);
  200. if (AE_SUCCESS != ae_ret)
  201. memset_s(encrypted_ppid, encrypted_ppid_buf_size, 0, *encrypted_ppid_out_size);
  202. return ae_ret;
  203. }
  204. uint32_t certify_enclave(const psvn_t* cert_psvn,
  205. const sgx_report_t* report,
  206. uint8_t *signature,
  207. uint32_t signature_buf_size,
  208. uint32_t *signature_out_size)
  209. {
  210. if(cert_psvn==NULL||
  211. report==NULL||
  212. signature == NULL||
  213. signature_out_size == NULL){
  214. return AE_INVALID_PARAMETER;
  215. }
  216. if(signature_buf_size < sizeof(sgx_ec256_signature_t)){
  217. *signature_out_size = sizeof(sgx_ec256_signature_t);
  218. return AE_INSUFFICIENT_DATA_IN_BUFFER;
  219. }
  220. ae_error_t ae_ret = AE_FAILURE;
  221. sgx_ecc_state_handle_t handle=NULL;
  222. sgx_ec256_private_t ec_prv_key = {0};
  223. sgx_status_t sgx_status = SGX_SUCCESS;
  224. if(SGX_SUCCESS != sgx_verify_report(report)){
  225. return PCE_INVALID_REPORT;
  226. }
  227. //only PvE could use the interface which has flag SGX_FLAGS_PROVISION_KEY
  228. if((report->body.attributes.flags & SGX_FLAGS_PROVISION_KEY) != SGX_FLAGS_PROVISION_KEY ||
  229. (report->body.attributes.flags & SGX_FLAGS_DEBUG) != 0){
  230. return PCE_INVALID_PRIVILEGE;
  231. }
  232. ae_ret = get_pce_priv_key(cert_psvn, &ec_prv_key);
  233. if(AE_SUCCESS!=ae_ret){
  234. goto ret_point;
  235. }
  236. SWAP_ENDIAN_32B(&ec_prv_key);
  237. sgx_status = sgx_ecc256_open_context(&handle);
  238. if (SGX_ERROR_OUT_OF_MEMORY == sgx_status)
  239. {
  240. ae_ret = AE_OUT_OF_MEMORY_ERROR;
  241. goto ret_point;
  242. }
  243. else if (SGX_SUCCESS != sgx_status) {
  244. ae_ret = AE_FAILURE;
  245. goto ret_point;
  246. }
  247. sgx_status = sgx_ecdsa_sign(reinterpret_cast<const uint8_t *>(&report->body), sizeof(report->body),
  248. &ec_prv_key, reinterpret_cast<sgx_ec256_signature_t *>(signature), handle);
  249. if (SGX_ERROR_OUT_OF_MEMORY == sgx_status)
  250. {
  251. ae_ret = AE_OUT_OF_MEMORY_ERROR;
  252. goto ret_point;
  253. }
  254. else if (SGX_SUCCESS != sgx_status) {
  255. ae_ret = AE_FAILURE;
  256. goto ret_point;
  257. }
  258. //swap from little endian used in sgx_crypto to big endian used in network byte order
  259. SWAP_ENDIAN_32B(reinterpret_cast<sgx_ec256_signature_t *>(signature)->x);
  260. SWAP_ENDIAN_32B(reinterpret_cast<sgx_ec256_signature_t *>(signature)->y);
  261. *signature_out_size = sizeof(sgx_ec256_signature_t);
  262. ae_ret = AE_SUCCESS;
  263. ret_point:
  264. (void)memset_s(&ec_prv_key, sizeof(ec_prv_key),0,sizeof(ec_prv_key));
  265. if(handle!=NULL){
  266. sgx_ecc256_close_context(handle);
  267. }
  268. if(AE_SUCCESS != ae_ret){
  269. (void)memset_s(signature, signature_buf_size, 0, sizeof(sgx_ec256_signature_t));
  270. }
  271. return ae_ret;
  272. }