systemMain.cpp 3.6 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. class Mitigator : public Php::Base
  23. {
  24. private:
  25. static std::string mitigator_pubkey_header_value;
  26. static std::string mitigator_pubkey_header;
  27. public:
  28. // adding to extension throws an error if this method is not public
  29. static void local_attestation_initiator_wrapper()
  30. {
  31. setbuf(stdout,NULL); uint32_t count;
  32. uint32_t return_sgx;
  33. unsigned char* base64_encoded_mitigator_header_and_value = (unsigned char*) malloc(mitigator_pubkey_header.length()+1+216);
  34. // unsigned char base64_encoded_mitigator_header[229] ; //216=(ceil(160/3) * 4) + 1 (for null character) + 21 for "Mitigator-Public-Key"
  35. memcpy(base64_encoded_mitigator_header_and_value, mitigator_pubkey_header.c_str(), mitigator_pubkey_header.length());
  36. return_sgx = local_attestation_initiator(DECRYPTOR_PORT, base64_encoded_mitigator_header_and_value+mitigator_pubkey_header.length());
  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. printf("length of string was %d\n", mitigator_pubkey_header.length()); fflush(stdout);
  54. // char* base64_encoding = (char*) malloc();
  55. // char base64_encoding[229];
  56. // for(count=0;count<229;count++)
  57. // base64_encoding[count]=base64_encoded_mitigator_header[count];
  58. mitigator_pubkey_header_value=std::string((char*)base64_encoded_mitigator_header_and_value,mitigator_pubkey_header.length()+216);
  59. }
  60. }
  61. Mitigator() = default; // mitigator_pubkey_header("Mitigator-Public-Key:");//, mitigator_pubkey_header_value("!") ;
  62. virtual ~Mitigator() = default;
  63. // call this function in PHP async or call add_header here.
  64. static Php::Value get_mitigator_header()
  65. {
  66. return mitigator_pubkey_header_value;
  67. }
  68. // call this func onRequest
  69. static Php::Value php_decrypt_wrapper(Php::Parameters &params )
  70. {
  71. return params[0];//"miti";
  72. }
  73. };
  74. std::string Mitigator::mitigator_pubkey_header_value=std::string("!");
  75. std::string Mitigator::mitigator_pubkey_header=std::string("Mitigator-Public-Key:");
  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. mitigator.method<&Mitigator::local_attestation_initiator_wrapper>("local_attestation_initiator_wrapper");
  86. mitigator.method<&Mitigator::php_decrypt_wrapper>("php_decrypt_wrapper", { Php::ByVal("string", Php::Type::String) } );
  87. extension.onStartup(&Mitigator::local_attestation_initiator_wrapper);
  88. // extension.onRequest(&Mitigator::php_decrypt_wrapper);
  89. // return the extension module
  90. extension.add(mitigator);
  91. return extension.module();
  92. }
  93. }