systemMain.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. Mitigator() = default;
  29. virtual ~Mitigator() = default;
  30. static void local_attestation_initiator_wrapper()
  31. {
  32. setbuf(stdout,NULL);
  33. uint32_t return_sgx, count, base64_encoded_token_H_length;
  34. unsigned char* base64_encoded_mitigator_header_and_value;
  35. // unsigned char base64_encoded_mitigator_header[229] ; //216=(ceil(160/3) * 4) + 1 (for null character) + 21 for "Mitigator-Public-Key"
  36. memcpy(base64_encoded_mitigator_header_and_value, mitigator_pubkey_header.c_str(), mitigator_pubkey_header.length());
  37. return_sgx = local_attestation_initiator(DECRYPTOR_PORT);
  38. if(return_sgx != 0)
  39. {
  40. if(return_sgx== 0xFFFFFFFF)
  41. {
  42. perror("\nCould not set up the socket: had the following error: "); fflush(stderr);
  43. }
  44. else
  45. {
  46. printf("\nHad the following error in SGX local attestation: 0x%x", return_sgx);
  47. fflush(stdout);
  48. }
  49. }
  50. else {
  51. printf("\nSuccessful LA with port %d.\n", DECRYPTOR_PORT);
  52. fflush(stdout);
  53. base64_encoded_mitigator_header_and_value = (unsigned char*) malloc(400);
  54. return_sgx= post_local_attestation_get_mitigator_header(base64_encoded_mitigator_header_and_value + mitigator_pubkey_header.length(),
  55. &base64_encoded_token_H_length);
  56. if(return_sgx != 0)
  57. {
  58. printf("\nHad the following error in SGX POST local attestation: 0x%x", return_sgx);
  59. fflush(stdout);
  60. }
  61. mitigator_pubkey_header_value=std::string((char*)base64_encoded_mitigator_header_and_value,mitigator_pubkey_header.length()+base64_encoded_token_H_length);
  62. }
  63. }
  64. static Php::Value get_mitigator_header()
  65. {
  66. return mitigator_pubkey_header_value;
  67. }
  68. static Php::Value php_decrypt_wrapper(Php::Parameters &params )
  69. {
  70. int counter;
  71. unsigned char* plaintext_user_data, * base64_client_ciphertext, *base64_client_public_key;
  72. uint32_t ciphertext_length, plaintext_length;
  73. base64_client_ciphertext = (unsigned char*) static_cast<const char*>(params[1]);
  74. base64_client_public_key = (unsigned char*) static_cast<const char*>(params[0]);
  75. base64_client_ciphertext_length = params[1].size();
  76. base64_client_public_key_length = params[0].size();
  77. base64_client_data = (unsigned char*) malloc(params[1].size() + params[0].size());
  78. strcpy(base64_client_data, base64_client_public_key, base64_client_public_key_length);
  79. strcpy(base64_client_data + base64_client_public_key_length, base64_client_ciphertext, base64_client_ciphertext_length);
  80. plaintext_user_data = (unsigned char*) malloc(ciphertext_length);
  81. printf("Base64 encoded key:\n");
  82. for(counter=0; counter < base64_client_public_key_length; counter++)
  83. printf("%c", base64_client_data[counter]);
  84. printf("\n"); fflush(stdout);
  85. printf("Ciphertext received:\n");
  86. for(counter=base64_client_public_key_length; counter < base64_client_public_key_length + base64_client_ciphertext_length; counter++ )
  87. printf("%02x", base64_client_data[counter]);
  88. printf("\n"); fflush(stdout);
  89. uint32_t ret_status=decrypt_client_data_through_decryptor(base64_client_data, base64_client_public_key_length + base64_client_ciphertext_length, (unsigned char*) plaintext_user_data, &plaintext_length);
  90. if(ret_status != 0)
  91. {
  92. printf("Received error code: 0x%02x\n", ret_status); fflush(stdout);
  93. free(plaintext_user_data);
  94. free(base64_client_data);
  95. }
  96. printf("Going to return this plaintext:\n");
  97. for(counter=0;counter<plaintext_length;counter++)
  98. printf("0x%02x ", plaintext_user_data[counter]);
  99. fflush(stdout);
  100. std::string plaintext_user_data_str = std::string((char*) plaintext_user_data, plaintext_length);
  101. free(plaintext_user_data); // TODO: For some reason, this causes a crash.
  102. free(base64_client_data);
  103. return plaintext_user_data_str;
  104. }
  105. };
  106. std::string Mitigator::mitigator_pubkey_header_value=std::string("!");
  107. std::string Mitigator::mitigator_pubkey_header=std::string("Mitigator-Public-Key:");
  108. extern "C" {
  109. // export the "get_module" function that will be called by the Zend engine
  110. PHPCPP_EXPORT void *get_module()
  111. {
  112. // create extension
  113. static Php::Extension extension("decryptor_la_setup_and_decryption","1.0");
  114. Php::Class<Mitigator> mitigator("Mitigator");
  115. mitigator.method<&Mitigator::get_mitigator_header>("get_mitigator_header");
  116. mitigator.method<&Mitigator::local_attestation_initiator_wrapper>("local_attestation_initiator_wrapper");
  117. mitigator.method<&Mitigator::php_decrypt_wrapper>("php_decrypt_wrapper", { Php::ByVal("string", Php::Type::String), Php::ByVal("string", Php::Type::String) } );
  118. extension.onStartup(&Mitigator::local_attestation_initiator_wrapper);
  119. // return the extension module
  120. extension.add(mitigator);
  121. return extension.module();
  122. }
  123. }