LocalAttestationUntrusted.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // Knows only protobuf_sgx objects, protobuf header.
  2. // For socket programming
  3. #include <sys/socket.h>
  4. #include <stdlib.h>
  5. #include <netinet/in.h>
  6. #include <string.h>
  7. #include <errno.h>
  8. #include <unistd.h>
  9. #include <stdio.h>
  10. #include "ProtobufLAMessages.pb.h"
  11. #include <google/protobuf/io/coded_stream.h>
  12. #include <google/protobuf/io/zero_copy_stream_impl.h>
  13. using namespace google::protobuf::io;
  14. #include "protobufLAInitiator.h"
  15. #include "../Decryptor/Decryptor_u.h"
  16. #include <iostream>
  17. class LocalAttestationUntrusted {
  18. int server_fd;
  19. int accept_fd;
  20. uint32_t session_id;
  21. protobuf_sgx_dh_msg1_t protobuf_msg1;
  22. int read_protobuf_msg_from_fd(int accept_fd, google::protobuf::MessageLite& message)
  23. {
  24. ZeroCopyInputStream* raw_input;
  25. CodedInputStream* coded_input;
  26. uint32_t size;
  27. CodedInputStream::Limit limit;
  28. raw_input = new FileInputStream(accept_fd);
  29. coded_input = new CodedInputStream(raw_input);
  30. if(!coded_input->ReadVarint32(&size))
  31. {
  32. printf("Error in reading size of msg");
  33. fflush(stdout);
  34. return -1;
  35. }
  36. //printf("size of msg was read to be %" PRIu32 " \n", size);
  37. fflush(stdout);
  38. limit = coded_input->PushLimit(size);
  39. if(!message.ParseFromCodedStream(coded_input))
  40. {
  41. printf("Error in parsing msg");
  42. fflush(stdout);
  43. return -1;
  44. }
  45. coded_input->PopLimit(limit);
  46. delete raw_input;
  47. delete coded_input;
  48. return 0;
  49. }
  50. int write_protobuf_msg_to_fd(int accept_fd, google::protobuf::MessageLite& message)
  51. {
  52. ZeroCopyOutputStream* raw_output = new FileOutputStream(accept_fd);
  53. CodedOutputStream* coded_output = new CodedOutputStream(raw_output);
  54. coded_output->WriteVarint32(message.ByteSize());
  55. if(!message.SerializeToCodedStream(coded_output))
  56. {
  57. printf("SerializeToCodedStream failed");
  58. fflush(stdout);
  59. return -1;
  60. }
  61. // As per this - https://stackoverflow.com/questions/22881876/protocol-buffers-how-to-serialize-and-deserialize-multiple-messages-into-a-file?noredirect=1&lq=1
  62. // TODO: There may be a better way to do this - 1) this happens with every accept now and 2) make it happen on the stack vs heap - destructor will be called on return from this function (main) and the items will then be written out. (We probably don't want that, actually)
  63. delete coded_output;
  64. delete raw_output;
  65. fflush(stdout);
  66. return 0;
  67. }
  68. // Sets up a socket to bind and listen to the given port. Returns FD of the socket on success, -1 on failure (and prints a msg to stdout with the errno)
  69. int set_up_socket_listen(int port, sockaddr_in* address)
  70. {
  71. int server_fd = 0;
  72. // Creating socket file descriptor for listening for attestation requests.
  73. server_fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
  74. if (server_fd == -1)
  75. {
  76. printf("Error in creating a socket - %d", errno);
  77. return -1;
  78. }
  79. // Preparing the address struct for binding
  80. address->sin_family = AF_INET;
  81. address->sin_addr.s_addr = INADDR_ANY; // Todo: should this be localhost?
  82. address->sin_port = htons(port);
  83. // memset(address->sin_zero,0,sizeof(address->sin_zero));
  84. socklen_t addrlen = sizeof(*address);
  85. // Binding
  86. if (bind(server_fd, (sockaddr*)address, addrlen)<0)
  87. {
  88. printf("Error in binding %d - port was %d - ", errno, port);
  89. return -1;
  90. }
  91. // Listening
  92. if (listen(server_fd, 128) < 0)
  93. {
  94. printf("Error in listening %d", errno);
  95. return -1;
  96. }
  97. return server_fd;
  98. }
  99. uint32_t local_attestation_msg2_msg3(uint32_t own_enclave_id, int accept_fd)
  100. {
  101. uint32_t protobuf_sgx_ret;
  102. protobuf_sgx_dh_msg2_t protobuf_msg2;
  103. protobuf_sgx_dh_msg3_t protobuf_msg3;
  104. if(write_protobuf_msg_to_fd(accept_fd, protobuf_msg1)!=0)
  105. return 0x1;
  106. if(read_protobuf_msg_from_fd(accept_fd, protobuf_msg2)!=0)
  107. return 0x2;
  108. // TODO: Edit function signature in the definition: last argument read_or_write is used to control the flow of the untrusted program:
  109. // no point in doing this as it is untrusted. Have an attribute in its class for it..
  110. protobuf_sgx_ret = process_protobuf_dh_msg2_generate_protobuf_dh_msg3(own_enclave_id, protobuf_msg2, protobuf_msg3, &session_id);
  111. if(protobuf_sgx_ret != 0)
  112. {
  113. printf("Error in generate_protobuf_dh_msg2: 0x%x", protobuf_sgx_ret); fflush(stdout); return protobuf_sgx_ret;
  114. }
  115. if(write_protobuf_msg_to_fd(accept_fd, protobuf_msg3)!=0)
  116. return 0x3;
  117. return 0;
  118. }
  119. int decrypt_client_data_wrapper(uint32_t own_enclave_id, int apache_fd)
  120. {
  121. protobuf_post_LA_encrypted_msg_t protobuf_msg;
  122. unsigned char* protobuf_msg_ptr;
  123. uint32_t sgx_ret_status;
  124. // TODO: THIS SHOULD BE WAYYYY GREATER THAN 144 -> CLIENTS PUBLIC KEY (64)+ ENCRYPTION TAG (16)+ SIGNATURE (64) + CLIENT DATA
  125. // 144 + client data length
  126. uint8_t* input_ciphertext_plus_tag;
  127. uint32_t input_ciphertext_plus_tag_length;
  128. uint8_t* output_ciphertext_plus_tag;
  129. uint32_t output_ciphertext_plus_tag_length;
  130. int counter;
  131. if(read_protobuf_msg_from_fd(apache_fd, protobuf_msg)!=0)
  132. return 0xfe;
  133. input_ciphertext_plus_tag_length = protobuf_msg.msg().length();
  134. // TODO: MAKE SURE THIS IS NOT 0XFFFFFFFF.
  135. input_ciphertext_plus_tag = (uint8_t*) malloc(input_ciphertext_plus_tag_length);
  136. output_ciphertext_plus_tag = (uint8_t*) malloc(input_ciphertext_plus_tag_length - 128 + 10);
  137. protobuf_msg_ptr = (uint8_t*) protobuf_msg.msg().c_str();
  138. for(counter=0; counter<input_ciphertext_plus_tag_length; counter++)
  139. input_ciphertext_plus_tag[counter] = *(protobuf_msg_ptr + counter);
  140. // Just so that the ciphertext - client data - is returned back to Apache in case decrypt_client_data fails.
  141. // client data is after public key (64 bytes) + signature (64 bytes) = 128 bytes.
  142. for(counter=0; counter<input_ciphertext_plus_tag_length; counter++)
  143. output_ciphertext_plus_tag[counter] = input_ciphertext_plus_tag[counter+128];
  144. // We assume that the output is not changed unless it is successful throughout.
  145. // Return value is not sent back..
  146. Decryptor_process_apache_message_generate_response_wrapper(own_enclave_id, &sgx_ret_status, input_ciphertext_plus_tag, input_ciphertext_plus_tag_length, output_ciphertext, &output_ciphertext_plus_tag_length);
  147. free(input_ciphertext_plus_tag);
  148. protobuf_msg.set_msg((void*) output_ciphertext_plus_tag, output_ciphertext_plus_tag_length);
  149. free(output_ciphertext_plus_tag);
  150. if(write_protobuf_msg_to_fd(apache_fd, protobuf_msg)!=0)
  151. return 0xfc;
  152. return 0;
  153. }
  154. public:
  155. int prepare_local_attestation_as_responder_msg1(uint32_t own_enclave_id) //, int port)
  156. {
  157. uint32_t protobuf_sgx_ret;
  158. int temp_server_fd=0;
  159. protobuf_sgx_ret = generate_protobuf_dh_msg1(own_enclave_id, protobuf_msg1, session_id);
  160. if(protobuf_sgx_ret != 0)
  161. {
  162. printf("Error in generate_protobuf_dh_msg1: 0x%x", protobuf_sgx_ret); fflush(stdout); return protobuf_sgx_ret;
  163. }
  164. return 0;
  165. }
  166. int setup_socket_for_local_attestation_requests( int port )
  167. {
  168. struct sockaddr_in own_addr;
  169. return set_up_socket(port, &own_addr);
  170. }
  171. // TODO: CHANGED SIGNATURE.
  172. int local_attestation_as_responder_msg2_msg3(uint32_t own_enclave_id, int server_fd)
  173. {
  174. uint32_t protobuf_sgx_ret;
  175. struct sockaddr_storage apache_addr;
  176. socklen_t apache_addr_size = sizeof(apache_addr);
  177. int temp_accept_fd;
  178. temp_accept_fd = accept(server_fd, (struct sockaddr *)&apache_addr,&apache_addr_size);
  179. if (temp_accept_fd <0)
  180. {
  181. printf("Error in accepting %d", errno); fflush(stdout);
  182. return temp_accept_fd;
  183. }
  184. accept_fd=temp_accept_fd;
  185. protobuf_sgx_ret = local_attestation_msg2_msg3(own_enclave_id, accept_fd);
  186. return protobuf_sgx_ret;
  187. */ return 0; }
  188. int post_local_attestation_with_verifier(uint32_t own_enclave_id)
  189. {
  190. /* uint32_t protobuf_sgx_ret;
  191. uint8_t encrypted_apache_mrsigner_and_tag[48];
  192. size_t bytes_read;
  193. printf("Here\n"); fflush(stdout);
  194. bytes_read=read(accept_fd, encrypted_apache_mrsigner_and_tag, 48);
  195. if(bytes_read_post_la!=48)
  196. {
  197. printf("Not all of the encrypted apache's mrsigner was read from the verifier.\n"); fflush(stdout);
  198. close(accept_fd);
  199. return 0xfe;
  200. }
  201. for(count=0;count<48;count++)
  202. printf("0x%02x ", encrypted_apache_mrsigner_and_tag[count]);
  203. printf("\n");fflush(stdout);
  204. Decryptor_decrypt_verifiers_message_set_apache_mrsigner_wrapper(own_enclave_id, &sgx_ret, encrypted_apache_mrsigner_and_tag);
  205. if(sgx_ret!=0)
  206. {
  207. printf("Error in decryption: 0x%x\n", sgx_ret); fflush(stdout);
  208. close(accept_fd);
  209. return sgx_ret;
  210. }
  211. printf("Successful decryption\n"); fflush(stdout);
  212. close(accept_fd);
  213. */ return 0;
  214. }
  215. int post_local_attestation_with_apache(uint32_t own_enclave_id)
  216. {
  217. /*
  218. protobuf_post_LA_encrypted_msg_t protobuf_encrypted_msg;
  219. uint8_t encrypted_sign_data_and_sign_and_tag[176];
  220. int apache_fd=accept_fd;
  221. memset(encrypted_sign_data_and_sign_and_tag,0x0,176);
  222. uint32_t internal_return_status;
  223. uint32_t count;
  224. sgx_status_t sgx_ret;
  225. Decryptor_create_and_encrypt_mitigator_header_H_wrapper(own_enclave_id, &sgx_ret, encrypted_sign_data_and_sign_and_tag);
  226. if(sgx_ret!=0)
  227. {
  228. printf("Error in generating encrypted mitigator header:0x%x\n", sgx_ret); fflush(stdout);
  229. close(accept_fd);
  230. return 0xf3;
  231. }
  232. for(count=0;count<176;count++)
  233. {
  234. printf("0x%02x ", encrypted_sign_data_and_sign_and_tag[count]);
  235. }
  236. printf("\n"); fflush(stdout);
  237. protobuf_encrypted_msg.set_msg((void*)encrypted_sign_data_and_sign_and_tag, 176);
  238. if(write_protobuf_msg_to_fd(apache_fd, protobuf_encrypted_msg) != 0)
  239. {
  240. printf("Not all of the mitigator token H was written to the Apache.\n"); fflush(stdout);
  241. close(accept_fd);
  242. return 0xfe;
  243. }
  244. do {
  245. internal_return_status = decrypt_client_data_wrapper(own_enclave_id, apache_fd);
  246. } while(internal_return_status==0);
  247. close(accept_fd);
  248. return internal_return_status;
  249. */
  250. return 0; }
  251. };