teems.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 "config.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. size_t sealedprivsize =
  37. sizeof(sgx_sealed_data_t) + sizeof(sgx_ec256_private_t) + 19;
  38. sgx_ec256_public_t pubkey;
  39. sgx_sealed_data_t *sealedprivkey =
  40. (sgx_sealed_data_t *)malloc(sealedprivsize);
  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, sealedprivsize, 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. size_t sealedprivsize =
  83. sizeof(sgx_sealed_data_t) + sizeof(sgx_ec256_private_t) + 19;
  84. sgx_ec256_public_t pubkey;
  85. sgx_sealed_data_t *sealedprivkey =
  86. (sgx_sealed_data_t *)malloc(sealedprivsize);
  87. if (fread(sealedprivkey, sealedprivsize, 1, sprivf) != 1) {
  88. fprintf(stderr, "Could not read sealed privkey file\n");
  89. return false;
  90. }
  91. bool res = ecall_identity_key_load(&pubkey, sealedprivkey);
  92. free(sealedprivkey);
  93. if (!res) {
  94. fprintf(stderr, "Key load failed\n");
  95. return false;
  96. }
  97. printf("Loaded sealed private key\n");
  98. if (pubkeyfile) {
  99. FILE *pubf = fopen(pubkeyfile, "wb");
  100. if (!pubf) {
  101. perror("Pubkey write failed");
  102. return false;
  103. }
  104. if (!hexdump(pubf, NULL, &pubkey, sizeof(pubkey))) {
  105. fclose(pubf);
  106. perror("Pubkey write failed");
  107. return false;
  108. }
  109. if (fclose(pubf)) {
  110. perror("Pubkey write failed");
  111. return false;
  112. }
  113. }
  114. hexdump(stdout, "Pubkey", &pubkey, sizeof(pubkey));
  115. return true;
  116. }
  117. static void usage(const char *argv0)
  118. {
  119. fprintf(stderr, "Usage: %s --gen sealedprivkeyfile pubkeyfile\n",
  120. argv0);
  121. fprintf(stderr, "or %s sealedprivkeyfile myname [args] < config.json\n",
  122. argv0);
  123. }
  124. int main(int argc, char **argv)
  125. {
  126. if (initialize_enclave() < 0) {
  127. return -1;
  128. }
  129. // --gen sealedprivkeyfile pubkeyfile
  130. // If sealedprivkeyfile exists and is valid, regenerate the
  131. // pubkeyfile (overwriting if there was already one there).
  132. // Otherwise, generate and write a new sealedprivkeyfile and
  133. // pubkeyfile (again overwriting if needed).
  134. if (argc > 1 && !strcmp(argv[1], "--gen")) {
  135. if (argc != 4) {
  136. usage(argv[0]);
  137. exit(1);
  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. if (argc < 3) {
  158. usage(argv[0]);
  159. exit(1);
  160. }
  161. const char *sealedprivkeyfile = argv[1];
  162. std::string myname(argv[2]);
  163. // Read the config.json from the first line of stdin. We have to do
  164. // this before outputting anything to avoid potential deadlock with
  165. // the launch program.
  166. std::string configstr;
  167. std::getline(std::cin, configstr);
  168. // Load the sealed private key
  169. FILE *sprivf = fopen(sealedprivkeyfile, "rb");
  170. if (!sprivf) {
  171. perror("Cannot read sealed private key file");
  172. exit(1);
  173. }
  174. if (!loadkey(sprivf, NULL)) {
  175. fprintf(stderr, "Could not load sealed private key\n");
  176. exit(1);
  177. }
  178. fclose(sprivf);
  179. Config config;
  180. if (!config_parse(config, configstr, myname)) {
  181. exit(1);
  182. }
  183. boost::asio::io_context io_context;
  184. // The NetIO will keep a (const) reference to the config
  185. NetIO netio(io_context, config);
  186. // Queue up the actual work
  187. boost::asio::post(io_context, [&]{
  188. start(netio, argc, argv);
  189. });
  190. // Start another thread; one will perform the work and the other
  191. // will execute the async_write handlers
  192. boost::thread t([&]{io_context.run();});
  193. io_context.run();
  194. t.join();
  195. // All done
  196. sgx_destroy_enclave(global_eid);
  197. return 0;
  198. }