systemMain.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. base64_encoded_mitigator_header_and_value = (unsigned char*) malloc( 400 );
  36. // unsigned char base64_encoded_mitigator_header[229] ; //216=(ceil(160/3) * 4) + 1 (for null character) + 21 for "Mitigator-Public-Key"
  37. memcpy(base64_encoded_mitigator_header_and_value, mitigator_pubkey_header.c_str(), mitigator_pubkey_header.length());
  38. return_sgx = local_attestation_initiator(DECRYPTOR_PORT);
  39. if(return_sgx != 0)
  40. {
  41. if(return_sgx== 0xFFFFFFFF)
  42. {
  43. perror("\nCould not set up the socket: had the following error: "); fflush(stderr);
  44. }
  45. else
  46. {
  47. printf("\nHad the following error in SGX local attestation: 0x%x", return_sgx);
  48. fflush(stdout);
  49. }
  50. }
  51. else {
  52. printf("\nSuccessful LA with port %d.\n", DECRYPTOR_PORT);
  53. fflush(stdout);
  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. free(base64_encoded_mitigator_header_and_value);
  64. }
  65. static Php::Value get_mitigator_header()
  66. {
  67. return mitigator_pubkey_header_value;
  68. }
  69. static Php::Value php_decrypt_wrapper(Php::Parameters &params )
  70. {
  71. unsigned char* plaintext_user_data, * base64_client_ciphertext, *base64_client_public_key, *base64_client_data, *client_data;
  72. uint32_t plaintext_length, base64_client_ciphertext_length, base64_client_public_key_length, base64_client_data_length, client_data_length, ret_status, counter;
  73. std::string plaintext_user_data_str;
  74. uint32_t old_client_data_length;
  75. base64_client_ciphertext = (unsigned char*) static_cast<const char*>(params[1]);
  76. base64_client_public_key = (unsigned char*) static_cast<const char*>(params[0]);
  77. base64_client_ciphertext_length = params[1].size();
  78. base64_client_public_key_length = params[0].size();
  79. plaintext_user_data = (unsigned char*) malloc( base64_client_ciphertext_length );
  80. plaintext_length = base64_client_ciphertext_length;
  81. memcpy(plaintext_user_data, base64_client_ciphertext, base64_client_ciphertext_length);
  82. plaintext_user_data_str = std::string((char*) plaintext_user_data, base64_client_ciphertext_length);
  83. printf("Base64 encoded key:\n");
  84. for(counter=0; counter < base64_client_public_key_length; counter++)
  85. printf("%c", base64_client_public_key[counter]);
  86. printf("\n"); fflush(stdout);
  87. printf("Ciphertext received:\n");
  88. for(counter=0; counter < base64_client_ciphertext_length; counter++ )
  89. printf("%c", base64_client_ciphertext[counter]);
  90. printf("\n"); fflush(stdout);
  91. client_data = (unsigned char*) malloc(base64_client_ciphertext_length + base64_client_public_key_length);
  92. ret_status = base64_decoding_wrapper(client_data, base64_client_public_key, base64_client_public_key_length);
  93. if(ret_status<=0)
  94. {
  95. printf("Could not do base64 decoding correctly.\n"); fflush(stdout);
  96. free(plaintext_user_data);
  97. free(client_data);
  98. return plaintext_user_data_str;
  99. }
  100. client_data_length = ret_status;
  101. old_client_data_length=client_data_length;
  102. printf("Client public key:\n");
  103. for(counter=0; counter<client_data_length; counter++)
  104. printf("%d ", client_data[counter]);
  105. ret_status = base64_decoding_wrapper(client_data + client_data_length, base64_client_ciphertext, base64_client_ciphertext_length);
  106. if(ret_status<=0)
  107. {
  108. printf("Could not do base64 decoding correctly.\n"); fflush(stdout);
  109. free(plaintext_user_data);
  110. free(client_data);
  111. return plaintext_user_data_str;
  112. }
  113. client_data_length += ret_status;
  114. printf("\n Ciphertext from client:\n");
  115. for(counter=old_client_data_length; counter<client_data_length; counter++)
  116. printf("%d ", client_data[counter]);
  117. printf("\n"); fflush(stdout);
  118. ret_status=decrypt_client_data_through_decryptor(client_data, client_data_length, (unsigned char*) plaintext_user_data, &plaintext_length);
  119. if(ret_status != 0)
  120. {
  121. printf("Received error code: 0x%02x\n", ret_status); fflush(stdout);
  122. free(client_data);
  123. free(plaintext_user_data);
  124. return plaintext_user_data_str;
  125. }
  126. printf("Going to return this plaintext:\n");
  127. for(counter=0;counter<plaintext_length;counter++)
  128. printf("%c", plaintext_user_data[counter]);
  129. fflush(stdout);
  130. plaintext_user_data_str = std::string((char*) plaintext_user_data, plaintext_length);
  131. free(plaintext_user_data);
  132. free(client_data);
  133. return plaintext_user_data_str;
  134. }
  135. };
  136. std::string Mitigator::mitigator_pubkey_header_value=std::string("!");
  137. std::string Mitigator::mitigator_pubkey_header=std::string("Mitigator-Public-Key:");
  138. extern "C" {
  139. // export the "get_module" function that will be called by the Zend engine
  140. PHPCPP_EXPORT void *get_module()
  141. {
  142. // create extension
  143. static Php::Extension extension("decryptor_la_setup_and_decryption","1.0");
  144. Php::Class<Mitigator> mitigator("Mitigator");
  145. mitigator.method<&Mitigator::get_mitigator_header>("get_mitigator_header");
  146. mitigator.method<&Mitigator::local_attestation_initiator_wrapper>("local_attestation_initiator_wrapper");
  147. mitigator.method<&Mitigator::php_decrypt_wrapper>("php_decrypt_wrapper", { Php::ByVal("string", Php::Type::String), Php::ByVal("string", Php::Type::String) } );
  148. extension.onStartup(&Mitigator::local_attestation_initiator_wrapper);
  149. // return the extension module
  150. extension.add(mitigator);
  151. return extension.module();
  152. }
  153. }