PostLAMessaging.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // Created by miti on 2019-12-24.
  3. //
  4. #include "PostLAMessages.pb.h"
  5. #include "PostLAMessaging.h"
  6. #include <unistd.h>
  7. #include <stdio.h>
  8. #include "Decryptor_u.h"
  9. int PostLAMessaging::process_target_data_msg(extension_to_decryptor_msg& incomingMsg, decryptor_to_extension_msg& outgoingMsg)
  10. {
  11. const char* client_public_key;
  12. uint8_t* incoming_client_data, *outgoing_client_data; const char *field_string;
  13. uint32_t* input_field_sizes, *output_field_sizes;
  14. std::string client_public_key_string;
  15. uint32_t sgx_ret_status, client_public_key_length, field_counter, byte_counter, field_size, no_of_fields, total_input_length, field_size_accumulator;
  16. uint32_t counter;
  17. // Initialize the client public key
  18. printf("Initialize the client public key\n"); fflush(stdout);
  19. client_public_key_string = incomingMsg.ciphertext_client_public_key();
  20. client_public_key_length = client_public_key_string.length();
  21. // client_public_key = (uint8_t*) malloc(client_public_key_length);
  22. client_public_key = client_public_key_string.c_str();
  23. // Initialize an array of sizes - first element is the length of the public key
  24. // and rest are sizes of each of the incoming ciphertext fields (an input arg. to the ecall).
  25. // Initialize an array to store the sizes of the *outgoing* fields too.
  26. printf("Initialize an array of sizes \n"); fflush(stdout);
  27. no_of_fields = incomingMsg.fields_size() + 1; // extra one for public key.
  28. input_field_sizes = (uint32_t*) malloc(no_of_fields);
  29. output_field_sizes = (uint32_t*) malloc(no_of_fields);
  30. total_input_length = client_public_key_length;
  31. printf("Setting input_field_sizes\n"); fflush(stdout);
  32. input_field_sizes[0] = client_public_key_length;
  33. for(field_counter=1; field_counter<no_of_fields; field_counter++)
  34. {
  35. input_field_sizes[field_counter] = incomingMsg.fields(field_counter-1).field().length();
  36. total_input_length+=input_field_sizes[field_counter];
  37. printf("%d ", input_field_sizes[field_counter]);
  38. }
  39. printf("Setting incoming client data\n"); fflush(stdout);
  40. // Initialize a contiguous array of bytes of the ciphertext fields.
  41. // The number of bytes in any given ciphertext field will be determined by the above array.
  42. // Also initialize an array to store the outgoing bytes for all ciphertext fields.
  43. incoming_client_data = (uint8_t*) malloc(total_input_length);
  44. // Did not tighten the upper bound for the length of the output data as the edl file cant handle functions of input arguments as the sizes.
  45. outgoing_client_data = (uint8_t*) malloc(total_input_length);
  46. printf("Setting public key of length %d\n", client_public_key_length);
  47. for(byte_counter=0; byte_counter<client_public_key_length; byte_counter++)
  48. {
  49. incoming_client_data[byte_counter] = client_public_key[byte_counter];
  50. printf("%02x ", incoming_client_data[byte_counter]);
  51. }
  52. field_size_accumulator=client_public_key_length;
  53. printf("\nSetting the other fields.\n");
  54. for(field_counter=1; field_counter<no_of_fields; field_counter++)
  55. {
  56. field_size = input_field_sizes[field_counter];
  57. printf("%d bytes\n", field_size);
  58. field_string = incomingMsg.fields(field_counter-1).field().c_str();
  59. for(byte_counter=0; byte_counter<field_size; byte_counter++)
  60. {
  61. incoming_client_data[field_size_accumulator + byte_counter] = field_string[byte_counter];
  62. printf("%02x ", incoming_client_data[field_size_accumulator + byte_counter]);
  63. }
  64. printf("\n");
  65. field_size_accumulator += field_size;
  66. }
  67. for(counter=0; counter<total_input_length; counter++)
  68. printf("%02x ", incoming_client_data[counter]);
  69. for(counter=0; counter<no_of_fields; counter++)
  70. printf("%d ", input_field_sizes[counter]);
  71. printf("\nAbout to ecall.\n");
  72. sgx_ret_status=0;
  73. Decryptor_decrypt_client_data_wrapper(enclave_id, &sgx_ret_status, incoming_client_data, input_field_sizes,
  74. no_of_fields, total_input_length, outgoing_client_data, output_field_sizes);
  75. // free(input_field_sizes);
  76. // free(incoming_client_data);
  77. if(sgx_ret_status!=0)
  78. {
  79. printf("Could not process client's data. Had error: %u\n", sgx_ret_status);
  80. free(outgoing_client_data);
  81. free(output_field_sizes);
  82. return sgx_ret_status;
  83. }
  84. printf("returned successfully\n");
  85. // Initialize the outgoing protobuf message using the field sizes in the outgoing_field_size array and the bytes in the outgoing_client_data array.
  86. field_size_accumulator=0;
  87. printf("No of fields: %d\n", no_of_fields - 1);
  88. for(field_counter=0; field_counter<no_of_fields - 1; field_counter++)
  89. {
  90. field_size = output_field_sizes[field_counter];
  91. outgoingMsg.add_fields()->set_field(outgoing_client_data + field_size_accumulator,field_size);
  92. printf("%u bytes\n", field_size);
  93. for(byte_counter=field_size_accumulator; byte_counter<field_size_accumulator + field_size; byte_counter++)
  94. printf("%02x ", outgoing_client_data[byte_counter]);
  95. printf("\n");
  96. field_size_accumulator += field_size;
  97. }
  98. printf("\n");
  99. return 0;
  100. }
  101. void PostLAMessaging::set_enclave_id(int given_id)
  102. {
  103. enclave_id = given_id;
  104. }
  105. int PostLAMessaging::receive_token_from_verifier()
  106. {
  107. uint8_t* token = (uint8_t*) malloc(400);
  108. uint32_t token_length = read(verifier_fd, token, 400);
  109. uint32_t sgx_ret_status;
  110. if(token_length < 0)
  111. {
  112. printf("Could not read verifier's token.\n"); fflush(stdout);
  113. return 0x1;
  114. }
  115. else if(token_length > 400)
  116. {
  117. printf("Could not fit all of verifiers token into the buffer.\n"); fflush(stdout);
  118. return 0x2;
  119. }
  120. Decryptor_process_verifiers_message_wrapper(enclave_id, &sgx_ret_status, token, token_length);
  121. free(token);
  122. return sgx_ret_status;
  123. }
  124. int PostLAMessaging::receive_decrypt_client_data_from_target()
  125. {
  126. extension_to_decryptor_msg incomingMsg;
  127. decryptor_to_extension_msg outgoingMsg;
  128. int ret_status;
  129. printf("Listening for the target's msg.\n"); fflush(stdout);
  130. if( targetChannelData.read_msg(incomingMsg) != 0)
  131. {
  132. printf("Error in protobuf in receiving msg from target.\n"); fflush(stdout);
  133. return 0x1;
  134. }
  135. printf("Obtained a msg from the target. Processing it.\n"); fflush(stdout);
  136. ret_status = process_target_data_msg(incomingMsg, outgoingMsg);
  137. if(ret_status != 0)
  138. {
  139. printf("Error in processing msg from target.\n"); fflush(stdout);
  140. return 0x2;
  141. }
  142. printf("Writing the msg back to the target.\n"); fflush(stdout);
  143. if( targetChannelData.write_msg(outgoingMsg) != 0)
  144. {
  145. printf("Error in protobuf in sending msg to target.\n"); fflush(stdout);
  146. return 0x3;
  147. }
  148. return 0;
  149. }
  150. int PostLAMessaging::read_and_write_header()
  151. {
  152. uint32_t sgx_ret_status, value_length, protobuf_ret_status;
  153. uint8_t* header_value = (uint8_t*) malloc(400);
  154. mitigator_header headerMsg;
  155. if(targetChannelHeaders.read_msg(headerMsg) != 0)
  156. {
  157. printf("Error in reading msg for headers.\n"); fflush(stdout); return 0x1;
  158. }
  159. Decryptor_create_and_encrypt_mitigator_header_H_wrapper(enclave_id, &sgx_ret_status, header_value, &value_length);
  160. if(sgx_ret_status != 0 )
  161. {
  162. printf("Could not create mitigator header.\n");
  163. fflush(stdout);
  164. return sgx_ret_status;
  165. }
  166. uint32_t counter;
  167. for(counter=0;counter<value_length;counter++)
  168. printf("%02x ", header_value[counter]);
  169. printf("\n"); fflush(stdout);
  170. headerMsg.set_name("Mitigator-Public-Key:", 21);
  171. headerMsg.set_value(header_value, value_length);
  172. free(header_value);
  173. protobuf_ret_status = targetChannelHeaders.write_msg(headerMsg);
  174. return protobuf_ret_status;
  175. }
  176. void PostLAMessaging::set_verifier_fd(int fd)
  177. {
  178. verifier_fd = fd;
  179. }
  180. void PostLAMessaging::set_target_data_fd(int fd)
  181. {
  182. targetChannelData.set_fd(fd);
  183. }
  184. void PostLAMessaging::set_target_headers_fd(int fd)
  185. {
  186. targetChannelHeaders.set_fd(fd);
  187. }