DeploymentStageLogic.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. uint32_t 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. // Set up an IPC channel for local attestation and post-LA messages.
  43. decryptor_fd = set_up_socket_connect(port);
  44. if(decryptor_fd == -1)
  45. {
  46. perror("\nCould not set up the socket: had the following error: ");
  47. fflush(stderr);
  48. }
  49. // Conduct LA.
  50. ret_status = laInitiator.conduct_la(decryptor_fd);
  51. if(ret_status != 0)
  52. return ret_status;
  53. // Use the same channel for post-LA messages as the one used above for LA messages.
  54. postLAMessaging.set_fd(decryptor_fd);
  55. // Use the symmetric key from LA to send messages for the rest of the deployment stage.
  56. postLAMessaging.set_la_symmetric_key(laInitiator.get_la_symmetric_key(key));
  57. // Send the target's hash to the decryptor enclave.
  58. return send_secure_msg(target_hash, 32);
  59. }
  60. /*
  61. uint32_t generate_encrypted_rsa_keypair_hash(uint8_t* op_ciphertext, uint32_t* length)
  62. {
  63. uint8_t tag[16];
  64. int ciphertext_len;// int plaintext_len=32;
  65. uint8_t iv[12];
  66. memset(iv, 0, 12);
  67. return_status=aes_cipher(1, key, iv, hash, 32, op_ciphertext, &ciphertext_len, tag);
  68. if(return_status == 0)
  69. {
  70. for(counter=0;counter<12;counter++)
  71. op_ciphertext[counter+ ciphertext_len] = iv[counter];
  72. for(counter=0;counter<16;counter++)
  73. op_ciphertext[counter+ ciphertext_len + 12] = tag[counter];
  74. ciphertext_len+=28;
  75. *length=ciphertext_len;
  76. }
  77. }
  78. */