systemMain.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include <phpcpp.h>
  2. #include <string.h>
  3. #include <string>
  4. #include <unistd.h>
  5. #include <errno.h>
  6. #include<sys/time.h>
  7. #include <fcntl.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <stdio.h>
  11. #include "crypto.h"
  12. #include "ProtobufLAInitiator.h"
  13. using namespace std;
  14. #define DECRYPTOR_PORT 3825
  15. int local_attestation_successful=0;
  16. int __ImageBase=0;
  17. class Mitigator : public Php::Base
  18. {
  19. private:
  20. static std::string mitigator_pubkey_header_value;
  21. static std::string mitigator_pubkey_header;
  22. static int time_file_fd;
  23. public:
  24. Mitigator() = default;
  25. virtual ~Mitigator() = default;
  26. static void local_attestation_initiator_wrapper()
  27. {
  28. setbuf(stdout,NULL);
  29. uint32_t return_sgx, base64_encoded_token_H_length;
  30. unsigned char* base64_encoded_mitigator_header_and_value;
  31. base64_encoded_mitigator_header_and_value = (unsigned char*) malloc( 400 );
  32. // unsigned char base64_encoded_mitigator_header[229] ; //216=(ceil(160/3) * 4) + 1 (for null character) + 21 for "Mitigator-Public-Key"
  33. memcpy(base64_encoded_mitigator_header_and_value, mitigator_pubkey_header.c_str(), mitigator_pubkey_header.length());
  34. return_sgx = local_attestation_initiator(DECRYPTOR_PORT);
  35. if(return_sgx != 0)
  36. {
  37. if(return_sgx== 0xFFFFFFFF)
  38. {
  39. perror("\nCould not set up the socket: had the following error: "); fflush(stderr);
  40. }
  41. else
  42. {
  43. printf("\nHad the following error in SGX local attestation: 0x%x", return_sgx);
  44. fflush(stdout);
  45. }
  46. }
  47. else {
  48. printf("\nSuccessful LA with port %d.\n", DECRYPTOR_PORT);
  49. fflush(stdout);
  50. return_sgx= post_local_attestation_get_mitigator_header(base64_encoded_mitigator_header_and_value + mitigator_pubkey_header.length(),
  51. &base64_encoded_token_H_length);
  52. if(return_sgx != 0)
  53. {
  54. printf("\nHad the following error in SGX POST local attestation: 0x%x", return_sgx);
  55. fflush(stdout);
  56. }
  57. mitigator_pubkey_header_value=std::string((char*)base64_encoded_mitigator_header_and_value,mitigator_pubkey_header.length()+base64_encoded_token_H_length);
  58. }
  59. free(base64_encoded_mitigator_header_and_value);
  60. time_file_fd=open("target_time.txt", O_APPEND | O_WRONLY);
  61. }
  62. static Php::Value get_mitigator_header()
  63. {
  64. return mitigator_pubkey_header_value;
  65. }
  66. static Php::Value php_decrypt_wrapper(Php::Parameters &params )
  67. {
  68. // struct timeval tv1, tv2;
  69. // char time_buf[60] = {0};
  70. // unsigned long int new_time, old_time;
  71. // gettimeofday(&tv1, NULL);
  72. uint32_t ret_status, field_size;
  73. std::vector<std::string> base64_fields, binary_ciphertext_client_fields, plaintext_client_fields;
  74. unsigned char *binary_ciphertext_client_field;
  75. const char* temp_ptr;
  76. Php::Object ret_object;
  77. Php::Value input_base64_array;
  78. ret_object["success"]="false";
  79. if(params.size() < 2 )
  80. {
  81. ret_object["error"]="Need to pass 2 or more arguments.";
  82. return ret_object;
  83. }
  84. input_base64_array = params;
  85. base64_fields = Php::array_values(input_base64_array);
  86. /*
  87. for (auto &base64_field : base64_fields)
  88. {
  89. field_size= base64_field.size();
  90. temp_ptr = base64_field.c_str();
  91. // upper limit - the binary data will always be smaller than this (base64 length ~= 4/3 * binary length)
  92. binary_ciphertext_client_field = (unsigned char*) malloc(field_size);
  93. ret_status = base64_decoding_wrapper(binary_ciphertext_client_field, temp_ptr, field_size);
  94. if(ret_status <= 0)
  95. {
  96. free(binary_ciphertext_client_field);
  97. ret_object["error"]="Could not perform base64 decoding correctly for this field: " + base64_field;
  98. return ret_object;
  99. }
  100. binary_ciphertext_client_fields.push_back(std::string(reinterpret_cast<const char*> (binary_ciphertext_client_field), ret_status));
  101. }
  102. ret_status=decrypt_client_data_through_decryptor(binary_ciphertext_client_fields, plaintext_client_fields);
  103. if(ret_status != 0)
  104. {
  105. ret_object["error"]="Received the following error code when trying to decrypt data thru decryptor " + std::to_string(ret_status);
  106. return ret_object;
  107. }
  108. */
  109. ret_object["success"]="true";
  110. ret_object["fields"]=Php::Array(base64_fields);
  111. //ret_object["fields"]=Php::Array(plaintext_client_fields);
  112. /*gettimeofday(&tv2, NULL);
  113. new_time=tv2.tv_usec + tv2.tv_sec * 1000000;
  114. old_time=tv1.tv_usec + tv1.tv_sec * 1000000;
  115. bytes_written=sprintf(time_buf, "%lu %lu\n", old_time, new_time);
  116. write(time_file_fd, time_buf, bytes_written);
  117. */
  118. return ret_object;
  119. }
  120. };
  121. std::string Mitigator::mitigator_pubkey_header_value=std::string("!");
  122. std::string Mitigator::mitigator_pubkey_header=std::string("Mitigator-Public-Key:");
  123. int Mitigator::time_file_fd=0;
  124. extern "C" {
  125. // export the "get_module" function that will be called by the Zend engine
  126. PHPCPP_EXPORT void *get_module()
  127. {
  128. // create extension
  129. static Php::Extension extension("decryptor_la_setup_and_decryption","1.0");
  130. Php::Class<Mitigator> mitigator("Mitigator");
  131. mitigator.method<&Mitigator::get_mitigator_header>("get_mitigator_header");
  132. mitigator.method<&Mitigator::local_attestation_initiator_wrapper>("local_attestation_initiator_wrapper");
  133. mitigator.method<&Mitigator::php_decrypt_wrapper>("php_decrypt_wrapper", { Php::ByVal("string", Php::Type::String), Php::ByVal("string", Php::Type::String) } );
  134. extension.onStartup(&Mitigator::local_attestation_initiator_wrapper);
  135. // return the extension module
  136. extension.add(mitigator);
  137. return extension.module();
  138. }
  139. }