teems.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 sealedprivkeyfile myname [args] < config.json\n",
  118. argv0);
  119. }
  120. int main(int argc, char **argv)
  121. {
  122. // Unbuffer stdout
  123. setbuf(stdout, NULL);
  124. if (initialize_enclave() < 0) {
  125. return -1;
  126. }
  127. // --gen sealedprivkeyfile pubkeyfile
  128. // If sealedprivkeyfile exists and is valid, regenerate the
  129. // pubkeyfile (overwriting if there was already one there).
  130. // Otherwise, generate and write a new sealedprivkeyfile and
  131. // pubkeyfile (again overwriting if needed).
  132. if (argc > 1 && !strcmp(argv[1], "--gen")) {
  133. if (argc != 4) {
  134. usage(argv[0]);
  135. exit(1);
  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. if (argc < 3) {
  156. usage(argv[0]);
  157. exit(1);
  158. }
  159. const char *sealedprivkeyfile = argv[1];
  160. std::string myname(argv[2]);
  161. // Read the config.json from the first line of stdin. We have to do
  162. // this before outputting anything to avoid potential deadlock with
  163. // the launch program.
  164. std::string configstr;
  165. std::getline(std::cin, configstr);
  166. // Load the sealed private key
  167. FILE *sprivf = fopen(sealedprivkeyfile, "rb");
  168. if (!sprivf) {
  169. perror("Cannot read sealed private key file");
  170. exit(1);
  171. }
  172. if (!loadkey(sprivf, NULL)) {
  173. fprintf(stderr, "Could not load sealed private key\n");
  174. exit(1);
  175. }
  176. fclose(sprivf);
  177. Config config;
  178. if (!config_parse(config, configstr, myname)) {
  179. exit(1);
  180. }
  181. boost::asio::io_context io_context;
  182. // The NetIO will keep a (const) reference to the config
  183. NetIO netio(io_context, config);
  184. // Queue up the actual work
  185. boost::asio::post(io_context, [&]{
  186. start(netio, argc, argv);
  187. });
  188. // Start another thread; one will perform the work and the other
  189. // will execute the async_write handlers
  190. boost::thread t([&]{io_context.run();});
  191. io_context.run();
  192. t.join();
  193. // All done
  194. sgx_destroy_enclave(global_eid);
  195. return 0;
  196. }