DeploymentStageLogic.cpp 2.0 KB

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