systemMain.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include <phpcpp.h>
  2. #include <string.h>
  3. #include <string>
  4. #include <unistd.h>
  5. #include <errno.h>
  6. //#include <fcntl.h>
  7. //#include <sys/types.h>
  8. //#include <sys/stat.h>
  9. #include <stdio.h>
  10. //#include "crypto.h"
  11. #include "ProtobufLAInitiator.h"
  12. using namespace std;
  13. // #include <unistd.h>
  14. // based off the example functionreturnvalue.cpp in PHP-CPP
  15. /**
  16. * Namespace to use
  17. */
  18. //using namespace std;
  19. #define DECRYPTOR_PORT 3825
  20. int local_attestation_successful=0;
  21. int __ImageBase=0;
  22. //extern "C" {
  23. class Mitigator : public Php::Base
  24. {
  25. private:
  26. static std::string mitigator_header;
  27. // int x;
  28. public:
  29. // adding to extension throws an error if this method is not public
  30. static void local_attestation_initiator_wrapper()
  31. {
  32. setbuf(stdout,NULL); uint32_t count;
  33. uint32_t return_sgx;
  34. unsigned char base64_encoded_mitigator_header[229] ; //216=(ceil(160/3) * 4) + 1 (for null character) + 12 for "X-Mitigator:"
  35. memcpy(base64_encoded_mitigator_header, "X-Mitigator:", 12);
  36. return_sgx = local_attestation_initiator(DECRYPTOR_PORT, base64_encoded_mitigator_header+12);
  37. if(return_sgx != 0)
  38. {
  39. if(return_sgx== 0xFFFFFFFF)
  40. {
  41. perror("\nCould not set up the socket: had the following error: "); fflush(stderr);
  42. }
  43. else
  44. {
  45. printf("\nHad the following error in SGX local attestation: 0x%x", return_sgx);
  46. fflush(stdout);
  47. }
  48. }
  49. else {
  50. printf("\nSuccessful LA with port %d.\n", DECRYPTOR_PORT);
  51. fflush(stdout);
  52. local_attestation_successful=1;
  53. char base64_encoding[229];
  54. for(count=0;count<229;count++)
  55. base64_encoding[count]=base64_encoded_mitigator_header[count];
  56. printf("String:%s\nThat string was of length 0x%x\n", base64_encoding, strlen(base64_encoding)); fflush(stdout);
  57. mitigator_header=std::string(base64_encoding,228);
  58. }
  59. }
  60. // std::cout << "MyCustomClass::MyCustomClass()" << std::endl;
  61. Mitigator() = default; // {x=3;} //{ mitigator_header=std::string("Miti: Miti"); }
  62. virtual ~Mitigator() = default;
  63. // call this function in PHP async or call add_header here.
  64. Php::Value get_mitigator_header()
  65. {
  66. return mitigator_header; //Php::Value(mitigator_header, 176);
  67. }
  68. // call this func onRequest
  69. /* Php::Value decrypt_php_wrapper()
  70. {
  71. return "miti";
  72. }
  73. */
  74. };
  75. std::string Mitigator::mitigator_header=std::string("Miti: Miti");
  76. extern "C" {
  77. // export the "get_module" function that will be called by the Zend engine
  78. PHPCPP_EXPORT void *get_module()
  79. {
  80. // create extension
  81. static Php::Extension extension("decryptor_la_setup_and_decryption","1.0");
  82. Php::Class<Mitigator> mitigator("Mitigator");
  83. mitigator.method<&Mitigator::get_mitigator_header>("get_mitigator_header");
  84. // mitigator.property("mitigator_header", &Mitigator::get_mitigator_header);
  85. extension.onStartup(&Mitigator::local_attestation_initiator_wrapper);
  86. // return the extension module
  87. extension.add(mitigator);
  88. return extension.module();
  89. }
  90. }