MainLogic.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // Created by miti on 2020-01-01.
  3. //
  4. #include "MainLogic.h"
  5. #include <stdio.h>
  6. // For socket programming
  7. #include <arpa/inet.h>
  8. #include <sys/socket.h>
  9. #include <netinet/in.h>
  10. int MainLogic::accept_fd(int fd)
  11. {
  12. struct sockaddr_storage apache_addr;
  13. socklen_t apache_addr_size = sizeof(apache_addr);
  14. return accept(fd, (struct sockaddr *)&apache_addr,&apache_addr_size);
  15. }
  16. int MainLogic::la_post_la_with_verifier()
  17. {
  18. int listen_fd, verifier_fd, la_ret, post_la_ret;
  19. // DEPLOYMENT STAGE.
  20. listen_fd = set_up_listening_socket(3824);
  21. if(listen_fd <=0)
  22. {
  23. printf("Error in setting up server socket."); fflush(stdout);
  24. return 0x1;
  25. }
  26. printf("Successfully set up a socket to communicate with the verifier enclave.\n");
  27. fflush(stdout);
  28. verifier_fd = accept_fd(listen_fd);
  29. if (verifier_fd <0)
  30. {
  31. printf("Error in accepting %d", errno); fflush(stdout);
  32. }
  33. la_ret = laInitiator.conduct_la(verifier_fd);
  34. if(la_ret !=0)
  35. {
  36. printf("local attestation - with the verifier - did not successfully return: %x\n", la_ret); fflush(stdout);
  37. return 0xFFFFFFFF;
  38. }
  39. printf("Successfully conducted LA with the verifier.\n"); fflush(stdout);
  40. messaging.set_verifier_fd(verifier_fd);
  41. post_la_ret = messaging.receive_token_from_verifier();
  42. if(post_la_ret != 0)
  43. {
  44. printf("Error in post-verifier LA: %ud\n", post_la_ret); fflush(stdout);
  45. return post_la_ret;
  46. }
  47. printf("Obtained the token from the verifier.\n"); fflush(stdout);
  48. return 0;
  49. }
  50. int MainLogic::la_post_la_with_target()
  51. {
  52. int listen_fd, la_ret, target_data_fd, target_headers_fd;
  53. listen_fd = set_up_listening_socket(3825);
  54. if(listen_fd <=0)
  55. {
  56. printf("Error in setting up server socket."); fflush(stdout);
  57. return listen_fd;
  58. }
  59. printf("Successfully set up a socket to communicate with the target enclave.\n");
  60. fflush(stdout);
  61. target_data_fd = accept_fd(listen_fd);
  62. if (target_data_fd <0)
  63. {
  64. printf("Error in accepting %d", errno); fflush(stdout);
  65. }
  66. la_ret = laInitiator.conduct_la(target_data_fd);
  67. if(la_ret !=0)
  68. {
  69. printf("local attestation - with the target - did not successfully return: %x\n", la_ret); fflush(stdout);
  70. return 0xFFFFFFFF;
  71. }
  72. printf("Successfully conducted LA with the target.\n"); fflush(stdout);
  73. // For post-la messaging of client data.
  74. messaging.set_target_data_fd(target_data_fd);
  75. listen_fd = set_up_listening_socket(3826);
  76. if(listen_fd <=0)
  77. {
  78. printf("Error in setting up server socket for the header.\n"); fflush(stdout);
  79. return listen_fd;
  80. }
  81. printf("Successfully set up a socket to send a header to the target.\n");
  82. fflush(stdout);
  83. target_headers_fd = accept_fd(listen_fd);
  84. if (target_headers_fd <0)
  85. {
  86. printf("Error in accepting %d\n", errno); fflush(stdout);
  87. }
  88. messaging.set_target_headers_fd(target_headers_fd);
  89. if(messaging.read_and_write_header() != 0)
  90. {
  91. printf("Could not write the header.\n");
  92. fflush(stdout);
  93. return 0x1;
  94. }
  95. printf("Wrote the header to the target fd for headers.\n"); fflush(stdout);
  96. return 0;
  97. }
  98. // 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)
  99. int MainLogic::set_up_listening_socket(int port)
  100. {
  101. struct sockaddr_in own_addr;
  102. sockaddr_in* address = &own_addr;
  103. int server_fd = 0;
  104. // Creating socket file descriptor for listening for attestation requests.
  105. server_fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
  106. if (server_fd == -1)
  107. {
  108. printf("Error in creating a socket - %d", errno);
  109. return -1;
  110. }
  111. // Preparing the address struct for binding
  112. address->sin_family = AF_INET;
  113. address->sin_addr.s_addr = INADDR_ANY; // Todo: should this be localhost?
  114. address->sin_port = htons(port);
  115. socklen_t addrlen = sizeof(*address);
  116. // Binding
  117. if (bind(server_fd, (sockaddr*)address, addrlen)<0)
  118. {
  119. printf("Error in binding %d - port was %d - ", errno, port);
  120. return -1;
  121. }
  122. // Listening
  123. if (listen(server_fd, 128) < 0)
  124. {
  125. printf("Error in listening %d", errno);
  126. return -1;
  127. }
  128. return server_fd;
  129. }
  130. void MainLogic::set_enclave_id(uint32_t given_enclave_id) {
  131. laInitiator.set_enclave_id(given_enclave_id);
  132. messaging.set_enclave_id(given_enclave_id);
  133. }
  134. int MainLogic::run_deployment_runtime_stage_logic() {
  135. int ret_status;
  136. pid_t child_pid;
  137. ret_status = la_post_la_with_verifier();
  138. if(ret_status != 0)
  139. return ret_status;
  140. ret_status = la_post_la_with_target();
  141. if(ret_status != 0)
  142. return ret_status;
  143. child_pid = fork();
  144. if(child_pid < 0)
  145. {
  146. printf("Error forking (to create a process to listen for header requests).\n"); fflush(stdout);
  147. return 0x1;
  148. }
  149. else if(child_pid == 0) // Child process - returns headers.
  150. {
  151. printf("Lol in child process.\n"); fflush(stdout);
  152. ret_status = 0;
  153. do
  154. {
  155. ret_status = messaging.read_and_write_header();
  156. } while(ret_status == 0);
  157. return ret_status;
  158. }
  159. else // Parent process - decrypts client data.
  160. {
  161. printf("Lol in parent process.\n"); fflush(stdout);
  162. ret_status = 0;
  163. do
  164. {
  165. ret_status = messaging.receive_decrypt_client_data_from_target();
  166. } while(ret_status == 0);
  167. return ret_status;
  168. }
  169. }