ProtobufLAInitiator.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Knows only protobuf_sgx objects, protobuf header.
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. #include<unistd.h>
  6. #include <stdio.h>
  7. #include "ProtobufLAMessages.h"
  8. #include <google/protobuf/io/coded_stream.h>
  9. #include <google/protobuf/io/zero_copy_stream_impl.h>
  10. using namespace google::protobuf::io;
  11. #include "SgxProtobufLAInitiator.h"
  12. // For socket programming
  13. #include <arpa/inet.h>
  14. #include <sys/socket.h>
  15. #include <netinet/in.h>
  16. // TODO: Make these private functions
  17. int read_protobuf_msg_from_fd(int accept_fd, google::protobuf::MessageLite& message)
  18. {
  19. ZeroCopyInputStream* raw_input;
  20. CodedInputStream* coded_input;
  21. uint32_t size;
  22. CodedInputStream::Limit limit;
  23. raw_input = new FileInputStream(accept_fd);
  24. coded_input = new CodedInputStream(raw_input);
  25. if(!coded_input->ReadVarint32(&size))
  26. {
  27. printf("Error in reading size of msg");
  28. fflush(stdout);
  29. return -1;
  30. }
  31. //printf("size of msg was read to be %" PRIu32 " \n", size);
  32. fflush(stdout);
  33. limit = coded_input->PushLimit(size);
  34. if(!message.ParseFromCodedStream(coded_input))
  35. {
  36. printf("Error in parsing msg");
  37. fflush(stdout);
  38. return -1;
  39. }
  40. coded_input->PopLimit(limit);
  41. return 0;
  42. }
  43. // TODO: private functions
  44. int write_protobuf_msg_to_fd(int accept_fd, google::protobuf::MessageLite& message)
  45. {
  46. ZeroCopyOutputStream* raw_output = new FileOutputStream(accept_fd);
  47. CodedOutputStream* coded_output = new CodedOutputStream(raw_output);
  48. coded_output->WriteVarint32(message.ByteSize());
  49. if(!message.SerializeToCodedStream(coded_output))
  50. {
  51. printf("SerializeToCodedStream failed");
  52. fflush(stdout);
  53. return -1;
  54. }
  55. // As per this - https://stackoverflow.com/questions/22881876/protocol-buffers-how-to-serialize-and-deserialize-multiple-messages-into-a-file?noredirect=1&lq=1
  56. // 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)
  57. delete coded_output;
  58. delete raw_output;
  59. fflush(stdout);
  60. return 0;
  61. }
  62. // Sets up a socket connected to the port passed as input - returns the socket FD on success and -1 on error.
  63. // Also prints the errno on error.
  64. int set_up_socket_connect(int port)
  65. {
  66. int sock = 0;
  67. if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  68. {
  69. printf("\n Error in socket call - errno is %d \n", errno);
  70. return -1;
  71. }
  72. struct sockaddr_in serv_addr;
  73. memset(&serv_addr, '0', sizeof(serv_addr));
  74. serv_addr.sin_family = AF_INET;
  75. serv_addr.sin_port = htons(port);
  76. // Convert IPv4 and IPv6 addresses from text to binary form
  77. if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
  78. {
  79. printf("\nError in inet_pton - errno is %d\n", errno);
  80. return -1;
  81. }
  82. if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
  83. {
  84. printf("\nError in connect - errno is %d \n", errno);
  85. return -1;
  86. }
  87. return sock;
  88. }
  89. int local_attestation_initiator(int port)
  90. {
  91. // declare msg1, msg2, msg3 protobuf objects
  92. protobuf_sgx_dh_msg1_t protobuf_msg1;
  93. protobuf_sgx_dh_msg2_t protobuf_msg2;
  94. protobuf_sgx_dh_msg3_t protobuf_msg3;
  95. uint32_t protobuf_sgx_ret;
  96. uint8_t encrypted_hash[32]; uint8_t encrypted_tag[16]; size_t post_la_bytes_written;
  97. // For socket to listen to the Apache enclave.
  98. // int server_fd=0; int accept_fd = 0;
  99. // struct sockaddr_in own_addr;
  100. // struct sockaddr_storage apache_addr; socklen_t apache_addr_size = sizeof(apache_addr);
  101. uint32_t session_id;
  102. // int counter;
  103. int decryptor_fd;
  104. setbuf(stdout,NULL);
  105. decryptor_fd=set_up_socket_connect(port);
  106. if(decryptor_fd == -1)
  107. {
  108. perror("\nCould not set up the socket: had the following error: ");
  109. fflush(stderr);
  110. }
  111. // printf("");
  112. if(read_protobuf_msg_from_fd(decryptor_fd, protobuf_msg1)!=0)
  113. return -1;
  114. protobuf_sgx_ret = process_protobuf_dh_msg1_generate_protobuf_dh_msg2(protobuf_msg1, protobuf_msg2, &session_id);
  115. if(protobuf_sgx_ret != 0)
  116. {
  117. printf("Error in process_protobuf_dh_msg1_generate_protobuf_dh_msg2: 0x%x", protobuf_sgx_ret); fflush(stdout); return protobuf_sgx_ret;
  118. }
  119. if(write_protobuf_msg_to_fd(decryptor_fd, protobuf_msg2)!=0)
  120. return -1;
  121. if(read_protobuf_msg_from_fd(decryptor_fd, protobuf_msg3)!=0)
  122. return -1;
  123. protobuf_sgx_ret = process_protobuf_dh_msg3(protobuf_msg3, &session_id);
  124. if(protobuf_sgx_ret != 0)
  125. {
  126. printf("Error in process_protobuf_dh_msg3: 0x%x", protobuf_sgx_ret); fflush(stdout); return protobuf_sgx_ret;
  127. }
  128. /* memset(encrypted_hash,0, 32); memset(encrypted_tag, 0, 16);
  129. protobuf_sgx_ret=generate_encrypted_rsa_keypair_hash(encrypted_hash, encrypted_tag);
  130. if(protobuf_sgx_ret==0)
  131. {
  132. printf("Done encryption of hash.\n"); fflush(stdout);
  133. }
  134. else
  135. {
  136. printf("Error in enc/dec of hash: 0x%x", protobuf_sgx_ret); fflush(stdout); return protobuf_sgx_ret;
  137. }
  138. post_la_bytes_written = write(decryptor_fd, encrypted_hash, 32);
  139. post_la_bytes_written = write(decryptor_fd, encrypted_tag, 16);
  140. if(post_la_bytes_written != 16)
  141. {
  142. printf("Not all of the post-LA message was written\n"); fflush(stdout); return 0xfe;
  143. }
  144. printf("Wrote the hash and the tag to the decryptor socket.\n"); fflush(stdout);
  145. */
  146. if(close(decryptor_fd)!= 0)
  147. {
  148. printf("Error in closing the socket connection.\n"); fflush(stdout); return 0xfd;
  149. }
  150. printf("Successfully done Local attestation\n");
  151. fflush(stdout);
  152. return 0;
  153. }