systemLA.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Knows only protobuf_sgx objects, protobuf header.
  2. // For socket programming
  3. #include <sys/socket.h>
  4. #include <stdlib.h>
  5. #include <netinet/in.h>
  6. #include <string.h>
  7. #include <errno.h>
  8. #include<unistd.h>
  9. #include <stdio.h>
  10. #include "dhmsgs.pb.h"
  11. #include <google/protobuf/io/coded_stream.h>
  12. #include <google/protobuf/io/zero_copy_stream_impl.h>
  13. using namespace google::protobuf::io;
  14. #include "protobufLAInitiator.h"
  15. // TODO: Make these private functions
  16. int read_protobuf_msg_from_fd(int accept_fd, google::protobuf::MessageLite& message)
  17. {
  18. ZeroCopyInputStream* raw_input;
  19. CodedInputStream* coded_input;
  20. uint32_t size;
  21. CodedInputStream::Limit limit;
  22. raw_input = new FileInputStream(accept_fd);
  23. coded_input = new CodedInputStream(raw_input);
  24. if(!coded_input->ReadVarint32(&size))
  25. {
  26. printf("Error in reading size of msg");
  27. fflush(stdout);
  28. return -1;
  29. }
  30. //printf("size of msg was read to be %" PRIu32 " \n", size);
  31. fflush(stdout);
  32. limit = coded_input->PushLimit(size);
  33. if(!message.ParseFromCodedStream(coded_input))
  34. {
  35. printf("Error in parsing msg");
  36. fflush(stdout);
  37. return -1;
  38. }
  39. coded_input->PopLimit(limit);
  40. return 0;
  41. }
  42. // TODO: private functions
  43. int write_protobuf_msg_to_fd(int accept_fd, google::protobuf::MessageLite& message)
  44. {
  45. ZeroCopyOutputStream* raw_output = new FileOutputStream(accept_fd);
  46. CodedOutputStream* coded_output = new CodedOutputStream(raw_output);
  47. coded_output->WriteVarint32(message.ByteSize());
  48. if(!message.SerializeToCodedStream(coded_output))
  49. {
  50. printf("SerializeToCodedStream failed");
  51. fflush(stdout);
  52. return -1;
  53. }
  54. // As per this - https://stackoverflow.com/questions/22881876/protocol-buffers-how-to-serialize-and-deserialize-multiple-messages-into-a-file?noredirect=1&lq=1
  55. // TODO: There may be a better way to do this - 1) this happens with every accept now and 2) make it happen on the stack vs heap - destructor will be called on return from this function (main) and the items will then be written out. (We probably don't want that, actually)
  56. delete coded_output;
  57. delete raw_output;
  58. fflush(stdout);
  59. return 0;
  60. }
  61. // 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)
  62. int set_up_socket(int port, sockaddr_in* address)
  63. {
  64. int server_fd = 0;
  65. // Creating socket file descriptor for listening for attestation requests.
  66. server_fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
  67. if (server_fd == -1)
  68. {
  69. printf("Error in creating a socket - %d", errno);
  70. return -1;
  71. }
  72. // Preparing the address struct for binding
  73. address->sin_family = AF_INET;
  74. address->sin_addr.s_addr = INADDR_ANY; // Todo: should this be localhost?
  75. address->sin_port = htons(port);
  76. // memset(address->sin_zero,0,sizeof(address->sin_zero));
  77. socklen_t addrlen = sizeof(*address);
  78. // Binding
  79. if (bind(server_fd, (sockaddr*)address, addrlen)<0)
  80. {
  81. printf("Error in binding %d - port was %d - ", errno, port);
  82. return -1;
  83. }
  84. // Listening
  85. if (listen(server_fd, 128) < 0)
  86. {
  87. printf("Error in listening %d", errno);
  88. return -1;
  89. }
  90. return server_fd;
  91. }
  92. int local_attestation_initiator(int port, uint32_t own_enclave_id)
  93. {
  94. // declare msg1, msg2, msg3 protobuf objects
  95. protobuf_sgx_dh_msg1_t protobuf_msg1;
  96. protobuf_sgx_dh_msg2_t protobuf_msg2;
  97. protobuf_sgx_dh_msg3_t protobuf_msg3;
  98. uint32_t protobuf_sgx_ret;
  99. // For socket to listen to the Apache enclave.
  100. int server_fd=0; int accept_fd = 0;
  101. struct sockaddr_in own_addr;
  102. struct sockaddr_storage apache_addr; socklen_t apache_addr_size = sizeof(apache_addr);
  103. uint32_t session_id;
  104. // int counter;
  105. server_fd=set_up_socket(port, &own_addr);
  106. if(server_fd==-1)
  107. return -1;
  108. printf("Successfully set up a socket to communicate with the Apache enclave.\n");
  109. fflush(stdout);
  110. protobuf_sgx_ret = generate_protobuf_dh_msg1(own_enclave_id, protobuf_msg1, &session_id);
  111. if(protobuf_sgx_ret != 0)
  112. {
  113. printf("Error in generate_protobuf_dh_msg1: 0x%x", protobuf_sgx_ret); fflush(stdout); return protobuf_sgx_ret;
  114. }
  115. accept_fd = accept(server_fd, (struct sockaddr *)&apache_addr,&apache_addr_size);
  116. if (accept_fd <0)
  117. {
  118. printf("Error in accepting %d", errno);
  119. return -1;
  120. }
  121. printf("Accepted fd\n"); fflush(stdout);
  122. if(write_protobuf_msg_to_fd(accept_fd, protobuf_msg1)!=0)
  123. return -1;
  124. if(read_protobuf_msg_from_fd(accept_fd, protobuf_msg2)!=0)
  125. return -1;
  126. protobuf_sgx_ret = process_protobuf_dh_msg2_generate_protobuf_dh_msg3(own_enclave_id, protobuf_msg2, protobuf_msg3, &session_id);
  127. if(protobuf_sgx_ret != 0)
  128. {
  129. printf("Error in generate_protobuf_dh_msg2: 0x%x", protobuf_sgx_ret); fflush(stdout); return protobuf_sgx_ret;
  130. }
  131. if(write_protobuf_msg_to_fd(accept_fd, protobuf_msg3)!=0)
  132. return -1;
  133. printf("Successfully done Local attestation\n");
  134. fflush(stdout);
  135. return 0;
  136. }