teems.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 [-t nthreads] [-d epoch_duration (in s)] [-e number of epochs] [-w number of Waksman Networks to precompute before starting any epochs] [command] < config.json\n",
  118. argv0);
  119. exit(1);
  120. }
  121. int main(int argc, char **argv)
  122. {
  123. // Unbuffer stdout
  124. setbuf(stdout, NULL);
  125. if (initialize_enclave() < 0) {
  126. return -1;
  127. }
  128. // --gen sealedprivkeyfile pubkeyfile
  129. // If sealedprivkeyfile exists and is valid, regenerate the
  130. // pubkeyfile (overwriting if there was already one there).
  131. // Otherwise, generate and write a new sealedprivkeyfile and
  132. // pubkeyfile (again overwriting if needed).
  133. if (argc > 1 && !strcmp(argv[1], "--gen")) {
  134. if (argc != 4) {
  135. usage(argv[0]);
  136. }
  137. const char *sealedprivkeyfile = argv[2];
  138. const char *pubkeyfile = argv[3];
  139. FILE *sprivf = fopen(sealedprivkeyfile, "rb");
  140. if (sprivf && loadkey(sprivf, pubkeyfile)) {
  141. // We successfully used an existing sealedprivkeyfile
  142. fclose(sprivf);
  143. return 0;
  144. }
  145. if (sprivf) {
  146. fclose(sprivf);
  147. }
  148. // Generate a new keypair
  149. if (genkey(sealedprivkeyfile, pubkeyfile)) {
  150. return 0;
  151. }
  152. // Something went wrong
  153. exit(1);
  154. }
  155. const char *sealedprivkeyfile = NULL;
  156. const char *myname = NULL;
  157. uint16_t nthreads = 1;
  158. const char *progname = argv[0];
  159. ++argv;
  160. // Parse options
  161. while (*argv && (*argv)[0] == '-') {
  162. if (!strcmp(*argv, "--")) {
  163. argv += 1;
  164. break;
  165. } else if (!strcmp(*argv, "-k")) {
  166. if (argv[1] == NULL) {
  167. usage(progname);
  168. }
  169. sealedprivkeyfile = argv[1];
  170. argv += 2;
  171. } else if (!strcmp(*argv, "-n")) {
  172. if (argv[1] == NULL) {
  173. usage(progname);
  174. }
  175. myname = argv[1];
  176. argv += 2;
  177. } else if (!strcmp(*argv, "-t")) {
  178. if (argv[1] == NULL) {
  179. usage(progname);
  180. }
  181. nthreads = uint16_t(atoi(argv[1]));
  182. argv += 2;
  183. } else if (!strcmp(*argv, "-d")) {
  184. if (argv[1] == NULL) {
  185. usage(progname);
  186. }
  187. epoch_duration = int(atoi(argv[1]));
  188. argv += 2;
  189. } else if (!strcmp(*argv, "-e")) {
  190. if (argv[1] == NULL) {
  191. usage(progname);
  192. }
  193. num_epochs = int(atoi(argv[1]));
  194. argv += 2;
  195. } else if (!strcmp(*argv, "-w")) {
  196. if (argv[1] == NULL) {
  197. usage(progname);
  198. }
  199. num_WN_to_precompute = int(atoi(argv[1]));
  200. argv += 2;
  201. } else {
  202. usage(progname);
  203. }
  204. }
  205. if (sealedprivkeyfile == NULL || myname == NULL) {
  206. usage(progname);
  207. }
  208. // Read the config.json from the first line of stdin. We have to do
  209. // this before outputting anything to avoid potential deadlock with
  210. // the launch program.
  211. std::string configstr;
  212. std::getline(std::cin, configstr);
  213. // Load the sealed private key
  214. FILE *sprivf = fopen(sealedprivkeyfile, "rb");
  215. if (!sprivf) {
  216. perror("Cannot read sealed private key file");
  217. exit(1);
  218. }
  219. if (!loadkey(sprivf, NULL)) {
  220. fprintf(stderr, "Could not load sealed private key\n");
  221. exit(1);
  222. }
  223. fclose(sprivf);
  224. Config config;
  225. if (!config_parse(config, configstr, myname, nthreads)) {
  226. exit(1);
  227. }
  228. boost::asio::io_context io_context;
  229. // The NetIO will keep a (const) reference to the config
  230. NetIO netio(io_context, config);
  231. g_netio = &netio;
  232. // Queue up the actual work
  233. boost::asio::post(io_context, [&]{
  234. // Start enclave-to-enclave communications
  235. ecall_comms_start([&]{
  236. boost::asio::post(io_context, [&]{
  237. // This runs when we have completed our handshakes with
  238. // all other nodes
  239. printf("Starting\n");
  240. start(netio, argv);
  241. });
  242. });
  243. printf("Reading\n");
  244. for (nodenum_t node_num = 0; node_num < netio.num_nodes; ++node_num) {
  245. if (node_num == netio.me) continue;
  246. NodeIO &node = netio.node(node_num);
  247. node.recv_commands(
  248. // error_cb
  249. [node_num, &config](boost::system::error_code ec) {
  250. printf("Error %s from %d %s\n", ec.message().c_str(), node_num, config.nodes[node_num].name.c_str());
  251. },
  252. // epoch_cb
  253. [](uint32_t epoch) {
  254. printf("Epoch %u\n", epoch);
  255. });
  256. }
  257. });
  258. // Start another thread; one will perform the work and the other
  259. // will execute the async_write handlers
  260. boost::thread t([&]{io_context.run();});
  261. io_context.run();
  262. t.join();
  263. // All done
  264. ecall_close();
  265. g_netio = NULL;
  266. sgx_destroy_enclave(global_eid);
  267. return 0;
  268. }