teems.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include "sgx_urts.h"
  5. #include "sgx_tcrypto.h"
  6. #include "sgx_tseal.h"
  7. #include "Untrusted.hpp"
  8. #include "config.hpp"
  9. #include "net.hpp"
  10. static bool hexdump(FILE *outf, const char *label, void *p, size_t len)
  11. {
  12. unsigned char *pc = (unsigned char *)p;
  13. if (label) {
  14. if (fprintf(outf, "%s: ", label) < 0) {
  15. return false;
  16. }
  17. }
  18. for (size_t i=0; i<len; ++i) {
  19. if (fprintf(outf, "%02x", pc[i]) < 0) {
  20. return false;
  21. }
  22. }
  23. if (fprintf(outf, "\n") < 0) {
  24. return false;
  25. }
  26. return true;
  27. }
  28. // Generate a new public/private keypair, and save the sealed private
  29. // key and the public key to the given filenames (overwriting if they're
  30. // already there). Return true on success, false on failure.
  31. static bool genkey(const char *sealedprivkeyfile, const char *pubkeyfile)
  32. {
  33. size_t sealedprivsize =
  34. sizeof(sgx_sealed_data_t) + sizeof(sgx_ec256_private_t) + 19;
  35. sgx_ec256_public_t pubkey;
  36. sgx_sealed_data_t *sealedprivkey =
  37. (sgx_sealed_data_t *)malloc(sealedprivsize);
  38. ecall_identity_key_new(&pubkey, sealedprivkey);
  39. printf("Generating and saving public key and sealed private key\n");
  40. FILE *sprivf = fopen(sealedprivkeyfile, "wb");
  41. if (!sprivf) {
  42. free(sealedprivkey);
  43. perror("Sealed privkey write failed");
  44. return false;
  45. }
  46. if (fwrite(sealedprivkey, sealedprivsize, 1, sprivf) != 1) {
  47. fclose(sprivf);
  48. free(sealedprivkey);
  49. perror("Sealed privkey write failed");
  50. return false;
  51. }
  52. free(sealedprivkey);
  53. if (fclose(sprivf)) {
  54. perror("Sealed privkey write failed");
  55. return false;
  56. }
  57. FILE *pubf = fopen(pubkeyfile, "wb");
  58. if (!pubf) {
  59. perror("Pubkey write failed");
  60. return false;
  61. }
  62. if (!hexdump(pubf, NULL, &pubkey, sizeof(pubkey))) {
  63. fclose(pubf);
  64. perror("Pubkey write failed");
  65. return false;
  66. }
  67. if (fclose(pubf)) {
  68. perror("Pubkey write failed");
  69. return false;
  70. }
  71. hexdump(stdout, "Pubkey", &pubkey, sizeof(pubkey));
  72. return true;
  73. }
  74. // Try to load a sealed private key from sprivf. If successful, save
  75. // the public key to pubkeyfile (if non-NULL), overwriting if already
  76. // present. Return true on success, false on failure.
  77. static bool loadkey(FILE *sprivf, const char *pubkeyfile)
  78. {
  79. size_t sealedprivsize =
  80. sizeof(sgx_sealed_data_t) + sizeof(sgx_ec256_private_t) + 19;
  81. sgx_ec256_public_t pubkey;
  82. sgx_sealed_data_t *sealedprivkey =
  83. (sgx_sealed_data_t *)malloc(sealedprivsize);
  84. if (fread(sealedprivkey, sealedprivsize, 1, sprivf) != 1) {
  85. fprintf(stderr, "Could not read sealed privkey file\n");
  86. return false;
  87. }
  88. bool res = ecall_identity_key_load(&pubkey, sealedprivkey);
  89. free(sealedprivkey);
  90. if (!res) {
  91. fprintf(stderr, "Key load failed\n");
  92. return false;
  93. }
  94. printf("Loaded sealed private key\n");
  95. if (pubkeyfile) {
  96. FILE *pubf = fopen(pubkeyfile, "wb");
  97. if (!pubf) {
  98. perror("Pubkey write failed");
  99. return false;
  100. }
  101. if (!hexdump(pubf, NULL, &pubkey, sizeof(pubkey))) {
  102. fclose(pubf);
  103. perror("Pubkey write failed");
  104. return false;
  105. }
  106. if (fclose(pubf)) {
  107. perror("Pubkey write failed");
  108. return false;
  109. }
  110. }
  111. hexdump(stdout, "Pubkey", &pubkey, sizeof(pubkey));
  112. return true;
  113. }
  114. static void usage(const char *argv0)
  115. {
  116. fprintf(stderr, "Usage: %s --gen sealedprivkeyfile pubkeyfile\n",
  117. argv0);
  118. fprintf(stderr, "or %s sealedprivkeyfile myname [args] < config.json\n",
  119. argv0);
  120. }
  121. int main(int argc, char **argv)
  122. {
  123. if (initialize_enclave() < 0) {
  124. return -1;
  125. }
  126. // --gen sealedprivkeyfile pubkeyfile
  127. // If sealedprivkeyfile exists and is valid, regenerate the
  128. // pubkeyfile (overwriting if there was already one there).
  129. // Otherwise, generate and write a new sealedprivkeyfile and
  130. // pubkeyfile (again overwriting if needed).
  131. if (argc > 1 && !strcmp(argv[1], "--gen")) {
  132. if (argc != 4) {
  133. usage(argv[0]);
  134. exit(1);
  135. }
  136. const char *sealedprivkeyfile = argv[2];
  137. const char *pubkeyfile = argv[3];
  138. FILE *sprivf = fopen(sealedprivkeyfile, "rb");
  139. if (sprivf && loadkey(sprivf, pubkeyfile)) {
  140. // We successfully used an existing sealedprivkeyfile
  141. fclose(sprivf);
  142. return 0;
  143. }
  144. if (sprivf) {
  145. fclose(sprivf);
  146. }
  147. // Generate a new keypair
  148. if (genkey(sealedprivkeyfile, pubkeyfile)) {
  149. return 0;
  150. }
  151. // Something went wrong
  152. exit(1);
  153. }
  154. if (argc < 3) {
  155. usage(argv[0]);
  156. exit(1);
  157. }
  158. const char *sealedprivkeyfile = argv[1];
  159. std::string myname(argv[2]);
  160. // Read the config.json from the first line of stdin. We have to do
  161. // this before outputting anything to avoid potential deadlock with
  162. // the launch program.
  163. std::string config;
  164. std::getline(std::cin, config);
  165. // Load the sealed private key
  166. FILE *sprivf = fopen(sealedprivkeyfile, "rb");
  167. if (!sprivf) {
  168. perror("Cannot read sealed private key file");
  169. exit(1);
  170. }
  171. if (!loadkey(sprivf, NULL)) {
  172. fprintf(stderr, "Could not load sealed private key\n");
  173. exit(1);
  174. }
  175. fclose(sprivf);
  176. if (!config_parse(config, myname)) {
  177. exit(1);
  178. }
  179. if (!net_setup()) {
  180. exit(1);
  181. }
  182. sgx_destroy_enclave(global_eid);
  183. return 0;
  184. }