systemMain.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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, counter, field_size;
  73. std::vector<std::string> binary_ciphertext_client_fields, plaintext_client_fields;
  74. unsigned char *binary_ciphertext_client_field;
  75. Php::Object ret_object;
  76. Php::Array php_plaintext_client_fields;
  77. const char* temp_ptr;
  78. std::string temp_str;
  79. ret_object["success"]="false";
  80. if(params.size() < 2 )
  81. {
  82. ret_object["error"]="Need to pass 2 or more arguments.";
  83. return ret_object;
  84. }
  85. binary_ciphertext_client_fields.push_back(params[0]);
  86. for (unsigned int i = 0; i < params.size(); i++)
  87. {
  88. field_size=params[i].size();
  89. temp_ptr = params[i];
  90. // upper limit - the binary data will always be smaller than this (base64 length ~= 4/3 * binary length)
  91. binary_ciphertext_client_field = (unsigned char*) malloc(field_size);
  92. ret_status = base64_decoding_wrapper(binary_ciphertext_client_field, temp_ptr, field_size);
  93. if(ret_status <= 0)
  94. {
  95. free(binary_ciphertext_client_field);
  96. ret_object["error"]="Could not perform base64 decoding correctly for the " + std::to_string(i) + "th argument.";
  97. return ret_object;
  98. }
  99. binary_ciphertext_client_fields.push_back(std::string(reinterpret_cast<const char*> (binary_ciphertext_client_field), ret_status));
  100. }
  101. ret_status=decrypt_client_data_through_decryptor(binary_ciphertext_client_fields, plaintext_client_fields);
  102. if(ret_status != 0)
  103. {
  104. ret_object["error"]="Received the following error code when trying to decrypt data thru decryptor " + std::to_string(ret_status);
  105. return ret_object;
  106. }
  107. ret_object["success"]="true";
  108. for(counter=0;counter<plaintext_client_fields.size();counter++)
  109. php_plaintext_client_fields[counter]=plaintext_client_fields[counter];
  110. ret_object["fields"]=php_plaintext_client_fields;
  111. /*gettimeofday(&tv2, NULL);
  112. new_time=tv2.tv_usec + tv2.tv_sec * 1000000;
  113. old_time=tv1.tv_usec + tv1.tv_sec * 1000000;
  114. bytes_written=sprintf(time_buf, "%lu %lu\n", old_time, new_time);
  115. write(time_file_fd, time_buf, bytes_written);
  116. */
  117. return ret_object;
  118. }
  119. };
  120. std::string Mitigator::mitigator_pubkey_header_value=std::string("!");
  121. std::string Mitigator::mitigator_pubkey_header=std::string("Mitigator-Public-Key:");
  122. int Mitigator::time_file_fd=0;
  123. extern "C" {
  124. // export the "get_module" function that will be called by the Zend engine
  125. PHPCPP_EXPORT void *get_module()
  126. {
  127. // create extension
  128. static Php::Extension extension("decryptor_la_setup_and_decryption","1.0");
  129. Php::Class<Mitigator> mitigator("Mitigator");
  130. mitigator.method<&Mitigator::get_mitigator_header>("get_mitigator_header");
  131. mitigator.method<&Mitigator::local_attestation_initiator_wrapper>("local_attestation_initiator_wrapper");
  132. mitigator.method<&Mitigator::php_decrypt_wrapper>("php_decrypt_wrapper", { Php::ByVal("string", Php::Type::String), Php::ByVal("string", Php::Type::String) } );
  133. extension.onStartup(&Mitigator::local_attestation_initiator_wrapper);
  134. // return the extension module
  135. extension.add(mitigator);
  136. return extension.module();
  137. }
  138. }