sign_tool.cpp 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  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. // 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 <openssl/bio.h>
  41. #include <openssl/bn.h>
  42. #include <openssl/sha.h>
  43. #include <openssl/rsa.h>
  44. #include <openssl/evp.h>
  45. #include <openssl/err.h>
  46. #include <openssl/crypto.h>
  47. #include "metadata.h"
  48. #include "manage_metadata.h"
  49. #include "parse_key_file.h"
  50. #include "enclave_creator_sign.h"
  51. #include "util_st.h"
  52. #include "se_trace.h"
  53. #include "sgx_error.h"
  54. #include "se_version.h"
  55. #include "se_map.h"
  56. #include "loader.h"
  57. #include "parserfactory.h"
  58. #include "elf_helper.h"
  59. #include "crypto_wrapper.h"
  60. #include <unistd.h>
  61. #include <stdio.h>
  62. #include <stdlib.h>
  63. #include <assert.h>
  64. #include <string>
  65. #include <memory>
  66. #include <sstream>
  67. #define SIGNATURE_SIZE 384
  68. #define REL_ERROR_BIT 0x1
  69. #define INIT_SEC_ERROR_BIT 0x2
  70. #define IGNORE_REL_ERROR(x) (((x) & REL_ERROR_BIT) != 0)
  71. #define IGNORE_INIT_SEC_ERROR(x) (((x) & INIT_SEC_ERROR_BIT) != 0)
  72. typedef enum _file_path_t
  73. {
  74. DLL = 0,
  75. XML = 1,
  76. KEY,
  77. OUTPUT,
  78. SIG,
  79. UNSIGNED,
  80. DUMPFILE,
  81. CSSFILE
  82. } file_path_t;
  83. static int load_enclave(BinParser *parser, metadata_t *metadata)
  84. {
  85. std::unique_ptr<CLoader> ploader(new CLoader(const_cast<uint8_t *>(parser->get_start_addr()), *parser));
  86. return ploader->load_enclave_ex(NULL, 0, metadata, NULL);
  87. }
  88. #define THE_INVALID_HANDLE (-1)
  89. static int open_file(const char* dllpath)
  90. {
  91. FILE *fp = fopen(dllpath, "rb");
  92. if (fp == NULL)
  93. return THE_INVALID_HANDLE;
  94. return fileno(fp);
  95. }
  96. static void close_handle(int fd)
  97. {
  98. close(fd);
  99. }
  100. static bool get_enclave_info(BinParser *parser, bin_fmt_t *bf, uint64_t * meta_offset, bool is_dump_mode = false)
  101. {
  102. uint64_t meta_rva = parser->get_metadata_offset();
  103. const uint8_t *base_addr = parser->get_start_addr();
  104. metadata_t *metadata = GET_PTR(metadata_t, base_addr, meta_rva);
  105. if(metadata->magic_num == METADATA_MAGIC && is_dump_mode == false)
  106. {
  107. se_trace(SE_TRACE_ERROR, ENCLAVE_ALREADY_SIGNED_ERROR);
  108. return false;
  109. }
  110. *bf = parser->get_bin_format();
  111. *meta_offset = meta_rva;
  112. return true;
  113. }
  114. // measure_enclave():
  115. // 1. Get the enclave hash by loading enclave
  116. // 2. Get the enclave info - metadata offset and enclave file format
  117. static bool measure_enclave(uint8_t *hash, const char *dllpath, const xml_parameter_t *parameter, uint32_t ignore_error_bits, metadata_t *metadata, uint64_t *meta_offset)
  118. {
  119. assert(hash && dllpath && metadata && meta_offset);
  120. bool res = false;
  121. uint32_t file_size = 0;
  122. uint64_t quota = 0;
  123. bin_fmt_t bin_fmt = BF_UNKNOWN;
  124. se_file_handle_t fh = open_file(dllpath);
  125. if (fh == THE_INVALID_HANDLE)
  126. {
  127. se_trace(SE_TRACE_ERROR, OPEN_FILE_ERROR, dllpath);
  128. return false;
  129. }
  130. // Probably we can use `decltype' if all major supported compilers support that.
  131. std::unique_ptr<map_handle_t, void (*)(map_handle_t*)> mh(map_file(fh, &file_size), unmap_file);
  132. if (!mh)
  133. {
  134. close_handle(fh);
  135. return false;
  136. }
  137. // Parse enclave
  138. std::unique_ptr<BinParser> parser(binparser::get_parser(mh->base_addr, (size_t)file_size));
  139. assert(parser != NULL);
  140. sgx_status_t status = parser->run_parser();
  141. if (status != SGX_SUCCESS)
  142. {
  143. se_trace(SE_TRACE_ERROR, INVALID_ENCLAVE_ERROR);
  144. close_handle(fh);
  145. return false;
  146. }
  147. if(parser->has_init_section() && IGNORE_INIT_SEC_ERROR(ignore_error_bits) == false)
  148. {
  149. se_trace(SE_TRACE_ERROR, INIT_SEC_ERROR);
  150. close_handle(fh);
  151. return false;
  152. }
  153. // generate metadata
  154. CMetadata meta(metadata, parser.get());
  155. if(meta.build_metadata(parameter) == false)
  156. {
  157. close_handle(fh);
  158. return false;
  159. }
  160. // Collect enclave info
  161. if(get_enclave_info(parser.get(), &bin_fmt, meta_offset) == false)
  162. {
  163. close_handle(fh);
  164. return false;
  165. }
  166. bool no_rel = false;
  167. if (bin_fmt == BF_ELF64)
  168. {
  169. no_rel = ElfHelper<64>::dump_textrels(parser.get());
  170. }
  171. else
  172. {
  173. no_rel = ElfHelper<32>::dump_textrels(parser.get());
  174. }
  175. if(no_rel == false && (IGNORE_REL_ERROR(ignore_error_bits) == false))
  176. {
  177. close_handle(fh);
  178. se_trace(SE_TRACE_ERROR, TEXT_REL_ERROR);
  179. return false;
  180. }
  181. // Load enclave to get enclave hash
  182. int ret = load_enclave(parser.release(), metadata);
  183. close_handle(fh);
  184. switch(ret)
  185. {
  186. case SGX_ERROR_INVALID_METADATA:
  187. se_trace(SE_TRACE_ERROR, OUT_OF_EPC_ERROR);
  188. res = false;
  189. break;
  190. case SGX_ERROR_INVALID_VERSION:
  191. se_trace(SE_TRACE_ERROR, META_VERSION_ERROR);
  192. res = false;
  193. break;
  194. case SGX_ERROR_INVALID_ENCLAVE:
  195. se_trace(SE_TRACE_ERROR, INVALID_ENCLAVE_ERROR);
  196. res = false;
  197. break;
  198. case SGX_SUCCESS:
  199. ret = dynamic_cast<EnclaveCreatorST*>(get_enclave_creator())->get_enclave_info(hash, SGX_HASH_SIZE, &quota);
  200. if(ret != SGX_SUCCESS)
  201. {
  202. res = false;
  203. break;
  204. }
  205. se_trace(SE_TRACE_ERROR, REQUIRED_ENCLAVE_SIZE, quota);
  206. res = true;
  207. break;
  208. default:
  209. res = false;
  210. break;
  211. }
  212. return res;
  213. }
  214. //fill_enclave_css()
  215. // fill the enclave_css_t structure with enclave_hash
  216. // If the 'rsa' is not null, fill the key part
  217. // If the path[UNSIGNED] != NULL, update the header.date(CATSIG mode)
  218. static bool fill_enclave_css(const RSA *rsa, const char **path,
  219. const uint8_t *enclave_hash, enclave_css_t *css)
  220. {
  221. assert(enclave_hash != NULL && path != NULL && css != NULL);
  222. //if rsa is not NULL, fill the public key part
  223. if(rsa)
  224. {
  225. const BIGNUM *e = NULL, *n = NULL;
  226. RSA_get0_key(rsa, &n, &e, NULL);
  227. int exponent_size = BN_num_bytes(e);
  228. int modulus_size = BN_num_bytes(n);
  229. if(modulus_size > SE_KEY_SIZE)
  230. return false;
  231. unsigned char *modulus = (unsigned char *)malloc(SE_KEY_SIZE);
  232. if(modulus == NULL)
  233. {
  234. return false;
  235. }
  236. memset(modulus, 0, SE_KEY_SIZE);
  237. exponent_size = (uint32_t)(ROUND_TO(exponent_size, sizeof(uint32_t)) / sizeof(uint32_t));
  238. modulus_size = (uint32_t)(ROUND_TO(modulus_size, sizeof(uint32_t)) / sizeof(uint32_t));
  239. if(BN_bn2bin(n, modulus) != SE_KEY_SIZE)
  240. {
  241. free(modulus);
  242. return false;
  243. }
  244. if(BN_bn2bin(e, (unsigned char *)&css->key.exponent) != 1)
  245. {
  246. free(modulus);
  247. return false;
  248. }
  249. for(unsigned int i = 0; i < SE_KEY_SIZE; i++)
  250. {
  251. css->key.modulus[i] = modulus[SE_KEY_SIZE -i - 1];
  252. }
  253. free(modulus);
  254. assert(css->key.exponent[0] == 0x03);
  255. assert(exponent_size == 0x1);
  256. assert(modulus_size == 0x60);
  257. }
  258. // fill the enclave hash
  259. memcpy_s(&css->body.enclave_hash, sizeof(css->body.enclave_hash), enclave_hash, SGX_HASH_SIZE);
  260. if(path[UNSIGNED] != NULL)
  261. {
  262. // In catsig mode, update the header.date as the time when the unsigned file is generated.
  263. enclave_css_t enclave_css;
  264. memset(&enclave_css, 0, sizeof(enclave_css));
  265. size_t fsize = get_file_size(path[UNSIGNED]);
  266. if(fsize != sizeof(enclave_css.header) + sizeof(enclave_css.body))
  267. {
  268. se_trace(SE_TRACE_ERROR, UNSIGNED_FILE_ERROR, path[UNSIGNED]);
  269. return false;
  270. }
  271. uint8_t *buf = new uint8_t[fsize];
  272. memset(buf, 0, fsize);
  273. if(read_file_to_buf(path[UNSIGNED], buf, fsize) == false)
  274. {
  275. se_trace(SE_TRACE_ERROR, READ_FILE_ERROR, path[UNSIGNED]);
  276. delete [] buf;
  277. return false;
  278. }
  279. memcpy_s(&enclave_css.header, sizeof(enclave_css.header), buf, sizeof(enclave_css.header));
  280. memcpy_s(&enclave_css.body, sizeof(enclave_css.body), buf + sizeof(enclave_css.header), fsize - sizeof(enclave_css.header));
  281. delete [] buf;
  282. css->header.date = enclave_css.header.date;
  283. // Verify the header and body read from the unsigned file to make sure it's the same as that generated from xml file
  284. if(memcmp(&enclave_css.header, &css->header, sizeof(enclave_css.header)) || memcmp(&enclave_css.body, &css->body, sizeof(enclave_css.body)))
  285. {
  286. se_trace(SE_TRACE_ERROR, UNSIGNED_FILE_XML_MISMATCH);
  287. return false;
  288. }
  289. }
  290. return true;
  291. }
  292. static bool calc_RSAq1q2(int length_s, const uint8_t *data_s, int length_m, const uint8_t *data_m,
  293. uint8_t *data_q1, uint8_t *data_q2)
  294. {
  295. assert(data_s && data_m && data_q1 && data_q2);
  296. bool ret = false;
  297. BIGNUM *ptemp1=NULL, *ptemp2=NULL, *pQ1=NULL, *pQ2=NULL, *pM=NULL, *pS = NULL;
  298. unsigned char *q1 = NULL, *q2= NULL;
  299. BN_CTX *ctx = NULL;
  300. do{
  301. if((ptemp1 = BN_new()) == NULL)
  302. break;
  303. if((ptemp2 = BN_new()) == NULL)
  304. break;
  305. if((pQ1 = BN_new()) == NULL)
  306. break;
  307. if((pQ2 = BN_new()) == NULL)
  308. break;
  309. if((pM = BN_new()) == NULL)
  310. break;
  311. if((pS = BN_new()) == NULL)
  312. break;
  313. if(BN_bin2bn((const unsigned char *)data_m, length_m, pM) == NULL)
  314. break;
  315. if(BN_bin2bn((const unsigned char *)data_s, length_s, pS) == NULL)
  316. break;
  317. if((ctx = BN_CTX_new()) == NULL)
  318. break;
  319. //q1 = floor(signature*signature/modulus)
  320. //q2 = floor((signature*signature.signature - q1*signature*Modulus)/Modulus)
  321. if(BN_mul(ptemp1, pS, pS, ctx) != 1)
  322. break;
  323. if(BN_div(pQ1, ptemp2, ptemp1, pM, ctx) !=1)
  324. break;
  325. if(BN_mul(ptemp1, pS, ptemp2, ctx) !=1)
  326. break;
  327. if(BN_div(pQ2, ptemp2, ptemp1, pM, ctx) !=1)
  328. break;
  329. int q1_len = BN_num_bytes(pQ1);
  330. int q2_len = BN_num_bytes(pQ2);
  331. if((q1 = (unsigned char *)malloc(q1_len)) == NULL)
  332. break;
  333. if((q2 = (unsigned char *)malloc(q2_len)) == NULL)
  334. break;
  335. if(q1_len != BN_bn2bin(pQ1, (unsigned char *)q1))
  336. break;
  337. if(q2_len != BN_bn2bin(pQ2, (unsigned char *)q2))
  338. break;
  339. int size_q1 = (q1_len < SE_KEY_SIZE) ? q1_len : SE_KEY_SIZE;
  340. int size_q2 = (q2_len < SE_KEY_SIZE) ? q2_len : SE_KEY_SIZE;
  341. for(int i = 0; i < size_q1; i++)
  342. {
  343. data_q1[i] = q1[size_q1 - i -1];
  344. }
  345. for(int i = 0; i < size_q2; i++)
  346. {
  347. data_q2[i] = q2[size_q2 - i -1];
  348. }
  349. ret = true;
  350. }while(0);
  351. if(q1)
  352. free(q1);
  353. if(q2)
  354. free(q2);
  355. if(ptemp1)
  356. BN_clear_free(ptemp1);
  357. if(ptemp2)
  358. BN_clear_free(ptemp2);
  359. if(pQ1)
  360. BN_clear_free(pQ1);
  361. if(pQ2)
  362. BN_clear_free(pQ2);
  363. if(pS)
  364. BN_clear_free(pS);
  365. if(pM)
  366. BN_clear_free(pM);
  367. if(ctx)
  368. BN_CTX_free(ctx);
  369. return ret;
  370. }
  371. static bool create_signature(const RSA *rsa, const char *sigpath, enclave_css_t *enclave_css)
  372. {
  373. assert(enclave_css != NULL);
  374. assert(!(rsa == NULL && sigpath == NULL) && !(rsa != NULL && sigpath != NULL));
  375. uint8_t signature[SIGNATURE_SIZE]; // keep the signature in big endian
  376. memset(signature, 0, SIGNATURE_SIZE);
  377. //**********get the signature*********
  378. if(sigpath != NULL)//CATSIG mode
  379. {
  380. if(get_file_size(sigpath) != SIGNATURE_SIZE)
  381. {
  382. se_trace(SE_TRACE_ERROR, SIG_FILE_ERROR, sigpath);
  383. return false;
  384. }
  385. if(read_file_to_buf(sigpath, signature, SIGNATURE_SIZE) == false)
  386. {
  387. se_trace(SE_TRACE_ERROR, READ_FILE_ERROR, sigpath);
  388. return false;
  389. }
  390. }
  391. else //SIGN mode
  392. {
  393. size_t buffer_size = sizeof(enclave_css->header) + sizeof(enclave_css->body);
  394. uint8_t * temp_buffer = (uint8_t *)malloc(buffer_size * sizeof(char));
  395. if(NULL == temp_buffer)
  396. {
  397. se_trace(SE_TRACE_ERROR, NO_MEMORY_ERROR);
  398. return false;
  399. }
  400. memcpy_s(temp_buffer, buffer_size, &enclave_css->header, sizeof(enclave_css->header));
  401. memcpy_s(temp_buffer + sizeof(enclave_css->header), buffer_size - sizeof(enclave_css->header),
  402. &enclave_css->body, sizeof(enclave_css->body));
  403. uint8_t hash[SGX_HASH_SIZE] = {0};
  404. unsigned int hash_size = SGX_HASH_SIZE;
  405. if(SGX_SUCCESS != sgx_EVP_Digest(EVP_sha256(), temp_buffer, (unsigned int)buffer_size, hash, &hash_size))
  406. {
  407. free(temp_buffer);
  408. return false;
  409. }
  410. size_t siglen;
  411. int ret = RSA_sign(NID_sha256, hash, hash_size, signature, (unsigned int *)&siglen, const_cast<RSA *>(rsa));
  412. free(temp_buffer);
  413. if(ret != 1)
  414. return false;
  415. }
  416. for(int i = 0; i<SIGNATURE_SIZE; i++)
  417. {
  418. (enclave_css->key.signature)[i] = signature[SIGNATURE_SIZE-1-i];
  419. }
  420. //************************calculate q1 and q2*********************
  421. uint8_t modulus[SE_KEY_SIZE];
  422. for(int i = 0; i<SE_KEY_SIZE; i++)
  423. {
  424. modulus[i] = enclave_css->key.modulus[SE_KEY_SIZE-1-i];
  425. }
  426. bool res = calc_RSAq1q2(sizeof(enclave_css->key.signature),
  427. (const uint8_t *)signature,
  428. sizeof(enclave_css->key.modulus),
  429. (const uint8_t *)modulus,
  430. (uint8_t *)enclave_css->buffer.q1,
  431. (uint8_t *)enclave_css->buffer.q2);
  432. return res;
  433. }
  434. static bool verify_signature(const RSA *rsa, const enclave_css_t *enclave_css)
  435. {
  436. assert(rsa != NULL && enclave_css != NULL);
  437. size_t buffer_size = sizeof(enclave_css->header) + sizeof(enclave_css->body);
  438. uint8_t *temp_buffer = (uint8_t *)malloc(buffer_size * sizeof(char));
  439. if(NULL == temp_buffer)
  440. {
  441. se_trace(SE_TRACE_ERROR, NO_MEMORY_ERROR);
  442. return false;
  443. }
  444. memcpy_s(temp_buffer, buffer_size, &enclave_css->header, sizeof(enclave_css->header));
  445. memcpy_s(temp_buffer + sizeof(enclave_css->header), buffer_size-sizeof(enclave_css->header),
  446. &enclave_css->body, sizeof(enclave_css->body));
  447. uint8_t hash[SGX_HASH_SIZE] = {0};
  448. unsigned int hash_size = SGX_HASH_SIZE;
  449. if(SGX_SUCCESS != sgx_EVP_Digest(EVP_sha256(), temp_buffer, (unsigned int)buffer_size, hash, &hash_size))
  450. {
  451. free(temp_buffer);
  452. return false;
  453. }
  454. free(temp_buffer);
  455. uint8_t signature[SIGNATURE_SIZE];
  456. for(int i=0; i<SIGNATURE_SIZE; i++)
  457. {
  458. signature[i] = enclave_css->key.signature[SIGNATURE_SIZE-1-i];
  459. }
  460. if(1 != RSA_verify(NID_sha256, hash, hash_size, signature, SIGNATURE_SIZE, const_cast<RSA *>(rsa)))
  461. {
  462. return false;
  463. }
  464. return true;
  465. }
  466. static bool gen_enclave_signing_file(const enclave_css_t *enclave_css, const char *outpath)
  467. {
  468. assert(enclave_css != NULL);
  469. size_t size = sizeof(enclave_css->header) + sizeof(enclave_css->body);
  470. uint8_t *buffer = (uint8_t *)malloc(size);
  471. if(buffer == NULL)
  472. {
  473. se_trace(SE_TRACE_ERROR, NO_MEMORY_ERROR);
  474. return false;
  475. }
  476. memcpy_s(buffer, sizeof(enclave_css->header), &enclave_css->header, sizeof(enclave_css->header));
  477. memcpy_s(buffer + sizeof(enclave_css->header), sizeof(enclave_css->body), &enclave_css->body, sizeof(enclave_css->body));
  478. if(write_data_to_file(outpath, std::ios::out|std::ios::binary, buffer, size) == false)
  479. {
  480. free(buffer);
  481. return false;
  482. }
  483. free(buffer);
  484. return true;
  485. }
  486. static bool cmdline_parse(unsigned int argc, char *argv[], int *mode, const char **path, uint32_t *ignore_error_bits)
  487. {
  488. assert(mode!=NULL && path != NULL);
  489. if(argc<2)
  490. {
  491. se_trace(SE_TRACE_ERROR, LACK_PARA_ERROR);
  492. return false;
  493. }
  494. if(argc == 2 && !STRCMP(argv[1], "-help"))
  495. {
  496. se_trace(SE_TRACE_ERROR, USAGE_STRING);
  497. *mode = -1;
  498. return true;
  499. }
  500. if(argc == 2 && !STRCMP(argv[1], "-version"))
  501. {
  502. se_trace(SE_TRACE_ERROR, VERSION_STRING, STRFILEVER, COPYRIGHT);
  503. *mode = -1;
  504. return true;
  505. }
  506. enum { PAR_REQUIRED, PAR_OPTIONAL, PAR_INVALID };
  507. typedef struct _param_struct_{
  508. const char *name; //options
  509. char *value; //keep the path
  510. int flag; //indicate this parameter is required(0), optional(1) or invalid(2)
  511. }param_struct_t; //keep the parameter pairs
  512. param_struct_t params_sign[] = {
  513. {"-enclave", NULL, PAR_REQUIRED},
  514. {"-config", NULL, PAR_OPTIONAL},
  515. {"-key", NULL, PAR_REQUIRED},
  516. {"-out", NULL, PAR_REQUIRED},
  517. {"-sig", NULL, PAR_INVALID},
  518. {"-unsigned", NULL, PAR_INVALID},
  519. {"-dumpfile", NULL, PAR_OPTIONAL},
  520. {"-cssfile", NULL, PAR_OPTIONAL}};
  521. param_struct_t params_gendata[] = {
  522. {"-enclave", NULL, PAR_REQUIRED},
  523. {"-config", NULL, PAR_OPTIONAL},
  524. {"-key", NULL, PAR_INVALID},
  525. {"-out", NULL, PAR_REQUIRED},
  526. {"-sig", NULL, PAR_INVALID},
  527. {"-unsigned", NULL, PAR_INVALID},
  528. {"-dumpfile", NULL, PAR_INVALID},
  529. {"-cssfile", NULL, PAR_INVALID}};
  530. param_struct_t params_catsig[] = {
  531. {"-enclave", NULL, PAR_REQUIRED},
  532. {"-config", NULL, PAR_OPTIONAL},
  533. {"-key", NULL, PAR_REQUIRED},
  534. {"-out", NULL, PAR_REQUIRED},
  535. {"-sig", NULL, PAR_REQUIRED},
  536. {"-unsigned", NULL, PAR_REQUIRED},
  537. {"-dumpfile", NULL, PAR_OPTIONAL},
  538. {"-cssfile", NULL, PAR_OPTIONAL}};
  539. param_struct_t params_dump[] = {
  540. {"-enclave", NULL, PAR_REQUIRED},
  541. {"-config", NULL, PAR_INVALID},
  542. {"-key", NULL, PAR_INVALID},
  543. {"-out", NULL, PAR_INVALID},
  544. {"-sig", NULL, PAR_INVALID},
  545. {"-unsigned", NULL, PAR_INVALID},
  546. {"-dumpfile", NULL, PAR_REQUIRED},
  547. {"-cssfile", NULL, PAR_OPTIONAL}};
  548. const char *mode_m[] ={"sign", "gendata","catsig", "dump"};
  549. param_struct_t *params[] = {params_sign, params_gendata, params_catsig, params_dump};
  550. unsigned int tempidx=0;
  551. for(; tempidx<sizeof(mode_m)/sizeof(mode_m[0]); tempidx++)
  552. {
  553. if(!STRCMP(mode_m[tempidx], argv[1]))//match
  554. {
  555. break;
  556. }
  557. }
  558. unsigned int tempmode = tempidx;
  559. if(tempmode>=sizeof(mode_m)/sizeof(mode_m[0]))
  560. {
  561. se_trace(SE_TRACE_ERROR, UNREC_CMD_ERROR, argv[1]);
  562. return false;
  563. }
  564. uint32_t ie_bits = 0;
  565. typedef struct _ignore_error_map_t
  566. {
  567. const char* para_str;
  568. int error_flag_bit;
  569. } ignore_error_map_t;
  570. ignore_error_map_t iem[] =
  571. {
  572. {"-ignore-rel-error", REL_ERROR_BIT},
  573. {"-ignore-init-sec-error", INIT_SEC_ERROR_BIT}
  574. };
  575. unsigned int params_count = (unsigned)(sizeof(params_sign)/sizeof(params_sign[0]));
  576. for(unsigned int i=2; i<argc; i++)
  577. {
  578. unsigned int idx = 0;
  579. for(; idx < sizeof(iem)/sizeof(iem[0]); idx++)
  580. {
  581. if(!STRCMP(argv[i], iem[idx].para_str))
  582. {
  583. if((ie_bits & iem[idx].error_flag_bit) != 0)
  584. {
  585. se_trace(SE_TRACE_ERROR, REPEAT_OPTION_ERROR, argv[i]);
  586. return false;
  587. }
  588. ie_bits |= iem[idx].error_flag_bit;
  589. break;
  590. }
  591. }
  592. if(idx != sizeof(iem)/sizeof(iem[0]))
  593. {
  594. continue;
  595. }
  596. unsigned int j=0;
  597. for(; j<params_count; j++)
  598. {
  599. if(STRCMP(argv[i], params[tempmode][j].name)==0) //match
  600. {
  601. if((i<argc-1)&&(STRNCMP(argv[i+1],"-", 1))) // assuming pathname doesn't contain "-"
  602. {
  603. if(params[tempmode][j].value != NULL)
  604. {
  605. se_trace(SE_TRACE_ERROR, REPEAT_OPTION_ERROR, params[tempmode][j].name);
  606. return false;
  607. }
  608. params[tempmode][j].value = argv[i+1];
  609. i++;
  610. break;
  611. }
  612. else //didn't match: 1) no path parameter behind option parameter 2) parameters format error.
  613. {
  614. se_trace(SE_TRACE_ERROR, INVALID_FILE_NAME_ERROR, params[tempmode][j].name);
  615. return false;
  616. }
  617. }
  618. }
  619. if(j == params_count)
  620. {
  621. se_trace(SE_TRACE_ERROR, UNREC_OPTION_ERROR, argv[i]);
  622. return false;
  623. }
  624. }
  625. for(unsigned int i = 0; i < params_count; i++)
  626. {
  627. if(params[tempmode][i].flag == PAR_REQUIRED && params[tempmode][i].value == NULL)
  628. {
  629. se_trace(SE_TRACE_ERROR, LACK_REQUIRED_OPTION_ERROR, params[tempmode][i].name, mode_m[tempmode]);
  630. return false;
  631. }
  632. if(params[tempmode][i].flag == PAR_INVALID && params[tempmode][i].value != NULL)
  633. {
  634. se_trace(SE_TRACE_ERROR, GIVE_INVALID_OPTION_ERROR, params[tempmode][i].name, mode_m[tempmode]);
  635. return false;
  636. }
  637. }
  638. // Set output parameters
  639. for(unsigned int i = 0; i < params_count; i++)
  640. {
  641. path[i] = params[tempmode][i].value;
  642. }
  643. *mode = tempmode;
  644. *ignore_error_bits = ie_bits;
  645. return true;
  646. }
  647. //generate_output:
  648. // To generate the final output file
  649. // SIGN- need to fill the enclave_css_t(key part included), sign the header and body and
  650. // update the metadata in the out file
  651. // GENDATA- need to fill the enclave_css_t(key part excluded), get the body and header,
  652. // and then write the whole out file with body+header+hash
  653. // CATSIG- need to fill the enclave_css_t(include key), read the signature from the sigpath,
  654. // and then update the metadata in the out file
  655. static bool generate_output(int mode, int ktype, const uint8_t *enclave_hash, const RSA *rsa, metadata_t *metadata,
  656. const char **path)
  657. {
  658. assert(enclave_hash != NULL && metadata != NULL && path != NULL);
  659. switch(mode)
  660. {
  661. case SIGN:
  662. {
  663. if(ktype != PRIVATE_KEY || !rsa)
  664. {
  665. se_trace(SE_TRACE_ERROR, LACK_PRI_KEY_ERROR);
  666. return false;
  667. }
  668. if(false == fill_enclave_css(rsa, path, enclave_hash, &(metadata->enclave_css)))
  669. {
  670. return false;
  671. }
  672. if(false == create_signature(rsa, NULL, &(metadata->enclave_css)))
  673. {
  674. return false;
  675. }
  676. break;
  677. }
  678. case GENDATA:
  679. {
  680. if(false == fill_enclave_css(NULL, path, enclave_hash, &(metadata->enclave_css)))
  681. {
  682. return false;
  683. }
  684. if(false == gen_enclave_signing_file(&(metadata->enclave_css), path[OUTPUT]))
  685. {
  686. return false;
  687. }
  688. break;
  689. }
  690. case CATSIG:
  691. {
  692. if(ktype != PUBLIC_KEY || !rsa)
  693. {
  694. se_trace(SE_TRACE_ERROR, LACK_PUB_KEY_ERROR);
  695. return false;
  696. }
  697. if(false == fill_enclave_css(rsa, path, enclave_hash, &(metadata->enclave_css)))
  698. {
  699. return false;
  700. }
  701. if(false == create_signature(NULL, path[SIG], &(metadata->enclave_css)))
  702. {
  703. return false;
  704. }
  705. break;
  706. }
  707. default:
  708. {
  709. return false;
  710. }
  711. }
  712. return true;
  713. }
  714. #include "se_page_attr.h"
  715. static void metadata_cleanup(metadata_t *metadata, uint32_t size_to_reduce)
  716. {
  717. layout_t *heap_max = NULL, *heap_init = NULL, *ut_stack_max = NULL;
  718. metadata->dirs[DIR_LAYOUT].size -= size_to_reduce;
  719. metadata->size -= size_to_reduce;
  720. layout_t *start = GET_PTR(layout_t, metadata, metadata->dirs[DIR_LAYOUT].offset);
  721. layout_t *end = GET_PTR(layout_t, start, metadata->dirs[DIR_LAYOUT].size);
  722. for (layout_t *l = start; l < end; l++)
  723. {
  724. if (heap_max != NULL && heap_init != NULL && ut_stack_max != NULL)
  725. break;
  726. if ((heap_max == NULL) && (l->entry.id == LAYOUT_ID_HEAP_MAX))
  727. {
  728. heap_max = l;
  729. continue;
  730. }
  731. if ((heap_init == NULL) && (l->entry.id == LAYOUT_ID_HEAP_INIT))
  732. {
  733. heap_init = l;
  734. continue;
  735. }
  736. if ((ut_stack_max == NULL) && (l->entry.id == LAYOUT_ID_STACK_MAX))
  737. {
  738. ut_stack_max = l;
  739. continue;
  740. }
  741. }
  742. // if there exists LAYOUT_ID_HEAP_MAX, modify it so that it won't be included
  743. // in the MRENCLAVE, also remove the PAGE_ATTR_POST_ADD attribute so that
  744. // dynamic range won't be created during enclave loading time
  745. if (heap_max)
  746. {
  747. heap_max->entry.si_flags = SI_FLAG_NONE;
  748. heap_max->entry.attributes &= (uint16_t)(~PAGE_ATTR_POST_ADD);
  749. }
  750. if (heap_init)
  751. {
  752. heap_init->entry.attributes &= (uint16_t)(~PAGE_ATTR_POST_ADD);
  753. }
  754. if (ut_stack_max)
  755. {
  756. ut_stack_max->entry.attributes &= (uint16_t)(~PAGE_ATTR_POST_ADD);
  757. }
  758. }
  759. static bool append_compatible_metadata(metadata_t *compat_metadata, metadata_t *metadata)
  760. {
  761. metadata_t *dest_meta = metadata;
  762. uint32_t size = 0;
  763. do{
  764. if(dest_meta->magic_num != METADATA_MAGIC || dest_meta->size == 0)
  765. break;
  766. size += dest_meta->size;
  767. if(size < dest_meta->size)
  768. return false;
  769. dest_meta = (metadata_t *)((size_t)dest_meta + dest_meta->size);
  770. } while(size < METADATA_SIZE);
  771. if(size + compat_metadata->size < size ||
  772. size + compat_metadata->size < compat_metadata->size ||
  773. size + compat_metadata->size > METADATA_SIZE)
  774. return false;
  775. if(memcpy_s(dest_meta, METADATA_SIZE - size , compat_metadata, compat_metadata->size))
  776. return false;
  777. return true;
  778. }
  779. static bool generate_compatible_metadata(metadata_t *metadata, const xml_parameter_t *parameter)
  780. {
  781. metadata_t *metadata2 = (metadata_t *)malloc(metadata->size);
  782. if(!metadata2)
  783. {
  784. se_trace(SE_TRACE_ERROR, NO_MEMORY_ERROR);
  785. return false;
  786. }
  787. // append 2_0 metadata
  788. memcpy(metadata2, metadata, metadata->size);
  789. metadata2->version = META_DATA_MAKE_VERSION(SGX_2_0_MAJOR_VERSION,SGX_2_0_MINOR_VERSION);
  790. if (!append_compatible_metadata(metadata2, metadata))
  791. {
  792. free(metadata2);
  793. return false;
  794. }
  795. // append 1_9 metadata
  796. metadata2->version = META_DATA_MAKE_VERSION(SGX_1_9_MAJOR_VERSION,SGX_1_9_MINOR_VERSION);
  797. layout_t *start = GET_PTR(layout_t, metadata2, metadata2->dirs[DIR_LAYOUT].offset);
  798. layout_t *end = GET_PTR(layout_t, start, metadata2->dirs[DIR_LAYOUT].size);
  799. layout_t tmp_layout, *first_dyn_entry = NULL, *first = NULL, *utility_td = NULL;
  800. uint32_t size_to_reduce = 0;
  801. bool ret = false;
  802. for (layout_t *l = start; l < end; l++)
  803. {
  804. if ((l->entry.id == LAYOUT_ID_STACK_DYN_MAX) ||
  805. (l->entry.id == LAYOUT_ID_STACK_DYN_MIN))
  806. {
  807. first_dyn_entry = l;
  808. break;
  809. }
  810. }
  811. // no dynamic layout, append the metadata directly
  812. if (first_dyn_entry == NULL)
  813. {
  814. ret = append_compatible_metadata(metadata2, metadata);
  815. free(metadata2);
  816. return ret;
  817. }
  818. //sizeof(layout_t) for the guard page before LAYOUT_ID_STACK_DYN_MAX
  819. size_to_reduce = (uint32_t)((size_t)end - (size_t)first_dyn_entry + sizeof(layout_t));
  820. layout_t *last = &first_dyn_entry[-2];
  821. for (layout_t *l = start; l <= last; l++)
  822. {
  823. if (l->entry.id == LAYOUT_ID_TD)
  824. {
  825. utility_td = l;
  826. break;
  827. }
  828. }
  829. assert(utility_td != NULL);
  830. //Besides dynamic threads, there's only a single utility thread
  831. if (utility_td == last)
  832. {
  833. metadata_cleanup(metadata2, size_to_reduce);
  834. ret = append_compatible_metadata(metadata2, metadata);
  835. free(metadata2);
  836. return ret;
  837. }
  838. layout_t *utility_start = NULL;
  839. for (layout_t *l = start; l <= last; l++)
  840. {
  841. if (l->entry.id == LAYOUT_ID_GUARD)
  842. {
  843. utility_start = l;
  844. break;
  845. }
  846. }
  847. assert(utility_start != NULL);
  848. // entry/group layout if they all exist:
  849. // utility thread | minpool thread | minpool group | eremove thread | eremove group | dyn thread | dyn group
  850. // build a group layout to represent all the possible minpool/eremoved layouts
  851. first = &utility_td[1];
  852. uint16_t num_of_entries = (uint16_t)(first - utility_start);
  853. memset(&tmp_layout, 0, sizeof(tmp_layout));
  854. tmp_layout.group.id = LAYOUT_ID_THREAD_GROUP;
  855. tmp_layout.group.entry_count = num_of_entries;
  856. tmp_layout.group.load_times = (uint32_t)parameter[TCSNUM].value - 1;
  857. for (uint32_t i = 0; i < tmp_layout.group.entry_count; i++)
  858. {
  859. tmp_layout.group.load_step += (((uint64_t)utility_start[i].entry.page_count) << SE_PAGE_SHIFT);
  860. }
  861. memcpy_s(first, sizeof(layout_t), &tmp_layout, sizeof(layout_t));
  862. size_to_reduce += (uint32_t)((size_t)last - (size_t)first);
  863. metadata_cleanup(metadata2, size_to_reduce);
  864. ret = append_compatible_metadata(metadata2, metadata);
  865. free(metadata2);
  866. return ret;
  867. }
  868. static bool dump_enclave_metadata(const char *enclave_path, const char *dumpfile_path, const char *cssfile)
  869. {
  870. assert(enclave_path != NULL && dumpfile_path != NULL);
  871. uint64_t meta_offset = 0;
  872. bin_fmt_t bin_fmt = BF_UNKNOWN;
  873. uint32_t file_size = 0;
  874. se_file_handle_t fh = open_file(enclave_path);
  875. if (fh == THE_INVALID_HANDLE)
  876. {
  877. se_trace(SE_TRACE_ERROR, OPEN_FILE_ERROR, enclave_path);
  878. return false;
  879. }
  880. std::unique_ptr<map_handle_t, void (*)(map_handle_t*)> mh(map_file(fh, &file_size), unmap_file);
  881. if (!mh)
  882. {
  883. close_handle(fh);
  884. return false;
  885. }
  886. // Parse enclave
  887. std::unique_ptr<BinParser> parser(binparser::get_parser(mh->base_addr, (size_t)file_size));
  888. assert(parser != NULL);
  889. sgx_status_t status = parser->run_parser();
  890. if (status != SGX_SUCCESS)
  891. {
  892. se_trace(SE_TRACE_ERROR, INVALID_ENCLAVE_ERROR);
  893. close_handle(fh);
  894. return false;
  895. }
  896. // Collect enclave info
  897. if(get_enclave_info(parser.get(), &bin_fmt, &meta_offset, true) == false)
  898. {
  899. close_handle(fh);
  900. return false;
  901. }
  902. const metadata_t *metadata = GET_PTR(metadata_t, mh->base_addr, meta_offset);
  903. if(print_metadata(dumpfile_path, metadata) == false)
  904. {
  905. close_handle(fh);
  906. return false;
  907. }
  908. if(cssfile != NULL)
  909. {
  910. if (write_data_to_file(cssfile, std::ios::binary | std::ios::out,
  911. (uint8_t *)&(metadata->enclave_css), sizeof(enclave_css_t)) == false)
  912. {
  913. close_handle(fh);
  914. return false;
  915. }
  916. }
  917. close_handle(fh);
  918. return true;
  919. }
  920. int main(int argc, char* argv[])
  921. {
  922. xml_parameter_t parameter[] = {{"ProdID", 0xFFFF, 0, 0, 0},
  923. {"ISVSVN", 0xFFFF, 0, 0, 0},
  924. {"ReleaseType", 1, 0, 0, 0},
  925. {"IntelSigned", 1, 0, 0, 0},
  926. {"ProvisionKey",1,0,0,0},
  927. {"LaunchKey",1,0,0,0},
  928. {"DisableDebug",1,0,0,0},
  929. {"HW", 0x10,0,0,0},
  930. {"TCSNum",0xFFFFFFFF,TCS_NUM_MIN,TCS_NUM_MIN,0},
  931. {"TCSMaxNum",0xFFFFFFFF,TCS_NUM_MIN,TCS_NUM_MIN,0},
  932. {"TCSMinPool",0xFFFFFFFF,0,TCS_NUM_MIN,0},
  933. {"TCSPolicy",TCS_POLICY_UNBIND,TCS_POLICY_BIND,TCS_POLICY_UNBIND,0},
  934. {"StackMaxSize",0x1FFFFFFFFF,STACK_SIZE_MIN,STACK_SIZE_MAX,0},
  935. {"StackMinSize",0x1FFFFFFFFF,STACK_SIZE_MIN,STACK_SIZE_MIN,0},
  936. {"HeapMaxSize",0x1FFFFFFFFF,0,HEAP_SIZE_MAX,0},
  937. {"HeapMinSize",0x1FFFFFFFFF,0,HEAP_SIZE_MIN,0},
  938. {"HeapInitSize",0x1FFFFFFFFF,0,HEAP_SIZE_MIN,0},
  939. {"MiscSelect", 0xFFFFFFFF, 0, DEFAULT_MISC_SELECT, 0},
  940. {"MiscMask", 0xFFFFFFFF, 0, DEFAULT_MISC_MASK, 0}};
  941. const char *path[8] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
  942. uint8_t enclave_hash[SGX_HASH_SIZE] = {0};
  943. uint8_t metadata_raw[METADATA_SIZE];
  944. metadata_t *metadata = (metadata_t*)metadata_raw;
  945. int res = -1, mode = -1;
  946. int key_type = UNIDENTIFIABLE_KEY; //indicate the type of the input key file
  947. size_t parameter_count = sizeof(parameter)/sizeof(parameter[0]);
  948. uint64_t meta_offset = 0;
  949. uint32_t ignore_error_bits = 0;
  950. RSA *rsa = NULL;
  951. memset(&metadata_raw, 0, sizeof(metadata_raw));
  952. #if OPENSSL_VERSION_NUMBER < 0x10100000L
  953. OpenSSL_add_all_algorithms();
  954. ERR_load_crypto_strings();
  955. #else
  956. OPENSSL_init_crypto(0, NULL);
  957. #endif
  958. //Parse command line
  959. if(cmdline_parse(argc, argv, &mode, path, &ignore_error_bits) == false)
  960. {
  961. se_trace(SE_TRACE_ERROR, USAGE_STRING);
  962. goto clear_return;
  963. }
  964. if(mode == -1) // User only wants to get the help info or version info
  965. {
  966. res = 0;
  967. goto clear_return;
  968. }
  969. else if(mode == DUMP)
  970. {
  971. // dump metadata info
  972. if(dump_enclave_metadata(path[DLL], path[DUMPFILE], path[CSSFILE]) == false)
  973. {
  974. se_trace(SE_TRACE_ERROR, DUMP_METADATA_ERROR, path[DUMPFILE]);
  975. goto clear_return;
  976. }
  977. se_trace(SE_TRACE_ERROR, SUCCESS_EXIT);
  978. res = 0;
  979. goto clear_return;
  980. }
  981. //Other modes
  982. //
  983. //Parse the xml file to get the metadata
  984. if(parse_metadata_file(path[XML], parameter, (int)parameter_count) == false)
  985. {
  986. goto clear_return;
  987. }
  988. //Parse the key file
  989. if(parse_key_file(mode, path[KEY], &rsa, &key_type) == false && key_type != NO_KEY)
  990. {
  991. goto clear_return;
  992. }
  993. if(copy_file(path[DLL], path[OUTPUT]) == false)
  994. {
  995. se_trace(SE_TRACE_ERROR, OVERALL_ERROR);
  996. goto clear_return;
  997. }
  998. if(measure_enclave(enclave_hash, path[OUTPUT], parameter, ignore_error_bits, metadata, &meta_offset) == false)
  999. {
  1000. se_trace(SE_TRACE_ERROR, OVERALL_ERROR);
  1001. goto clear_return;
  1002. }
  1003. if((generate_output(mode, key_type, enclave_hash, rsa, metadata, path)) == false)
  1004. {
  1005. se_trace(SE_TRACE_ERROR, OVERALL_ERROR);
  1006. goto clear_return;
  1007. }
  1008. //to verify
  1009. if(mode == SIGN || mode == CATSIG)
  1010. {
  1011. if(verify_signature(rsa, &(metadata->enclave_css)) == false)
  1012. {
  1013. se_trace(SE_TRACE_ERROR, OVERALL_ERROR);
  1014. goto clear_return;
  1015. }
  1016. if(false == generate_compatible_metadata(metadata, parameter))
  1017. {
  1018. se_trace(SE_TRACE_ERROR, OVERALL_ERROR);
  1019. goto clear_return;
  1020. }
  1021. if(false == update_metadata(path[OUTPUT], metadata, meta_offset))
  1022. {
  1023. se_trace(SE_TRACE_ERROR, OVERALL_ERROR);
  1024. goto clear_return;
  1025. }
  1026. }
  1027. if(path[DUMPFILE] != NULL)
  1028. {
  1029. if(print_metadata(path[DUMPFILE], metadata) == false)
  1030. {
  1031. se_trace(SE_TRACE_ERROR, DUMP_METADATA_ERROR, path[DUMPFILE]);
  1032. goto clear_return;
  1033. }
  1034. }
  1035. if (path[CSSFILE] != NULL)
  1036. {
  1037. if (write_data_to_file(path[CSSFILE], std::ios::binary | std::ios::out,
  1038. (uint8_t *)&(metadata->enclave_css), sizeof(enclave_css_t)) == false)
  1039. goto clear_return;
  1040. }
  1041. se_trace(SE_TRACE_ERROR, SUCCESS_EXIT);
  1042. res = 0;
  1043. clear_return:
  1044. if(rsa)
  1045. RSA_free(rsa);
  1046. if(res == -1 && path[OUTPUT])
  1047. remove(path[OUTPUT]);
  1048. if(res == -1 && path[DUMPFILE])
  1049. remove(path[DUMPFILE]);
  1050. if(res == -1 && path[CSSFILE])
  1051. remove(path[CSSFILE]);
  1052. #if OPENSSL_VERSION_NUMBER < 0x10100000L
  1053. EVP_cleanup();
  1054. CRYPTO_cleanup_all_ex_data();
  1055. ERR_remove_thread_state(NULL);
  1056. ERR_free_strings();
  1057. #endif
  1058. return res;
  1059. }