#include #include #include #include #include //#include //#include //#include #include //#include "crypto.h" #include "ProtobufLAInitiator.h" using namespace std; // #include // based off the example functionreturnvalue.cpp in PHP-CPP /** * Namespace to use */ //using namespace std; #define DECRYPTOR_PORT 3825 int local_attestation_successful=0; int __ImageBase=0; class Mitigator : public Php::Base { private: static std::string mitigator_pubkey_header_value; static std::string mitigator_pubkey_header; public: Mitigator() = default; virtual ~Mitigator() = default; static void local_attestation_initiator_wrapper() { setbuf(stdout,NULL); uint32_t return_sgx, count, base64_encoded_token_H_length; unsigned char* base64_encoded_mitigator_header_and_value; // unsigned char base64_encoded_mitigator_header[229] ; //216=(ceil(160/3) * 4) + 1 (for null character) + 21 for "Mitigator-Public-Key" memcpy(base64_encoded_mitigator_header_and_value, mitigator_pubkey_header.c_str(), mitigator_pubkey_header.length()); return_sgx = local_attestation_initiator(DECRYPTOR_PORT); if(return_sgx != 0) { if(return_sgx== 0xFFFFFFFF) { perror("\nCould not set up the socket: had the following error: "); fflush(stderr); } else { printf("\nHad the following error in SGX local attestation: 0x%x", return_sgx); fflush(stdout); } } else { printf("\nSuccessful LA with port %d.\n", DECRYPTOR_PORT); fflush(stdout); base64_encoded_mitigator_header_and_value = (unsigned char*) malloc(400); return_sgx= post_local_attestation_get_mitigator_header(base64_encoded_mitigator_header_and_value + mitigator_pubkey_header.length(), &base64_encoded_token_H_length); if(return_sgx != 0) { printf("\nHad the following error in SGX POST local attestation: 0x%x", return_sgx); fflush(stdout); } mitigator_pubkey_header_value=std::string((char*)base64_encoded_mitigator_header_and_value,mitigator_pubkey_header.length()+base64_encoded_token_H_length); } } static Php::Value get_mitigator_header() { return mitigator_pubkey_header_value; } static Php::Value php_decrypt_wrapper(Php::Parameters ¶ms ) { int counter; unsigned char* plaintext_user_data, * base64_client_ciphertext, *base64_client_public_key; uint32_t ciphertext_length, plaintext_length; base64_client_ciphertext = (unsigned char*) static_cast(params[1]); base64_client_public_key = (unsigned char*) static_cast(params[0]); base64_client_ciphertext_length = params[1].size(); base64_client_public_key_length = params[0].size(); base64_client_data = (unsigned char*) malloc(params[1].size() + params[0].size()); strcpy(base64_client_data, base64_client_public_key, base64_client_public_key_length); strcpy(base64_client_data + base64_client_public_key_length, base64_client_ciphertext, base64_client_ciphertext_length); plaintext_user_data = (unsigned char*) malloc(ciphertext_length); printf("Base64 encoded key:\n"); for(counter=0; counter < base64_client_public_key_length; counter++) printf("%c", base64_client_data[counter]); printf("\n"); fflush(stdout); printf("Ciphertext received:\n"); for(counter=base64_client_public_key_length; counter < base64_client_public_key_length + base64_client_ciphertext_length; counter++ ) printf("%02x", base64_client_data[counter]); printf("\n"); fflush(stdout); 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); if(ret_status != 0) { printf("Received error code: 0x%02x\n", ret_status); fflush(stdout); free(plaintext_user_data); free(base64_client_data); } printf("Going to return this plaintext:\n"); for(counter=0;counter mitigator("Mitigator"); mitigator.method<&Mitigator::get_mitigator_header>("get_mitigator_header"); mitigator.method<&Mitigator::local_attestation_initiator_wrapper>("local_attestation_initiator_wrapper"); mitigator.method<&Mitigator::php_decrypt_wrapper>("php_decrypt_wrapper", { Php::ByVal("string", Php::Type::String), Php::ByVal("string", Php::Type::String) } ); extension.onStartup(&Mitigator::local_attestation_initiator_wrapper); // return the extension module extension.add(mitigator); return extension.module(); } }