DeploymentStageLogic.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // Created by miti on 2019-12-24.
  3. //
  4. #include "DeploymentStageLogic.h"
  5. #include "stdio.h"
  6. #include <errno.h>
  7. #include <string.h>
  8. // For socket programming
  9. #include <arpa/inet.h>
  10. #include <sys/socket.h>
  11. #include <netinet/in.h>
  12. // Sets up a socket connected to the port passed as input - returns the socket FD on success and -1 on error.
  13. // Also prints the errno on error.
  14. int DeploymentStageLogic::set_up_socket_connect(int port)
  15. {
  16. int sock = 0;
  17. if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  18. {
  19. printf("\n Error in socket call - errno is %d \n", errno); fflush(stdout);
  20. return -1;
  21. }
  22. struct sockaddr_in serv_addr;
  23. memset(&serv_addr, '0', sizeof(serv_addr));
  24. serv_addr.sin_family = AF_INET;
  25. serv_addr.sin_port = htons(port);
  26. // Convert IPv4 and IPv6 addresses from text to binary form
  27. if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
  28. {
  29. printf("\nError in inet_pton - errno is %d\n", errno); fflush(stdout);
  30. return -1;
  31. }
  32. if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
  33. {
  34. printf("\nError in connect - errno is %d \n", errno); fflush(stdout);
  35. return -1;
  36. }
  37. return sock;
  38. }
  39. void DeploymentStageLogic::set_target_hash(uint8_t* given_hash)
  40. {
  41. uint32_t counter;
  42. for(counter=0; counter<32; counter++)
  43. target_hash[counter] = given_hash[counter];
  44. }
  45. int DeploymentStageLogic::main_logic(int decryptor_fd)
  46. {
  47. uint8_t key[16];
  48. uint32_t ret_status;
  49. // Conduct LA.
  50. ret_status = laInitiator.conduct_la(decryptor_fd);
  51. if(ret_status != 0)
  52. {
  53. printf("LA initiator returned an error: %d\n", ret_status);
  54. fflush(stdout);
  55. return ret_status;
  56. }
  57. /*
  58. *
  59. printf("\nSuccessful LA with port %d.\n", DECRYPTOR_PORT);
  60. fflush(stdout);
  61. */
  62. printf("Setting up fd for post LA messaging.\n"); fflush(stdout);
  63. // Use the same channel for post-LA messages as the one used above for LA messages.
  64. postLaMessaging.set_fd(decryptor_fd);
  65. // Use the symmetric key from LA to send messages for the rest of the deployment stage.
  66. printf("Retrieving key from LA inititator.\n "); fflush(stdout);
  67. laInitiator.get_la_symmetric_key(key);
  68. printf("Setting key for post LA messaging.\n "); fflush(stdout);
  69. postLaMessaging.set_la_symmetric_key(key);
  70. // Send the target's hash to the decryptor enclave.
  71. return postLaMessaging.send_secure_msg(target_hash, 32);
  72. }