provision_enclave.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. /**
  32. * File: provision_enclave.cpp
  33. * Description: Definition for interfaces provided by provision enclave..
  34. *
  35. * Definition for interfaces provided by provision enclave.
  36. */
  37. #include "se_cdefs.h"
  38. #include "string.h"
  39. #include "helper.h"
  40. #include "cipher.h"
  41. #include "protocol.h"
  42. #include "provision_msg.h"
  43. #include "provision_enclave_t.c"
  44. #include "sgx_utils.h"
  45. #include "aeerror.h"
  46. ae_error_t pve_error_2_ae_error(pve_status_t pve_error)
  47. {
  48. switch(pve_error){
  49. case PVEC_SUCCESS:
  50. return AE_SUCCESS;
  51. case PVEC_PARAMETER_ERROR:
  52. return PVE_PARAMETER_ERROR;
  53. case PVEC_EPID_BLOB_ERROR:
  54. return PVE_EPIDBLOB_ERROR;
  55. case PVEC_INSUFFICIENT_MEMORY_ERROR:
  56. return PVE_INSUFFICIENT_MEMORY_ERROR;
  57. case PVEC_REVOKED_ERROR:
  58. return PVE_REVOKED_ERROR;
  59. case PVEC_INTEGER_OVERFLOW_ERROR:
  60. return PVE_INTEGRITY_CHECK_ERROR;
  61. case PVEC_SIGRL_INTEGRITY_CHECK_ERROR:
  62. return PVE_SIGRL_INTEGRITY_CHECK_ERROR;
  63. case PVEC_PEK_SIGN_ERROR:
  64. return PVE_PEK_SIGN_ERROR;
  65. case PVEC_XEGDSK_SIGN_ERROR:
  66. return PVE_XEGDSK_SIGN_ERROR;
  67. case PVEC_MSG_ERROR:
  68. case PVEC_UNSUPPORTED_VERSION_ERROR:
  69. case PVEC_INVALID_CPU_ISV_SVN:
  70. case PVEC_INVALID_EPID_KEY:
  71. return PVE_MSG_ERROR;
  72. default:
  73. return PVE_UNEXPECTED_ERROR;
  74. }
  75. }
  76. //proxy function to generate data for ProvMsg1
  77. uint32_t gen_prov_msg1_data_wrapper(
  78. const extended_epid_group_blob_t *xegb,
  79. const signed_pek_t *pek,
  80. const sgx_target_info_t *pce_target_info,
  81. sgx_report_t *pek_report)//output data for generating ProvMsg1
  82. {
  83. pve_status_t status = PVEC_SUCCESS;
  84. if(pce_target_info == NULL || !sgx_is_within_enclave(pce_target_info, sizeof(sgx_target_info_t))){
  85. status = PVEC_PARAMETER_ERROR;
  86. goto ret_point;
  87. }
  88. if(xegb == NULL || !sgx_is_within_enclave(xegb, sizeof(extended_epid_group_blob_t))){
  89. status = PVEC_PARAMETER_ERROR;
  90. goto ret_point;
  91. }
  92. if(pek==NULL||!sgx_is_within_enclave(pek, sizeof(signed_pek_t))){
  93. status = PVEC_PARAMETER_ERROR;
  94. goto ret_point;
  95. }
  96. if( pek_report==NULL||!sgx_is_within_enclave(pek_report, sizeof(sgx_report_t))){//require pek_report not to be NULL and memory allocated in EPC
  97. status = PVEC_PARAMETER_ERROR;
  98. goto ret_point;
  99. }
  100. status = gen_prov_msg1_data(
  101. *pce_target_info,
  102. *xegb,
  103. *pek,
  104. *pek_report);
  105. ret_point:
  106. return pve_error_2_ae_error(status);
  107. }
  108. //edger8r will copy memory in/out EPC for all buffers except for sigrl in msg2 and epidSignature of msg3 in proc_prov_msg2_data_wrapper.
  109. //proxy function to process data from ProvMsg2 and generate ProvMsg3 data
  110. uint32_t proc_prov_msg2_data_wrapper(
  111. const proc_prov_msg2_blob_input_t *msg2_blob_input,
  112. uint8_t performance_rekey_used,
  113. const uint8_t *sigrl, uint32_t sigrl_size,//pointer to external memory if the sigrl is not NULL
  114. gen_prov_msg3_output_t *msg3_fixed_output,
  115. uint8_t *epid_sig, uint32_t epid_sig_buffer_size)//This is an optional pointer to external memory for epid signature to be generated
  116. {
  117. pve_status_t status = PVEC_SUCCESS;
  118. const external_memory_byte_t *emp_sigrl = NULL;
  119. external_memory_byte_t *emp_epid_sig = NULL;
  120. if( msg2_blob_input==NULL||!sgx_is_within_enclave(msg2_blob_input, sizeof(proc_prov_msg2_blob_input_t))){//input data should be inside EPC
  121. status = PVEC_PARAMETER_ERROR;
  122. goto ret_point;
  123. }
  124. if( sigrl!=NULL && !sgx_is_outside_enclave(sigrl, sigrl_size)){//sigrl should be outside enclave if it is not NULL
  125. status = PVEC_PARAMETER_ERROR;
  126. goto ret_point;
  127. }
  128. //
  129. // for user_check SigRL input
  130. // based on sigrl_size input parameter
  131. //
  132. __builtin_ia32_lfence();
  133. if((sigrl==NULL&&sigrl_size!=0)||
  134. (sigrl!=NULL&&sigrl_size==0)){
  135. status = PVEC_PARAMETER_ERROR;
  136. goto ret_point;
  137. }
  138. if(msg3_fixed_output==NULL ||!sgx_is_within_enclave(msg3_fixed_output, sizeof(gen_prov_msg3_output_t))){
  139. status = PVEC_PARAMETER_ERROR;
  140. goto ret_point;
  141. }
  142. if(epid_sig == NULL && epid_sig_buffer_size!=0){
  143. status = PVEC_PARAMETER_ERROR;
  144. goto ret_point;
  145. }
  146. if(epid_sig!=NULL && !sgx_is_outside_enclave(epid_sig, epid_sig_buffer_size)){
  147. status = PVEC_PARAMETER_ERROR;
  148. goto ret_point;
  149. }
  150. //type-cast the pointer to emp after pointer checking
  151. emp_sigrl = reinterpret_cast<const external_memory_byte_t *>(sigrl);
  152. emp_epid_sig = reinterpret_cast<external_memory_byte_t *>(epid_sig);
  153. status = proc_prov_msg2_data(msg2_blob_input, performance_rekey_used, emp_sigrl, sigrl_size,
  154. msg3_fixed_output, emp_epid_sig,
  155. epid_sig_buffer_size);
  156. ret_point:
  157. return pve_error_2_ae_error(status);
  158. }
  159. //Proxy function to process ProvMsg4 data to generate sealed EPID data blob
  160. uint32_t proc_prov_msg4_data_wrapper(
  161. const proc_prov_msg4_input_t *msg4_input,
  162. proc_prov_msg4_output_t* data_blob)
  163. {
  164. pve_status_t status = PVEC_SUCCESS;
  165. if(msg4_input == NULL || !sgx_is_within_enclave(msg4_input, sizeof(proc_prov_msg4_input_t))){
  166. status = PVEC_PARAMETER_ERROR;
  167. goto ret_point;
  168. }
  169. if(data_blob == NULL||!sgx_is_within_enclave(data_blob, sizeof(proc_prov_msg4_output_t)))
  170. {
  171. status = PVEC_PARAMETER_ERROR;
  172. goto ret_point;
  173. }
  174. status = proc_prov_msg4_data( msg4_input, reinterpret_cast<sgx_sealed_data_t*>(data_blob));
  175. ret_point:
  176. return pve_error_2_ae_error(status);
  177. }
  178. //proxy function to create es selector for endpoint selection
  179. uint32_t gen_es_msg1_data_wrapper(gen_endpoint_selection_output_t *es_output)
  180. {
  181. pve_status_t status = PVEC_SUCCESS;
  182. if(es_output == NULL || !sgx_is_within_enclave(es_output, sizeof(gen_endpoint_selection_output_t))){
  183. status = PVEC_PARAMETER_ERROR;
  184. goto ret_point;
  185. }
  186. status = gen_es_msg1_data(es_output);
  187. ret_point:
  188. return pve_error_2_ae_error(status);
  189. }