teems.cpp 5.8 KB

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