sign_tool.cpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. /*
  2. * Copyright (C) 2011-2016 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. // SignTool.cpp : Defines the entry point for the console application.
  32. //
  33. /**
  34. * File:
  35. * sign_tool.cpp
  36. *Description:
  37. * Defines the entry point for the application.
  38. *
  39. */
  40. #include "ippcp.h"
  41. #include "ippcore.h"
  42. #include "metadata.h"
  43. #include "manage_metadata.h"
  44. #include "ipp_wrapper.h"
  45. #include "parse_key_file.h"
  46. #include "enclave_creator_sign.h"
  47. #include "util_st.h"
  48. #include "se_trace.h"
  49. #include "sgx_error.h"
  50. #include "se_map.h"
  51. #include "loader.h"
  52. #include "parserfactory.h"
  53. #include "elf_helper.h"
  54. #include <unistd.h>
  55. #include <stdio.h>
  56. #include <stdlib.h>
  57. #include <time.h>
  58. #include <assert.h>
  59. #include <string>
  60. #include <memory>
  61. #include <sstream>
  62. #define SIGNATURE_SIZE 384
  63. typedef enum _command_mode_t
  64. {
  65. SIGN = 0,
  66. GENDATA,
  67. CATSIG,
  68. COMPARE
  69. } command_mode_t;
  70. typedef enum _file_path_t
  71. {
  72. DLL = 0,
  73. XML = 1,
  74. KEY,
  75. OUTPUT,
  76. SIG,
  77. UNSIGNED,
  78. REVIEW_ENCLAVE
  79. } file_path_t;
  80. static bool get_time(uint32_t *date)
  81. {
  82. assert(date != NULL);
  83. time_t rawtime = 0;
  84. if(time( &rawtime) == -1)
  85. return false;
  86. struct tm *timeinfo = gmtime(&rawtime);
  87. if(timeinfo == NULL)
  88. return false;
  89. uint32_t tmp_date = (timeinfo->tm_year+1900)*10000 + (timeinfo->tm_mon+1)*100 + timeinfo->tm_mday;
  90. stringstream ss;
  91. ss<<"0x"<<tmp_date;
  92. ss>>hex>>tmp_date;
  93. *date = tmp_date;
  94. return true;
  95. }
  96. static int load_enclave(BinParser *parser, metadata_t *metadata)
  97. {
  98. std::unique_ptr<CLoader> ploader(new CLoader(const_cast<uint8_t *>(parser->get_start_addr()), *parser));
  99. return ploader->load_enclave_ex(NULL, 0, metadata, NULL);
  100. }
  101. #define THE_INVALID_HANDLE (-1)
  102. static int open_file(const char* dllpath)
  103. {
  104. FILE *fp = fopen(dllpath, "rb");
  105. if (fp == NULL)
  106. return THE_INVALID_HANDLE;
  107. return fileno(fp);
  108. }
  109. static void close_handle(int fd)
  110. {
  111. close(fd);
  112. }
  113. static bool get_enclave_info(BinParser *parser, bin_fmt_t *bf, uint64_t * meta_offset)
  114. {
  115. uint64_t meta_rva = parser->get_metadata_offset();
  116. const uint8_t *base_addr = parser->get_start_addr();
  117. metadata_t *metadata = GET_PTR(metadata_t, base_addr, meta_rva);
  118. if(metadata->magic_num == METADATA_MAGIC)
  119. {
  120. se_trace(SE_TRACE_ERROR, ENCLAVE_ALREADY_SIGNED_ERROR);
  121. return false;
  122. }
  123. *bf = parser->get_bin_format();
  124. *meta_offset = meta_rva;
  125. return true;
  126. }
  127. // measure_enclave():
  128. // 1. Get the enclave hash by loading enclave
  129. // 2. Get the enclave info - metadata offset and enclave file format
  130. static bool measure_enclave(uint8_t *hash, const char *dllpath, const xml_parameter_t *parameter, metadata_t *metadata, bin_fmt_t *bin_fmt, uint64_t *meta_offset)
  131. {
  132. assert(hash && dllpath && metadata && bin_fmt && meta_offset);
  133. bool res = false;
  134. uint32_t file_size = 0;
  135. se_file_handle_t fh = open_file(dllpath);
  136. if (fh == THE_INVALID_HANDLE)
  137. {
  138. se_trace(SE_TRACE_ERROR, OPEN_FILE_ERROR, dllpath);
  139. return false;
  140. }
  141. // Probably we can use `decltype' if all major supported compilers support that.
  142. std::unique_ptr<map_handle_t, void (*)(map_handle_t*)> mh(map_file(fh, &file_size), unmap_file);
  143. if (!mh)
  144. {
  145. close_handle(fh);
  146. return false;
  147. }
  148. // Parse enclave
  149. std::unique_ptr<BinParser> parser(binparser::get_parser(mh->base_addr, (size_t)file_size));
  150. assert(parser != NULL);
  151. sgx_status_t status = parser->run_parser();
  152. if (status != SGX_SUCCESS)
  153. {
  154. se_trace(SE_TRACE_ERROR, INVALID_ENCLAVE_ERROR);
  155. close_handle(fh);
  156. return false;
  157. }
  158. // generate metadata
  159. CMetadata meta(metadata, parser.get());
  160. if(meta.build_metadata(parameter) == false)
  161. {
  162. close_handle(fh);
  163. return false;
  164. }
  165. // Collect enclave info
  166. if(get_enclave_info(parser.get(), bin_fmt, meta_offset) == false)
  167. {
  168. close_handle(fh);
  169. return false;
  170. }
  171. if (*bin_fmt == BF_ELF64)
  172. {
  173. ElfHelper<64>::dump_textrels(parser.get());
  174. }
  175. else if (*bin_fmt == BF_ELF32)
  176. {
  177. ElfHelper<32>::dump_textrels(parser.get());
  178. }
  179. // Load enclave to get enclave hash
  180. int ret = load_enclave(parser.release(), metadata);
  181. close_handle(fh);
  182. switch(ret)
  183. {
  184. case SGX_ERROR_INVALID_METADATA:
  185. se_trace(SE_TRACE_ERROR, OUT_OF_EPC_ERROR);
  186. res = false;
  187. break;
  188. case SGX_ERROR_INVALID_VERSION:
  189. se_trace(SE_TRACE_ERROR, META_VERSION_ERROR);
  190. res = false;
  191. break;
  192. case SGX_ERROR_INVALID_ENCLAVE:
  193. se_trace(SE_TRACE_ERROR, INVALID_ENCLAVE_ERROR);
  194. res = false;
  195. break;
  196. case SGX_SUCCESS:
  197. ret = dynamic_cast<EnclaveCreatorST*>(get_enclave_creator())->get_enclave_info(hash, SGX_HASH_SIZE);
  198. if(ret != SGX_SUCCESS)
  199. {
  200. res = false;
  201. break;
  202. }
  203. res = true;
  204. break;
  205. default:
  206. res = false;
  207. break;
  208. }
  209. return res;
  210. }
  211. static void set_meta_attributes(metadata_t *meta)
  212. {
  213. assert(meta != NULL);
  214. //set metadata.attributes
  215. //low 64 bit: it's the same as enclave_css
  216. memset(&meta->attributes, 0, sizeof(sgx_attributes_t));
  217. meta->attributes.flags = meta->enclave_css.body.attributes.flags;
  218. //high 64 bit
  219. //set bits that will not be checked
  220. meta->attributes.xfrm = ~meta->enclave_css.body.attribute_mask.xfrm;
  221. //set bits that have been set '1' and need to be checked
  222. meta->attributes.xfrm |= (meta->enclave_css.body.attributes.xfrm & meta->enclave_css.body.attribute_mask.xfrm);
  223. return;
  224. }
  225. //fill_enclave_css()
  226. // file the enclave_css_t structure with the parameter, enclave_hash
  227. // If the RSA_key is not null, fill the key part
  228. // If RSA_key == NULL, fill the header and body(GENDATA mode)
  229. // If the path[UNSIGNED] != NULL, update the header.date(CATSIG mode)
  230. static bool fill_enclave_css(const IppsRSAPublicKeyState *pub_key, const xml_parameter_t *para, const uint8_t *enclave_hash,
  231. const char **path, enclave_css_t *css, bin_fmt_t bf)
  232. {
  233. assert(para != NULL && enclave_hash != NULL && path != NULL && css != NULL);
  234. enclave_css_t enclave_css;
  235. memset(&enclave_css, 0, sizeof(enclave_css_t));
  236. uint32_t date = 0;
  237. if(false == get_time(&date))
  238. return false;
  239. //*****fill the header*******************
  240. uint8_t header[12] = {6, 0, 0, 0, 0xE1, 0, 0, 0, 0, 0, 1, 0};
  241. uint8_t header2[16] = {1, 1, 0, 0, 0x60, 0, 0, 0, 0x60, 0, 0, 0, 1, 0, 0, 0};
  242. memcpy_s(&enclave_css.header.header, sizeof(enclave_css.header.header), &header, sizeof(header));
  243. memcpy_s(&enclave_css.header.header2, sizeof(enclave_css.header.header2), &header2, sizeof(header2));
  244. // For 'type', signing tool clears the bit 31 for product enclaves
  245. // and set the bit 31 for debug enclaves
  246. enclave_css.header.type = (para[RELEASETYPE].value & 0x01) ? (1<<31) : 0;
  247. enclave_css.header.module_vendor = (para[INTELSIGNED].value&0x01) ? 0x8086 : 0;
  248. enclave_css.header.date = date;
  249. //if pub_key is not NULL, fill the key part
  250. if(pub_key)
  251. {
  252. int exponent_size = 0;
  253. int modulus_size = 0;
  254. IppStatus error_code = get_pub_key(pub_key, &exponent_size,
  255. (Ipp32u *)&enclave_css.key.exponent,
  256. &modulus_size,
  257. (Ipp32u *)&enclave_css.key.modulus);
  258. if(error_code != ippStsNoErr)
  259. {
  260. return false;
  261. }
  262. exponent_size = (uint32_t)(ROUND_TO(exponent_size, sizeof(Ipp32u)) / sizeof(Ipp32u));
  263. modulus_size = (uint32_t)(ROUND_TO(modulus_size, sizeof(Ipp32u)) / sizeof(Ipp32u));
  264. assert(enclave_css.key.exponent[0] == 0x03);
  265. assert(exponent_size == 0x1);
  266. assert(modulus_size == 0x60);
  267. }
  268. //hardware version
  269. enclave_css.header.hw_version = (uint32_t)para[HW].value;
  270. //****************************fill the body***********************
  271. // Misc_select/Misc_mask
  272. enclave_css.body.misc_select = (uint32_t)para[MISCSELECT].value;
  273. enclave_css.body.misc_mask = (uint32_t)para[MISCMASK].value;
  274. //low 64 bit
  275. enclave_css.body.attributes.flags = 0;
  276. enclave_css.body.attribute_mask.flags = ~SGX_FLAGS_DEBUG;
  277. if(para[DISABLEDEBUG].value == 1)
  278. {
  279. enclave_css.body.attributes.flags &= ~SGX_FLAGS_DEBUG;
  280. enclave_css.body.attribute_mask.flags |= SGX_FLAGS_DEBUG;
  281. }
  282. if(para[PROVISIONKEY].value == 1)
  283. {
  284. enclave_css.body.attributes.flags |= SGX_FLAGS_PROVISION_KEY;
  285. enclave_css.body.attribute_mask.flags |= SGX_FLAGS_PROVISION_KEY;
  286. }
  287. if(para[LAUNCHKEY].value == 1)
  288. {
  289. enclave_css.body.attributes.flags |= SGX_FLAGS_LICENSE_KEY;
  290. enclave_css.body.attribute_mask.flags |= SGX_FLAGS_LICENSE_KEY;
  291. }
  292. if(bf == BF_PE64 || bf == BF_ELF64)
  293. {
  294. enclave_css.body.attributes.flags |= SGX_FLAGS_MODE64BIT;
  295. enclave_css.body.attribute_mask.flags |= SGX_FLAGS_MODE64BIT;
  296. }
  297. // high 64 bit
  298. //default setting
  299. enclave_css.body.attributes.xfrm = SGX_XFRM_LEGACY;
  300. enclave_css.body.attribute_mask.xfrm = SGX_XFRM_LEGACY | SGX_XFRM_RESERVED; // LEGACY and reserved bits would be checked.
  301. memcpy_s(&enclave_css.body.enclave_hash, sizeof(enclave_css.body.enclave_hash), enclave_hash, SGX_HASH_SIZE);
  302. enclave_css.body.isv_prod_id = (uint16_t)para[PRODID].value;
  303. enclave_css.body.isv_svn = (uint16_t)para[ISVSVN].value;
  304. //Copy the css to output css buffer
  305. memcpy_s(css, sizeof(enclave_css_t), &enclave_css, sizeof(enclave_css_t));
  306. if(path[UNSIGNED] != NULL)
  307. {
  308. // In catsig mode, update the header.date as the time when the unsigned file is generated.
  309. memset(&enclave_css, 0, sizeof(enclave_css));
  310. size_t fsize = get_file_size(path[UNSIGNED]);
  311. if(fsize != sizeof(enclave_css.header) + sizeof(enclave_css.body))
  312. {
  313. se_trace(SE_TRACE_ERROR, UNSIGNED_FILE_ERROR, path[UNSIGNED]);
  314. return false;
  315. }
  316. uint8_t *buf = new uint8_t[fsize];
  317. memset(buf, 0, fsize);
  318. if(read_file_to_buf(path[UNSIGNED], buf, fsize) == false)
  319. {
  320. se_trace(SE_TRACE_ERROR, READ_FILE_ERROR, path[UNSIGNED]);
  321. return false;
  322. }
  323. memcpy_s(&enclave_css.header, sizeof(enclave_css.header), buf, sizeof(enclave_css.header));
  324. memcpy_s(&enclave_css.body, sizeof(enclave_css.body), buf + sizeof(enclave_css.header), fsize - sizeof(enclave_css.header));
  325. delete [] buf;
  326. css->header.date = enclave_css.header.date;
  327. // Verify the header and body read from the unsigned file to make sure it's the same as that generated from xml file
  328. if(memcmp(&enclave_css.header, &css->header, sizeof(enclave_css.header)) || memcmp(&enclave_css.body, &css->body, sizeof(enclave_css.body)))
  329. {
  330. se_trace(SE_TRACE_ERROR, UNSIGNED_FILE_XML_MISMATCH);
  331. return false;
  332. }
  333. }
  334. return true;
  335. }
  336. static IppStatus calc_RSAq1q2(int length_s, const Ipp32u *data_s, int length_m, const Ipp32u *data_m,
  337. int *length_q1, Ipp32u *data_q1, int *length_q2, Ipp32u *data_q2)
  338. {
  339. IppStatus error_code = ippStsSAReservedErr1;
  340. IppsBigNumState *pM=0, *pS=0, *pQ1=0, *pQ2=0, *ptemp1=0, *ptemp2=0;
  341. IppsBigNumSGN sgn = IppsBigNumPOS;
  342. int length_in_bit = 0;
  343. Ipp32u *pdata = NULL;
  344. //create 6 big number
  345. if(!data_s || !data_m || !length_q1 || !data_q1 || !length_q2
  346. || !data_q2 || length_s <= 0 || length_m <= 0)
  347. {
  348. error_code = ippStsBadArgErr;
  349. goto clean_return;
  350. }
  351. error_code = newBN(data_s, length_s, &pS);
  352. if(error_code != ippStsNoErr)
  353. {
  354. goto clean_return;
  355. }
  356. error_code = newBN(data_m, length_m, &pM);
  357. if(error_code != ippStsNoErr)
  358. {
  359. goto clean_return;
  360. }
  361. error_code = newBN(0, length_m, &pQ1);
  362. if(error_code != ippStsNoErr)
  363. {
  364. goto clean_return;
  365. }
  366. error_code = newBN(0, length_m, &pQ2);
  367. if(error_code != ippStsNoErr)
  368. {
  369. goto clean_return;
  370. }
  371. error_code = newBN(0, length_m*2, &ptemp1);
  372. if(error_code != ippStsNoErr)
  373. {
  374. goto clean_return;
  375. }
  376. error_code = newBN(0, length_m, &ptemp2);
  377. if(error_code != ippStsNoErr)
  378. {
  379. goto clean_return;
  380. }
  381. //signed big number operation
  382. //multiplies pS and pS, ptemp1 is the multiplication result
  383. error_code = ippsMul_BN(pS, pS, ptemp1);
  384. if(error_code != ippStsNoErr)
  385. {
  386. goto clean_return;
  387. }
  388. //ptemp1: dividend, pM: divisor, pQ1: qutient, ptemp2: remainder
  389. error_code = ippsDiv_BN(ptemp1, pM, pQ1, ptemp2);
  390. if(error_code != ippStsNoErr)
  391. {
  392. goto clean_return;
  393. }
  394. error_code = ippsMul_BN(pS, ptemp2, ptemp1);
  395. if(error_code != ippStsNoErr)
  396. {
  397. goto clean_return;
  398. }
  399. error_code = ippsDiv_BN(ptemp1, pM, pQ2, ptemp2);
  400. if(error_code != ippStsNoErr)
  401. {
  402. goto clean_return;
  403. }
  404. //extract the sign and value of the integer big number from the input structure(pQ1)
  405. error_code = ippsRef_BN(&sgn, &length_in_bit, &pdata, pQ1);
  406. if(error_code != ippStsNoErr)
  407. {
  408. goto clean_return;
  409. }
  410. *length_q1 = ROUND_TO(length_in_bit, 8)/8;
  411. memset(data_q1, 0, *length_q1);
  412. memcpy_s(data_q1, *length_q1, pdata, *length_q1);
  413. error_code = ippsRef_BN(&sgn, &length_in_bit, &pdata, pQ2);
  414. if(error_code != ippStsNoErr)
  415. {
  416. goto clean_return;
  417. }
  418. *length_q2 = ROUND_TO(length_in_bit, 8)/8;
  419. memset(data_q2, 0, *length_q2);
  420. memcpy_s(data_q2, *length_q2, pdata, *length_q2);
  421. goto clean_return;
  422. clean_return:
  423. secure_free_BN(pM, length_m);
  424. secure_free_BN(pS, length_s);
  425. secure_free_BN(pQ1, length_m);
  426. secure_free_BN(pQ2, length_m);
  427. secure_free_BN(ptemp1, length_m*2);
  428. secure_free_BN(ptemp2, length_m);
  429. return error_code;
  430. }
  431. static bool create_signature(const IppsRSAPrivateKeyState *pri_key1, const char *sigpath, enclave_css_t *enclave_css)
  432. {
  433. IppStatus error_code = ippStsNoErr;
  434. assert(enclave_css != NULL);
  435. assert(!(pri_key1 == NULL && sigpath == NULL) && !(pri_key1 != NULL && sigpath != NULL));
  436. uint8_t signature[SIGNATURE_SIZE]; // keep the signature in big endian
  437. memset(signature, 0, SIGNATURE_SIZE);
  438. //**********get the signature*********
  439. if(sigpath != NULL)//CATSIG mode
  440. {
  441. if(get_file_size(sigpath) != SIGNATURE_SIZE)
  442. {
  443. se_trace(SE_TRACE_ERROR, SIG_FILE_ERROR, sigpath);
  444. return false;
  445. }
  446. if(read_file_to_buf(sigpath, signature, SIGNATURE_SIZE) == false)
  447. {
  448. se_trace(SE_TRACE_ERROR, READ_FILE_ERROR, sigpath);
  449. return false;
  450. }
  451. }
  452. else //SIGN mode
  453. {
  454. size_t buffer_size = sizeof(enclave_css->header) + sizeof(enclave_css->body);
  455. Ipp8u * temp_buffer = (Ipp8u *)malloc(buffer_size * sizeof(char));
  456. if(NULL == temp_buffer)
  457. {
  458. se_trace(SE_TRACE_ERROR, NO_MEMORY_ERROR);
  459. return false;
  460. }
  461. memcpy_s(temp_buffer, buffer_size, &enclave_css->header, sizeof(enclave_css->header));
  462. memcpy_s(temp_buffer + sizeof(enclave_css->header), buffer_size - sizeof(enclave_css->header),
  463. &enclave_css->body, sizeof(enclave_css->body));
  464. int pri1_size = 0;
  465. if(ippsRSA_GetBufferSizePrivateKey(&pri1_size, pri_key1) != ippStsNoErr)
  466. {
  467. free(temp_buffer);
  468. return false;
  469. }
  470. Ipp8u *scratch_buf = (Ipp8u *)malloc(pri1_size);
  471. if(NULL == scratch_buf)
  472. {
  473. se_trace(SE_TRACE_ERROR, NO_MEMORY_ERROR);
  474. free(temp_buffer);
  475. return false;
  476. }
  477. memset(scratch_buf, 0, pri1_size);
  478. error_code = ippsRSASign_PKCS1v15((const Ipp8u *)temp_buffer, (int)buffer_size, (Ipp8u *)signature, pri_key1, NULL, ippHashAlg_SHA256, scratch_buf);
  479. free(scratch_buf);
  480. free(temp_buffer);
  481. if(error_code != ippStsNoErr)
  482. {
  483. return false;
  484. }
  485. }
  486. for(int i = 0; i<SIGNATURE_SIZE; i++)
  487. {
  488. (enclave_css->key.signature)[i] = signature[SIGNATURE_SIZE-1-i];
  489. }
  490. //************************calculate q1 and q2*********************
  491. int length_q1 = 0, length_q2 = 0;
  492. error_code = calc_RSAq1q2(sizeof(enclave_css->key.signature),
  493. (Ipp32u *)&enclave_css->key.signature,
  494. sizeof(enclave_css->key.modulus),
  495. (Ipp32u *)&enclave_css->key.modulus,
  496. &length_q1,
  497. (Ipp32u *)&enclave_css->buffer.q1,
  498. &length_q2,
  499. (Ipp32u *)&enclave_css->buffer.q2);
  500. if(error_code != ippStsNoErr)
  501. {
  502. return false;
  503. }
  504. return true;
  505. }
  506. static bool verify_signature(const rsa_params_t *rsa, const enclave_css_t *enclave_css, int *signature_verified)
  507. {
  508. assert(rsa != NULL && enclave_css != NULL && signature_verified != NULL);
  509. IppsRSAPublicKeyState *pub_key = NULL;
  510. IppStatus error_code = create_rsa_pub_key(N_SIZE_IN_BYTES, E_SIZE_IN_BYTES, rsa->n, rsa->e, &pub_key);
  511. if(error_code != ippStsNoErr)
  512. {
  513. return false;
  514. }
  515. size_t buffer_size = sizeof(enclave_css->header) + sizeof(enclave_css->body);
  516. Ipp8u * temp_buffer = (Ipp8u *)malloc(buffer_size * sizeof(char));
  517. if(NULL == temp_buffer)
  518. {
  519. se_trace(SE_TRACE_ERROR, NO_MEMORY_ERROR);
  520. secure_free_rsa_pub_key(N_SIZE_IN_BYTES, E_SIZE_IN_BYTES, pub_key);
  521. return false;
  522. }
  523. memcpy_s(temp_buffer, buffer_size, &enclave_css->header, sizeof(enclave_css->header));
  524. memcpy_s(temp_buffer + sizeof(enclave_css->header), buffer_size-sizeof(enclave_css->header),
  525. &enclave_css->body, sizeof(enclave_css->body));
  526. uint8_t signature[SIGNATURE_SIZE];
  527. for(int i=0; i<SIGNATURE_SIZE; i++)
  528. {
  529. signature[i] = enclave_css->key.signature[SIGNATURE_SIZE-1-i];
  530. }
  531. int pub_size = 0;
  532. if(ippsRSA_GetBufferSizePublicKey(&pub_size, pub_key) != ippStsNoErr)
  533. {
  534. secure_free_rsa_pub_key(N_SIZE_IN_BYTES, E_SIZE_IN_BYTES, pub_key);
  535. free(temp_buffer);
  536. return false;
  537. }
  538. Ipp8u *scratch_buf = (Ipp8u *)malloc(pub_size);
  539. if(NULL == scratch_buf)
  540. {
  541. se_trace(SE_TRACE_ERROR, NO_MEMORY_ERROR);
  542. secure_free_rsa_pub_key(N_SIZE_IN_BYTES, E_SIZE_IN_BYTES, pub_key);
  543. free(temp_buffer);
  544. return false;
  545. }
  546. memset(scratch_buf, 0, pub_size);
  547. error_code = ippsRSAVerify_PKCS1v15((const Ipp8u *)temp_buffer, (int)buffer_size, (Ipp8u *)signature, signature_verified, pub_key, ippHashAlg_SHA256, scratch_buf);
  548. free(temp_buffer);
  549. free(scratch_buf);
  550. secure_free_rsa_pub_key(N_SIZE_IN_BYTES, E_SIZE_IN_BYTES, pub_key);
  551. if(error_code != ippStsNoErr)
  552. {
  553. se_trace(SE_TRACE_DEBUG, "ippsRSASSAVerify_SHA256_PKCSv15() returns failure. The ipperrorCode is %d \n", error_code);
  554. return false;
  555. }
  556. else
  557. {
  558. se_trace(SE_TRACE_DEBUG, "RSAVerify() returns success. The signature_verified is %d\n", *signature_verified);
  559. return true;
  560. }
  561. }
  562. static bool gen_enclave_signing_file(const enclave_css_t *enclave_css, const char *outpath)
  563. {
  564. assert(enclave_css != NULL);
  565. size_t size = sizeof(enclave_css->header) + sizeof(enclave_css->body);
  566. uint8_t *buffer = (uint8_t *)malloc(size);
  567. if(buffer == NULL)
  568. {
  569. se_trace(SE_TRACE_ERROR, NO_MEMORY_ERROR);
  570. return false;
  571. }
  572. memcpy_s(buffer, sizeof(enclave_css->header), &enclave_css->header, sizeof(enclave_css->header));
  573. memcpy_s(buffer + sizeof(enclave_css->header), sizeof(enclave_css->body), &enclave_css->body, sizeof(enclave_css->body));
  574. if(write_data_to_file(outpath, std::ios::out|std::ios::binary, buffer, size) == false)
  575. {
  576. free(buffer);
  577. return false;
  578. }
  579. free(buffer);
  580. return true;
  581. }
  582. static bool cmdline_parse(unsigned int argc, char *argv[], int *mode, const char **path)
  583. {
  584. assert(mode!=NULL && path != NULL);
  585. if(argc<2)
  586. {
  587. se_trace(SE_TRACE_ERROR, LACK_PARA_ERROR);
  588. return false;
  589. }
  590. if(argc == 2 && !STRCMP(argv[1], "-help"))
  591. {
  592. se_trace(SE_TRACE_ERROR, USAGE_STRING);
  593. *mode = -1;
  594. return true;
  595. }
  596. enum { PAR_REQUIRED, PAR_OPTIONAL, PAR_INVALID };
  597. typedef struct _param_struct_{
  598. const char *name; //options
  599. char *value; //keep the path
  600. int flag; //indicate this parameter is required(0), optional(1) or invalid(2)
  601. }param_struct_t; //keep the parameter pairs
  602. param_struct_t params_sign[] = {
  603. {"-enclave", NULL, PAR_REQUIRED},
  604. {"-config", NULL, PAR_OPTIONAL},
  605. {"-key", NULL, PAR_REQUIRED},
  606. {"-out", NULL, PAR_REQUIRED},
  607. {"-sig", NULL, PAR_INVALID},
  608. {"-unsigned", NULL, PAR_INVALID},
  609. {"-review_enclave", NULL, PAR_INVALID}};
  610. param_struct_t params_gendata[] = {
  611. {"-enclave", NULL, PAR_REQUIRED},
  612. {"-config", NULL, PAR_OPTIONAL},
  613. {"-key", NULL, PAR_INVALID},
  614. {"-out", NULL, PAR_REQUIRED},
  615. {"-sig", NULL, PAR_INVALID},
  616. {"-unsigned", NULL, PAR_INVALID},
  617. {"-review_enclave", NULL, PAR_INVALID}};
  618. param_struct_t params_catsig[] = {
  619. {"-enclave", NULL, PAR_REQUIRED},
  620. {"-config", NULL, PAR_OPTIONAL},
  621. {"-key", NULL, PAR_REQUIRED},
  622. {"-out", NULL, PAR_REQUIRED},
  623. {"-sig", NULL, PAR_REQUIRED},
  624. {"-unsigned", NULL, PAR_REQUIRED},
  625. {"-review_enclave", NULL, PAR_INVALID}};
  626. param_struct_t params_compare[] = {
  627. {"-enclave", NULL, PAR_REQUIRED},
  628. {"-config", NULL, PAR_OPTIONAL},
  629. {"-key", NULL, PAR_INVALID},
  630. {"-out", NULL, PAR_INVALID},
  631. {"-sig", NULL, PAR_INVALID},
  632. {"-unsigned", NULL, PAR_REQUIRED},
  633. {"-review_enclave", NULL, PAR_REQUIRED}};
  634. const char *mode_m[] ={"sign", "gendata","catsig", "compare"};
  635. param_struct_t *params[] = {params_sign, params_gendata, params_catsig, params_compare};
  636. unsigned int tempidx=0;
  637. for(; tempidx<sizeof(mode_m)/sizeof(mode_m[0]); tempidx++)
  638. {
  639. if(!STRCMP(mode_m[tempidx], argv[1]))//match
  640. {
  641. break;
  642. }
  643. }
  644. unsigned int tempmode = tempidx;
  645. if(tempmode>=sizeof(mode_m)/sizeof(mode_m[0]))
  646. {
  647. se_trace(SE_TRACE_ERROR, UNREC_CMD_ERROR, argv[1]);
  648. return false;
  649. }
  650. unsigned int params_count = (unsigned)(sizeof(params_sign)/sizeof(params_sign[0]));
  651. unsigned int params_count_min = 0;
  652. unsigned int params_count_max =0;
  653. for(unsigned int i=0; i< params_count; i++)
  654. {
  655. params_count_max ++;
  656. if(params[tempmode][i].flag == PAR_REQUIRED)
  657. params_count_min ++;
  658. }
  659. unsigned int additional_param = 2;
  660. if(argc<params_count_min * 2 + additional_param)
  661. return false;
  662. if(argc>params_count_max * 2 + additional_param)
  663. return false;
  664. for(unsigned int i=2; i<argc; i=i+2)
  665. {
  666. unsigned int j=0;
  667. for(; j<params_count; j++)
  668. {
  669. if(STRCMP(argv[i], params[tempmode][j].name)==0) //match
  670. {
  671. if((i<argc-1)&&(STRNCMP(argv[i+1],"-", 1))) // assuming pathname doesn't contain "-"
  672. {
  673. if(params[tempmode][j].value != NULL)
  674. {
  675. se_trace(SE_TRACE_ERROR, REPEAT_OPTION_ERROR, params[tempmode][j].name);
  676. return false;
  677. }
  678. params[tempmode][j].value = argv[i+1];
  679. break;
  680. }
  681. else //didn't match: 1) no path parameter behind option parameter 2) parameters format error.
  682. {
  683. se_trace(SE_TRACE_ERROR, INVALID_FILE_NAME_ERROR, params[tempmode][j].name);
  684. return false;
  685. }
  686. }
  687. }
  688. if(j>=params_count_max)
  689. {
  690. return false;
  691. }
  692. }
  693. for(unsigned int i = 0; i < params_count; i ++)
  694. {
  695. if(params[tempmode][i].flag == PAR_REQUIRED && params[tempmode][i].value == NULL)
  696. {
  697. se_trace(SE_TRACE_ERROR, LACK_REQUIRED_OPTION_ERROR, params[tempmode][i].name, mode_m[tempmode]);
  698. return false;
  699. }
  700. if(params[tempmode][i].flag == PAR_INVALID && params[tempmode][i].value != NULL)
  701. {
  702. se_trace(SE_TRACE_ERROR, GIVE_INVALID_OPTION_ERROR, params[tempmode][i].name, mode_m[tempmode]);
  703. return false;
  704. }
  705. }
  706. for(unsigned int i = 0; i < params_count; i++)
  707. {
  708. path[i] = params[tempmode][i].value;
  709. }
  710. *mode = tempmode;
  711. return true;
  712. }
  713. static bool fill_meta_without_signature(const IppsRSAPublicKeyState *pub_key, const char **path, const uint8_t *enclave_hash,
  714. const xml_parameter_t *para, metadata_t *metadata, bin_fmt_t bf)
  715. {
  716. assert(path && enclave_hash && para && metadata);
  717. if(false == fill_enclave_css(pub_key, para, enclave_hash, path, &metadata->enclave_css, bf))
  718. {
  719. return false;
  720. }
  721. set_meta_attributes(metadata);
  722. return true;
  723. }
  724. //generate_output:
  725. // To generate the final output file
  726. // SIGN- need to fill the enclave_css_t(key part included), sign the header and body and
  727. // update the metadata in the out file
  728. // GENDATA- need to fill the enclave_css_t(key part excluded), get the body and header,
  729. // and then write the whole out file with body+header+hash
  730. // CATSIG- need to fill the enclave_css_t(include key), read the signature from the sigpath,
  731. // and then update the metadata in the out file
  732. static bool generate_output(int mode, int ktype, const uint8_t *enclave_hash, const xml_parameter_t *para, const rsa_params_t *rsa, metadata_t *metadata,
  733. const char **path, bin_fmt_t bf, uint64_t meta_offset)
  734. {
  735. assert(enclave_hash != NULL && para != NULL && metadata != NULL && path != NULL && rsa != NULL);
  736. IppsRSAPrivateKeyState *pri_key1 = NULL;
  737. IppsRSAPublicKeyState *pub_key = NULL;
  738. int validate_result = IS_INVALID_KEY;
  739. IppStatus error_code = ippStsNoErr;
  740. switch(mode)
  741. {
  742. case SIGN:
  743. {
  744. if(ktype != PRIVATE_KEY)
  745. {
  746. se_trace(SE_TRACE_ERROR, LACK_PRI_KEY_ERROR);
  747. return false;
  748. }
  749. error_code = create_validate_rsa_key_pair(N_SIZE_IN_BYTES, E_SIZE_IN_BYTES, rsa->n, rsa->d, rsa->e,
  750. rsa->p, rsa->q, rsa->dmp1, rsa->dmq1, rsa->iqmp, &pri_key1, &pub_key, &validate_result);
  751. if(error_code != ippStsNoErr || validate_result != IS_VALID_KEY)
  752. {
  753. se_trace(SE_TRACE_ERROR, KEY_FORMAT_ERROR);
  754. secure_free_rsa_pri1_key(N_SIZE_IN_BYTES, D_SIZE_IN_BYTES, pri_key1);
  755. secure_free_rsa_pub_key(N_SIZE_IN_BYTES, E_SIZE_IN_BYTES, pub_key);
  756. return false;
  757. }
  758. if(false == fill_meta_without_signature(pub_key, path, enclave_hash, para, metadata, bf))
  759. {
  760. secure_free_rsa_pri1_key(N_SIZE_IN_BYTES, D_SIZE_IN_BYTES, pri_key1);
  761. secure_free_rsa_pub_key(N_SIZE_IN_BYTES, E_SIZE_IN_BYTES, pub_key);
  762. return false;
  763. }
  764. if(false == create_signature(pri_key1, NULL, &(metadata->enclave_css)))
  765. {
  766. secure_free_rsa_pri1_key(N_SIZE_IN_BYTES, D_SIZE_IN_BYTES, pri_key1);
  767. secure_free_rsa_pub_key(N_SIZE_IN_BYTES, E_SIZE_IN_BYTES, pub_key);
  768. return false;
  769. }
  770. secure_free_rsa_pri1_key(N_SIZE_IN_BYTES, D_SIZE_IN_BYTES, pri_key1);
  771. secure_free_rsa_pub_key(N_SIZE_IN_BYTES, E_SIZE_IN_BYTES, pub_key);
  772. if(false == update_metadata(path[OUTPUT], metadata, meta_offset))
  773. {
  774. return false;
  775. }
  776. break;
  777. }
  778. case GENDATA:
  779. {
  780. if(false == fill_meta_without_signature(NULL, path, enclave_hash, para, metadata, bf))
  781. {
  782. return false;
  783. }
  784. if(false == gen_enclave_signing_file(&(metadata->enclave_css), path[OUTPUT]))
  785. {
  786. return false;
  787. }
  788. break;
  789. }
  790. case CATSIG:
  791. {
  792. if(ktype != PUBLIC_KEY)
  793. {
  794. se_trace(SE_TRACE_ERROR, LACK_PUB_KEY_ERROR);
  795. return false;
  796. }
  797. if(create_rsa_pub_key(N_SIZE_IN_BYTES, E_SIZE_IN_BYTES, rsa->n, rsa->e, &pub_key) != ippStsNoErr)
  798. {
  799. se_trace(SE_TRACE_ERROR, KEY_FORMAT_ERROR);
  800. return false;
  801. }
  802. if(false == fill_meta_without_signature(pub_key, path, enclave_hash, para, metadata, bf))
  803. {
  804. secure_free_rsa_pub_key(N_SIZE_IN_BYTES, E_SIZE_IN_BYTES, pub_key);
  805. return false;
  806. }
  807. secure_free_rsa_pub_key(N_SIZE_IN_BYTES, E_SIZE_IN_BYTES, pub_key);
  808. if(false == create_signature(NULL, path[SIG], &(metadata->enclave_css)))
  809. {
  810. return false;
  811. }
  812. if(false == update_metadata(path[OUTPUT], metadata, meta_offset))
  813. {
  814. return false;
  815. }
  816. break;
  817. }
  818. default:
  819. {
  820. return false;
  821. }
  822. }
  823. return true;
  824. }
  825. //compare two enclaves
  826. static bool compare_enclave(const char **path, const xml_parameter_t *para)
  827. {
  828. assert(path != NULL && para != NULL);
  829. bool res = false;
  830. int ret = SGX_SUCCESS;
  831. sgx_status_t status1 = SGX_SUCCESS, status2 = SGX_SUCCESS;
  832. uint32_t file_size1 =0 , file_size2 = 0;
  833. size_t file_size = 0;
  834. bin_fmt_t bin_fmt1 = BF_UNKNOWN, bin_fmt2 = BF_UNKNOWN;
  835. uint8_t enclave_hash[SGX_HASH_SIZE] = {0};
  836. uint8_t *buf = NULL;
  837. CMetadata *meta = NULL;
  838. metadata_t metadata;
  839. enclave_diff_info_t enclave_diff_info1, enclave_diff_info2;
  840. enclave_css_t enclave_css;
  841. memset(&enclave_css, 0, sizeof(enclave_css_t));
  842. memset(&enclave_diff_info1, 0, sizeof(enclave_diff_info_t));
  843. memset(&enclave_diff_info2, 0, sizeof(enclave_diff_info_t));
  844. memset(&metadata, 0, sizeof(metadata_t));
  845. se_file_handle_t fh1 = open_file(path[DLL]);
  846. if (fh1 == THE_INVALID_HANDLE)
  847. {
  848. se_trace(SE_TRACE_ERROR, OPEN_FILE_ERROR, path[DLL]);
  849. return false;
  850. }
  851. se_file_handle_t fh2 = open_file(path[REVIEW_ENCLAVE]);
  852. if (fh2 == THE_INVALID_HANDLE)
  853. {
  854. se_trace(SE_TRACE_ERROR, OPEN_FILE_ERROR, path[REVIEW_ENCLAVE]);
  855. close_handle(fh1);
  856. return false;
  857. }
  858. std::unique_ptr<map_handle_t, void (*)(map_handle_t*)> mh1(map_file(fh1, &file_size1), unmap_file);
  859. if (!mh1)
  860. {
  861. close_handle(fh1);
  862. close_handle(fh2);
  863. return false;
  864. }
  865. std::unique_ptr<map_handle_t, void (*)(map_handle_t*)> mh2(map_file(fh2, &file_size2), unmap_file);
  866. if (!mh2)
  867. {
  868. close_handle(fh1);
  869. close_handle(fh2);
  870. return false;
  871. }
  872. //check if file_size is the same
  873. if(file_size1 != file_size2)
  874. {
  875. close_handle(fh1);
  876. close_handle(fh2);
  877. return false;
  878. }
  879. // Parse enclave
  880. std::unique_ptr<BinParser> parser1(binparser::get_parser(mh1->base_addr, (size_t)file_size1));
  881. assert(parser1 != NULL);
  882. std::unique_ptr<BinParser> parser2(binparser::get_parser(mh2->base_addr, (size_t)file_size2));
  883. assert(parser2 != NULL);
  884. status1 = parser1->run_parser();
  885. if (status1 != SGX_SUCCESS)
  886. {
  887. goto clear_return;
  888. }
  889. status2 = parser2->run_parser();
  890. if (status2 != SGX_SUCCESS)
  891. {
  892. goto clear_return;
  893. }
  894. // Collect enclave info
  895. bin_fmt1 = parser1->get_bin_format();
  896. bin_fmt2 = parser2->get_bin_format();
  897. //two enclave should have same format
  898. if(bin_fmt1 != bin_fmt2)
  899. {
  900. goto clear_return;
  901. }
  902. //modify some info of enclave: timestamp etc.
  903. status1 = parser1->get_info(&enclave_diff_info1);
  904. if(status1 != SGX_SUCCESS)
  905. {
  906. goto clear_return;
  907. }
  908. status2 = parser2->get_info(&enclave_diff_info2);
  909. if(status2 != SGX_SUCCESS)
  910. {
  911. goto clear_return;
  912. }
  913. status2 = parser2->modify_info(&enclave_diff_info1);
  914. if(status2 != SGX_SUCCESS)
  915. {
  916. goto clear_return;
  917. }
  918. //get enclave hash from unsigned file
  919. file_size = get_file_size(path[UNSIGNED]);
  920. if (file_size != sizeof(enclave_css.header) + sizeof(enclave_css.body) &&
  921. file_size != sizeof(enclave_css.header) + sizeof(enclave_css.body) + sizeof(enclave_css.key))
  922. {
  923. goto clear_return;
  924. }
  925. buf = (uint8_t *)malloc(file_size);
  926. if (buf == NULL)
  927. {
  928. goto clear_return;
  929. }
  930. memset(buf, 0, file_size);
  931. if(read_file_to_buf(path[UNSIGNED], buf, file_size) == false)
  932. {
  933. free(buf);
  934. goto clear_return;
  935. }
  936. memcpy_s(&enclave_css.header, sizeof(enclave_css.header), buf, sizeof(enclave_css.header));
  937. memcpy_s(&enclave_css.body, sizeof(enclave_css.body), buf + (file_size - sizeof(enclave_css.body)), sizeof(enclave_css.body));
  938. free(buf);
  939. // Load enclave to get enclave hash
  940. meta = new CMetadata(&metadata, parser2.get());
  941. if(meta->build_metadata(para) == false)
  942. {
  943. delete meta;
  944. goto clear_return;
  945. }
  946. delete meta;
  947. ret = load_enclave(parser2.release(), &metadata);
  948. if(ret != SGX_SUCCESS)
  949. {
  950. goto clear_return;
  951. }
  952. ret = dynamic_cast<EnclaveCreatorST*>(get_enclave_creator())->get_enclave_info(enclave_hash, SGX_HASH_SIZE);
  953. if(ret != SGX_SUCCESS)
  954. {
  955. goto clear_return;
  956. }
  957. //make path[UNSIGNED] = NULL, so fill_meta_without_signature won't treat it as catsig
  958. path[UNSIGNED] = NULL;
  959. if(false == fill_meta_without_signature(NULL, path, enclave_hash, para, &metadata, bin_fmt2))
  960. {
  961. goto clear_return;
  962. }
  963. //compare
  964. metadata.enclave_css.header.date = 0;
  965. enclave_css.header.date = 0;
  966. if(memcmp(&metadata.enclave_css.header, &enclave_css.header, sizeof(enclave_css.header)) != 0)
  967. {
  968. goto clear_return;
  969. }
  970. if(memcmp(&metadata.enclave_css.body, &enclave_css.body, sizeof(enclave_css.body)) != 0)
  971. {
  972. goto clear_return;
  973. }
  974. res = true;
  975. clear_return:
  976. close_handle(fh1);
  977. close_handle(fh2);
  978. return res;
  979. }
  980. int main(int argc, char* argv[])
  981. {
  982. xml_parameter_t parameter[] = {{"ProdID", 0xFFFF, 0, 0, 0},
  983. {"ISVSVN", 0xFFFF, 0, 0, 0},
  984. {"ReleaseType", 1, 0, 0, 0},
  985. {"IntelSigned", 1, 0, 0, 0},
  986. {"ProvisionKey",1,0,0,0},
  987. {"LaunchKey",1,0,0,0},
  988. {"DisableDebug",1,0,0,0},
  989. {"HW", 0x10,0,0,0},
  990. {"TCSNum",0xFFFFFFFF,TCS_NUM_MIN,1,0},
  991. {"TCSPolicy",TCS_POLICY_UNBIND,TCS_POLICY_BIND,TCS_POLICY_UNBIND,0},
  992. {"StackMaxSize",0x1FFFFFFFFF,STACK_SIZE_MIN,0x40000,0},
  993. {"HeapMaxSize",0x1FFFFFFFFF,HEAP_SIZE_MIN,0x100000,0},
  994. {"MiscSelect", 0xFFFFFFFF, 0, DEFAULT_MISC_SELECT, 0},
  995. {"MiscMask", 0xFFFFFFFF, 0, DEFAULT_MISC_MASK, 0}};
  996. const char *path[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
  997. uint8_t enclave_hash[SGX_HASH_SIZE] = {0};
  998. metadata_t metadata;
  999. int res = -1, mode = -1;
  1000. int key_type = UNIDENTIFIABLE_KEY; //indicate the type of the input key file
  1001. size_t parameter_count = sizeof(parameter)/sizeof(parameter[0]);
  1002. bin_fmt_t bin_fmt = BF_UNKNOWN;
  1003. uint64_t meta_offset = 0;
  1004. rsa_params_t rsa;
  1005. memset(&rsa, 0, sizeof(rsa));
  1006. memset(&metadata, 0, sizeof(metadata));
  1007. //Parse command line
  1008. if(cmdline_parse(argc, argv, &mode, path) == false)
  1009. {
  1010. se_trace(SE_TRACE_ERROR, USAGE_STRING);
  1011. goto clear_return;
  1012. }
  1013. if(mode == -1) // User only wants to get the help info
  1014. {
  1015. return 0;
  1016. }
  1017. //Parse the xml file to get the metadata
  1018. if(parse_metadata_file(path[XML], parameter, (int)parameter_count) == false)
  1019. {
  1020. goto clear_return;
  1021. }
  1022. //Parse the key file
  1023. if(parse_key_file(path[KEY], &rsa, &key_type) == false && key_type != NO_KEY)
  1024. {
  1025. goto clear_return;
  1026. }
  1027. //compare two enclave
  1028. if(mode == COMPARE)
  1029. {
  1030. if(compare_enclave(path, parameter) == false)
  1031. {
  1032. se_trace(SE_TRACE_ERROR, "The two enclaves are not matched\n");
  1033. return -1;
  1034. }
  1035. se_trace(SE_TRACE_ERROR, "The two enclaves are matched\n");
  1036. return 0;
  1037. }
  1038. if(copy_file(path[DLL], path[OUTPUT]) == false)
  1039. {
  1040. se_trace(SE_TRACE_ERROR, OVERALL_ERROR);
  1041. goto clear_return;
  1042. }
  1043. if(measure_enclave(enclave_hash, path[OUTPUT], parameter, &metadata, &bin_fmt, &meta_offset) == false)
  1044. {
  1045. se_trace(SE_TRACE_ERROR, OVERALL_ERROR);
  1046. goto clear_return;
  1047. }
  1048. if((generate_output(mode, key_type, enclave_hash, parameter, &rsa, &metadata, path, bin_fmt, meta_offset)) == false)
  1049. {
  1050. se_trace(SE_TRACE_ERROR, OVERALL_ERROR);
  1051. goto clear_return;
  1052. }
  1053. //to verify
  1054. if(mode == SIGN || mode == CATSIG)
  1055. {
  1056. int signature_verified = ippFalse;
  1057. if(verify_signature(&rsa, &(metadata.enclave_css), &signature_verified) == false || signature_verified != ippTrue)
  1058. {
  1059. se_trace(SE_TRACE_ERROR, OVERALL_ERROR);
  1060. goto clear_return;
  1061. }
  1062. }
  1063. se_trace(SE_TRACE_ERROR, SUCCESS_EXIT);
  1064. res = 0;
  1065. clear_return:
  1066. if(res == -1 && path[OUTPUT])
  1067. remove(path[OUTPUT]);
  1068. return res;
  1069. }