teems.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <boost/asio.hpp>
  5. #include <boost/thread.hpp>
  6. #include "sgx_urts.h"
  7. #include "sgx_tcrypto.h"
  8. #include "sgx_tseal.h"
  9. #include "Untrusted.hpp"
  10. #include "appconfig.hpp"
  11. #include "net.hpp"
  12. #include "start.hpp"
  13. static bool hexdump(FILE *outf, const char *label, void *p, size_t len)
  14. {
  15. unsigned char *pc = (unsigned char *)p;
  16. if (label) {
  17. if (fprintf(outf, "%s: ", label) < 0) {
  18. return false;
  19. }
  20. }
  21. for (size_t i=0; i<len; ++i) {
  22. if (fprintf(outf, "%02x", pc[i]) < 0) {
  23. return false;
  24. }
  25. }
  26. if (fprintf(outf, "\n") < 0) {
  27. return false;
  28. }
  29. return true;
  30. }
  31. // Generate a new public/private keypair, and save the sealed private
  32. // key and the public key to the given filenames (overwriting if they're
  33. // already there). Return true on success, false on failure.
  34. static bool genkey(const char *sealedprivkeyfile, const char *pubkeyfile)
  35. {
  36. sgx_ec256_public_t pubkey;
  37. sgx_sealed_data_t *sealedprivkey =
  38. (sgx_sealed_data_t *)malloc(SEALED_PRIVKEY_SIZE);
  39. ecall_identity_key_new(&pubkey, sealedprivkey);
  40. printf("Generating and saving public key and sealed private key\n");
  41. FILE *sprivf = fopen(sealedprivkeyfile, "wb");
  42. if (!sprivf) {
  43. free(sealedprivkey);
  44. perror("Sealed privkey write failed");
  45. return false;
  46. }
  47. if (fwrite(sealedprivkey, SEALED_PRIVKEY_SIZE, 1, sprivf) != 1) {
  48. fclose(sprivf);
  49. free(sealedprivkey);
  50. perror("Sealed privkey write failed");
  51. return false;
  52. }
  53. free(sealedprivkey);
  54. if (fclose(sprivf)) {
  55. perror("Sealed privkey write failed");
  56. return false;
  57. }
  58. FILE *pubf = fopen(pubkeyfile, "wb");
  59. if (!pubf) {
  60. perror("Pubkey write failed");
  61. return false;
  62. }
  63. if (!hexdump(pubf, NULL, &pubkey, sizeof(pubkey))) {
  64. fclose(pubf);
  65. perror("Pubkey write failed");
  66. return false;
  67. }
  68. if (fclose(pubf)) {
  69. perror("Pubkey write failed");
  70. return false;
  71. }
  72. hexdump(stdout, "Pubkey", &pubkey, sizeof(pubkey));
  73. return true;
  74. }
  75. // Try to load a sealed private key from sprivf. If successful, save
  76. // the public key to pubkeyfile (if non-NULL), overwriting if already
  77. // present. Return true on success, false on failure.
  78. static bool loadkey(FILE *sprivf, const char *pubkeyfile)
  79. {
  80. sgx_ec256_public_t pubkey;
  81. sgx_sealed_data_t *sealedprivkey =
  82. (sgx_sealed_data_t *)malloc(SEALED_PRIVKEY_SIZE);
  83. if (fread(sealedprivkey, SEALED_PRIVKEY_SIZE, 1, sprivf) != 1) {
  84. fprintf(stderr, "Could not read sealed privkey file\n");
  85. return false;
  86. }
  87. bool res = ecall_identity_key_load(&pubkey, sealedprivkey);
  88. free(sealedprivkey);
  89. if (!res) {
  90. fprintf(stderr, "Key load failed\n");
  91. return false;
  92. }
  93. printf("Loaded sealed private key\n");
  94. if (pubkeyfile) {
  95. FILE *pubf = fopen(pubkeyfile, "wb");
  96. if (!pubf) {
  97. perror("Pubkey write failed");
  98. return false;
  99. }
  100. if (!hexdump(pubf, NULL, &pubkey, sizeof(pubkey))) {
  101. fclose(pubf);
  102. perror("Pubkey write failed");
  103. return false;
  104. }
  105. if (fclose(pubf)) {
  106. perror("Pubkey write failed");
  107. return false;
  108. }
  109. }
  110. hexdump(stdout, "Pubkey", &pubkey, sizeof(pubkey));
  111. return true;
  112. }
  113. static void usage(const char *argv0)
  114. {
  115. fprintf(stderr, "Usage: %s --gen sealedprivkeyfile pubkeyfile\n",
  116. argv0);
  117. fprintf(stderr, "or %s -k sealedprivkeyfile -n myname "
  118. "[-t nthreads] [-d epoch wait time (in s)] [-e number of epochs] "
  119. "[-w number of Waksman Networks to precompute before starting "
  120. "any epochs] [command] < config.json\n",
  121. argv0);
  122. exit(1);
  123. }
  124. int main(int argc, char **argv)
  125. {
  126. // Unbuffer stdout
  127. setbuf(stdout, NULL);
  128. if (initialize_enclave() < 0) {
  129. return -1;
  130. }
  131. // --gen sealedprivkeyfile pubkeyfile
  132. // If sealedprivkeyfile exists and is valid, regenerate the
  133. // pubkeyfile (overwriting if there was already one there).
  134. // Otherwise, generate and write a new sealedprivkeyfile and
  135. // pubkeyfile (again overwriting if needed).
  136. if (argc > 1 && !strcmp(argv[1], "--gen")) {
  137. if (argc != 4) {
  138. usage(argv[0]);
  139. }
  140. const char *sealedprivkeyfile = argv[2];
  141. const char *pubkeyfile = argv[3];
  142. FILE *sprivf = fopen(sealedprivkeyfile, "rb");
  143. if (sprivf && loadkey(sprivf, pubkeyfile)) {
  144. // We successfully used an existing sealedprivkeyfile
  145. fclose(sprivf);
  146. return 0;
  147. }
  148. if (sprivf) {
  149. fclose(sprivf);
  150. }
  151. // Generate a new keypair
  152. if (genkey(sealedprivkeyfile, pubkeyfile)) {
  153. return 0;
  154. }
  155. // Something went wrong
  156. exit(1);
  157. }
  158. const char *sealedprivkeyfile = NULL;
  159. const char *myname = NULL;
  160. uint16_t nthreads = 1;
  161. const char *progname = argv[0];
  162. ++argv;
  163. // Parse options
  164. while (*argv && (*argv)[0] == '-') {
  165. if (!strcmp(*argv, "--")) {
  166. argv += 1;
  167. break;
  168. } else if (!strcmp(*argv, "-k")) {
  169. if (argv[1] == NULL) {
  170. usage(progname);
  171. }
  172. sealedprivkeyfile = argv[1];
  173. argv += 2;
  174. } else if (!strcmp(*argv, "-n")) {
  175. if (argv[1] == NULL) {
  176. usage(progname);
  177. }
  178. myname = argv[1];
  179. argv += 2;
  180. } else if (!strcmp(*argv, "-t")) {
  181. if (argv[1] == NULL) {
  182. usage(progname);
  183. }
  184. nthreads = uint16_t(atoi(argv[1]));
  185. argv += 2;
  186. } else if (!strcmp(*argv, "-d")) {
  187. if (argv[1] == NULL) {
  188. usage(progname);
  189. }
  190. epoch_wait_time = int(atoi(argv[1]));
  191. argv += 2;
  192. } else if (!strcmp(*argv, "-e")) {
  193. if (argv[1] == NULL) {
  194. usage(progname);
  195. }
  196. num_epochs = int(atoi(argv[1]));
  197. argv += 2;
  198. } else if (!strcmp(*argv, "-w")) {
  199. if (argv[1] == NULL) {
  200. usage(progname);
  201. }
  202. num_WN_to_precompute = int(atoi(argv[1]));
  203. argv += 2;
  204. } else {
  205. usage(progname);
  206. }
  207. }
  208. if (sealedprivkeyfile == NULL || myname == NULL) {
  209. usage(progname);
  210. }
  211. // Read the config.json from the first line of stdin. We have to do
  212. // this before outputting anything to avoid potential deadlock with
  213. // the launch program.
  214. std::string configstr;
  215. std::getline(std::cin, configstr);
  216. // Load the sealed private key
  217. FILE *sprivf = fopen(sealedprivkeyfile, "rb");
  218. if (!sprivf) {
  219. perror("Cannot read sealed private key file");
  220. exit(1);
  221. }
  222. if (!loadkey(sprivf, NULL)) {
  223. fprintf(stderr, "Could not load sealed private key\n");
  224. exit(1);
  225. }
  226. fclose(sprivf);
  227. Config config;
  228. if (!config_parse(config, configstr, myname, nthreads)) {
  229. exit(1);
  230. }
  231. boost::asio::io_context io_context;
  232. // The NetIO will keep a (const) reference to the config
  233. NetIO netio(io_context, config);
  234. g_netio = &netio;
  235. // Queue up the actual work
  236. boost::asio::post(io_context, [&]{
  237. // Start enclave-to-enclave communications
  238. ecall_comms_start([&]{
  239. boost::asio::post(io_context, [&]{
  240. // This runs when we have completed our handshakes with
  241. // all other nodes
  242. printf("Starting\n");
  243. start(netio, argv);
  244. });
  245. });
  246. printf("Reading\n");
  247. for (nodenum_t node_num = 0; node_num < netio.num_nodes; ++node_num) {
  248. if (node_num == netio.me) continue;
  249. NodeIO &node = netio.node(node_num);
  250. node.recv_commands(
  251. // error_cb
  252. [node_num, &config](boost::system::error_code ec) {
  253. printf("Error %s from %d %s\n",
  254. ec.message().c_str(), node_num,
  255. config.nodes[node_num].name.c_str());
  256. },
  257. // epoch_cb
  258. [](uint32_t epoch) {
  259. printf("Epoch %u\n", epoch);
  260. });
  261. }
  262. });
  263. // Start threads to handle the async io handlers
  264. boost::thread t0([&]{io_context.run();});
  265. boost::thread t1([&]{io_context.run();});
  266. io_context.run();
  267. t0.join();
  268. t1.join();
  269. // All done
  270. ecall_close();
  271. g_netio = NULL;
  272. sgx_destroy_enclave(global_eid);
  273. return 0;
  274. }