systemLA.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 "ProtobufLAMessages.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. #include <iostream>
  18. // TODO: Make these private functions
  19. int apache_fd;
  20. int decrypt_client_data_wrapper(unsigned char* op_plaintext , uint32_t own_enclave_id);
  21. int read_protobuf_msg_from_fd(int accept_fd, google::protobuf::MessageLite& message)
  22. {
  23. ZeroCopyInputStream* raw_input;
  24. CodedInputStream* coded_input;
  25. uint32_t size;
  26. CodedInputStream::Limit limit;
  27. raw_input = new FileInputStream(accept_fd);
  28. coded_input = new CodedInputStream(raw_input);
  29. if(!coded_input->ReadVarint32(&size))
  30. {
  31. printf("Error in reading size of msg");
  32. fflush(stdout);
  33. return -1;
  34. }
  35. //printf("size of msg was read to be %" PRIu32 " \n", size);
  36. fflush(stdout);
  37. limit = coded_input->PushLimit(size);
  38. if(!message.ParseFromCodedStream(coded_input))
  39. {
  40. printf("Error in parsing msg");
  41. fflush(stdout);
  42. return -1;
  43. }
  44. coded_input->PopLimit(limit);
  45. delete raw_input;
  46. delete coded_input;
  47. return 0;
  48. }
  49. // TODO: private functions
  50. int write_protobuf_msg_to_fd(int accept_fd, google::protobuf::MessageLite& message)
  51. {
  52. ZeroCopyOutputStream* raw_output = new FileOutputStream(accept_fd);
  53. CodedOutputStream* coded_output = new CodedOutputStream(raw_output);
  54. coded_output->WriteVarint32(message.ByteSize());
  55. if(!message.SerializeToCodedStream(coded_output))
  56. {
  57. printf("SerializeToCodedStream failed");
  58. fflush(stdout);
  59. return -1;
  60. }
  61. // As per this - https://stackoverflow.com/questions/22881876/protocol-buffers-how-to-serialize-and-deserialize-multiple-messages-into-a-file?noredirect=1&lq=1
  62. // 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)
  63. delete coded_output;
  64. delete raw_output;
  65. fflush(stdout);
  66. return 0;
  67. }
  68. // 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)
  69. int set_up_socket(int port, sockaddr_in* address)
  70. {
  71. int server_fd = 0;
  72. // Creating socket file descriptor for listening for attestation requests.
  73. server_fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
  74. if (server_fd == -1)
  75. {
  76. printf("Error in creating a socket - %d", errno);
  77. return -1;
  78. }
  79. // Preparing the address struct for binding
  80. address->sin_family = AF_INET;
  81. address->sin_addr.s_addr = INADDR_ANY; // Todo: should this be localhost?
  82. address->sin_port = htons(port);
  83. // memset(address->sin_zero,0,sizeof(address->sin_zero));
  84. socklen_t addrlen = sizeof(*address);
  85. // Binding
  86. if (bind(server_fd, (sockaddr*)address, addrlen)<0)
  87. {
  88. printf("Error in binding %d - port was %d - ", errno, port);
  89. return -1;
  90. }
  91. // Listening
  92. if (listen(server_fd, 128) < 0)
  93. {
  94. printf("Error in listening %d", errno);
  95. return -1;
  96. }
  97. return server_fd;
  98. }
  99. int local_attestation_initiator(int port, uint32_t own_enclave_id)
  100. {
  101. // declare msg1, msg2, msg3 protobuf objects
  102. protobuf_sgx_dh_msg1_t protobuf_msg1;
  103. protobuf_sgx_dh_msg2_t protobuf_msg2;
  104. protobuf_sgx_dh_msg3_t protobuf_msg3;
  105. protobuf_post_LA_encrypted_msg_t protobuf_encrypted_msg;
  106. uint32_t protobuf_sgx_ret; uint32_t sgx_ret;
  107. // For socket to listen to the Apache enclave.
  108. int server_fd=0; int accept_fd = 0;
  109. struct sockaddr_in own_addr;
  110. struct sockaddr_storage apache_addr; socklen_t apache_addr_size = sizeof(apache_addr);
  111. uint32_t session_id; uint8_t read_or_write;
  112. size_t bytes_read_post_la; uint8_t encrypted_apache_mrsigner_and_tag[48];
  113. size_t bytes_written_post_la;
  114. // int counter;
  115. server_fd=set_up_socket(port, &own_addr);
  116. if(server_fd==-1)
  117. return -1;
  118. printf("Successfully set up a socket to communicate with the Apache enclave.\n");
  119. fflush(stdout);
  120. protobuf_sgx_ret = generate_protobuf_dh_msg1(own_enclave_id, protobuf_msg1, &session_id);
  121. if(protobuf_sgx_ret != 0)
  122. {
  123. printf("Error in generate_protobuf_dh_msg1: 0x%x", protobuf_sgx_ret); fflush(stdout); return protobuf_sgx_ret;
  124. }
  125. accept_fd = accept(server_fd, (struct sockaddr *)&apache_addr,&apache_addr_size);
  126. if (accept_fd <0)
  127. {
  128. printf("Error in accepting %d", errno);
  129. return -1;
  130. }
  131. printf("Accepted fd\n"); fflush(stdout);
  132. if(write_protobuf_msg_to_fd(accept_fd, protobuf_msg1)!=0)
  133. return -1;
  134. if(read_protobuf_msg_from_fd(accept_fd, protobuf_msg2)!=0)
  135. return -1;
  136. protobuf_sgx_ret = process_protobuf_dh_msg2_generate_protobuf_dh_msg3(own_enclave_id, protobuf_msg2, protobuf_msg3, &session_id, &read_or_write);
  137. if(protobuf_sgx_ret != 0)
  138. {
  139. printf("Error in generate_protobuf_dh_msg2: 0x%x", protobuf_sgx_ret); fflush(stdout); return protobuf_sgx_ret;
  140. }
  141. if(write_protobuf_msg_to_fd(accept_fd, protobuf_msg3)!=0)
  142. return -1;
  143. // read_or_write=0;
  144. printf("Here\n"); fflush(stdout);
  145. if(read_or_write)
  146. {
  147. bytes_read_post_la=read(accept_fd, encrypted_apache_mrsigner_and_tag, 32);
  148. if(bytes_read_post_la!=32)
  149. {
  150. printf("Not all of the encrypted apache's mrsigner was read from the verifier.\n"); fflush(stdout); return 0xfe;
  151. }
  152. bytes_read_post_la=read(accept_fd, encrypted_apache_mrsigner_and_tag+32, 16);
  153. if(bytes_read_post_la!=16)
  154. {
  155. printf("Not all of the encrypted apache's mrsigner **tag** was read from the verifier.\n"); fflush(stdout); return 0xfe;
  156. }
  157. uint32_t count;
  158. for(count=0;count<48;count++)
  159. printf("0x%02x ", encrypted_apache_mrsigner_and_tag[count]);
  160. printf("\n");fflush(stdout);
  161. // sgx_ret=decrypt_wrapper(own_enclave_id, encrypted_apache_mrsigner_and_tag, 32, encrypted_apache_mrsigner_and_tag+32 , plaintext);
  162. Decryptor_decrypt_verifiers_message_set_apache_mrsigner(own_enclave_id, &sgx_ret, encrypted_apache_mrsigner_and_tag, encrypted_apache_mrsigner_and_tag+32);
  163. if(sgx_ret!=0)
  164. {
  165. printf("Error in decryption: 0x%x\n", sgx_ret); fflush(stdout); return sgx_ret;
  166. }
  167. printf("Successful decryption\n"); fflush(stdout);
  168. }
  169. else
  170. {
  171. apache_fd=accept_fd;
  172. uint8_t encrypted_sign_data_and_sign_and_tag[176];
  173. memset(encrypted_sign_data_and_sign_and_tag,0x0,176);
  174. uint8_t plaintext_sign_data_and_sign[160];
  175. uint8_t plaintext_priv_key[32];
  176. sgx_ec256_signature_t sig2;
  177. 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);
  178. if(sgx_ret!=0)
  179. {
  180. printf("Error in generating encrypted mitigator header:0x%x\n", sgx_ret); fflush(stdout); return 0xf3;
  181. }
  182. uint32_t count;
  183. for(count=0;count<160;count++)
  184. {
  185. printf("0x%02x ", encrypted_sign_data_and_sign_and_tag[count]);
  186. }
  187. printf("\n"); fflush(stdout);
  188. printf("Plaintext Signature data:\n"); fflush(stdout);
  189. for(count=0;count<96;count++)
  190. {
  191. printf("0x%02x ", plaintext_sign_data_and_sign[count]);
  192. }
  193. printf("\n"); fflush(stdout);
  194. printf("Plaintext signature: \n"); fflush(stdout);
  195. for(count=0;count<32;count++)
  196. {
  197. printf("%02x", plaintext_sign_data_and_sign[count+96]);
  198. }
  199. printf("\n"); fflush(stdout);
  200. for(count=32;count<64;count++)
  201. {
  202. printf("%02x", plaintext_sign_data_and_sign[count+96]);
  203. }
  204. printf("\n"); fflush(stdout);
  205. printf("Heres the private key used to sign this \n"); // TODO: Remove this printf and the private key parts to the ecall
  206. for(count=0;count<32;count++)
  207. printf("%02x", plaintext_priv_key[31-count]);
  208. printf("\n"); fflush(stdout);
  209. for(count=0;count<8;count++)
  210. {
  211. printf("%02x ", sig2.x[count]);
  212. }
  213. printf("\n"); fflush(stdout);
  214. for(count=0;count<8;count++)
  215. {
  216. printf("%02x ", sig2.y[count]);
  217. }
  218. printf("\n"); fflush(stdout);
  219. protobuf_encrypted_msg.set_msg((void*)encrypted_sign_data_and_sign_and_tag, 176);
  220. if(write_protobuf_msg_to_fd(apache_fd, protobuf_encrypted_msg) != 0)
  221. {
  222. printf("Not all of the decryptor's signature was written to the Apache.\n"); fflush(stdout); return 0xfe;
  223. }
  224. unsigned char op_plaintext[160];
  225. do {
  226. // sleep(100);
  227. decrypt_client_data_wrapper(op_plaintext, own_enclave_id);
  228. // sleep(100);
  229. } while(true);
  230. }
  231. printf("Successfully done Local attestation\n");
  232. fflush(stdout);
  233. return 0;
  234. }
  235. // decrypt_client_data function that does a read-then-write loop
  236. // say msg length amount of bytes are read
  237. ////// TODO: first need to decrypt all data received with the aes-gcm apache_iv
  238. ////// Decryptor.cpp function gets the first 64 bytes as first arg and the rest of ciphertext as 2nd arg.
  239. // then need to call the sgx's compute_shared_key fn for ECDH key - on first 64 bytes.
  240. // print this key
  241. // call sha256 on it
  242. // TODO: decrypt rest of msglength - 64 bytes using this key. (set IV in crypto function)
  243. ////// TODO: then encrypt all user data with apache's key
  244. ////// return here
  245. /// write data.
  246. int decrypt_client_data_wrapper(unsigned char* op_plaintext , uint32_t own_enclave_id)
  247. {
  248. protobuf_post_LA_encrypted_msg_t protobuf_encrypted_msg; uint32_t sgx_ret_status;
  249. printf("Reading msg from apache"); fflush(stdout);
  250. if(read_protobuf_msg_from_fd(apache_fd, protobuf_encrypted_msg)!=0)
  251. {
  252. printf("Not all of the Apache's message was read\n"); fflush(stdout); return 0xfe;
  253. }
  254. std::string protobuf_encrypted_msg_string(protobuf_encrypted_msg.msg());
  255. // printf("%s\n", protobuf_encrypted_msg_string.c_str()); fflush(stdout);
  256. const char* temp_array = protobuf_encrypted_msg_string.c_str();
  257. int counter;
  258. for(counter=0;counter<protobuf_encrypted_msg_string.length();counter++)
  259. printf("%d ",temp_array[counter]);
  260. printf("\n"); fflush(stdout);
  261. unsigned char client_data_from_apache[160+64]; //protobuf_encrypted_msg_string.size()); // set a large limit for "size" for third argument - client data - of 160 - so 160+64
  262. memset(client_data_from_apache, 0, 160+64);
  263. unsigned char* protobuf_string = (unsigned char*) protobuf_encrypted_msg_string.c_str();
  264. // client_data_from_apache = (unsigned char*) protobuf_encrypted_msg_string.c_str(); //temp_array;//(unsigned char*)&(std::vector<char> (protobuf_encrypted_msg_string.begin(), protobuf_encrypted_msg_string.end()))[0]; // This shit eats up 8 starting bytes - sets them to 0s
  265. for(counter=0;counter<protobuf_encrypted_msg_string.length();counter++)
  266. client_data_from_apache[counter]=*(protobuf_string+counter);
  267. uint32_t ciphertext_length = protobuf_encrypted_msg_string.length()-64; // we hope it's greater than 0
  268. printf("Key in big endian form:\n"); fflush(stdout);
  269. for(counter=0; counter<64; counter++)
  270. printf("0x%02x ", *(client_data_from_apache + counter));
  271. printf("\n"); fflush(stdout);
  272. unsigned char client_data_to_apache[160+64]; //ciphertext_length);
  273. // Just so that the ciphertext is returned back to Apache in case decrypt_client_data fails.
  274. for(counter=0;counter<ciphertext_length;counter++)
  275. client_data_to_apache[counter]=client_data_from_apache[counter+64];
  276. uint8_t clen;
  277. Decryptor_decrypt_client_data(own_enclave_id, &sgx_ret_status, client_data_from_apache, ciphertext_length, client_data_from_apache + 64, client_data_to_apache, &clen);
  278. if(sgx_ret_status != 0)
  279. {
  280. printf("decrypt_client_data returned :0x%x\n", sgx_ret_status); fflush(stdout); //return sgx_ret_status;
  281. }
  282. // TODO: REMOVE THIS -
  283. // for(counter=0;counter<ciphertext_length;counter++)
  284. // client_data_to_apache[counter]=0x46;//client_data_from_apache[counter+64];
  285. // TODO: Print the bytesize of the message. should be ciphertext_length=4 + more bytes for protobuf packing.
  286. printf("About to write the following bytes to the apache\n"); fflush(stdout);
  287. for(counter=0;counter<ciphertext_length;counter++)
  288. printf("0x%02x ", client_data_to_apache[counter]);
  289. printf("\n"); fflush(stdout);
  290. printf("Following shared secret was established.\n");
  291. for(counter=ciphertext_length;counter<32+ciphertext_length;counter++)
  292. printf("0x%02x ", client_data_to_apache[counter]);
  293. printf("\n"); fflush(stdout);
  294. printf("Following key was derived.\n");
  295. for(counter=ciphertext_length+32;counter<64+ciphertext_length;counter++)
  296. printf("0x%02x ", client_data_to_apache[counter]);
  297. printf("\n"); fflush(stdout);
  298. printf("Ciphertext for the string Miti \n");
  299. for(counter=ciphertext_length+64;counter<64+ciphertext_length + 4;counter++)
  300. printf("%d ", client_data_to_apache[counter]);
  301. printf("\n"); fflush(stdout);
  302. printf("Tag for the string Miti \n");
  303. for(counter=ciphertext_length+64+4;counter<80+ciphertext_length + 4;counter++)
  304. printf("%d ", client_data_to_apache[counter]);
  305. printf("\n"); fflush(stdout);
  306. protobuf_encrypted_msg.set_msg((void*) client_data_to_apache, ciphertext_length);// Is this message set tho?
  307. if(write_protobuf_msg_to_fd(apache_fd, protobuf_encrypted_msg)!=0)
  308. {
  309. printf("Not all of the protobuf message was written to Apache.\n"); fflush(stdout); return 0xfe;
  310. }
  311. printf("Wrote data to Apache of length %d \n", protobuf_encrypted_msg.ByteSize());fflush(stdout);
  312. return 0;
  313. }