DeploymentStageLogic.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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);
  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);
  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);
  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. return ret_status;
  53. /*
  54. *
  55. printf("\nSuccessful LA with port %d.\n", DECRYPTOR_PORT);
  56. fflush(stdout);
  57. */
  58. // Use the same channel for post-LA messages as the one used above for LA messages.
  59. postLaMessaging.set_fd(decryptor_fd);
  60. // Use the symmetric key from LA to send messages for the rest of the deployment stage.
  61. laInitiator.get_la_symmetric_key(key);
  62. postLaMessaging.set_la_symmetric_key(key);
  63. // Send the target's hash to the decryptor enclave.
  64. return postLaMessaging.send_secure_msg(target_hash, 32);
  65. }