teems.cpp 5.5 KB

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