systemMain.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 "ProtobufLAInitiator.h"
  9. #include "SealerWrapper.h"
  10. #include "crypto.h"
  11. using namespace std;
  12. #define DECRYPTOR_PORT 3825
  13. #define SGX_HASH_SIZE 32
  14. int __ImageBase=0;
  15. int verify_apache(std::string& path, std::string& keypair) {return 0; }
  16. int main(int argc, char** argv)
  17. {
  18. // TODO: Generation of keys used to sign Apache Enclave.
  19. uint8_t expected_apache_mr_signer[SGX_HASH_SIZE] = {0x3};
  20. std::string apache_signature_keypair_private("1234567890");
  21. std::string apache_public_key;
  22. std::string apache_private_key2;
  23. // generate_rsa_key(apache_public_key, apache_private_key2);
  24. // uint8_t decryptor_mr_enclave[SGX_HASH_SIZE] = {0x1};
  25. // uint8_t decryptor_mr_signer[SGX_HASH_SIZE] = {0x2};
  26. uint32_t return_sgx; uint32_t return_internal;
  27. std::string recovered_plaintext;
  28. uint32_t expected_sealed_msg_size=0;
  29. return_internal = generate_rsa_key(apache_public_key, apache_private_key2);
  30. if(return_internal !=0)
  31. {
  32. printf("Could not generate RSA keypair - error 0x%x\n", return_internal); fflush(stdout); return return_internal;
  33. }
  34. printf("Generated key pair - outside the fun\n"); fflush(stdout);
  35. int sealed_file_fd = open("sealed_msg.txt", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
  36. if(sealed_file_fd == -1)
  37. {
  38. perror("\nError in opening or creating the file sealed_msg.txt - ");
  39. fflush(stderr);
  40. return 0xFFFFFFFF;
  41. }
  42. printf("\nSuccessfully opened a file to seal the apache signature keypair to.\n");
  43. fflush(stdout);
  44. return_sgx = seal_message_to_file(sealed_file_fd, apache_signature_keypair_private, &expected_sealed_msg_size);
  45. if(return_sgx!=0 && return_sgx!=0xFFFFFFFF)
  46. {
  47. printf("Sealing SGX error %x", return_sgx);
  48. fflush(stdout);
  49. return return_sgx;
  50. }
  51. else if(return_sgx == 0xFFFFFFFF)
  52. {
  53. perror("Successful SGX sealing, but error in writing to a file or write returned 0 bytes because the disk was full etc.\n");
  54. fflush(stdout);
  55. return return_sgx;
  56. }
  57. printf("\n Successfully sealed the plaintext %s to length 0x%x.\n", apache_signature_keypair_private.c_str(), expected_sealed_msg_size);
  58. fflush(stdout);
  59. return_sgx = local_attestation_initiator(DECRYPTOR_PORT);
  60. if(return_sgx != 0)
  61. {
  62. if(return_sgx== 0xFFFFFFFF)
  63. {
  64. perror("\nCould not set up the socket: had the following error: ");
  65. fflush(stderr);
  66. }
  67. else
  68. {
  69. printf("\nHad the following error in SGX local attestation: 0x%x", return_sgx);
  70. fflush(stdout);
  71. }
  72. return return_sgx;
  73. }
  74. printf("\nSuccessful LA with port %d.\n", DECRYPTOR_PORT);
  75. fflush(stdout);
  76. // sleep(50);
  77. 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");
  78. return_sgx = unseal_message_from_file(sealed_file_fd, recovered_plaintext, &expected_sealed_msg_size);
  79. if(return_sgx!=0 && return_sgx!=0xFFFFFFFF)
  80. {
  81. printf("Successful read from file, but error in SGX unsealing: %x.\n", return_sgx);
  82. fflush(stdout);
  83. return return_sgx;
  84. }
  85. else if(return_sgx == 0xFFFFFFFF)
  86. {
  87. perror("\n Could not read the file.\n");
  88. fflush(stdout);
  89. return return_sgx;
  90. }
  91. printf("\n Unsealed the keypair.\n");
  92. fflush(stdout);
  93. std::string path("../apache/source/code/path");
  94. return_internal = verify_apache(path, apache_signature_keypair_private);
  95. if(return_internal != 0)
  96. {
  97. printf("\nThe signed manifest was not created due to the above errors.\n");
  98. fflush(stdout);
  99. return return_internal;
  100. }
  101. printf("Successfully verified the Apache enclave and signed its manifest.\n");
  102. fflush(stdout);
  103. return 0;
  104. }