systemLA.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. #include "../Decryptor/Decryptor_u.h"
  16. #include "sgx_tcrypto.h"
  17. // TODO: Make these private functions
  18. int read_protobuf_msg_from_fd(int accept_fd, google::protobuf::MessageLite& message)
  19. {
  20. ZeroCopyInputStream* raw_input;
  21. CodedInputStream* coded_input;
  22. uint32_t size;
  23. CodedInputStream::Limit limit;
  24. raw_input = new FileInputStream(accept_fd);
  25. coded_input = new CodedInputStream(raw_input);
  26. if(!coded_input->ReadVarint32(&size))
  27. {
  28. printf("Error in reading size of msg");
  29. fflush(stdout);
  30. return -1;
  31. }
  32. //printf("size of msg was read to be %" PRIu32 " \n", size);
  33. fflush(stdout);
  34. limit = coded_input->PushLimit(size);
  35. if(!message.ParseFromCodedStream(coded_input))
  36. {
  37. printf("Error in parsing msg");
  38. fflush(stdout);
  39. return -1;
  40. }
  41. coded_input->PopLimit(limit);
  42. return 0;
  43. }
  44. // TODO: private functions
  45. int write_protobuf_msg_to_fd(int accept_fd, google::protobuf::MessageLite& message)
  46. {
  47. ZeroCopyOutputStream* raw_output = new FileOutputStream(accept_fd);
  48. CodedOutputStream* coded_output = new CodedOutputStream(raw_output);
  49. coded_output->WriteVarint32(message.ByteSize());
  50. if(!message.SerializeToCodedStream(coded_output))
  51. {
  52. printf("SerializeToCodedStream failed");
  53. fflush(stdout);
  54. return -1;
  55. }
  56. // As per this - https://stackoverflow.com/questions/22881876/protocol-buffers-how-to-serialize-and-deserialize-multiple-messages-into-a-file?noredirect=1&lq=1
  57. // 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)
  58. delete coded_output;
  59. delete raw_output;
  60. fflush(stdout);
  61. return 0;
  62. }
  63. // 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)
  64. int set_up_socket(int port, sockaddr_in* address)
  65. {
  66. int server_fd = 0;
  67. // Creating socket file descriptor for listening for attestation requests.
  68. server_fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
  69. if (server_fd == -1)
  70. {
  71. printf("Error in creating a socket - %d", errno);
  72. return -1;
  73. }
  74. // Preparing the address struct for binding
  75. address->sin_family = AF_INET;
  76. address->sin_addr.s_addr = INADDR_ANY; // Todo: should this be localhost?
  77. address->sin_port = htons(port);
  78. // memset(address->sin_zero,0,sizeof(address->sin_zero));
  79. socklen_t addrlen = sizeof(*address);
  80. // Binding
  81. if (bind(server_fd, (sockaddr*)address, addrlen)<0)
  82. {
  83. printf("Error in binding %d - port was %d - ", errno, port);
  84. return -1;
  85. }
  86. // Listening
  87. if (listen(server_fd, 128) < 0)
  88. {
  89. printf("Error in listening %d", errno);
  90. return -1;
  91. }
  92. return server_fd;
  93. }
  94. int local_attestation_initiator(int port, uint32_t own_enclave_id)
  95. {
  96. // declare msg1, msg2, msg3 protobuf objects
  97. protobuf_sgx_dh_msg1_t protobuf_msg1;
  98. protobuf_sgx_dh_msg2_t protobuf_msg2;
  99. protobuf_sgx_dh_msg3_t protobuf_msg3;
  100. uint32_t protobuf_sgx_ret; uint32_t sgx_ret;
  101. // For socket to listen to the Apache enclave.
  102. int server_fd=0; int accept_fd = 0;
  103. struct sockaddr_in own_addr;
  104. struct sockaddr_storage apache_addr; socklen_t apache_addr_size = sizeof(apache_addr);
  105. uint32_t session_id; uint8_t read_or_write;
  106. size_t bytes_read_post_la; uint8_t encrypted_apache_mrsigner_and_tag[48];
  107. size_t bytes_written_post_la;
  108. // int counter;
  109. server_fd=set_up_socket(port, &own_addr);
  110. if(server_fd==-1)
  111. return -1;
  112. printf("Successfully set up a socket to communicate with the Apache enclave.\n");
  113. fflush(stdout);
  114. protobuf_sgx_ret = generate_protobuf_dh_msg1(own_enclave_id, protobuf_msg1, &session_id);
  115. if(protobuf_sgx_ret != 0)
  116. {
  117. printf("Error in generate_protobuf_dh_msg1: 0x%x", protobuf_sgx_ret); fflush(stdout); return protobuf_sgx_ret;
  118. }
  119. accept_fd = accept(server_fd, (struct sockaddr *)&apache_addr,&apache_addr_size);
  120. if (accept_fd <0)
  121. {
  122. printf("Error in accepting %d", errno);
  123. return -1;
  124. }
  125. printf("Accepted fd\n"); fflush(stdout);
  126. if(write_protobuf_msg_to_fd(accept_fd, protobuf_msg1)!=0)
  127. return -1;
  128. if(read_protobuf_msg_from_fd(accept_fd, protobuf_msg2)!=0)
  129. return -1;
  130. protobuf_sgx_ret = process_protobuf_dh_msg2_generate_protobuf_dh_msg3(own_enclave_id, protobuf_msg2, protobuf_msg3, &session_id, &read_or_write);
  131. if(protobuf_sgx_ret != 0)
  132. {
  133. printf("Error in generate_protobuf_dh_msg2: 0x%x", protobuf_sgx_ret); fflush(stdout); return protobuf_sgx_ret;
  134. }
  135. if(write_protobuf_msg_to_fd(accept_fd, protobuf_msg3)!=0)
  136. return -1;
  137. // read_or_write=0;
  138. printf("Here\n"); fflush(stdout);
  139. if(read_or_write)
  140. {
  141. bytes_read_post_la=read(accept_fd, encrypted_apache_mrsigner_and_tag, 32);
  142. if(bytes_read_post_la!=32)
  143. {
  144. printf("Not all of the encrypted apache's mrsigner was read from the verifier.\n"); fflush(stdout); return 0xfe;
  145. }
  146. bytes_read_post_la=read(accept_fd, encrypted_apache_mrsigner_and_tag+32, 16);
  147. if(bytes_read_post_la!=16)
  148. {
  149. printf("Not all of the encrypted apache's mrsigner **tag** was read from the verifier.\n"); fflush(stdout); return 0xfe;
  150. }
  151. uint32_t count;
  152. for(count=0;count<48;count++)
  153. printf("0x%02x ", encrypted_apache_mrsigner_and_tag[count]);
  154. printf("\n");fflush(stdout);
  155. // sgx_ret=decrypt_wrapper(own_enclave_id, encrypted_apache_mrsigner_and_tag, 32, encrypted_apache_mrsigner_and_tag+32 , plaintext);
  156. Decryptor_decrypt_verifiers_message_set_apache_mrsigner(own_enclave_id, &sgx_ret, encrypted_apache_mrsigner_and_tag, encrypted_apache_mrsigner_and_tag+32);
  157. if(sgx_ret!=0)
  158. {
  159. printf("Error in decryption: 0x%x\n", sgx_ret); fflush(stdout); return sgx_ret;
  160. }
  161. printf("Successful decryption\n"); fflush(stdout);
  162. //Decryptor_aes_gcm_internal_call(own_enclave_id, &sgx_ret, NULL, 0, NULL, NULL, NULL, NULL);
  163. }
  164. else
  165. {
  166. uint8_t encrypted_sign_data_and_sign_and_tag[176];
  167. memset(encrypted_sign_data_and_sign_and_tag,0x0,176);
  168. uint8_t plaintext_sign_data_and_sign[160];
  169. uint8_t plaintext_priv_key[32];
  170. //uint32_t create_and_encrypt_mitigator_header_value(uint8_t* plaintext_sign_data_and_sign, uint8_t* encrypted_sign_data_and_sign, uint8_t* tag);
  171. sgx_ec256_signature_t sig2;
  172. Decryptor_create_and_encrypt_mitigator_header_value(own_enclave_id, &sgx_ret, plaintext_sign_data_and_sign, encrypted_sign_data_and_sign_and_tag , encrypted_sign_data_and_sign_and_tag+160, plaintext_priv_key,&sig2);
  173. if(sgx_ret!=0)
  174. {
  175. printf("Error in generating encrypted mitigator header:0x%x\n", sgx_ret); fflush(stdout); return 0xf3;
  176. }uint32_t count;
  177. for(count=0;count<160;count++)
  178. {
  179. printf("0x%02x ", encrypted_sign_data_and_sign_and_tag[count]);
  180. }
  181. printf("\n"); fflush(stdout);
  182. printf("Plaintext Signature data:\n"); fflush(stdout);
  183. for(count=0;count<96;count++)
  184. {
  185. printf("0x%02x ", plaintext_sign_data_and_sign[count]);
  186. }
  187. printf("\n"); fflush(stdout);
  188. printf("Plaintext signature: \n"); fflush(stdout);
  189. // printf("Signature data:\n"); fflush(stdout);
  190. for(count=0;count<32;count++)
  191. {
  192. printf("%02x", plaintext_sign_data_and_sign[count+96]);
  193. }
  194. printf("\n"); fflush(stdout);
  195. for(count=32;count<64;count++)
  196. {
  197. printf("%02x", plaintext_sign_data_and_sign[count+96]);
  198. }
  199. printf("\n"); fflush(stdout);
  200. printf("Heres the private key used to sign this \n"); // TODO: Remove this printf and the private key parts to the ecall
  201. for(count=0;count<32;count++)
  202. printf("%02x", plaintext_priv_key[31-count]);
  203. printf("\n"); fflush(stdout);
  204. for(count=0;count<8;count++)
  205. {
  206. printf("%02x ", sig2.x[count]);
  207. }
  208. printf("\n"); fflush(stdout);
  209. for(count=0;count<8;count++)
  210. {
  211. printf("%02x ", sig2.y[count]);
  212. }
  213. printf("\n"); fflush(stdout);
  214. // TODO: code to write signature data first
  215. bytes_written_post_la=write(accept_fd, encrypted_sign_data_and_sign_and_tag, 176);
  216. if(bytes_written_post_la!=176)
  217. {
  218. printf("Not all of the decryptor's signature was written to the Apache.\n"); fflush(stdout); return 0xfe;
  219. }
  220. }
  221. printf("Successfully done Local attestation\n");
  222. fflush(stdout);
  223. return 0;
  224. }