manage_metadata.cpp 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  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. * manage_metadata.cpp
  34. * Description:
  35. * Parse the xml file to get the metadata and generate the output DLL
  36. * with metadata.
  37. */
  38. #include "metadata.h"
  39. #include "tinyxml2.h"
  40. #include "manage_metadata.h"
  41. #include "se_trace.h"
  42. #include "util_st.h"
  43. #include "section.h"
  44. #include "se_page_attr.h"
  45. #include "elf_util.h"
  46. #include "crypto_wrapper.h"
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <errno.h>
  50. #include <assert.h>
  51. #include <iostream>
  52. #include <iomanip>
  53. #include <fstream>
  54. using namespace tinyxml2;
  55. #define ALIGN_SIZE 0x1000
  56. static bool traverser_parameter(const char *temp_name, const char *temp_text, xml_parameter_t *parameter, int parameter_count)
  57. {
  58. assert(temp_name != NULL && parameter != NULL);
  59. uint64_t temp_value=0;
  60. if(temp_text == NULL)
  61. {
  62. se_trace(SE_TRACE_ERROR, LACK_VALUE_FOR_ELEMENT_ERROR, temp_name);
  63. return false;
  64. }
  65. else
  66. {
  67. if(strchr(temp_text, '-'))
  68. {
  69. se_trace(SE_TRACE_ERROR, INVALID_VALUE_FOR_ELEMENT_ERROR, temp_name);
  70. return false;
  71. }
  72. errno = 0;
  73. char* endptr = NULL;
  74. temp_value = (uint64_t)strtoull(temp_text, &endptr, 0);
  75. if(*endptr!='\0'||errno!=0) //Invalid value or valid value but out of the representable range
  76. {
  77. se_trace(SE_TRACE_ERROR, INVALID_VALUE_FOR_ELEMENT_ERROR, temp_name);
  78. return false;
  79. }
  80. }
  81. //Look for the matched one
  82. int i=0;
  83. for(; i<parameter_count&&STRCMP(temp_name,parameter[i].name); i++);
  84. if(i>=parameter_count) //no matched, return false
  85. {
  86. se_trace(SE_TRACE_ERROR, UNREC_ELEMENT_ERROR, temp_name);
  87. return false;
  88. }
  89. //found one matched
  90. if(parameter[i].flag==1) //repeated definition of XML element, return false
  91. {
  92. se_trace(SE_TRACE_ERROR, REPEATED_DEFINE_ERROR, temp_name);
  93. return false;
  94. }
  95. parameter[i].flag = 1;
  96. if((temp_value<parameter[i].min_value)||
  97. (temp_value>parameter[i].max_value)) // the value is invalid, return false
  98. {
  99. se_trace(SE_TRACE_ERROR, VALUE_OUT_OF_RANGE_ERROR, temp_name);
  100. return false;
  101. }
  102. parameter[i].value = temp_value;
  103. return true;
  104. }
  105. bool parse_metadata_file(const char *xmlpath, xml_parameter_t *parameter, int parameter_count)
  106. {
  107. const char* temp_name=NULL;
  108. assert(parameter != NULL);
  109. if(xmlpath == NULL) // user didn't define the metadata xml file.
  110. {
  111. se_trace(SE_TRACE_NOTICE, "Use default metadata...\n");
  112. return true;
  113. }
  114. //use the metadata file that user gives us. parse xml file
  115. tinyxml2::XMLDocument doc;
  116. XMLError loadOkay = doc.LoadFile(xmlpath);
  117. if(loadOkay != XML_SUCCESS)
  118. {
  119. if(doc.ErrorID() == XML_ERROR_FILE_COULD_NOT_BE_OPENED)
  120. {
  121. se_trace(SE_TRACE_ERROR, OPEN_FILE_ERROR, xmlpath);
  122. }
  123. else if(doc.ErrorID() == XML_ERROR_FILE_NOT_FOUND)
  124. {
  125. se_trace(SE_TRACE_ERROR, XML_NOT_FOUND_ERROR, xmlpath);
  126. }
  127. else
  128. {
  129. se_trace(SE_TRACE_ERROR, XML_FORMAT_ERROR);
  130. }
  131. return false;
  132. }
  133. doc.Print();//Write the document to standard out using formatted printing ("pretty print").
  134. XMLElement *pmetadata_element = doc.FirstChildElement("EnclaveConfiguration");
  135. if(!pmetadata_element || pmetadata_element->GetText() != NULL)
  136. {
  137. se_trace(SE_TRACE_ERROR, XML_FORMAT_ERROR);
  138. return false;
  139. }
  140. XMLElement *sub_element = NULL;
  141. sub_element = pmetadata_element->FirstChildElement();
  142. const char *temp_text = NULL;
  143. while(sub_element)//parse xml node
  144. {
  145. if(sub_element->FirstAttribute() != NULL)
  146. {
  147. se_trace(SE_TRACE_ERROR, XML_FORMAT_ERROR);
  148. return false;
  149. }
  150. temp_name = sub_element->Value();
  151. temp_text = sub_element->GetText();
  152. //traverse every node. Compare with the default value.
  153. if(traverser_parameter(temp_name, temp_text, parameter, parameter_count) == false)
  154. {
  155. se_trace(SE_TRACE_ERROR, XML_FORMAT_ERROR);
  156. return false;
  157. }
  158. sub_element= sub_element->NextSiblingElement();
  159. }
  160. return true;
  161. }
  162. CMetadata::CMetadata(metadata_t *metadata, BinParser *parser)
  163. : m_metadata(metadata)
  164. , m_parser(parser), m_rva(0), m_gd_size(0), m_gd_template(NULL)
  165. {
  166. memset(m_metadata, 0, sizeof(metadata_t));
  167. memset(&m_create_param, 0, sizeof(m_create_param));
  168. }
  169. CMetadata::~CMetadata()
  170. {
  171. }
  172. bool CMetadata::build_metadata(const xml_parameter_t *parameter)
  173. {
  174. if(!modify_metadata(parameter))
  175. {
  176. return false;
  177. }
  178. // layout table
  179. if(!build_layout_table())
  180. {
  181. return false;
  182. }
  183. // patch table
  184. if(!build_patch_table())
  185. {
  186. return false;
  187. }
  188. if(!build_layout_entries())
  189. {
  190. return false;
  191. }
  192. if(!build_gd_template(m_gd_template, &m_gd_size))
  193. {
  194. return false;
  195. }
  196. return true;
  197. }
  198. #include <sstream>
  199. #include <time.h>
  200. bool CMetadata::get_time(uint32_t *date)
  201. {
  202. assert(date != NULL);
  203. time_t rawtime = 0;
  204. if(time( &rawtime) == -1)
  205. return false;
  206. struct tm *timeinfo = gmtime(&rawtime);
  207. if(timeinfo == NULL)
  208. return false;
  209. uint32_t tmp_date = (timeinfo->tm_year+1900)*10000 + (timeinfo->tm_mon+1)*100 + timeinfo->tm_mday;
  210. stringstream ss;
  211. ss<<"0x"<<tmp_date;
  212. ss>>hex>>tmp_date;
  213. *date = tmp_date;
  214. return true;
  215. }
  216. bool CMetadata::fill_enclave_css(const xml_parameter_t *para)
  217. {
  218. assert(para != NULL);
  219. uint32_t date = 0;
  220. if(false == get_time(&date))
  221. return false;
  222. //*****fill the header*******************
  223. uint8_t header[12] = {6, 0, 0, 0, 0xE1, 0, 0, 0, 0, 0, 1, 0};
  224. uint8_t header2[16] = {1, 1, 0, 0, 0x60, 0, 0, 0, 0x60, 0, 0, 0, 1, 0, 0, 0};
  225. memcpy_s(&m_metadata->enclave_css.header.header, sizeof(m_metadata->enclave_css.header.header), &header, sizeof(header));
  226. memcpy_s(&m_metadata->enclave_css.header.header2, sizeof(m_metadata->enclave_css.header.header2), &header2, sizeof(header2));
  227. // For 'type', signing tool clears the bit 31 for product enclaves
  228. // and set the bit 31 for debug enclaves
  229. m_metadata->enclave_css.header.type = (para[RELEASETYPE].value & 0x01) ? (1<<31) : 0;
  230. m_metadata->enclave_css.header.module_vendor = (para[INTELSIGNED].value&0x01) ? 0x8086 : 0;
  231. m_metadata->enclave_css.header.date = date;
  232. //hardware version
  233. m_metadata->enclave_css.header.hw_version = (uint32_t)para[HW].value;
  234. // Misc_select/Misc_mask
  235. m_metadata->enclave_css.body.misc_select = (uint32_t)para[MISCSELECT].value;
  236. m_metadata->enclave_css.body.misc_mask = (uint32_t)para[MISCMASK].value;
  237. //low 64 bit
  238. m_metadata->enclave_css.body.attributes.flags = 0;
  239. m_metadata->enclave_css.body.attribute_mask.flags = ~SGX_FLAGS_DEBUG;
  240. if(para[DISABLEDEBUG].value == 1)
  241. {
  242. m_metadata->enclave_css.body.attributes.flags &= ~SGX_FLAGS_DEBUG;
  243. m_metadata->enclave_css.body.attribute_mask.flags |= SGX_FLAGS_DEBUG;
  244. }
  245. if(para[PROVISIONKEY].value == 1)
  246. {
  247. m_metadata->enclave_css.body.attributes.flags |= SGX_FLAGS_PROVISION_KEY;
  248. m_metadata->enclave_css.body.attribute_mask.flags |= SGX_FLAGS_PROVISION_KEY;
  249. }
  250. if(para[LAUNCHKEY].value == 1)
  251. {
  252. m_metadata->enclave_css.body.attributes.flags |= SGX_FLAGS_EINITTOKEN_KEY;
  253. m_metadata->enclave_css.body.attribute_mask.flags |= SGX_FLAGS_EINITTOKEN_KEY;
  254. }
  255. bin_fmt_t bf = m_parser->get_bin_format();
  256. if(bf == BF_PE64 || bf == BF_ELF64)
  257. {
  258. m_metadata->enclave_css.body.attributes.flags |= SGX_FLAGS_MODE64BIT;
  259. m_metadata->enclave_css.body.attribute_mask.flags |= SGX_FLAGS_MODE64BIT;
  260. }
  261. // high 64 bit
  262. //default setting
  263. m_metadata->enclave_css.body.attributes.xfrm = SGX_XFRM_LEGACY;
  264. m_metadata->enclave_css.body.attribute_mask.xfrm = SGX_XFRM_LEGACY | SGX_XFRM_RESERVED; // LEGACY and reservied bits would be checked.
  265. m_metadata->enclave_css.body.isv_prod_id = (uint16_t)para[PRODID].value;
  266. m_metadata->enclave_css.body.isv_svn = (uint16_t)para[ISVSVN].value;
  267. return true;
  268. }
  269. bool CMetadata::modify_metadata(const xml_parameter_t *parameter)
  270. {
  271. assert(parameter != NULL);
  272. if(!check_xml_parameter(parameter))
  273. return false;
  274. if(!fill_enclave_css(parameter))
  275. return false;
  276. m_metadata->version = META_DATA_MAKE_VERSION(MAJOR_VERSION,MINOR_VERSION );
  277. m_metadata->size = offsetof(metadata_t, data);
  278. m_metadata->tcs_policy = (uint32_t)parameter[TCSPOLICY].value;
  279. m_metadata->ssa_frame_size = SSA_FRAME_SIZE;
  280. m_metadata->max_save_buffer_size = MAX_SAVE_BUF_SIZE;
  281. m_metadata->magic_num = METADATA_MAGIC;
  282. m_metadata->desired_misc_select = 0;
  283. m_metadata->tcs_min_pool = (uint32_t)parameter[TCSMINPOOL].value;
  284. m_metadata->enclave_css.body.misc_select = (uint32_t)parameter[MISCSELECT].value;
  285. m_metadata->enclave_css.body.misc_mask = (uint32_t)parameter[MISCMASK].value;
  286. //set metadata.attributes
  287. //low 64 bit: it's the same as enclave_css
  288. memset(&m_metadata->attributes, 0, sizeof(sgx_attributes_t));
  289. m_metadata->attributes.flags = m_metadata->enclave_css.body.attributes.flags;
  290. //high 64 bit
  291. //set bits that will not be checked
  292. m_metadata->attributes.xfrm = ~m_metadata->enclave_css.body.attribute_mask.xfrm;
  293. //set bits that have been set '1' and need to be checked
  294. m_metadata->attributes.xfrm |= (m_metadata->enclave_css.body.attributes.xfrm & m_metadata->enclave_css.body.attribute_mask.xfrm);
  295. return true;
  296. }
  297. bool CMetadata::check_xml_parameter(const xml_parameter_t *parameter)
  298. {
  299. //stack/heap must be page-align
  300. if( (parameter[STACKMAXSIZE].value % ALIGN_SIZE)
  301. || (parameter[STACKMINSIZE].value % ALIGN_SIZE) )
  302. {
  303. se_trace(SE_TRACE_ERROR, SET_STACK_SIZE_ERROR);
  304. return false;
  305. }
  306. if(parameter[STACKMINSIZE].value > parameter[STACKMAXSIZE].value)
  307. {
  308. se_trace(SE_TRACE_ERROR, SET_STACK_SIZE_ERROR);
  309. return false;
  310. }
  311. if( (parameter[HEAPMAXSIZE].value % ALIGN_SIZE)
  312. || (parameter[HEAPMINSIZE].value % ALIGN_SIZE)
  313. || (parameter[HEAPINITSIZE].value % ALIGN_SIZE) )
  314. {
  315. se_trace(SE_TRACE_ERROR, SET_HEAP_SIZE_ALIGN_ERROR);
  316. return false;
  317. }
  318. if (parameter[HEAPINITSIZE].flag != 0)
  319. {
  320. if (parameter[HEAPINITSIZE].value > parameter[HEAPMAXSIZE].value)
  321. {
  322. se_trace(SE_TRACE_ERROR, SET_HEAP_SIZE_INIT_MAX_ERROR);
  323. return false;
  324. }
  325. if (parameter[HEAPMINSIZE].value > parameter[HEAPINITSIZE].value)
  326. {
  327. se_trace(SE_TRACE_ERROR, SET_HEAP_SIZE_INIT_MIN_ERROR);
  328. return false;
  329. }
  330. }
  331. else
  332. {
  333. if (parameter[HEAPMINSIZE].value > parameter[HEAPMAXSIZE].value)
  334. {
  335. se_trace(SE_TRACE_ERROR, SET_HEAP_SIZE_MAX_MIN_ERROR);
  336. return false;
  337. }
  338. }
  339. // LE setting: HW != 0, Licensekey = 1
  340. // Other enclave setting: HW = 0, Licensekey = 0
  341. if((parameter[HW].value == 0 && parameter[LAUNCHKEY].value != 0) ||
  342. (parameter[HW].value != 0 && parameter[LAUNCHKEY].value == 0))
  343. {
  344. se_trace(SE_TRACE_ERROR, SET_HW_LE_ERROR);
  345. return false;
  346. }
  347. if (parameter[TCSMAXNUM].flag != 0)
  348. {
  349. if (parameter[TCSMAXNUM].value < parameter[TCSNUM].value)
  350. {
  351. se_trace(SE_TRACE_ERROR, SET_TCS_MAX_NUM_ERROR);
  352. return false;
  353. }
  354. if ((parameter[TCSMINPOOL].flag != 0)
  355. && (parameter[TCSMINPOOL].value > parameter[TCSMAXNUM].value))
  356. {
  357. se_trace(SE_TRACE_ERROR, SET_TCS_MIN_POOL_ERROR);
  358. return false;
  359. }
  360. }
  361. else if ((parameter[TCSMINPOOL].flag != 0)
  362. && (parameter[TCSMINPOOL].value > parameter[TCSNUM].value))
  363. {
  364. se_trace(SE_TRACE_ERROR, SET_TCS_MIN_POOL_ERROR);
  365. return false;
  366. }
  367. m_create_param.heap_init_size = parameter[HEAPINITSIZE].flag ? parameter[HEAPINITSIZE].value : parameter[HEAPMAXSIZE].value;
  368. m_create_param.heap_min_size = parameter[HEAPMINSIZE].value;
  369. m_create_param.heap_max_size = parameter[HEAPMAXSIZE].value;
  370. m_create_param.stack_max_size = parameter[STACKMAXSIZE].value;
  371. m_create_param.stack_min_size = parameter[STACKMINSIZE].value;
  372. m_create_param.tcs_num = (uint32_t)parameter[TCSNUM].value;
  373. m_create_param.tcs_max_num = (uint32_t)(parameter[TCSMAXNUM].flag ? parameter[TCSMAXNUM].value : parameter[TCSNUM].value);
  374. m_create_param.tcs_min_pool = (uint32_t)parameter[TCSMINPOOL].value;
  375. m_create_param.tcs_policy = (uint32_t)parameter[TCSPOLICY].value;
  376. se_trace(SE_TRACE_ERROR, "tcs_num %d, tcs_max_num %d, tcs_min_pool %d\n", m_create_param.tcs_num, m_create_param.tcs_max_num, m_create_param.tcs_min_pool);
  377. return true;
  378. }
  379. void *CMetadata::alloc_buffer_from_metadata(uint32_t size)
  380. {
  381. void *addr = GET_PTR(void, m_metadata, m_metadata->size);
  382. m_metadata->size += size;
  383. if((m_metadata->size < size) || (m_metadata->size > METADATA_SIZE))
  384. {
  385. return NULL;
  386. }
  387. return addr;
  388. }
  389. /*
  390. * Called within build_layout_table(), used to assign the rva to entry layout
  391. * and load_step to group layout.
  392. */
  393. bool CMetadata::update_layout_entries()
  394. {
  395. m_rva = calculate_sections_size();
  396. if(m_rva == 0)
  397. {
  398. se_trace(SE_TRACE_ERROR, INVALID_ENCLAVE_ERROR);
  399. return false;
  400. }
  401. for(uint32_t i = 0; i < m_layouts.size(); i++)
  402. {
  403. if(!IS_GROUP_ID(m_layouts[i].entry.id))
  404. {
  405. m_layouts[i].entry.rva = m_rva;
  406. m_rva += (((uint64_t)m_layouts[i].entry.page_count) << SE_PAGE_SHIFT);
  407. }
  408. else
  409. {
  410. for (uint32_t j = 0; j < m_layouts[i].group.entry_count; j++)
  411. {
  412. m_layouts[i].group.load_step += ((uint64_t)(m_layouts[i-j-1].entry.page_count)) << SE_PAGE_SHIFT;
  413. }
  414. m_rva += m_layouts[i].group.load_times * m_layouts[i].group.load_step;
  415. }
  416. }
  417. return true;
  418. }
  419. bool CMetadata::build_layout_entries()
  420. {
  421. uint32_t size = (uint32_t)(m_layouts.size() * sizeof(layout_t));
  422. layout_t *layout_table = (layout_t *) alloc_buffer_from_metadata(size);
  423. if(layout_table == NULL)
  424. {
  425. se_trace(SE_TRACE_ERROR, INVALID_ENCLAVE_ERROR);
  426. return false;
  427. }
  428. m_metadata->dirs[DIR_LAYOUT].offset = (uint32_t)PTR_DIFF(layout_table, m_metadata);
  429. m_metadata->dirs[DIR_LAYOUT].size = size;
  430. for(uint32_t i = 0; i < m_layouts.size(); i++, layout_table++)
  431. {
  432. memcpy_s(layout_table, sizeof(layout_t), &m_layouts[i], sizeof(layout_t));
  433. }
  434. // enclave virtual size
  435. m_metadata->enclave_size = calculate_enclave_size(m_rva);
  436. if(m_metadata->enclave_size == (uint64_t)-1)
  437. {
  438. se_trace(SE_TRACE_ERROR, OUT_OF_EPC_ERROR);
  439. return false;
  440. }
  441. // the last guard page entry to round the enclave size to power of 2
  442. if(m_metadata->enclave_size - m_rva > 0)
  443. {
  444. layout_table = (layout_t *)alloc_buffer_from_metadata(sizeof(layout_t));
  445. if(layout_table == NULL)
  446. {
  447. se_trace(SE_TRACE_ERROR, INVALID_ENCLAVE_ERROR);
  448. return false;
  449. }
  450. layout_table->entry.id = LAYOUT_ID_GUARD;
  451. layout_table->entry.rva = m_rva;
  452. layout_table->entry.page_count = (uint32_t)((m_metadata->enclave_size - m_rva) >> SE_PAGE_SHIFT);
  453. m_metadata->dirs[DIR_LAYOUT].size += (uint32_t)sizeof(layout_t);
  454. }
  455. return true;
  456. }
  457. bool CMetadata::build_layout_table()
  458. {
  459. layout_t layout;
  460. memset(&layout, 0, sizeof(layout));
  461. layout_t guard_page;
  462. memset(&guard_page, 0, sizeof(guard_page));
  463. guard_page.entry.id = LAYOUT_ID_GUARD;
  464. guard_page.entry.page_count = SE_GUARD_PAGE_SIZE >> SE_PAGE_SHIFT;
  465. size_t thread_start;
  466. size_t thread_end;
  467. // heap
  468. layout.entry.id = LAYOUT_ID_HEAP_MIN;
  469. layout.entry.page_count = (uint32_t)(m_create_param.heap_min_size >> SE_PAGE_SHIFT);
  470. layout.entry.attributes = PAGE_ATTR_EADD;
  471. layout.entry.si_flags = SI_FLAGS_RW;
  472. m_layouts.push_back(layout);
  473. if(m_create_param.heap_init_size > m_create_param.heap_min_size)
  474. {
  475. layout.entry.id = LAYOUT_ID_HEAP_INIT;
  476. layout.entry.page_count = (uint32_t)((m_create_param.heap_init_size - m_create_param.heap_min_size) >> SE_PAGE_SHIFT);
  477. layout.entry.attributes = PAGE_ATTR_EADD | PAGE_ATTR_POST_REMOVE | PAGE_ATTR_POST_ADD;
  478. layout.entry.si_flags = SI_FLAGS_RW;
  479. m_layouts.push_back(layout);
  480. }
  481. if(m_create_param.heap_max_size > m_create_param.heap_init_size)
  482. {
  483. layout.entry.id = LAYOUT_ID_HEAP_MAX;
  484. layout.entry.page_count = (uint32_t)((m_create_param.heap_max_size - m_create_param.heap_init_size) >> SE_PAGE_SHIFT);
  485. layout.entry.attributes = PAGE_ATTR_POST_ADD;
  486. layout.entry.si_flags = SI_FLAGS_RW;
  487. m_layouts.push_back(layout);
  488. }
  489. // thread context memory layout
  490. // guard page | stack | TCS | SSA | guard page | TLS
  491. thread_start = m_layouts.size();
  492. //ulitity tcs
  493. // guard page
  494. m_layouts.push_back(guard_page);
  495. // stack
  496. if(m_create_param.stack_max_size > m_create_param.stack_min_size)
  497. {
  498. layout.entry.id = LAYOUT_ID_STACK_MAX;
  499. layout.entry.page_count = (uint32_t)((m_create_param.stack_max_size - m_create_param.stack_min_size) >> SE_PAGE_SHIFT);
  500. layout.entry.attributes = PAGE_ATTR_EADD | PAGE_ATTR_EEXTEND | PAGE_DIR_GROW_DOWN; // | PAGE_ATTR_POST_ADD | PAGE_ATTR_POST_REMOVE;
  501. layout.entry.si_flags = SI_FLAGS_RW;
  502. layout.entry.content_size = 0xCCCCCCCC;
  503. m_layouts.push_back(layout);
  504. }
  505. layout.entry.id = LAYOUT_ID_STACK_MIN;
  506. layout.entry.page_count = (uint32_t)(m_create_param.stack_min_size >> SE_PAGE_SHIFT);
  507. layout.entry.attributes = PAGE_ATTR_EADD | PAGE_ATTR_EEXTEND;
  508. layout.entry.si_flags = SI_FLAGS_RW;
  509. layout.entry.content_size = 0xCCCCCCCC;
  510. m_layouts.push_back(layout);
  511. // guard page
  512. m_layouts.push_back(guard_page);
  513. // tcs
  514. layout.entry.id = LAYOUT_ID_TCS;
  515. layout.entry.page_count = TCS_SIZE >> SE_PAGE_SHIFT;
  516. layout.entry.attributes = PAGE_ATTR_EADD | PAGE_ATTR_EEXTEND;
  517. layout.entry.si_flags = SI_FLAGS_TCS;
  518. tcs_t *tcs_template = (tcs_t *) alloc_buffer_from_metadata(TCS_TEMPLATE_SIZE);
  519. if(tcs_template == NULL)
  520. {
  521. se_trace(SE_TRACE_ERROR, INVALID_ENCLAVE_ERROR);
  522. return false;
  523. }
  524. layout.entry.content_offset = (uint32_t)PTR_DIFF(tcs_template, m_metadata),
  525. layout.entry.content_size = TCS_TEMPLATE_SIZE;
  526. m_layouts.push_back(layout);
  527. memset(&layout, 0, sizeof(layout));
  528. // ssa
  529. layout.entry.id = LAYOUT_ID_SSA;
  530. layout.entry.page_count = SSA_FRAME_SIZE * SSA_NUM;
  531. layout.entry.attributes = PAGE_ATTR_EADD | PAGE_ATTR_EEXTEND;
  532. layout.entry.si_flags = SI_FLAGS_RW;
  533. m_layouts.push_back(layout);
  534. // guard page
  535. m_layouts.push_back(guard_page);
  536. // td
  537. layout.entry.id = LAYOUT_ID_TD;
  538. layout.entry.page_count = 1;
  539. const Section *section = m_parser->get_tls_section();
  540. if(section)
  541. {
  542. layout.entry.page_count += (uint32_t)(ROUND_TO_PAGE(section->virtual_size()) >> SE_PAGE_SHIFT);
  543. }
  544. layout.entry.attributes = PAGE_ATTR_EADD | PAGE_ATTR_EEXTEND;
  545. layout.entry.si_flags = SI_FLAGS_RW;
  546. m_layouts.push_back(layout);
  547. thread_end = m_layouts.size();
  548. uint32_t tcs_min_pool = 0;
  549. uint32_t tcs_eremove = 0;
  550. if(m_create_param.tcs_min_pool > m_create_param.tcs_num - 1)
  551. {
  552. tcs_min_pool = m_create_param.tcs_num - 1;
  553. tcs_eremove = 0;
  554. }
  555. else
  556. {
  557. tcs_min_pool = m_create_param.tcs_min_pool;
  558. tcs_eremove = m_create_param.tcs_num -1 - m_create_param.tcs_min_pool;
  559. }
  560. //tcs to fill the tcs mini pool
  561. if (tcs_min_pool > 0)
  562. {
  563. // group for static thread contexts
  564. memset(&layout, 0, sizeof(layout));
  565. layout.group.id = LAYOUT_ID_THREAD_GROUP;
  566. layout.group.entry_count = (uint16_t)(thread_end - thread_start);
  567. layout.group.load_times = tcs_min_pool;
  568. m_layouts.push_back(layout);
  569. }
  570. //tcs will be eremoved
  571. if (tcs_eremove > 0)
  572. {
  573. for(size_t i = thread_start; i < thread_end; i++)
  574. {
  575. layout = m_layouts[i];
  576. if(layout.entry.id != LAYOUT_ID_GUARD)
  577. {
  578. layout.entry.attributes |= PAGE_ATTR_EREMOVE;
  579. }
  580. m_layouts.push_back(layout);
  581. }
  582. if (tcs_eremove > 1)
  583. {
  584. memset(&layout, 0, sizeof(layout));
  585. layout.group.id = LAYOUT_ID_THREAD_GROUP;
  586. layout.group.entry_count = (uint16_t)(thread_end - thread_start);
  587. layout.group.load_times = tcs_eremove-1;
  588. m_layouts.push_back(layout);
  589. }
  590. }
  591. // dynamic thread contexts
  592. if (m_create_param.tcs_max_num > tcs_min_pool + 1)
  593. {
  594. for(size_t i = thread_start; i < thread_end; i++)
  595. {
  596. layout = m_layouts[i];
  597. if(layout.entry.id == LAYOUT_ID_STACK_MAX)
  598. {
  599. layout.entry.id = (uint16_t)(LAYOUT_ID_HEAP_DYN_MIN - LAYOUT_ID_HEAP_MIN + layout.entry.id);
  600. layout.entry.attributes = PAGE_ATTR_POST_ADD | PAGE_DIR_GROW_DOWN;
  601. }
  602. else if(layout.entry.id != LAYOUT_ID_GUARD)
  603. {
  604. layout.entry.id = (uint16_t)(LAYOUT_ID_HEAP_DYN_MIN - LAYOUT_ID_HEAP_MIN + layout.entry.id);
  605. layout.entry.attributes = PAGE_ATTR_POST_ADD | PAGE_ATTR_DYN_THREAD;
  606. }
  607. m_layouts.push_back(layout);
  608. }
  609. // dynamic thread group
  610. if (m_create_param.tcs_max_num > m_create_param.tcs_min_pool)
  611. {
  612. memset(&layout, 0, sizeof(layout));
  613. layout.group.id = LAYOUT_ID_THREAD_GROUP_DYN;
  614. layout.group.entry_count = (uint16_t)(thread_end - thread_start);
  615. layout.group.load_times = m_create_param.tcs_max_num - tcs_min_pool - 1;
  616. m_layouts.push_back(layout);
  617. }
  618. }
  619. // update layout entries
  620. if(false == update_layout_entries())
  621. {
  622. return false;
  623. }
  624. // tcs template
  625. if(false == build_tcs_template(tcs_template))
  626. {
  627. se_trace(SE_TRACE_ERROR, INVALID_ENCLAVE_ERROR);
  628. return false;
  629. }
  630. return true;
  631. }
  632. bool CMetadata::build_patch_entries(vector<patch_entry_t> &patches)
  633. {
  634. uint32_t size = (uint32_t)(patches.size() * sizeof(patch_entry_t));
  635. patch_entry_t *patch_table = (patch_entry_t *) alloc_buffer_from_metadata(size);
  636. if(patch_table == NULL)
  637. {
  638. se_trace(SE_TRACE_ERROR, INVALID_ENCLAVE_ERROR);
  639. return false;
  640. }
  641. m_metadata->dirs[DIR_PATCH].offset = (uint32_t)PTR_DIFF(patch_table, m_metadata);
  642. m_metadata->dirs[DIR_PATCH].size = size;
  643. for(uint32_t i = 0; i < patches.size(); i++)
  644. {
  645. memcpy_s(patch_table, sizeof(patch_entry_t), &patches[i], sizeof(patch_entry_t));
  646. patch_table++;
  647. }
  648. return true;
  649. }
  650. bool CMetadata::build_patch_table()
  651. {
  652. const uint8_t *base_addr = (const uint8_t *)m_parser->get_start_addr();
  653. vector<patch_entry_t> patches;
  654. patch_entry_t patch;
  655. memset(&patch, 0, sizeof(patch));
  656. // td template
  657. m_gd_size = m_parser->get_global_data_size();
  658. m_gd_template = (uint8_t *)alloc_buffer_from_metadata(m_gd_size);
  659. if(m_gd_template == NULL)
  660. {
  661. return false;
  662. }
  663. uint64_t rva = m_parser->get_symbol_rva("g_global_data");
  664. if(0 == rva)
  665. {
  666. se_trace(SE_TRACE_ERROR, INVALID_ENCLAVE_ERROR);
  667. return false;
  668. }
  669. patch.dst = (uint64_t)PTR_DIFF(get_rawdata_by_rva(rva), base_addr);
  670. patch.src = (uint32_t)PTR_DIFF(m_gd_template, m_metadata);
  671. patch.size = m_gd_size;
  672. patches.push_back(patch);
  673. // patch the image header
  674. uint64_t *zero = (uint64_t *)alloc_buffer_from_metadata(sizeof(*zero));
  675. if(zero == NULL)
  676. {
  677. se_trace(SE_TRACE_ERROR, INVALID_ENCLAVE_ERROR);
  678. return false;
  679. }
  680. *zero = 0;
  681. bin_fmt_t bf = m_parser->get_bin_format();
  682. if(bf == BF_ELF32)
  683. {
  684. Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *)base_addr;
  685. patch.dst = (uint64_t)PTR_DIFF(&elf_hdr->e_shnum, base_addr);
  686. patch.src = (uint32_t)PTR_DIFF(zero, m_metadata);
  687. patch.size = (uint32_t)sizeof(elf_hdr->e_shnum);
  688. patches.push_back(patch);
  689. patch.dst = (uint64_t)PTR_DIFF(&elf_hdr->e_shoff, base_addr);
  690. patch.src = (uint32_t)PTR_DIFF(zero, m_metadata);
  691. patch.size = (uint32_t)sizeof(elf_hdr->e_shoff);
  692. patches.push_back(patch);
  693. patch.dst = (uint64_t)PTR_DIFF(&elf_hdr->e_shstrndx, base_addr);
  694. patch.src = (uint32_t)PTR_DIFF(zero, m_metadata);
  695. patch.size = (uint32_t)sizeof(elf_hdr->e_shstrndx);
  696. patches.push_back(patch);
  697. // Modify GNU_RELRO info to eliminate the impact of enclave measurement.
  698. Elf32_Phdr *prg_hdr = GET_PTR(Elf32_Phdr, base_addr, elf_hdr->e_phoff);
  699. for (unsigned idx = 0; idx < elf_hdr->e_phnum; ++idx, ++prg_hdr)
  700. {
  701. if(prg_hdr->p_type == PT_GNU_RELRO)
  702. {
  703. patch.dst = (uint64_t)PTR_DIFF(prg_hdr, base_addr);
  704. patch.src = (uint32_t)PTR_DIFF(zero, m_metadata);
  705. patch.size = (uint32_t)sizeof(Elf32_Phdr);
  706. patches.push_back(patch);
  707. break;
  708. }
  709. }
  710. }
  711. else if(bf == BF_ELF64)
  712. {
  713. Elf64_Ehdr *elf_hdr = (Elf64_Ehdr *)base_addr;
  714. patch.dst = (uint64_t)PTR_DIFF(&elf_hdr->e_shnum, base_addr);
  715. patch.src = (uint32_t)PTR_DIFF(zero, m_metadata);
  716. patch.size = (uint32_t)sizeof(elf_hdr->e_shnum);
  717. patches.push_back(patch);
  718. patch.dst = (uint64_t)PTR_DIFF(&elf_hdr->e_shoff, base_addr);
  719. patch.src = (uint32_t)PTR_DIFF(zero, m_metadata);
  720. patch.size = (uint32_t)sizeof(elf_hdr->e_shoff);
  721. patches.push_back(patch);
  722. patch.dst = (uint64_t)PTR_DIFF(&elf_hdr->e_shstrndx, base_addr);
  723. patch.src = (uint32_t)PTR_DIFF(zero, m_metadata);
  724. patch.size = (uint32_t)sizeof(elf_hdr->e_shstrndx);
  725. patches.push_back(patch);
  726. }
  727. if(false == build_patch_entries(patches))
  728. {
  729. se_trace(SE_TRACE_ERROR, NO_MEMORY_ERROR);
  730. return false;
  731. }
  732. return true;
  733. }
  734. layout_entry_t *CMetadata::get_entry_by_id(uint16_t id)
  735. {
  736. for (uint32_t i = 0; i < m_layouts.size(); i++)
  737. {
  738. if(m_layouts[i].entry.id == id)
  739. return (layout_entry_t *)&m_layouts[i];
  740. }
  741. assert(false);
  742. return NULL;
  743. }
  744. bool CMetadata::get_xsave_size(uint64_t xfrm, uint32_t *xsave_size)
  745. {
  746. assert (xsave_size != NULL);
  747. struct {
  748. uint64_t bits;
  749. uint32_t size;
  750. } xsave_size_table[] = { // Note that the xsave_size should be in ascending order
  751. {SGX_XFRM_LEGACY, 512 + 64}, // 512 for legacy features, 64 for xsave header
  752. {SGX_XFRM_AVX, 512 + 64 + 256}, // 256 for YMM0_H - YMM15_H registers
  753. {SGX_XFRM_MPX, 512 + 64 + 256 + 256}, // 256 for MPX
  754. {SGX_XFRM_AVX512, 512 + 64 + 256 + 256 + 1600}, // 1600 for k0 - k7, ZMM0_H - ZMM15_H, ZMM16 - ZMM31
  755. // PT, PKRU ...
  756. };
  757. bool ret = true;
  758. *xsave_size = 0;
  759. if(!xfrm || (xfrm & SGX_XFRM_RESERVED))
  760. {
  761. return false;
  762. }
  763. for(size_t i = 0; i < sizeof(xsave_size_table)/sizeof(xsave_size_table[0]); i++)
  764. {
  765. if((xfrm & xsave_size_table[i].bits) == xsave_size_table[i].bits)
  766. {
  767. *xsave_size = xsave_size_table[i].size;
  768. }
  769. }
  770. return ret;
  771. }
  772. bool CMetadata::build_gd_template(uint8_t *data, uint32_t *data_size)
  773. {
  774. if(false == get_xsave_size(m_metadata->attributes.xfrm, &m_create_param.xsave_size))
  775. {
  776. return false;
  777. }
  778. m_create_param.stack_base_addr = (size_t)(get_entry_by_id(LAYOUT_ID_STACK_MIN)->rva + m_create_param.stack_min_size - get_entry_by_id(LAYOUT_ID_TCS)->rva);
  779. m_create_param.stack_limit_addr = (size_t)(m_create_param.stack_base_addr - m_create_param.stack_max_size);
  780. m_create_param.ssa_base_addr = (size_t)(get_entry_by_id(LAYOUT_ID_SSA)->rva - get_entry_by_id(LAYOUT_ID_TCS)->rva);
  781. m_create_param.enclave_size = m_metadata->enclave_size;
  782. m_create_param.heap_offset = (size_t)get_entry_by_id(LAYOUT_ID_HEAP_MIN)->rva;
  783. size_t tmp_tls_addr = (size_t)(get_entry_by_id(LAYOUT_ID_TD)->rva - get_entry_by_id(LAYOUT_ID_TCS)->rva);
  784. m_create_param.td_addr = tmp_tls_addr + (size_t)((get_entry_by_id(LAYOUT_ID_TD)->page_count - 1) << SE_PAGE_SHIFT);
  785. const Section *section = m_parser->get_tls_section();
  786. if(section)
  787. {
  788. /* adjust the tls_addr to be the pointer to the actual TLS data area */
  789. m_create_param.tls_addr = (size_t)(m_create_param.td_addr - section->virtual_size());
  790. assert(TRIM_TO_PAGE(m_create_param.tls_addr) == tmp_tls_addr);
  791. }
  792. else
  793. m_create_param.tls_addr = tmp_tls_addr;
  794. if(false == m_parser->update_global_data(m_metadata, &m_create_param, data, data_size))
  795. {
  796. se_trace(SE_TRACE_ERROR, NO_MEMORY_ERROR); // metadata structure doesnot have enough memory for global_data template
  797. return false;
  798. }
  799. return true;
  800. }
  801. bool CMetadata::build_tcs_template(tcs_t *tcs)
  802. {
  803. tcs->oentry = m_parser->get_symbol_rva("enclave_entry");
  804. if(tcs->oentry == 0)
  805. {
  806. return false;
  807. }
  808. tcs->nssa = SSA_NUM;
  809. tcs->cssa = 0;
  810. tcs->ossa = get_entry_by_id(LAYOUT_ID_SSA)->rva - get_entry_by_id(LAYOUT_ID_TCS)->rva;
  811. //fs/gs pointer at TLS/TD
  812. tcs->ofs_base = tcs->ogs_base = get_entry_by_id(LAYOUT_ID_TD)->rva - get_entry_by_id(LAYOUT_ID_TCS)->rva + (((uint64_t)get_entry_by_id(LAYOUT_ID_TD)->page_count - 1) << SE_PAGE_SHIFT);
  813. tcs->ofs_limit = tcs->ogs_limit = (uint32_t)-1;
  814. return true;
  815. }
  816. void* CMetadata::get_rawdata_by_rva(uint64_t rva)
  817. {
  818. std::vector<Section*> sections = m_parser->get_sections();
  819. for(unsigned int i = 0; i < sections.size() ; i++)
  820. {
  821. uint64_t start_rva = TRIM_TO_PAGE(sections[i]->get_rva());
  822. uint64_t end_rva = ROUND_TO_PAGE(sections[i]->get_rva() + sections[i]->virtual_size());
  823. if(start_rva <= rva && rva < end_rva)
  824. {
  825. uint64_t offset = rva - sections[i]->get_rva();
  826. if (offset > sections[i]->raw_data_size())
  827. {
  828. return 0;
  829. }
  830. return GET_PTR(void, sections[i]->raw_data(), offset);
  831. }
  832. }
  833. return 0;
  834. }
  835. uint64_t CMetadata::calculate_sections_size()
  836. {
  837. std::vector<Section*> sections = m_parser->get_sections();
  838. uint64_t max_rva = 0;
  839. Section *last_section = NULL;
  840. for(unsigned int i = 0; i < sections.size() ; i++)
  841. {
  842. if(sections[i]->get_rva() > max_rva) {
  843. max_rva = sections[i]->get_rva();
  844. last_section = sections[i];
  845. }
  846. }
  847. uint64_t size = (NULL == last_section) ? (0) : (last_section->get_rva() + last_section->virtual_size());
  848. size = ROUND_TO_PAGE(size);
  849. return size;
  850. }
  851. uint64_t CMetadata::calculate_enclave_size(uint64_t size)
  852. {
  853. uint64_t enclave_max_size = m_parser->get_enclave_max_size();
  854. if(size > enclave_max_size)
  855. return (uint64_t)-1;
  856. uint64_t round_size = 1;
  857. while (round_size < size)
  858. {
  859. round_size <<=1;
  860. if(!round_size)
  861. return (uint64_t)-1;
  862. }
  863. if(round_size > enclave_max_size)
  864. return (uint64_t)-1;
  865. return round_size;
  866. }
  867. bool update_metadata(const char *path, const metadata_t *metadata, uint64_t meta_offset)
  868. {
  869. assert(path != NULL && metadata != NULL);
  870. return write_data_to_file(path, std::ios::in | std::ios::binary| std::ios::out,
  871. reinterpret_cast<uint8_t *>(const_cast<metadata_t *>( metadata)), METADATA_SIZE, (long)meta_offset);
  872. }
  873. #define PRINT_ELEMENT(stream, structure, element) \
  874. do { \
  875. (stream) << #structure << "->" << #element << ": " << std::hex << "0x" << structure->element << std::endl; \
  876. }while(0)
  877. #define PRINT_ARRAY(stream, structure, array, size) \
  878. do{ \
  879. (stream) << #structure << "->" << #array << ":" << std::hex; \
  880. for(size_t i = 0; i < size; i++) \
  881. { \
  882. if (i % 16 == 0) (stream) << std::endl; \
  883. (stream) << "0x" << std::setfill('0') << std::setw(2) << (uint32_t)(structure)->array[i] << " "; \
  884. } \
  885. (stream) << std::endl; \
  886. }while(0)
  887. #define CONCAT(name, num) name##num
  888. #define A(num) CONCAT(metadata, num)
  889. static void print_metadata_internal(std::ofstream &meta_ofs, const metadata_t *metadata)
  890. {
  891. assert(metadata != NULL);
  892. PRINT_ELEMENT(meta_ofs, metadata, magic_num);
  893. PRINT_ELEMENT(meta_ofs, metadata, version);
  894. PRINT_ELEMENT(meta_ofs, metadata, size);
  895. PRINT_ELEMENT(meta_ofs, metadata, tcs_policy);
  896. PRINT_ELEMENT(meta_ofs, metadata, ssa_frame_size);
  897. PRINT_ELEMENT(meta_ofs, metadata, max_save_buffer_size);
  898. PRINT_ELEMENT(meta_ofs, metadata, desired_misc_select);
  899. PRINT_ELEMENT(meta_ofs, metadata, enclave_size);
  900. PRINT_ELEMENT(meta_ofs, metadata, attributes.flags);
  901. PRINT_ELEMENT(meta_ofs, metadata, attributes.xfrm);
  902. // css.header
  903. PRINT_ARRAY(meta_ofs, metadata, enclave_css.header.header, 12);
  904. PRINT_ELEMENT(meta_ofs, metadata, enclave_css.header.type);
  905. PRINT_ELEMENT(meta_ofs, metadata, enclave_css.header.module_vendor);
  906. PRINT_ELEMENT(meta_ofs, metadata, enclave_css.header.date);
  907. PRINT_ARRAY(meta_ofs, metadata, enclave_css.header.header2, 16);
  908. PRINT_ELEMENT(meta_ofs, metadata, enclave_css.header.hw_version);
  909. // css.key
  910. PRINT_ARRAY(meta_ofs, metadata, enclave_css.key.modulus, SE_KEY_SIZE);
  911. PRINT_ARRAY(meta_ofs, metadata, enclave_css.key.exponent, SE_EXPONENT_SIZE);
  912. PRINT_ARRAY(meta_ofs, metadata, enclave_css.key.signature, SE_KEY_SIZE);
  913. // css.body
  914. PRINT_ELEMENT(meta_ofs, metadata, enclave_css.body.misc_select);
  915. PRINT_ELEMENT(meta_ofs, metadata, enclave_css.body.misc_mask);
  916. PRINT_ELEMENT(meta_ofs, metadata, enclave_css.body.attributes.flags);
  917. PRINT_ELEMENT(meta_ofs, metadata, enclave_css.body.attributes.xfrm);
  918. PRINT_ELEMENT(meta_ofs, metadata, enclave_css.body.attribute_mask.flags);
  919. PRINT_ELEMENT(meta_ofs, metadata, enclave_css.body.attribute_mask.xfrm);
  920. PRINT_ARRAY(meta_ofs, metadata, enclave_css.body.enclave_hash.m, SGX_HASH_SIZE);
  921. PRINT_ELEMENT(meta_ofs, metadata, enclave_css.body.isv_prod_id);
  922. PRINT_ELEMENT(meta_ofs, metadata, enclave_css.body.isv_svn);
  923. // css.buffer
  924. PRINT_ARRAY(meta_ofs, metadata, enclave_css.buffer.q1, SE_KEY_SIZE);
  925. PRINT_ARRAY(meta_ofs, metadata, enclave_css.buffer.q2, SE_KEY_SIZE);
  926. }
  927. bool print_metadata(const char *path, const metadata_t *metadata)
  928. {
  929. assert(path != NULL && metadata != NULL);
  930. std::ofstream meta_ofs(path, std::ofstream::out | std::ofstream::trunc);
  931. if (!meta_ofs.good())
  932. {
  933. se_trace(SE_TRACE_ERROR, OPEN_FILE_ERROR, path);
  934. return false;
  935. }
  936. meta_ofs << "=============================" << std::endl
  937. << "The metadata information:" << std::endl
  938. << "=============================" << std::endl;
  939. print_metadata_internal(meta_ofs, metadata);
  940. // Print the compatible metadata info
  941. size_t compat_meta_count = 0;
  942. do {
  943. metadata_t *compatible_metadata = GET_PTR(metadata_t, metadata, metadata->size);
  944. if(compatible_metadata == NULL || (compatible_metadata->magic_num == METADATA_MAGIC && compatible_metadata->size == 0))
  945. {
  946. meta_ofs.close();
  947. return false;
  948. }
  949. if(compatible_metadata->magic_num != METADATA_MAGIC)
  950. break;
  951. compat_meta_count++;
  952. if(compat_meta_count == 1)
  953. {
  954. meta_ofs << std::endl << std::endl << std::endl
  955. << "====================================" << std::endl
  956. << "The compatible metadata information: " << std::endl
  957. << "====================================" << std::endl;
  958. }
  959. meta_ofs << std::endl << "Compatible metadata number "
  960. << compat_meta_count << ":" << std::endl
  961. << "------------------------------" << std::endl;
  962. print_metadata_internal(meta_ofs, compatible_metadata);
  963. metadata = compatible_metadata;
  964. }while(1);
  965. typedef struct _mrsigner_t
  966. {
  967. uint8_t value[SGX_HASH_SIZE];
  968. } mrsigner_t;
  969. mrsigner_t ms;
  970. memset(&ms, 0, sizeof(mrsigner_t));
  971. mrsigner_t *mrsigner = &ms;
  972. unsigned int signer_len = SGX_HASH_SIZE;
  973. if(sgx_EVP_Digest(EVP_sha256(), metadata->enclave_css.key.modulus, SE_KEY_SIZE, ms.value, &signer_len) != SGX_SUCCESS)
  974. {
  975. se_trace(SE_TRACE_ERROR, "ERROR: failed to calculate the mrsigner.\n");
  976. meta_ofs.close();
  977. return false;
  978. }
  979. meta_ofs << std::endl << std::endl
  980. << "===================" << std::endl
  981. << "The mrsigner value:" << std::endl
  982. << "===================" << std::endl;
  983. PRINT_ARRAY(meta_ofs, mrsigner, value, SGX_HASH_SIZE);
  984. meta_ofs.close();
  985. return true;
  986. }