ProtobufLAInitiator.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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_and_tag[150];// uint8_t encrypted_tag[16];
  97. uint32_t total_length;
  98. size_t post_la_bytes_written;
  99. // For socket to listen to the Apache enclave.
  100. uint32_t session_id;
  101. // int counter;
  102. int decryptor_fd;
  103. setbuf(stdout,NULL);
  104. decryptor_fd=set_up_socket_connect(port);
  105. if(decryptor_fd == -1)
  106. {
  107. perror("\nCould not set up the socket: had the following error: ");
  108. fflush(stderr);
  109. }
  110. // printf("");
  111. if(read_protobuf_msg_from_fd(decryptor_fd, protobuf_msg1)!=0)
  112. return -1;
  113. protobuf_sgx_ret = process_protobuf_dh_msg1_generate_protobuf_dh_msg2(protobuf_msg1, protobuf_msg2, &session_id);
  114. if(protobuf_sgx_ret != 0)
  115. {
  116. printf("Error in process_protobuf_dh_msg1_generate_protobuf_dh_msg2: 0x%x", protobuf_sgx_ret); fflush(stdout); return protobuf_sgx_ret;
  117. }
  118. if(write_protobuf_msg_to_fd(decryptor_fd, protobuf_msg2)!=0)
  119. return -1;
  120. if(read_protobuf_msg_from_fd(decryptor_fd, protobuf_msg3)!=0)
  121. return -1;
  122. protobuf_sgx_ret = process_protobuf_dh_msg3(protobuf_msg3, &session_id);
  123. if(protobuf_sgx_ret != 0)
  124. {
  125. printf("Error in process_protobuf_dh_msg3: 0x%x", protobuf_sgx_ret); fflush(stdout); return protobuf_sgx_ret;
  126. }
  127. memset(encrypted_hash_and_tag, 0, 150);
  128. protobuf_sgx_ret=generate_encrypted_rsa_keypair_hash(encrypted_hash_and_tag, &total_length);
  129. if(protobuf_sgx_ret==0)
  130. {
  131. printf("Done encryption of hash.\n"); fflush(stdout);
  132. }
  133. else
  134. {
  135. printf("Error in enc/dec of hash: 0x%x", protobuf_sgx_ret); fflush(stdout); return protobuf_sgx_ret;
  136. }
  137. post_la_bytes_written = write(decryptor_fd, encrypted_hash_and_tag, total_length);
  138. printf("Wrote the hash and the tag to the decryptor socket.\n Wrote this many bytes: %d\n", post_la_bytes_written); fflush(stdout);
  139. if(close(decryptor_fd)!= 0)
  140. {
  141. printf("Error in closing the socket connection.\n"); fflush(stdout); return 0xfd;
  142. }
  143. printf("Successfully done Local attestation\n");
  144. fflush(stdout);
  145. return 0;
  146. }