sign_tool.cpp 36 KB

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