teems.cpp 5.2 KB

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