LocalAttestationUntrusted.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #include "LocalAttestationUntrusted.h"
  2. namespace LocalAttestationUntrusted {
  3. namespace {
  4. uint32_t session_id = 0;
  5. protobuf_sgx_dh_msg1_t protobuf_msg1;
  6. uint32_t local_attestation_msg2_msg3(uint32_t own_enclave_id, int accept_fd) {
  7. uint32_t protobuf_sgx_ret;
  8. protobuf_sgx_dh_msg2_t protobuf_msg2;
  9. protobuf_sgx_dh_msg3_t protobuf_msg3;
  10. printf("Writing message 1\n");
  11. fflush(stdout);
  12. if (protobufReadWrite::write_protobuf_msg_to_fd(accept_fd, protobuf_msg1) != 0)
  13. return 0x1;
  14. printf("Reading message 2\n");
  15. fflush(stdout);
  16. if (protobufReadWrite::read_protobuf_msg_from_fd(accept_fd, protobuf_msg2) != 0)
  17. return 0x2;
  18. protobuf_sgx_ret = protobufLAInitiator::process_protobuf_dh_msg2_generate_protobuf_dh_msg3(own_enclave_id,
  19. protobuf_msg2, protobuf_msg3, &session_id);
  20. if (protobuf_sgx_ret != 0) {
  21. printf("Error in generate_protobuf_dh_msg2: 0x%x", protobuf_sgx_ret);
  22. fflush(stdout);
  23. return protobuf_sgx_ret;
  24. }
  25. printf("Writing message 3\n");
  26. fflush(stdout);
  27. if (protobufReadWrite::write_protobuf_msg_to_fd(accept_fd, protobuf_msg3) != 0)
  28. return 0x3;
  29. return 0;
  30. }
  31. }
  32. int decrypt_client_data(uint32_t own_enclave_id, int fd, int time_file_fd) {
  33. extension_to_decryptor_enclosed_msg protobuf_extension_decryptor_msg;
  34. unsigned char *double_ciphertext, *ciphertext;
  35. uint32_t *input_sizes_array, *output_sizes_array;
  36. uint32_t double_ciphertext_length;
  37. uint32_t number_of_double_ciphertext_fields;
  38. uint32_t sgx_ret;
  39. // Get a message of the type decryptor_to_extension msg
  40. if (!protobufReadWrite::read_protobuf_msg_from_fd(fd, protobuf_extension_decryptor_msg)) {
  41. printf("Not all of the extension's message was read\n");
  42. fflush(stdout);
  43. return 0xf3;
  44. }
  45. PostLAProtobufNativeTransforms::get_lengths_for_protobuf_serialized_array(protobuf_extension_decryptor_msg, &double_ciphertext_length,
  46. &number_of_double_ciphertext_fields);
  47. double_ciphertext=(unsigned char*) malloc(double_ciphertext_length);
  48. ciphertext = (unsigned char*) malloc(double_ciphertext_length);
  49. input_sizes_array = (uint32_t*) malloc(number_of_double_ciphertext_fields * sizeof(uint32_t));
  50. output_sizes_array = (uint32_t*) malloc(number_of_double_ciphertext_fields * sizeof(uint32_t));
  51. PostLAProtobufNativeTransforms::create_array_from_protobuf(protobuf_extension_decryptor_msg, double_ciphertext,
  52. input_sizes_array, &number_of_double_ciphertext_fields);
  53. // Call the enclave's decryption function with these arguments and get back another vector of ciphertexts.
  54. Decryptor_process_apache_message_generate_response_wrapper(own_enclave_id, &sgx_ret,
  55. double_ciphertext,
  56. double_ciphertext_length,
  57. input_sizes_array,
  58. number_of_double_ciphertext_fields,
  59. ciphertext,
  60. output_sizes_array);
  61. free(double_ciphertext);
  62. free(input_sizes_array);
  63. // Error checking
  64. if(!sgx_ret)
  65. {
  66. free(ciphertext);
  67. free(output_sizes_array);
  68. return 0x32;
  69. }
  70. // Clear the protobuf msg above and reset it with the output arguments of the ecall.
  71. protobuf_extension_decryptor_msg.clear_ciphertext_client_public_key();
  72. protobuf_extension_decryptor_msg.clear_ciphertext_fields();
  73. PostLAProtobufNativeTransforms::create_protobuf_from_array(ciphertext, output_sizes_array,
  74. number_of_double_ciphertext_fields,
  75. protobuf_extension_decryptor_msg);
  76. free(ciphertext);
  77. free(output_sizes_array);
  78. // write message to apache extension
  79. if (!protobufReadWrite::write_protobuf_msg_to_fd(fd, protobuf_extension_decryptor_msg)) {
  80. printf("Not all of the client's ciphertext data was written to the extension.\n");
  81. fflush(stdout);
  82. return 31;
  83. }
  84. /*
  85. gettimeofday(&tv2, NULL);
  86. new_time=tv2.tv_usec + tv2.tv_sec * 1000000;
  87. old_time=tv1.tv_usec + tv1.tv_sec * 1000000;
  88. bytes_written=sprintf(time_buf, "%lu %lu\n", old_time, new_time);
  89. FileIO::write_to_fd(time_file_fd, time_buf, bytes_written);
  90. */
  91. return 0;
  92. }
  93. int prepare_local_attestation_as_responder_msg1(uint32_t own_enclave_id)
  94. {
  95. uint32_t protobuf_sgx_ret;
  96. protobuf_sgx_ret = protobufLAInitiator::generate_protobuf_dh_msg1(own_enclave_id, protobuf_msg1, &session_id);
  97. if (protobuf_sgx_ret != 0) {
  98. printf("Error in generate_protobuf_dh_msg1: 0x%x", protobuf_sgx_ret);
  99. fflush(stdout);
  100. return protobuf_sgx_ret;
  101. }
  102. return 0;
  103. }
  104. int setup_socket_for_local_attestation_requests(int port) {
  105. struct sockaddr_in own_addr;
  106. return Ipc::set_up_socket(port, &own_addr);
  107. }
  108. // TODO: CHANGED SIGNATURE.
  109. int local_attestation_as_responder_msg2_msg3(uint32_t own_enclave_id, int server_fd,
  110. int *accept_fd) {
  111. uint32_t protobuf_sgx_ret;
  112. struct sockaddr_storage apache_addr;
  113. socklen_t apache_addr_size = sizeof(apache_addr);
  114. int temp_accept_fd;
  115. temp_accept_fd = accept(server_fd, (struct sockaddr *) &apache_addr, &apache_addr_size);
  116. if (temp_accept_fd < 0) {
  117. printf("Error in accepting %d", errno);
  118. fflush(stdout);
  119. return temp_accept_fd;
  120. }
  121. *accept_fd = temp_accept_fd;
  122. protobuf_sgx_ret = local_attestation_msg2_msg3(own_enclave_id, temp_accept_fd);
  123. return protobuf_sgx_ret;
  124. }
  125. int post_local_attestation_with_verifier(uint32_t own_enclave_id, int accept_fd) {
  126. uint32_t protobuf_sgx_ret;
  127. uint8_t encrypted_apache_mrsigner_and_tag[150];
  128. size_t bytes_read;
  129. int count, ret_status;
  130. printf("Here\n");
  131. fflush(stdout);
  132. bytes_read = 60;
  133. ret_status = FileIO::read_from_fd(accept_fd, encrypted_apache_mrsigner_and_tag, &bytes_read);
  134. if (ret_status != 0) {
  135. printf("Not all of the encrypted apache's mrsigner was read from the verifier.\n");
  136. fflush(stdout);
  137. return 0xfe;
  138. }
  139. for (count = 0; count < 60; count++)
  140. printf("0x%02x ", encrypted_apache_mrsigner_and_tag[count]);
  141. printf("\n");
  142. fflush(stdout);
  143. Decryptor_process_verifiers_message_wrapper(own_enclave_id, &protobuf_sgx_ret, encrypted_apache_mrsigner_and_tag,
  144. 60);
  145. if (protobuf_sgx_ret != 0) {
  146. printf("Error in decryption: 0x%x\n", protobuf_sgx_ret);
  147. fflush(stdout);
  148. return protobuf_sgx_ret;
  149. }
  150. printf("Successful decryption\n");
  151. fflush(stdout);
  152. close(accept_fd);
  153. uint8_t output[64];
  154. Decryptor_get_verifier_mrenclave_apache_mrsigner_wrapper(own_enclave_id, output);
  155. uint32_t counter;
  156. for (counter = 0; counter < 32; counter++)
  157. printf("0x%x ", output[counter]);
  158. printf("/n");
  159. for (counter = 32; counter < 64; counter++)
  160. printf("0x%x ", output[counter]);
  161. printf("/n");
  162. fflush(stdout);
  163. return 0;
  164. }
  165. int post_local_attestation_with_apache(uint32_t own_enclave_id, int accept_fd) {
  166. protobuf_post_LA_encrypted_msg_t protobuf_encrypted_msg;
  167. uint8_t encrypted_sign_data_and_sign_and_tag[200]; // 176+12 for IV = 188
  168. uint32_t op_length, internal_return_status, count, sgx_ret;
  169. uint8_t public_key[64];
  170. int time_file_fd;
  171. memset(encrypted_sign_data_and_sign_and_tag, 0x0, 200);
  172. Decryptor_create_and_encrypt_mitigator_header_H_wrapper(own_enclave_id, &sgx_ret,
  173. encrypted_sign_data_and_sign_and_tag, &op_length);
  174. if (sgx_ret != 0) {
  175. printf("Error in generating encrypted mitigator header:0x%x\n", sgx_ret);
  176. fflush(stdout);
  177. return 0xf3;
  178. }
  179. for (count = 0; count < op_length; count++) {
  180. printf("0x%02x ", encrypted_sign_data_and_sign_and_tag[count]);
  181. }
  182. printf("\n");
  183. fflush(stdout);
  184. protobuf_encrypted_msg.set_msg((void *) encrypted_sign_data_and_sign_and_tag, op_length);
  185. if (protobufReadWrite::write_protobuf_msg_to_fd(accept_fd, protobuf_encrypted_msg) != 0) {
  186. printf("Not all of the mitigator token H was written to the Apache.\n");
  187. fflush(stdout);
  188. return 0xfe;
  189. }
  190. Decryptor_get_short_term_public_key_wrapper(own_enclave_id, public_key);
  191. for (count = 0; count < 64; count++)
  192. printf("%02x ", public_key[count]);
  193. printf("\n");
  194. fflush(stdout);
  195. time_file_fd = open("decryptor_time.txt", O_APPEND | O_WRONLY);
  196. do {
  197. internal_return_status = decrypt_client_data(own_enclave_id, accept_fd, time_file_fd);
  198. } while (internal_return_status == 0);
  199. close(accept_fd);
  200. close(time_file_fd);
  201. return internal_return_status;
  202. }
  203. };