provision_enclave.cpp 7.5 KB

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