teems.cpp 8.0 KB

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