LocalAttestationUntrusted.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. #include "LocalAttestationUntrusted.h"
  18. uint32_t LocalAttestationUntrusted::session_id=0;
  19. protobuf_sgx_dh_msg1_t LocalAttestationUntrusted::protobuf_msg1;
  20. int LocalAttestationUntrusted::read_protobuf_msg_from_fd(int accept_fd, google::protobuf::MessageLite& message)
  21. {
  22. ZeroCopyInputStream* raw_input;
  23. CodedInputStream* coded_input;
  24. uint32_t size;
  25. CodedInputStream::Limit limit;
  26. raw_input = new FileInputStream(accept_fd);
  27. coded_input = new CodedInputStream(raw_input);
  28. if(!coded_input->ReadVarint32(&size))
  29. {
  30. printf("Error in reading size of msg");
  31. fflush(stdout);
  32. return -1;
  33. }
  34. //printf("size of msg was read to be %" PRIu32 " \n", size);
  35. fflush(stdout);
  36. limit = coded_input->PushLimit(size);
  37. if(!message.ParseFromCodedStream(coded_input))
  38. {
  39. printf("Error in parsing msg");
  40. fflush(stdout);
  41. return -1;
  42. }
  43. coded_input->PopLimit(limit);
  44. delete raw_input;
  45. delete coded_input;
  46. return 0;
  47. }
  48. int LocalAttestationUntrusted::write_protobuf_msg_to_fd(int accept_fd, google::protobuf::MessageLite& message)
  49. {
  50. ZeroCopyOutputStream* raw_output = new FileOutputStream(accept_fd);
  51. CodedOutputStream* coded_output = new CodedOutputStream(raw_output);
  52. coded_output->WriteVarint32(message.ByteSize());
  53. if(!message.SerializeToCodedStream(coded_output))
  54. {
  55. printf("SerializeToCodedStream failed");
  56. fflush(stdout);
  57. return -1;
  58. }
  59. // As per this - https://stackoverflow.com/questions/22881876/protocol-buffers-how-to-serialize-and-deserialize-multiple-messages-into-a-file?noredirect=1&lq=1
  60. // 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)
  61. delete coded_output;
  62. delete raw_output;
  63. fflush(stdout);
  64. return 0;
  65. }
  66. // 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)
  67. int LocalAttestationUntrusted::set_up_socket(int port, sockaddr_in* address)
  68. {
  69. int server_fd = 0;
  70. // Creating socket file descriptor for listening for attestation requests.
  71. server_fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
  72. if (server_fd == -1)
  73. {
  74. printf("Error in creating a socket - %d", errno);
  75. return -1;
  76. }
  77. // Preparing the address struct for binding
  78. address->sin_family = AF_INET;
  79. address->sin_addr.s_addr = INADDR_ANY; // Todo: should this be localhost?
  80. address->sin_port = htons(port);
  81. // memset(address->sin_zero,0,sizeof(address->sin_zero));
  82. socklen_t addrlen = sizeof(*address);
  83. // Binding
  84. if (bind(server_fd, (sockaddr*)address, addrlen)<0)
  85. {
  86. printf("Error in binding %d - port was %d - ", errno, port);
  87. return -1;
  88. }
  89. // Listening
  90. if (listen(server_fd, 128) < 0)
  91. {
  92. printf("Error in listening %d", errno);
  93. return -1;
  94. }
  95. return server_fd;
  96. }
  97. uint32_t LocalAttestationUntrusted::local_attestation_msg2_msg3(uint32_t own_enclave_id, int accept_fd)
  98. {
  99. uint32_t protobuf_sgx_ret;
  100. protobuf_sgx_dh_msg2_t protobuf_msg2;
  101. protobuf_sgx_dh_msg3_t protobuf_msg3;
  102. printf("Writing message 1\n"); fflush(stdout);
  103. if(write_protobuf_msg_to_fd(accept_fd, protobuf_msg1)!=0)
  104. return 0x1;
  105. printf("Reading message 2\n"); fflush(stdout);
  106. if(read_protobuf_msg_from_fd(accept_fd, protobuf_msg2)!=0)
  107. return 0x2;
  108. protobuf_sgx_ret = process_protobuf_dh_msg2_generate_protobuf_dh_msg3(own_enclave_id, protobuf_msg2, protobuf_msg3, &LocalAttestationUntrusted::session_id);
  109. if(protobuf_sgx_ret != 0)
  110. {
  111. printf("Error in generate_protobuf_dh_msg2: 0x%x", protobuf_sgx_ret); fflush(stdout); return protobuf_sgx_ret;
  112. }
  113. printf("Writing message 3\n"); fflush(stdout);
  114. if(write_protobuf_msg_to_fd(accept_fd, protobuf_msg3)!=0)
  115. return 0x3;
  116. return 0;
  117. }
  118. int LocalAttestationUntrusted::decrypt_client_data(uint32_t own_enclave_id, int fd)
  119. {
  120. protobuf_post_LA_encrypted_msg_t protobuf_msg;
  121. unsigned char* protobuf_msg_ptr;
  122. uint32_t sgx_ret_status=0;
  123. uint8_t* input_ciphertext_plus_tag;
  124. uint32_t input_ciphertext_plus_tag_length;
  125. uint8_t* output_ciphertext_plus_tag;
  126. uint32_t output_ciphertext_plus_tag_length;
  127. int counter;
  128. if(read_protobuf_msg_from_fd(fd, protobuf_msg)!=0)
  129. {
  130. printf("Could not read apache's message post-local attestation\n"); fflush(stdout);
  131. return 0xfe;
  132. }
  133. printf("Clients data\n"); fflush(stdout);
  134. input_ciphertext_plus_tag_length = protobuf_msg.msg().length();
  135. // TODO: MAKE SURE THIS IS NOT 0XFFFFFFFF.
  136. input_ciphertext_plus_tag = (uint8_t*) malloc(1000); //malloc(input_ciphertext_plus_tag_length);
  137. output_ciphertext_plus_tag = (uint8_t*) malloc(1000); //malloc(input_ciphertext_plus_tag_length); //128 = client public key token length?
  138. protobuf_msg_ptr = (uint8_t*) protobuf_msg.msg().c_str();
  139. for(counter=0; counter<input_ciphertext_plus_tag_length; counter++)
  140. {
  141. input_ciphertext_plus_tag[counter] = *(protobuf_msg_ptr + counter);
  142. printf("0x%02x ", input_ciphertext_plus_tag[counter]);
  143. }
  144. // Just so that the ciphertext - client data - is returned back to Apache in case this function fails.
  145. // client data is after public key (64 bytes) + signature (64 bytes) = 128 bytes.
  146. for(counter=0; counter<input_ciphertext_plus_tag_length-64; counter++)
  147. output_ciphertext_plus_tag[counter] = input_ciphertext_plus_tag[counter+64];
  148. output_ciphertext_plus_tag_length=input_ciphertext_plus_tag_length - 64;
  149. protobuf_msg.set_msg((void*) output_ciphertext_plus_tag, output_ciphertext_plus_tag_length);
  150. // We assume that the output is not changed unless it is successful throughout.
  151. Decryptor_process_apache_message_generate_response_wrapper(own_enclave_id, &sgx_ret_status, input_ciphertext_plus_tag, input_ciphertext_plus_tag_length, output_ciphertext_plus_tag, &output_ciphertext_plus_tag_length);
  152. free(input_ciphertext_plus_tag);
  153. if(sgx_ret_status==0)
  154. {
  155. protobuf_msg.set_msg((void*) output_ciphertext_plus_tag, output_ciphertext_plus_tag_length);
  156. }
  157. else
  158. printf("\n0x%02x\n", sgx_ret_status);
  159. printf("Returning this: \n");
  160. for(counter=0;counter<output_ciphertext_plus_tag_length;counter++)
  161. printf("%02x ",output_ciphertext_plus_tag[counter]);
  162. printf("\n");
  163. free(output_ciphertext_plus_tag);
  164. if(write_protobuf_msg_to_fd(fd, protobuf_msg)!=0)
  165. return 0xfc;
  166. return 0;
  167. }
  168. int LocalAttestationUntrusted::prepare_local_attestation_as_responder_msg1(uint32_t own_enclave_id) //, int port)
  169. {
  170. uint32_t protobuf_sgx_ret;
  171. int temp_server_fd=0;
  172. protobuf_sgx_ret = generate_protobuf_dh_msg1(own_enclave_id, protobuf_msg1, &LocalAttestationUntrusted::session_id);
  173. if(protobuf_sgx_ret != 0)
  174. {
  175. printf("Error in generate_protobuf_dh_msg1: 0x%x", protobuf_sgx_ret); fflush(stdout); return protobuf_sgx_ret;
  176. }
  177. return 0;
  178. }
  179. int LocalAttestationUntrusted::setup_socket_for_local_attestation_requests(int port)
  180. {
  181. struct sockaddr_in own_addr;
  182. return set_up_socket(port, &own_addr);
  183. }
  184. // TODO: CHANGED SIGNATURE.
  185. int LocalAttestationUntrusted::local_attestation_as_responder_msg2_msg3(uint32_t own_enclave_id, int server_fd, int* accept_fd)
  186. {
  187. uint32_t protobuf_sgx_ret;
  188. struct sockaddr_storage apache_addr;
  189. socklen_t apache_addr_size = sizeof(apache_addr);
  190. int temp_accept_fd;
  191. temp_accept_fd = accept(server_fd, (struct sockaddr *)&apache_addr,&apache_addr_size);
  192. if (temp_accept_fd <0)
  193. {
  194. printf("Error in accepting %d", errno); fflush(stdout);
  195. return temp_accept_fd;
  196. }
  197. *accept_fd=temp_accept_fd;
  198. protobuf_sgx_ret = local_attestation_msg2_msg3(own_enclave_id, temp_accept_fd);
  199. return protobuf_sgx_ret;
  200. }
  201. int LocalAttestationUntrusted::post_local_attestation_with_verifier(uint32_t own_enclave_id, int accept_fd)
  202. {
  203. uint32_t protobuf_sgx_ret;
  204. uint8_t encrypted_apache_mrsigner_and_tag[48];
  205. size_t bytes_read;
  206. int count;
  207. printf("Here\n"); fflush(stdout);
  208. bytes_read=read(accept_fd, encrypted_apache_mrsigner_and_tag, 48);
  209. if(bytes_read!=48)
  210. {
  211. printf("Not all of the encrypted apache's mrsigner was read from the verifier.\n"); fflush(stdout);
  212. close(accept_fd);
  213. return 0xfe;
  214. }
  215. for(count=0;count<48;count++)
  216. printf("0x%02x ", encrypted_apache_mrsigner_and_tag[count]);
  217. printf("\n");fflush(stdout);
  218. Decryptor_process_verifiers_message_wrapper(own_enclave_id, &protobuf_sgx_ret, encrypted_apache_mrsigner_and_tag);
  219. if(protobuf_sgx_ret!=0)
  220. {
  221. printf("Error in decryption: 0x%x\n", protobuf_sgx_ret); fflush(stdout);
  222. close(accept_fd);
  223. return protobuf_sgx_ret;
  224. }
  225. printf("Successful decryption\n"); fflush(stdout);
  226. close(accept_fd);
  227. uint8_t output[64];
  228. Decryptor_get_verifier_mrenclave_apache_mrsigner_wrapper(own_enclave_id, output);
  229. uint32_t counter;
  230. for(counter=0; counter<32; counter++)
  231. printf("0x%x ", output[counter]);
  232. printf("/n");
  233. for(counter=32; counter<64; counter++)
  234. printf("0x%x ", output[counter]);
  235. printf("/n");
  236. fflush(stdout);
  237. return 0;
  238. }
  239. int LocalAttestationUntrusted::post_local_attestation_with_apache(uint32_t own_enclave_id, int accept_fd)
  240. {
  241. protobuf_post_LA_encrypted_msg_t protobuf_encrypted_msg;
  242. uint8_t encrypted_sign_data_and_sign_and_tag[176];
  243. memset(encrypted_sign_data_and_sign_and_tag,0x0,176);
  244. uint32_t internal_return_status;
  245. uint32_t count;
  246. uint32_t sgx_ret;
  247. Decryptor_create_and_encrypt_mitigator_header_H_wrapper(own_enclave_id, &sgx_ret, encrypted_sign_data_and_sign_and_tag);
  248. if(sgx_ret!=0)
  249. {
  250. printf("Error in generating encrypted mitigator header:0x%x\n", sgx_ret); fflush(stdout);
  251. close(accept_fd);
  252. return 0xf3;
  253. }
  254. for(count=0;count<176;count++)
  255. {
  256. printf("0x%02x ", encrypted_sign_data_and_sign_and_tag[count]);
  257. }
  258. printf("\n"); fflush(stdout);
  259. protobuf_encrypted_msg.set_msg((void*)encrypted_sign_data_and_sign_and_tag, 176);
  260. if(write_protobuf_msg_to_fd(accept_fd, protobuf_encrypted_msg) != 0)
  261. {
  262. printf("Not all of the mitigator token H was written to the Apache.\n"); fflush(stdout);
  263. close(accept_fd);
  264. return 0xfe;
  265. }
  266. uint8_t public_key[64];
  267. Decryptor_get_short_term_public_key_wrapper(own_enclave_id, public_key);
  268. for(count=0;count<64;count++)
  269. printf("%02x ",public_key[count]);
  270. printf("\n"); fflush(stdout);
  271. do {
  272. internal_return_status = decrypt_client_data(own_enclave_id, accept_fd);
  273. } while(internal_return_status==0);
  274. close(accept_fd);
  275. return internal_return_status;
  276. return 0; }