enclave_creator_sign.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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:
  33. * enclave_creator_sign.cpp
  34. * Description:
  35. * Measure the necessary information of the enclave
  36. * to calculate the HASH value using SHA256 algorithm.
  37. */
  38. #include "enclave_creator.h"
  39. #include "sgx_eid.h"
  40. #include "enclave_creator_sign.h"
  41. #include "se_trace.h"
  42. #include "sgx_error.h"
  43. #include "util_st.h"
  44. #include "util.h"
  45. #include "se_page_attr.h"
  46. #include <stdlib.h>
  47. #include <string.h>
  48. #include <assert.h>
  49. #include <openssl/err.h>
  50. #define DATA_BLOCK_SIZE 64
  51. #define EID 0x44444444
  52. EnclaveCreatorST::EnclaveCreatorST()
  53. {
  54. m_hash_valid_flag = false;
  55. memset(m_enclave_hash, 0, SGX_HASH_SIZE);
  56. m_ctx = NULL;
  57. m_eid = EID;
  58. m_quota = 0;
  59. }
  60. EnclaveCreatorST::~EnclaveCreatorST()
  61. {
  62. if(m_ctx)
  63. EVP_MD_CTX_destroy(m_ctx);
  64. }
  65. int EnclaveCreatorST::create_enclave(secs_t *secs, sgx_enclave_id_t *enclave_id, void **start_addr, bool ae)
  66. {
  67. if(!secs || !enclave_id || !start_addr)
  68. {
  69. se_trace(SE_TRACE_DEBUG, "ERROR: Bad pointer.\n");
  70. return SGX_ERROR_UNEXPECTED;
  71. }
  72. UNUSED(ae);
  73. memset(m_enclave_hash, 0, SGX_HASH_SIZE);
  74. if((m_ctx = EVP_MD_CTX_create()) == NULL)
  75. {
  76. se_trace(SE_TRACE_DEBUG, "ERROR - EVP_MD_CTX_create: %s.\n", ERR_error_string(ERR_get_error(), NULL));
  77. return SGX_ERROR_UNEXPECTED;
  78. }
  79. if(EVP_DigestInit_ex(m_ctx, EVP_sha256(), NULL) != 1)
  80. {
  81. se_trace(SE_TRACE_DEBUG, "ERROR - EVP_DigestInit_ex: %s.\n", ERR_error_string(ERR_get_error(), NULL));
  82. return SGX_ERROR_UNEXPECTED;
  83. }
  84. uint8_t ecreat_val[SIZE_NAMED_VALUE] = "ECREATE";
  85. uint8_t data_block[DATA_BLOCK_SIZE];
  86. size_t offset = 0;
  87. memset(data_block, 0, DATA_BLOCK_SIZE);
  88. memcpy_s(data_block, sizeof(data_block), ecreat_val, SIZE_NAMED_VALUE);
  89. offset += SIZE_NAMED_VALUE;
  90. memcpy_s(&data_block[offset], sizeof(data_block)-offset, &secs->ssa_frame_size, sizeof(secs->ssa_frame_size));
  91. offset += sizeof(secs->ssa_frame_size);
  92. memcpy_s(&data_block[offset], sizeof(data_block)-offset, &secs->size, sizeof(secs->size));
  93. if(EVP_DigestUpdate(m_ctx, &data_block, DATA_BLOCK_SIZE) != 1)
  94. {
  95. se_trace(SE_TRACE_DEBUG, "ERROR - EVP_DigestUpdate: %s.\n", ERR_error_string(ERR_get_error(), NULL));
  96. return SGX_ERROR_UNEXPECTED;
  97. }
  98. *enclave_id = m_eid;
  99. *start_addr = secs->base;
  100. return SGX_SUCCESS;
  101. }
  102. int EnclaveCreatorST::add_enclave_page(sgx_enclave_id_t enclave_id, void *src, uint64_t offset, const sec_info_t &sinfo, uint32_t attr)
  103. {
  104. assert(m_ctx!=NULL);
  105. UNUSED(enclave_id);
  106. void* source = src;
  107. uint8_t color_page[SE_PAGE_SIZE];
  108. if(!source)
  109. {
  110. memset(color_page, 0, SE_PAGE_SIZE);
  111. source = reinterpret_cast<void*>(&color_page);
  112. }
  113. for(unsigned int i = 0; i< sizeof(sinfo.reserved)/sizeof(sinfo.reserved[0]); i++)
  114. {
  115. if(sinfo.reserved[i] != 0)
  116. return SGX_ERROR_UNEXPECTED;
  117. }
  118. /* sinfo.flags[64:16] should be 0 */
  119. if((sinfo.flags & (~SI_FLAGS_EXTERNAL)) != 0)
  120. {
  121. return SGX_ERROR_UNEXPECTED;
  122. }
  123. //check the page attributes
  124. if (!(attr & PAGE_ATTR_EADD))
  125. {
  126. return SGX_SUCCESS;
  127. }
  128. uint64_t page_offset = (uint64_t)offset;
  129. uint8_t eadd_val[SIZE_NAMED_VALUE] = "EADD\0\0\0";
  130. uint8_t data_block[DATA_BLOCK_SIZE];
  131. size_t db_offset = 0;
  132. memset(data_block, 0, DATA_BLOCK_SIZE);
  133. memcpy_s(data_block, sizeof(data_block), eadd_val, SIZE_NAMED_VALUE);
  134. db_offset += SIZE_NAMED_VALUE;
  135. memcpy_s(data_block+db_offset, sizeof(data_block)-db_offset, &page_offset, sizeof(page_offset));
  136. db_offset += sizeof(page_offset);
  137. memcpy_s(data_block+db_offset, sizeof(data_block)-db_offset, &sinfo, sizeof(data_block)-db_offset);
  138. if(EVP_DigestUpdate(m_ctx, data_block, DATA_BLOCK_SIZE) != 1)
  139. {
  140. se_trace(SE_TRACE_DEBUG, "ERROR - EVP_digestUpdate: %s.\n", ERR_error_string(ERR_get_error(), NULL));
  141. return SGX_ERROR_UNEXPECTED;
  142. }
  143. /* If the page need to eextend, do eextend. */
  144. if((attr & ADD_EXTEND_PAGE) == ADD_EXTEND_PAGE)
  145. {
  146. uint8_t *pdata = (uint8_t *)source;
  147. uint8_t eextend_val[SIZE_NAMED_VALUE] = "EEXTEND";
  148. #define EEXTEND_TIME 4
  149. for(int i = 0; i < SE_PAGE_SIZE; i += (DATA_BLOCK_SIZE * EEXTEND_TIME))
  150. {
  151. db_offset = 0;
  152. memset(data_block, 0, DATA_BLOCK_SIZE);
  153. memcpy_s(data_block, sizeof(data_block), eextend_val, SIZE_NAMED_VALUE);
  154. db_offset += SIZE_NAMED_VALUE;
  155. memcpy_s(data_block+db_offset, sizeof(data_block)-db_offset, &page_offset, sizeof(page_offset));
  156. if(EVP_DigestUpdate(m_ctx, data_block, DATA_BLOCK_SIZE) != 1)
  157. {
  158. se_trace(SE_TRACE_DEBUG, "ERROR - EVP_digestUpdate: %s.\n", ERR_error_string(ERR_get_error(), NULL));
  159. return SGX_ERROR_UNEXPECTED;
  160. }
  161. for(int j = 0; j < EEXTEND_TIME; j++)
  162. {
  163. memcpy_s(data_block, sizeof(data_block), pdata, DATA_BLOCK_SIZE);
  164. if(EVP_DigestUpdate(m_ctx, data_block, DATA_BLOCK_SIZE) != 1)
  165. {
  166. se_trace(SE_TRACE_DEBUG, "ERROR - EVP_digestUpdate: %s.\n", ERR_error_string(ERR_get_error(), NULL));
  167. return SGX_ERROR_UNEXPECTED;
  168. }
  169. pdata += DATA_BLOCK_SIZE;
  170. page_offset += DATA_BLOCK_SIZE;
  171. }
  172. }
  173. }
  174. m_quota += SE_PAGE_SIZE;
  175. return SGX_SUCCESS;
  176. }
  177. int EnclaveCreatorST::init_enclave(sgx_enclave_id_t enclave_id, enclave_css_t *enclave_css, SGXLaunchToken *lc, le_prd_css_file_t *prd_css_file)
  178. {
  179. assert(m_ctx != NULL);
  180. UNUSED(enclave_id), UNUSED(enclave_css), UNUSED(lc), UNUSED(prd_css_file);
  181. uint8_t temp_hash[SGX_HASH_SIZE];
  182. memset(temp_hash, 0, SGX_HASH_SIZE);
  183. unsigned int hash_len;
  184. /* Complete computation of the SHA256 digest and store the result into the hash. */
  185. if(EVP_DigestFinal_ex(m_ctx, temp_hash, &hash_len) != 1)
  186. {
  187. se_trace(SE_TRACE_DEBUG, "ERROR - EVP_digestFinal_ex: %s.\n", ERR_error_string(ERR_get_error(), NULL));
  188. return SGX_ERROR_UNEXPECTED;
  189. }
  190. for (int i = 0; i< SGX_HASH_SIZE; i++)
  191. {
  192. m_enclave_hash[i] = temp_hash[i];
  193. }
  194. m_hash_valid_flag = true;
  195. return SGX_SUCCESS;
  196. }
  197. int EnclaveCreatorST::get_misc_attr(sgx_misc_attribute_t *sgx_misc_attr, metadata_t *metadata, SGXLaunchToken * const lc, uint32_t flag)
  198. {
  199. UNUSED(metadata), UNUSED(lc), UNUSED(flag);
  200. memset(sgx_misc_attr, 0, sizeof(sgx_misc_attribute_t));
  201. return SGX_SUCCESS;
  202. }
  203. int EnclaveCreatorST::destroy_enclave(sgx_enclave_id_t enclave_id, uint64_t enclave_size)
  204. {
  205. UNUSED(enclave_id);
  206. UNUSED(enclave_size);
  207. if(m_ctx){
  208. EVP_MD_CTX_destroy(m_ctx);
  209. m_ctx = NULL;
  210. }
  211. return SGX_SUCCESS;
  212. }
  213. bool EnclaveCreatorST::get_plat_cap(sgx_misc_attribute_t *se_attr)
  214. {
  215. UNUSED(se_attr);
  216. return false;
  217. }
  218. int EnclaveCreatorST::initialize(sgx_enclave_id_t enclave_id)
  219. {
  220. UNUSED(enclave_id);
  221. return SGX_SUCCESS;
  222. }
  223. bool EnclaveCreatorST::use_se_hw() const
  224. {
  225. return false;
  226. }
  227. bool EnclaveCreatorST::is_EDMM_supported(sgx_enclave_id_t enclave_id)
  228. {
  229. UNUSED(enclave_id);
  230. return false;
  231. }
  232. bool EnclaveCreatorST::is_driver_compatible()
  233. {
  234. return true;
  235. }
  236. bool EnclaveCreatorST::is_in_kernel_driver()
  237. {
  238. return false;
  239. }
  240. int EnclaveCreatorST::get_enclave_info(uint8_t *hash, int size, uint64_t *quota)
  241. {
  242. if(hash == NULL || size != SGX_HASH_SIZE || m_hash_valid_flag == false)
  243. {
  244. se_trace(SE_TRACE_DEBUG, "ERROR: something went wrong in the function get_enclave_hash().\n");
  245. return SGX_ERROR_UNEXPECTED;
  246. }
  247. else
  248. {
  249. memcpy_s(hash, size, m_enclave_hash, SGX_HASH_SIZE);
  250. }
  251. *quota = m_quota;
  252. return SGX_SUCCESS;
  253. }
  254. int EnclaveCreatorST::emodpr(uint64_t addr, uint64_t size, uint64_t flag)
  255. {
  256. UNUSED(addr);
  257. UNUSED(size);
  258. UNUSED(flag);
  259. return SGX_SUCCESS;
  260. }
  261. int EnclaveCreatorST::mktcs(uint64_t tcs_addr)
  262. {
  263. UNUSED(tcs_addr);
  264. return SGX_SUCCESS;
  265. }
  266. int EnclaveCreatorST::trim_range(uint64_t fromaddr, uint64_t toaddr)
  267. {
  268. UNUSED(fromaddr);
  269. UNUSED(toaddr);
  270. return SGX_SUCCESS;
  271. }
  272. int EnclaveCreatorST::trim_accept(uint64_t addr)
  273. {
  274. UNUSED(addr);
  275. return SGX_SUCCESS;
  276. }
  277. int EnclaveCreatorST::remove_range(uint64_t fromaddr, uint64_t numpages)
  278. {
  279. UNUSED(fromaddr);
  280. UNUSED(numpages);
  281. return SGX_SUCCESS;
  282. }
  283. static EnclaveCreatorST g_enclave_creator_st;
  284. EnclaveCreator* g_enclave_creator = &g_enclave_creator_st;