systemMain.cpp 3.9 KB

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