systemMain.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include <string.h>
  2. #include <string>
  3. #include <unistd.h>
  4. #include <errno.h>
  5. #include <fcntl.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include "ipc.h"
  9. #include "SgxLAInititator.h"
  10. #include "SealerWrapper.h"
  11. #include "SgxCrypto.h"
  12. using namespace std;
  13. #define DECRYPTOR_PORT 3825
  14. #define SGX_HASH_SIZE 32
  15. int __ImageBase=0;
  16. int verify_apache(std::string& path, std::string& keypair) {return 0; }
  17. int local_attestation(int port, uint8_t* mr_enclave, uint8_t* mr_signer, uint8_t* send_mr_signer)
  18. {
  19. int decryptor_fd;
  20. setbuf(stdout,NULL);
  21. decryptor_fd=set_up_socket_connect(port);
  22. if(decryptor_fd!=-1)
  23. {
  24. printf("Set up a socket with the other module\n");
  25. return create_session(decryptor_fd, mr_enclave, mr_signer, send_mr_signer);
  26. }
  27. else
  28. return 0xFFFFFFFF;
  29. }
  30. int main(int argc, char** argv)
  31. {
  32. // TODO: Generation of keys used to sign Apache Enclave.
  33. uint8_t expected_apache_mr_signer[SGX_HASH_SIZE] = {0x3};
  34. std::string apache_signature_keypair_private("1234567890");
  35. std::string apache_public_key;
  36. uint8_t decryptor_mr_enclave[SGX_HASH_SIZE] = {0x1};
  37. uint8_t decryptor_mr_signer[SGX_HASH_SIZE] = {0x2};
  38. uint32_t return_sgx; uint32_t return_internal;
  39. std::string recovered_plaintext;
  40. uint32_t expected_sealed_msg_size=0;
  41. /*
  42. printf("about to generate rsa key pair\n");
  43. fflush(stdout);
  44. return_sgx=create_rsa_key_pair_for_signing_manifest();
  45. if(return_sgx!=0)
  46. {
  47. printf("Could not successfully create rsa key pair.\n");
  48. fflush(stdout);
  49. return 0xFFFFFFFF;
  50. }
  51. printf("generated rsa key pair\n");
  52. fflush(stdout); */
  53. int sealed_file_fd = open("sealed_msg.txt", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
  54. if(sealed_file_fd == -1)
  55. {
  56. perror("\nError in opening or creating the file sealed_msg.txt - ");
  57. fflush(stderr);
  58. return 0xFFFFFFFF;
  59. }
  60. printf("\nSuccessfully opened a file to seal the apache signature keypair to.\n");
  61. fflush(stdout);
  62. return_sgx = seal_message_to_file(sealed_file_fd, apache_signature_keypair_private, &expected_sealed_msg_size);
  63. if(return_sgx!=0 && return_sgx!=0xFFFFFFFF)
  64. {
  65. printf("Sealing SGX error %x", return_sgx);
  66. fflush(stdout);
  67. return return_sgx;
  68. }
  69. else if(return_sgx == 0xFFFFFFFF)
  70. {
  71. perror("Successful SGX sealing, but error in writing to a file or write returned 0 bytes because the disk was full etc.\n");
  72. fflush(stdout);
  73. return return_sgx;
  74. }
  75. printf("\n Successfully sealed the plaintext %s to length 0x%x.\n", apache_signature_keypair_private.c_str(), expected_sealed_msg_size);
  76. fflush(stdout);
  77. return_sgx = local_attestation(DECRYPTOR_PORT, decryptor_mr_enclave, decryptor_mr_signer, expected_apache_mr_signer);
  78. if(return_sgx != 0)
  79. {
  80. if(return_sgx== 0xFFFFFFFF)
  81. {
  82. perror("\nCould not set up the socket: had the following error: ");
  83. fflush(stderr);
  84. }
  85. else
  86. {
  87. printf("\nHad the following error in SGX local attestation: %d", return_sgx);
  88. fflush(stdout);
  89. }
  90. return return_sgx;
  91. }
  92. printf("\nSuccessful LA with port %d.\n", DECRYPTOR_PORT);
  93. fflush(stdout);
  94. sleep(50);
  95. printf("\n z z z z z z z z z z z z z (sleeping for a bit) z z z z z z z z (meant to emulate the '2nd' stage of validator, that will be rerun whenever Apache changes)\n");
  96. return_sgx = unseal_message_from_file(sealed_file_fd, recovered_plaintext, &expected_sealed_msg_size);
  97. if(return_sgx!=0 && return_sgx!=0xFFFFFFFF)
  98. {
  99. printf("Successful read from file, but error in SGX unsealing: %x.\n", return_sgx);
  100. fflush(stdout);
  101. return return_sgx;
  102. }
  103. else if(return_sgx == 0xFFFFFFFF)
  104. {
  105. perror("\n Could not read the file.\n");
  106. fflush(stdout);
  107. return return_sgx;
  108. }
  109. printf("\n Unsealed the keypair.\n");
  110. fflush(stdout);
  111. std::string path("../apache/source/code/path");
  112. return_internal = verify_apache(path, apache_signature_keypair_private);
  113. if(return_internal != 0)
  114. {
  115. printf("\nThe signed manifest was not created due to the above errors.\n");
  116. fflush(stdout);
  117. return return_internal;
  118. }
  119. printf("Successfully verified the Apache enclave and signed its manifest.\n");
  120. fflush(stdout);
  121. return 0;
  122. }