ref_wl_gen.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. #include "ref_wl_gen.h"
  32. #include "arch.h"
  33. #include "parse_key_file.h"
  34. #include "ref_le.h"
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <ctype.h>
  39. #include <stdarg.h>
  40. #define USAGE \
  41. "ref_wl_gen <Command> <Options>\n" \
  42. "Command:\n"\
  43. " gen-wl: Generate a white-list file based on the information provided in the config file. \n" \
  44. "Options:\n" \
  45. " -out <file-name>: The output file name for the white-list. \n" \
  46. " -cfg <file-name>: A CSV configuration file with the list of hash values or keys to sign. \n" \
  47. " -key <file-name>: The private key to sign the white-list with. \n" \
  48. " -ver <version>: An integer value of the white-list version. \n" \
  49. " -verbose: Print extended report while generating the white-list. \n" \
  50. "CSV file columns: \n" \
  51. " allow provision key, mr_enclave valid, mr_signer hash, mr_signer file, mr_enclave hash, mr_enclave file, comments (ignored) \n" \
  52. "Notes: \n" \
  53. " * Column 1 and 2 should be true or false. \n" \
  54. " * If mr_enclave valid is false the mr_enclave columns will be ignored. \n" \
  55. " * If mr_signer/mr_enclave hash is not empty the mr_signer/mr_enclave file will be ignored. \n" \
  56. " * mr_signer file should be key file (pem), mr_enclave file should be sigstruct (bin). \n" \
  57. " * Key hash representation should be in little endian, i.e. LSB byte first. \n" \
  58. "Example:\n" \
  59. " ref_wl_gen gen-wl -out wl.bin -cfg cfg.csv -key private.pem \n"
  60. #define LINE_LEN 1024
  61. #define MAX_NUM_OF_RECORDS 512
  62. #define WL_FILE_VERSION 1
  63. CRefWLGen::CRefWLGen() : m_cfgfile(NULL), m_outfile(NULL), m_keyfile(NULL), m_version(0), m_verbose(false)
  64. {
  65. }
  66. CRefWLGen::~CRefWLGen()
  67. {
  68. }
  69. void CRefWLGen::print_line(bool always_print, const char* format, ...)
  70. {
  71. if (!always_print && !m_verbose)
  72. {
  73. return;
  74. }
  75. va_list args;
  76. va_start(args, format);
  77. vprintf(format, args);
  78. va_end(args);
  79. }
  80. void CRefWLGen::print_byte_array(bool always_print, const uint8_t *array, size_t size, const char *line_prefix, int line_len)
  81. {
  82. if (!always_print && !m_verbose)
  83. {
  84. return;
  85. }
  86. for (size_t i = 0; i < size; ++i)
  87. {
  88. if (i % line_len == 0)
  89. {
  90. print_line(always_print, "\n%s", line_prefix);
  91. }
  92. print_line(always_print, "%02X ", array[i]);
  93. }
  94. print_line(always_print, "\n");
  95. }
  96. void CRefWLGen::reverse_byte_array(uint8_t *array, size_t size)
  97. {
  98. for (size_t i = 0; i < size / 2; i++)
  99. {
  100. uint8_t temp = array[i];
  101. array[i] = array[size - i - 1];
  102. array[size - i - 1] = temp;
  103. }
  104. }
  105. gen_wl_cmd_t CRefWLGen::parse_cmd(int argc, char **argv)
  106. {
  107. gen_wl_cmd_t cmd = UNKNOWN;
  108. argc--;
  109. argv++;
  110. if (argc <= 0 || argv == NULL)
  111. {
  112. return cmd;
  113. }
  114. if (strcmp(*argv, "gen-wl") == 0)
  115. cmd = GEN_WL;
  116. argc--;
  117. argv++;
  118. while (argc >= 1)
  119. {
  120. if (strcmp(*argv, "-out") == 0)
  121. {
  122. if (--argc < 1) { return UNKNOWN; }
  123. m_outfile = *(++argv);
  124. }
  125. else if (strcmp(*argv, "-cfg") == 0)
  126. {
  127. if (--argc < 1) { return UNKNOWN; }
  128. m_cfgfile = *(++argv);
  129. }
  130. else if (strcmp(*argv, "-key") == 0)
  131. {
  132. if (--argc < 1) { return UNKNOWN; }
  133. m_keyfile = *(++argv);
  134. }
  135. else if (strcmp(*argv, "-ver") == 0)
  136. {
  137. if (--argc < 1) { return UNKNOWN; }
  138. m_version = atoi(*(++argv));
  139. }
  140. else if (strcmp(*argv, "-verbose") == 0)
  141. {
  142. m_verbose = true;
  143. }
  144. else
  145. {
  146. return UNKNOWN;
  147. }
  148. argc--;
  149. argv++;
  150. }
  151. return cmd;
  152. }
  153. char* CRefWLGen::clean_start(char *str)
  154. {
  155. if (str == NULL)
  156. {
  157. return NULL;
  158. }
  159. while (isspace(*str))
  160. {
  161. str++;
  162. }
  163. return str;
  164. }
  165. bool CRefWLGen::get_hash_from_pubkey_file(const char* pubkey_file, sgx_measurement_t* p_hash)
  166. {
  167. if (pubkey_file == NULL || p_hash == NULL || *pubkey_file == 0)
  168. {
  169. return false;
  170. }
  171. rsa_params_t rsa_params;
  172. int key_type;
  173. if (!parse_key_file(pubkey_file, &rsa_params, &key_type))
  174. {
  175. return false;
  176. }
  177. if (sgx_sha256_msg((uint8_t*)&(rsa_params.n), sizeof(rsa_params.n), (sgx_sha256_hash_t*)p_hash) != SGX_SUCCESS)
  178. {
  179. return false;
  180. }
  181. return true;
  182. }
  183. bool CRefWLGen::get_hash_from_string(char* hash_str, sgx_measurement_t* p_hash)
  184. {
  185. if (hash_str == NULL || p_hash == NULL)
  186. {
  187. return false;
  188. }
  189. char* ptr = clean_start(hash_str);
  190. for (size_t count = 0; count < sizeof(sgx_measurement_t); ++count)
  191. {
  192. ptr = clean_start(ptr);
  193. if (*ptr == 0)
  194. {
  195. return false;
  196. }
  197. p_hash->m[count] = (char)strtol(ptr, &ptr, 16);
  198. }
  199. return true;
  200. }
  201. bool CRefWLGen::get_hash_from_sigstruct_file(const char* sig_file, sgx_measurement_t* p_hash)
  202. {
  203. if (sig_file == NULL || p_hash == NULL || *sig_file == 0)
  204. {
  205. return false;
  206. }
  207. FILE *p_sig_file = fopen(sig_file, "rb");
  208. if (!p_sig_file)
  209. {
  210. print_line(true, "Failed to open the input file '%s'.\n", sig_file);
  211. return false;
  212. }
  213. fseek(p_sig_file, 0, SEEK_END);
  214. size_t length = ftell(p_sig_file);
  215. rewind(p_sig_file);
  216. if (length < sizeof(enclave_css_t))
  217. {
  218. fclose(p_sig_file);
  219. return false;
  220. }
  221. enclave_css_t sigstruct;
  222. length = fread((&sigstruct), 1, sizeof(enclave_css_t), p_sig_file);
  223. fclose(p_sig_file);
  224. if (length < sizeof(enclave_css_t))
  225. {
  226. return false;
  227. }
  228. memcpy((char*)p_hash, &(sigstruct.body.enclave_hash), sizeof(sgx_measurement_t));
  229. return true;
  230. }
  231. bool CRefWLGen::process_line(char *line, ref_le_white_list_entry_t *entry)
  232. {
  233. char *item = strtok(line, ",");
  234. // parse item: allow provision key
  235. item = clean_start(item);
  236. if (strncmp(item, "true", 4) == 0)
  237. {
  238. entry->provision_key = 1;
  239. }
  240. else
  241. {
  242. entry->provision_key = 0;
  243. }
  244. print_line(false, " provision_key: %d\n", entry->provision_key);
  245. // parse item: mr_enclave valid
  246. item = strtok(NULL, ",");
  247. item = clean_start(item);
  248. if (strncmp(item, "true", 4) == 0)
  249. {
  250. entry->match_mr_enclave = 1;
  251. }
  252. else
  253. {
  254. entry->match_mr_enclave = 0;
  255. }
  256. print_line(false, " match_mr_enclave: %d\n", entry->match_mr_enclave);
  257. // parse item: mr_signer hash/file
  258. item = strtok(NULL, ",");
  259. item = clean_start(item);
  260. if (*item != 0)
  261. { // hash
  262. if (get_hash_from_string(item, &(entry->mr_signer)) == false)
  263. {
  264. return false;
  265. }
  266. item = strtok(NULL, ","); // skip the next item
  267. }
  268. else
  269. { // file
  270. item = strtok(NULL, ",");
  271. item = clean_start(item);
  272. if (get_hash_from_pubkey_file(item, &(entry->mr_signer)) == false)
  273. {
  274. return false;
  275. }
  276. }
  277. print_line(false, " mr_signer: ");
  278. print_byte_array(false, (uint8_t*)&(entry->mr_signer), sizeof(sgx_measurement_t), " ");
  279. reverse_byte_array((uint8_t*)&(entry->mr_signer), sizeof(entry->mr_signer));
  280. // parse item: mr_enclave hash/file
  281. if (entry->match_mr_enclave == 1)
  282. {
  283. item = strtok(NULL, ",");
  284. item = clean_start(item);
  285. if (*item != 0)
  286. { // hash
  287. if (get_hash_from_string(item, &(entry->mr_enclave)) == false)
  288. {
  289. return false;
  290. }
  291. item = strtok(NULL, ","); // skip the next item
  292. }
  293. else
  294. { // file
  295. item = strtok(NULL, ",");
  296. item = clean_start(item);
  297. if (get_hash_from_sigstruct_file(item, &(entry->mr_enclave)) == false)
  298. {
  299. return false;
  300. }
  301. }
  302. print_line(false, " mr_enclave: ");
  303. print_byte_array(false, (uint8_t*)&(entry->mr_enclave), sizeof(sgx_measurement_t), " ");
  304. reverse_byte_array((uint8_t*)&(entry->mr_enclave), sizeof(entry->mr_enclave));
  305. }
  306. print_line(false, " Full entry (big endian): ");
  307. print_byte_array(false, (uint8_t*)entry, sizeof(ref_le_white_list_entry_t), " ");
  308. return true;
  309. }
  310. bool CRefWLGen::set_key_and_sign(const char* prikey_file, ref_le_white_list_t *p_wl, uint16_t wl_count, sgx_rsa3072_signature_t* p_signature)
  311. {
  312. rsa_params_t rsa_params;
  313. int key_type;
  314. if (!parse_key_file(prikey_file, &rsa_params, &key_type))
  315. {
  316. return false;
  317. }
  318. rsa_params.e[0] = 3;
  319. sgx_rsa3072_key_t rsa_key;
  320. memcpy(&(rsa_key.mod), &(rsa_params.n), sizeof(rsa_key.mod));
  321. memcpy(&(rsa_key.d), &(rsa_params.d), sizeof(rsa_key.d));
  322. memcpy(&(rsa_key.e), &(rsa_params.e), sizeof(rsa_key.e));
  323. memcpy(&(p_wl->signer_pubkey.mod), &(rsa_params.n), sizeof(p_wl->signer_pubkey.mod));
  324. memcpy(&(p_wl->signer_pubkey.exp), &(rsa_params.e), sizeof(p_wl->signer_pubkey.exp));
  325. print_line(false, " Signer public key modolus: ");
  326. print_byte_array(false, (uint8_t*)&(p_wl->signer_pubkey.mod), sizeof(p_wl->signer_pubkey.mod), " ");
  327. print_line(false, " Signer public key exponent: %d\n", p_wl->signer_pubkey.exp);
  328. reverse_byte_array((uint8_t*)&(p_wl->signer_pubkey.mod), sizeof(p_wl->signer_pubkey.mod));
  329. reverse_byte_array((uint8_t*)&(p_wl->signer_pubkey.exp), sizeof(p_wl->signer_pubkey.exp));
  330. int len = REF_LE_WL_SIZE(wl_count);
  331. sgx_status_t res = sgx_rsa3072_sign((const uint8_t*) p_wl, len, &rsa_key, p_signature);
  332. if (res != SGX_SUCCESS)
  333. {
  334. return false;
  335. }
  336. return true;
  337. }
  338. bool CRefWLGen::generate_wl()
  339. {
  340. print_line(true, "Building white list...\n");
  341. ref_le_white_list_t* wl = (ref_le_white_list_t*)malloc(REF_LE_WL_SIZE(MAX_NUM_OF_RECORDS));
  342. memset(wl, 0, REF_LE_WL_SIZE(MAX_NUM_OF_RECORDS));
  343. // Init values of white-list
  344. print_line(true, "While list format version: %d\n", WL_FILE_VERSION);
  345. wl->version = WL_FILE_VERSION;
  346. print_line(true, "While list instance version: %d\n", m_version);
  347. wl->wl_version = m_version;
  348. // Read configuration information
  349. print_line(true, "Reading configuration file: %s\n", m_cfgfile);
  350. FILE *cfgfile = fopen(m_cfgfile, "r");
  351. if (!cfgfile)
  352. {
  353. print_line(true, "Failed to open the configuration file '%s'.\n", m_cfgfile);
  354. free(wl);
  355. return false;
  356. }
  357. char line[LINE_LEN];
  358. // TODO: what if line len is not enough?
  359. while (fgets(line, LINE_LEN, cfgfile) != NULL)
  360. {
  361. char* start = clean_start(line);
  362. if (*start == 0)
  363. {
  364. continue;
  365. }
  366. print_line(false, "Entry #%d:\n", wl->entries_count);
  367. if (process_line(line, &(wl->wl_entries[wl->entries_count])) == false)
  368. {
  369. print_line(true, "Failed to process the configuration line.\n");
  370. fclose(cfgfile);
  371. free(wl);
  372. return false;
  373. }
  374. wl->entries_count++;
  375. }
  376. fclose(cfgfile);
  377. print_line(false, "Parsed entries count: %d\n", wl->entries_count);
  378. uint16_t wl_count = wl->entries_count;
  379. reverse_byte_array((uint8_t*)&(wl->version), sizeof(wl->version));
  380. reverse_byte_array((uint8_t*)&(wl->wl_version), sizeof(wl->wl_version));
  381. reverse_byte_array((uint8_t*)&(wl->entries_count), sizeof(wl->entries_count));
  382. print_line(true, "Signing using key file: %s\n", m_keyfile);
  383. sgx_rsa3072_signature_t sig;
  384. if (!set_key_and_sign(m_keyfile, wl, wl_count, &sig))
  385. {
  386. free(wl);
  387. return false;
  388. }
  389. reverse_byte_array((uint8_t*)&sig, sizeof(sgx_rsa3072_signature_t));
  390. print_line(false, "Complete white list (big endian): ");
  391. print_byte_array(false, (uint8_t*)wl, REF_LE_WL_SIZE(wl_count), " ");
  392. print_line(false, "Signature (big endian): ");
  393. print_byte_array(false, (uint8_t*)&sig, sizeof(sgx_rsa3072_signature_t), " ");
  394. print_line(true, "Writing output file: %s\n", m_outfile);
  395. FILE *pOut = fopen(m_outfile, "wb");
  396. size_t written = fwrite(wl, 1, REF_LE_WL_SIZE(wl_count), pOut);
  397. written += fwrite(&sig, 1, sizeof(sgx_rsa3072_signature_t), pOut);
  398. fclose(pOut);
  399. free(wl);
  400. if (written != REF_LE_WL_SIZE(wl_count) + sizeof(sgx_rsa3072_signature_t))
  401. {
  402. return false;
  403. }
  404. print_line(true, "White list generation completed successfully. \n");
  405. return true;
  406. }
  407. bool CRefWLGen::run(int argc, char **argv)
  408. {
  409. gen_wl_cmd_t cmd = parse_cmd(argc, argv);
  410. switch (cmd)
  411. {
  412. case GEN_WL:
  413. if (m_cfgfile == NULL || m_outfile == NULL || m_keyfile == NULL)
  414. {
  415. print_line(true, "Mising parameters. \n%s", USAGE);
  416. return false;
  417. }
  418. if (generate_wl() == false)
  419. {
  420. print_line(true, "Failed to generate white-list.\n");
  421. return false;
  422. }
  423. break;
  424. default:
  425. print_line(true, "Command line is not correct. \n%s", USAGE);
  426. return false;
  427. }
  428. return true;
  429. }