LocalAttestationUntrusted.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 = process_protobuf_dh_msg2_generate_protobuf_dh_msg3(own_enclave_id, protobuf_msg2, protobuf_msg3,
  19. &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. /*
  33. void get_lengths_for_protobuf_serialized_array(extension_to_decryptor_enclosed_msg &protobuf_ext_to_decryptor,
  34. uint32_t *output_lengths)
  35. {
  36. uint32_t number_of_ciphertext_fields, counter, total_length;
  37. // Didn't use bytesize() or bytesizelong() for getting the lengths of the public key or the ciphertext string
  38. // as that gives the *serialized* length of the message which should be an upper-bound.
  39. // Can switch to that if necessary for time performance reasons.
  40. total_length=protobuf_ext_to_decryptor.ciphertext_client_public_key().length();
  41. number_of_ciphertext_fields=protobuf_ext_to_decryptor.ciphertext_fields_size();
  42. for(counter=0; counter<number_of_ciphertext_fields; counter++)
  43. total_length+=protobuf_ext_to_decryptor.ciphertext_fields(counter).field().length();
  44. output_lengths[0]=total_length;
  45. output_lengths[1]=number_of_ciphertext_fields;
  46. }
  47. void create_array_from_protobuf(extension_to_decryptor_enclosed_msg &protobuf_ext_to_decryptor,
  48. unsigned char* double_ciphertext_client_data, uint32_t* sizes_array, uint32_t* sizes_array_length)
  49. {
  50. uint32_t counter, size_of_field, number_of_fields;
  51. unsigned char* ptr;
  52. // Set the size of the first element - the public key - and copy it to the output array.
  53. sizes_array[0] = protobuf_ext_to_decryptor.ciphertext_client_public_key().length();
  54. ptr=strncpy((char*)double_ciphertext_client_data, protobuf_ext_to_decryptor.ciphertext_client_public_key().c_str(),
  55. size_of_field);
  56. // Start copying past the length copied above, copy all ciphertext fields to the output string array
  57. // and set their lengths in the output integers array
  58. number_of_fields=protobuf_ext_to_decryptor.ciphertext_fields_size();
  59. for(counter=0;counter<number_of_fields;counter++)
  60. {
  61. // First element of the LHS array is the length of the client's public key.
  62. sizes_array[counter+1] = protobuf_ext_to_decryptor.ciphertext_fields(counter).field().length();
  63. ptr = strncpy((char*)ptr, protobuf_ext_to_decryptor.ciphertext_fields(counter).field().c_str(), size_of_field);
  64. }
  65. sizes_array_length=number_of_fields+1;
  66. }
  67. void create_protobuf_from_array( unsigned char* ciphertext_client_data, uint32_t* sizes_array, uint32_t sizes_array_length,
  68. extension_to_decryptor_enclosed_msg protobuf_extension_decryptor_msg)
  69. {
  70. uint32_t counter;
  71. void* ptr;
  72. // Note that we don't care about setting the client public key as we don't include that in the outgoing message
  73. // to the extension.
  74. ptr=ciphertext_client_data;
  75. for(counter=0; counter<sizes_array_length; counter++)
  76. {
  77. protobuf_extension_decryptor_msg.mutable_ciphertext_fields(counter).set_field(ptr, sizes_array[counter]);
  78. ptr+=sizes_array[counter];
  79. }
  80. }
  81. */
  82. uint32_t get_decrypted_client_data(uint8_t* array, uint32_t array_length)
  83. {
  84. return 0;
  85. }
  86. int decrypt_client_data(uint32_t own_enclave_id, int fd, int time_file_fd) {
  87. /* extension_to_decryptor_enclosed_msg protobuf_extension_decryptor_msg;
  88. unsigned char* double_ciphertext_client_data, ciphertext_client_data;
  89. uint32_t* input_sizes_array, output_sizes_array;
  90. uint32_t ecall_input_lengths[2];
  91. uint32_t ecall_output_lengths[2];
  92. uint32_t sgx_ret;
  93. // Get a message of the type decryptor_to_extension msg
  94. if (!protobufReadWrite::read_protobuf_msg_from_fd(fd, protobuf_extension_decryptor_msg)) {
  95. printf("Not all of the extension's message was read\n");
  96. fflush(stdout);
  97. return 0xf3;
  98. }
  99. get_length_of_protobuf_serialized_array(protobuf_extension_decryptor_msg, lengths);
  100. double_ciphertext_client_data=(unsigned char*) malloc(ecall_input_lengths[0]);
  101. ciphertext_client_data = (unsigned char*) malloc(ecall_input_lengths[0]);
  102. input_sizes_array = (uint32_t*) malloc(ecall_input_lengths[1] * sizeof(uint32_t));
  103. create_array_from_protobuf(protobuf_extension_decryptor_msg, double_ciphertext_client_data,
  104. input_sizes_array, ecall_input_lengths[1]);
  105. // Call the enclave's decryption function with these arguments and get back another vector of ciphertexts.
  106. Decryptor_decrypt_client_data_wrapper(own_enclave_id, &sgx_ret,
  107. double_ciphertext_client_data,
  108. ecall_input_lengths[0],
  109. input_sizes_array,
  110. ecall_input_lengths[1]);
  111. free(double_ciphertext_client_data);
  112. free(input_sizes_array);
  113. // Error checking
  114. if(!sgx_ret)
  115. {
  116. free(ciphertext_client_data);
  117. free(output_sizes_array);
  118. return 0x32;
  119. }
  120. output_sizes_array = (uint32_t *) malloc(ecall_input_lengths[1] * sizeof(uint32_t)); // Upper bound: it should be lengths[1]-1.
  121. // Clear the protobuf msg above and reset it with the output arguments of the ecall.
  122. protobuf_extension_decryptor_msg.clear_ciphertext_client_public_key();
  123. protobuf_extension_decryptor_msg.clear_ciphertext_fields();
  124. create_protobuf_from_array(ciphertext_client_data, output_sizes_array, ecall_output_lengths[1],
  125. protobuf_extension_decryptor_msg);
  126. free(ciphertext_client_data);
  127. free(output_sizes_array);
  128. // write message to apache extension
  129. if (!protobufReadWrite::write_protobuf_msg_to_fd(fd, protobuf_extension_decryptor_msg)) {
  130. printf("Not all of the client's ciphertext data was written to the extension.\n");
  131. fflush(stdout);
  132. return 31;
  133. }
  134. /*
  135. gettimeofday(&tv2, NULL);
  136. new_time=tv2.tv_usec + tv2.tv_sec * 1000000;
  137. old_time=tv1.tv_usec + tv1.tv_sec * 1000000;
  138. bytes_written=sprintf(time_buf, "%lu %lu\n", old_time, new_time);
  139. FileIO::write_to_fd(time_file_fd, time_buf, bytes_written);
  140. */
  141. return 0;
  142. }
  143. int prepare_local_attestation_as_responder_msg1(uint32_t own_enclave_id)
  144. {
  145. uint32_t protobuf_sgx_ret;
  146. protobuf_sgx_ret = generate_protobuf_dh_msg1(own_enclave_id, protobuf_msg1, &session_id);
  147. if (protobuf_sgx_ret != 0) {
  148. printf("Error in generate_protobuf_dh_msg1: 0x%x", protobuf_sgx_ret);
  149. fflush(stdout);
  150. return protobuf_sgx_ret;
  151. }
  152. return 0;
  153. }
  154. int setup_socket_for_local_attestation_requests(int port) {
  155. struct sockaddr_in own_addr;
  156. return Ipc::set_up_socket(port, &own_addr);
  157. }
  158. // TODO: CHANGED SIGNATURE.
  159. int local_attestation_as_responder_msg2_msg3(uint32_t own_enclave_id, int server_fd,
  160. int *accept_fd) {
  161. uint32_t protobuf_sgx_ret;
  162. struct sockaddr_storage apache_addr;
  163. socklen_t apache_addr_size = sizeof(apache_addr);
  164. int temp_accept_fd;
  165. temp_accept_fd = accept(server_fd, (struct sockaddr *) &apache_addr, &apache_addr_size);
  166. if (temp_accept_fd < 0) {
  167. printf("Error in accepting %d", errno);
  168. fflush(stdout);
  169. return temp_accept_fd;
  170. }
  171. *accept_fd = temp_accept_fd;
  172. protobuf_sgx_ret = local_attestation_msg2_msg3(own_enclave_id, temp_accept_fd);
  173. return protobuf_sgx_ret;
  174. }
  175. int post_local_attestation_with_verifier(uint32_t own_enclave_id, int accept_fd) {
  176. uint32_t protobuf_sgx_ret;
  177. uint8_t encrypted_apache_mrsigner_and_tag[150];
  178. size_t bytes_read;
  179. int count;
  180. printf("Here\n");
  181. fflush(stdout);
  182. bytes_read = FileIO::read_from_fd(accept_fd, encrypted_apache_mrsigner_and_tag, 60);
  183. if (bytes_read != 60) {
  184. printf("Not all of the encrypted apache's mrsigner was read from the verifier.\n");
  185. fflush(stdout);
  186. return 0xfe;
  187. }
  188. for (count = 0; count < 60; count++)
  189. printf("0x%02x ", encrypted_apache_mrsigner_and_tag[count]);
  190. printf("\n");
  191. fflush(stdout);
  192. Decryptor_process_verifiers_message_wrapper(own_enclave_id, &protobuf_sgx_ret, encrypted_apache_mrsigner_and_tag,
  193. 60);
  194. if (protobuf_sgx_ret != 0) {
  195. printf("Error in decryption: 0x%x\n", protobuf_sgx_ret);
  196. fflush(stdout);
  197. return protobuf_sgx_ret;
  198. }
  199. printf("Successful decryption\n");
  200. fflush(stdout);
  201. close(accept_fd);
  202. uint8_t output[64];
  203. Decryptor_get_verifier_mrenclave_apache_mrsigner_wrapper(own_enclave_id, output);
  204. uint32_t counter;
  205. for (counter = 0; counter < 32; counter++)
  206. printf("0x%x ", output[counter]);
  207. printf("/n");
  208. for (counter = 32; counter < 64; counter++)
  209. printf("0x%x ", output[counter]);
  210. printf("/n");
  211. fflush(stdout);
  212. return 0;
  213. }
  214. int post_local_attestation_with_apache(uint32_t own_enclave_id, int accept_fd) {
  215. protobuf_post_LA_encrypted_msg_t protobuf_encrypted_msg;
  216. uint8_t encrypted_sign_data_and_sign_and_tag[200]; // 176+12 for IV = 188
  217. uint32_t op_length, internal_return_status, count, sgx_ret;
  218. uint8_t public_key[64];
  219. int time_file_fd;
  220. memset(encrypted_sign_data_and_sign_and_tag, 0x0, 200);
  221. Decryptor_create_and_encrypt_mitigator_header_H_wrapper(own_enclave_id, &sgx_ret,
  222. encrypted_sign_data_and_sign_and_tag, &op_length);
  223. if (sgx_ret != 0) {
  224. printf("Error in generating encrypted mitigator header:0x%x\n", sgx_ret);
  225. fflush(stdout);
  226. return 0xf3;
  227. }
  228. for (count = 0; count < op_length; count++) {
  229. printf("0x%02x ", encrypted_sign_data_and_sign_and_tag[count]);
  230. }
  231. printf("\n");
  232. fflush(stdout);
  233. protobuf_encrypted_msg.set_msg((void *) encrypted_sign_data_and_sign_and_tag, op_length);
  234. if (protobufReadWrite::write_protobuf_msg_to_fd(accept_fd, protobuf_encrypted_msg) != 0) {
  235. printf("Not all of the mitigator token H was written to the Apache.\n");
  236. fflush(stdout);
  237. return 0xfe;
  238. }
  239. Decryptor_get_short_term_public_key_wrapper(own_enclave_id, public_key);
  240. for (count = 0; count < 64; count++)
  241. printf("%02x ", public_key[count]);
  242. printf("\n");
  243. fflush(stdout);
  244. /*
  245. uint8_t *output_ciphertext_plus_tag = (uint8_t *) malloc(
  246. 4100); // 12 bytes for ciphertext iv + 16 bytes for ciphertext tag = 28 byte
  247. uint8_t *input_ciphertext_plus_tag = (uint8_t *) malloc(4100);
  248. */
  249. time_file_fd = open("decryptor_time.txt", O_APPEND | O_WRONLY);
  250. do {
  251. internal_return_status = decrypt_client_data(own_enclave_id, accept_fd, time_file_fd);
  252. } while (internal_return_status == 0);
  253. close(accept_fd);
  254. close(time_file_fd);
  255. return internal_return_status;
  256. }
  257. };