systemLA.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 "dhmsgs.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. // TODO: Make these private functions
  17. int read_protobuf_msg_from_fd(int accept_fd, google::protobuf::MessageLite& message)
  18. {
  19. ZeroCopyInputStream* raw_input;
  20. CodedInputStream* coded_input;
  21. uint32_t size;
  22. CodedInputStream::Limit limit;
  23. raw_input = new FileInputStream(accept_fd);
  24. coded_input = new CodedInputStream(raw_input);
  25. if(!coded_input->ReadVarint32(&size))
  26. {
  27. printf("Error in reading size of msg");
  28. fflush(stdout);
  29. return -1;
  30. }
  31. //printf("size of msg was read to be %" PRIu32 " \n", size);
  32. fflush(stdout);
  33. limit = coded_input->PushLimit(size);
  34. if(!message.ParseFromCodedStream(coded_input))
  35. {
  36. printf("Error in parsing msg");
  37. fflush(stdout);
  38. return -1;
  39. }
  40. coded_input->PopLimit(limit);
  41. return 0;
  42. }
  43. // TODO: private functions
  44. int write_protobuf_msg_to_fd(int accept_fd, google::protobuf::MessageLite& message)
  45. {
  46. ZeroCopyOutputStream* raw_output = new FileOutputStream(accept_fd);
  47. CodedOutputStream* coded_output = new CodedOutputStream(raw_output);
  48. coded_output->WriteVarint32(message.ByteSize());
  49. if(!message.SerializeToCodedStream(coded_output))
  50. {
  51. printf("SerializeToCodedStream failed");
  52. fflush(stdout);
  53. return -1;
  54. }
  55. // As per this - https://stackoverflow.com/questions/22881876/protocol-buffers-how-to-serialize-and-deserialize-multiple-messages-into-a-file?noredirect=1&lq=1
  56. // 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)
  57. delete coded_output;
  58. delete raw_output;
  59. fflush(stdout);
  60. return 0;
  61. }
  62. // 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)
  63. int set_up_socket(int port, sockaddr_in* address)
  64. {
  65. int server_fd = 0;
  66. // Creating socket file descriptor for listening for attestation requests.
  67. server_fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
  68. if (server_fd == -1)
  69. {
  70. printf("Error in creating a socket - %d", errno);
  71. return -1;
  72. }
  73. // Preparing the address struct for binding
  74. address->sin_family = AF_INET;
  75. address->sin_addr.s_addr = INADDR_ANY; // Todo: should this be localhost?
  76. address->sin_port = htons(port);
  77. // memset(address->sin_zero,0,sizeof(address->sin_zero));
  78. socklen_t addrlen = sizeof(*address);
  79. // Binding
  80. if (bind(server_fd, (sockaddr*)address, addrlen)<0)
  81. {
  82. printf("Error in binding %d - port was %d - ", errno, port);
  83. return -1;
  84. }
  85. // Listening
  86. if (listen(server_fd, 128) < 0)
  87. {
  88. printf("Error in listening %d", errno);
  89. return -1;
  90. }
  91. return server_fd;
  92. }
  93. int local_attestation_initiator(int port, uint32_t own_enclave_id)
  94. {
  95. // declare msg1, msg2, msg3 protobuf objects
  96. protobuf_sgx_dh_msg1_t protobuf_msg1;
  97. protobuf_sgx_dh_msg2_t protobuf_msg2;
  98. protobuf_sgx_dh_msg3_t protobuf_msg3;
  99. uint32_t protobuf_sgx_ret; uint32_t sgx_ret;
  100. // For socket to listen to the Apache enclave.
  101. int server_fd=0; int accept_fd = 0;
  102. struct sockaddr_in own_addr;
  103. struct sockaddr_storage apache_addr; socklen_t apache_addr_size = sizeof(apache_addr);
  104. uint32_t session_id; uint8_t read_or_write;
  105. size_t bytes_read_post_la; uint8_t encrypted_apache_mrsigner_and_tag[48];
  106. size_t bytes_written_post_la;
  107. // int counter;
  108. server_fd=set_up_socket(port, &own_addr);
  109. if(server_fd==-1)
  110. return -1;
  111. printf("Successfully set up a socket to communicate with the Apache enclave.\n");
  112. fflush(stdout);
  113. protobuf_sgx_ret = generate_protobuf_dh_msg1(own_enclave_id, protobuf_msg1, &session_id);
  114. if(protobuf_sgx_ret != 0)
  115. {
  116. printf("Error in generate_protobuf_dh_msg1: 0x%x", protobuf_sgx_ret); fflush(stdout); return protobuf_sgx_ret;
  117. }
  118. accept_fd = accept(server_fd, (struct sockaddr *)&apache_addr,&apache_addr_size);
  119. if (accept_fd <0)
  120. {
  121. printf("Error in accepting %d", errno);
  122. return -1;
  123. }
  124. printf("Accepted fd\n"); fflush(stdout);
  125. if(write_protobuf_msg_to_fd(accept_fd, protobuf_msg1)!=0)
  126. return -1;
  127. if(read_protobuf_msg_from_fd(accept_fd, protobuf_msg2)!=0)
  128. return -1;
  129. protobuf_sgx_ret = process_protobuf_dh_msg2_generate_protobuf_dh_msg3(own_enclave_id, protobuf_msg2, protobuf_msg3, &session_id, &read_or_write);
  130. if(protobuf_sgx_ret != 0)
  131. {
  132. printf("Error in generate_protobuf_dh_msg2: 0x%x", protobuf_sgx_ret); fflush(stdout); return protobuf_sgx_ret;
  133. }
  134. if(write_protobuf_msg_to_fd(accept_fd, protobuf_msg3)!=0)
  135. return -1;
  136. read_or_write=0;
  137. printf("Here\n"); fflush(stdout);
  138. if(read_or_write)
  139. {
  140. bytes_read_post_la=read(accept_fd, encrypted_apache_mrsigner_and_tag, 32);
  141. if(bytes_read_post_la!=32)
  142. {
  143. printf("Not all of the encrypted apache's mrsigner was read from the verifier.\n"); fflush(stdout); return 0xfe;
  144. }
  145. bytes_read_post_la=read(accept_fd, encrypted_apache_mrsigner_and_tag+32, 16);
  146. if(bytes_read_post_la!=16)
  147. {
  148. printf("Not all of the encrypted apache's mrsigner **tag** was read from the verifier.\n"); fflush(stdout); return 0xfe;
  149. }
  150. uint32_t count;
  151. for(count=0;count<48;count++)
  152. printf("0x%02x ", encrypted_apache_mrsigner_and_tag[count]);
  153. printf("\n");fflush(stdout);
  154. // sgx_ret=decrypt_wrapper(own_enclave_id, encrypted_apache_mrsigner_and_tag, 32, encrypted_apache_mrsigner_and_tag+32 , plaintext);
  155. Decryptor_decrypt_verifiers_message_set_apache_mrsigner(own_enclave_id, &sgx_ret, encrypted_apache_mrsigner_and_tag, encrypted_apache_mrsigner_and_tag+32);
  156. if(sgx_ret!=0)
  157. {
  158. printf("Error in decryption: 0x%x\n", sgx_ret); fflush(stdout); return sgx_ret;
  159. }
  160. printf("Successful decryption\n"); fflush(stdout);
  161. }
  162. else
  163. {
  164. uint8_t encrypted_sign_data_and_sign_and_tag[176];
  165. memset(encrypted_sign_data_and_sign_and_tag,0x0,176);
  166. uint8_t plaintext_sign_data_and_sign[160];
  167. //uint32_t create_and_encrypt_mitigator_header_value(uint8_t* plaintext_sign_data_and_sign, uint8_t* encrypted_sign_data_and_sign, uint8_t* tag);
  168. Decryptor_create_and_encrypt_mitigator_header_value(own_enclave_id, &sgx_ret, plaintext_sign_data_and_sign, encrypted_sign_data_and_sign_and_tag , encrypted_sign_data_and_sign_and_tag+160);
  169. if(sgx_ret!=0)
  170. {
  171. printf("Error in generating encrypted mitigator header:0x%x\n", sgx_ret); fflush(stdout); return 0xf3;
  172. }uint32_t count;
  173. for(count=0;count<160;count++)
  174. {
  175. printf("0x%02x ", encrypted_sign_data_and_sign_and_tag[count]);
  176. }
  177. printf("\n"); fflush(stdout);
  178. printf("Plaintext Signature data:\n"); fflush(stdout);
  179. for(count=0;count<96;count++)
  180. {
  181. printf("0x%02x ", plaintext_sign_data_and_sign[count]);
  182. }
  183. printf("\n"); fflush(stdout);
  184. printf("Plaintext signature: \n"); fflush(stdout);
  185. // printf("Signature data:\n"); fflush(stdout);
  186. for(count=0;count<64;count++)
  187. {
  188. printf("0x%02x ", plaintext_sign_data_and_sign[count+96]);
  189. }
  190. printf("\n"); fflush(stdout);
  191. // TODO: code to write signature data first
  192. bytes_written_post_la=write(accept_fd, encrypted_sign_data_and_sign_and_tag, 176);
  193. if(bytes_written_post_la!=176)
  194. {
  195. printf("Not all of the decryptor's signature was written to the Apache.\n"); fflush(stdout); return 0xfe;
  196. }
  197. }
  198. printf("Successfully done Local attestation\n");
  199. fflush(stdout);
  200. return 0;
  201. }