PostLAMessaging.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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;
  13. uint32_t* input_field_sizes, *output_field_sizes;
  14. std::string client_public_key_string, field_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. // Initialize the client public key
  17. client_public_key_string = incomingMsg.ciphertext_client_public_key();
  18. client_public_key_length = client_public_key_string.length();
  19. // client_public_key = (uint8_t*) malloc(client_public_key_length);
  20. client_public_key = client_public_key_string.c_str();
  21. // Initialize an array of sizes - first element is the length of the public key
  22. // and rest are sizes of each of the incoming ciphertext fields (an input arg. to the ecall).
  23. // Initialize an array to store the sizes of the *outgoing* fields too.
  24. no_of_fields = incomingMsg.fields_size();
  25. input_field_sizes = (uint32_t*) malloc(no_of_fields + 1); // extra one for public key.
  26. output_field_sizes = (uint32_t*) malloc(no_of_fields + 1); // Made them the same as the edl file doesnt support fns of input args as sizes.
  27. total_input_length = client_public_key_length;
  28. input_field_sizes[0] = client_public_key_length;
  29. for(field_counter=1; field_counter<no_of_fields+1; field_counter++)
  30. {
  31. input_field_sizes[field_counter] = incomingMsg.fields(field_counter-1).field().length();
  32. total_input_length+=input_field_sizes[field_counter];
  33. }
  34. // Initialize a contiguous array of bytes of the ciphertext fields.
  35. // The number of bytes in any given ciphertext field will be determined by the above array.
  36. // Also initialize an array to store the outgoing bytes for all ciphertext fields.
  37. incoming_client_data = (uint8_t*) malloc(total_input_length);
  38. // 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.
  39. outgoing_client_data = (uint8_t*) malloc(total_input_length);
  40. for(byte_counter=0; byte_counter<client_public_key_length; byte_counter++)
  41. {
  42. incoming_client_data[byte_counter] = client_public_key[byte_counter];
  43. }
  44. field_size_accumulator=client_public_key_length;
  45. for(field_counter=0; field_counter<no_of_fields; field_counter++)
  46. {
  47. field_size = input_field_sizes[field_counter];
  48. field_string = incomingMsg.fields(field_counter).field().c_str();
  49. for(byte_counter=0; byte_counter<field_size; byte_counter++)
  50. {
  51. incoming_client_data[field_size_accumulator + byte_counter] = field_string[byte_counter];
  52. }
  53. field_size_accumulator += field_size;
  54. }
  55. sgx_ret_status=0;
  56. Decryptor_decrypt_client_data_wrapper(enclave_id, &sgx_ret_status, incoming_client_data, input_field_sizes,
  57. no_of_fields, total_input_length, outgoing_client_data, output_field_sizes);
  58. free(input_field_sizes);
  59. free(incoming_client_data);
  60. if(sgx_ret_status!=0)
  61. {
  62. printf("Could not process client's data. Had error: %ud\n", sgx_ret_status);
  63. free(outgoing_client_data);
  64. free(output_field_sizes);
  65. return sgx_ret_status;
  66. }
  67. // Initialize the outgoing protobuf message using the field sizes in the outgoing_field_size array and the bytes in the outgoing_client_data array.
  68. field_size_accumulator=0;
  69. for(field_counter=0; field_counter<no_of_fields; field_counter++)
  70. {
  71. field_size = output_field_sizes[field_counter];
  72. outgoingMsg.add_fields()->set_field(outgoing_client_data + field_size_accumulator,field_size);
  73. field_size_accumulator += field_size;
  74. }
  75. free(outgoing_client_data);
  76. free(output_field_sizes);
  77. return 0;
  78. }
  79. void PostLAMessaging::set_enclave_id(int given_id)
  80. {
  81. enclave_id = given_id;
  82. }
  83. int PostLAMessaging::receive_token_from_verifier()
  84. {
  85. uint8_t* token = (uint8_t*) malloc(400);
  86. uint32_t token_length = read(verifier_fd, token, 400);
  87. uint32_t sgx_ret_status;
  88. if(token_length < 0)
  89. {
  90. printf("Could not read verifier's token.\n"); fflush(stdout);
  91. return 0x1;
  92. }
  93. else if(token_length > 400)
  94. {
  95. printf("Could not fit all of verifiers token into the buffer.\n"); fflush(stdout);
  96. return 0x2;
  97. }
  98. Decryptor_process_verifiers_message_wrapper(enclave_id, &sgx_ret_status, token, token_length);
  99. free(token);
  100. return sgx_ret_status;
  101. }
  102. int PostLAMessaging::receive_decrypt_client_data_from_target()
  103. {
  104. extension_to_decryptor_msg incomingMsg;
  105. decryptor_to_extension_msg outgoingMsg;
  106. int ret_status;
  107. if( targetChannelData.read_msg(incomingMsg) != 0)
  108. {
  109. printf("Error in protobuf in receiving msg from target.\n"); fflush(stdout);
  110. return 0x1;
  111. }
  112. ret_status = process_target_data_msg(incomingMsg, outgoingMsg);
  113. if(ret_status != 0)
  114. {
  115. printf("Error in processing msg from target.\n"); fflush(stdout);
  116. return 0x2;
  117. }
  118. if( targetChannelData.write_msg(outgoingMsg) != 0)
  119. {
  120. printf("Error in protobuf in sending msg to target.\n"); fflush(stdout);
  121. return 0x3;
  122. }
  123. return 0;
  124. }
  125. int PostLAMessaging::read_and_write_header()
  126. {
  127. uint32_t sgx_ret_status, value_length, protobuf_ret_status;
  128. uint8_t* header_value = (uint8_t*) malloc(400);
  129. mitigator_header headerMsg;
  130. if(targetChannelHeaders.read_msg(headerMsg) != 0)
  131. {
  132. printf("Error in reading msg for headers.\n"); fflush(stdout); return 0x1;
  133. }
  134. Decryptor_create_and_encrypt_mitigator_header_H_wrapper(enclave_id, &sgx_ret_status, header_value, &value_length);
  135. if(sgx_ret_status != 0 )
  136. {
  137. printf("Could not create mitigator header.\n");
  138. fflush(stdout);
  139. return sgx_ret_status;
  140. }
  141. uint32_t counter;
  142. for(counter=0;counter<value_length;counter++)
  143. printf("%02x ", header_value[counter]);
  144. printf("\n"); fflush(stdout);
  145. headerMsg.set_name("Mitigator-Public-Key:", 21);
  146. headerMsg.set_value(header_value, value_length);
  147. free(header_value);
  148. protobuf_ret_status = targetChannelHeaders.write_msg(headerMsg);
  149. return protobuf_ret_status;
  150. }
  151. void PostLAMessaging::set_verifier_fd(int fd)
  152. {
  153. verifier_fd = fd;
  154. }
  155. void PostLAMessaging::set_target_data_fd(int fd)
  156. {
  157. targetChannelData.set_fd(fd);
  158. }
  159. void PostLAMessaging::set_target_headers_fd(int fd)
  160. {
  161. targetChannelHeaders.set_fd(fd);
  162. }