ProtobufLAInitiator.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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.pb.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. #include "crypto.h"
  13. // For socket programming
  14. #include <arpa/inet.h>
  15. #include <sys/socket.h>
  16. #include <netinet/in.h>
  17. int global_decryptor_fd;
  18. // TODO: Make these private functions
  19. int read_protobuf_msg_from_fd(int accept_fd, google::protobuf::MessageLite& message)
  20. {
  21. ZeroCopyInputStream* raw_input;
  22. CodedInputStream* coded_input;
  23. uint32_t size;
  24. CodedInputStream::Limit limit;
  25. raw_input = new FileInputStream(accept_fd);
  26. coded_input = new CodedInputStream(raw_input);
  27. if(!coded_input->ReadVarint32(&size))
  28. {
  29. printf("Error in reading size of msg\n");
  30. fflush(stdout);
  31. return -1;
  32. }
  33. printf("size of msg was read to be %d \n", size);
  34. fflush(stdout);
  35. limit = coded_input->PushLimit(size);
  36. if(!message.ParseFromCodedStream(coded_input))
  37. {
  38. printf("Error in parsing msg\n");
  39. fflush(stdout);
  40. return -1;
  41. }
  42. printf("Done parsing msg\n"); fflush(stdout);
  43. coded_input->PopLimit(limit);
  44. return 0;
  45. }
  46. // TODO: private functions
  47. int write_protobuf_msg_to_fd(int accept_fd, google::protobuf::MessageLite& message)
  48. {
  49. ZeroCopyOutputStream* raw_output = new FileOutputStream(accept_fd);
  50. CodedOutputStream* coded_output = new CodedOutputStream(raw_output);
  51. coded_output->WriteVarint32(message.ByteSize());
  52. if(!message.SerializeToCodedStream(coded_output))
  53. {
  54. printf("SerializeToCodedStream failed\n");
  55. fflush(stdout);
  56. return -1;
  57. }
  58. // As per this - https://stackoverflow.com/questions/22881876/protocol-buffers-how-to-serialize-and-deserialize-multiple-messages-into-a-file?noredirect=1&lq=1
  59. // 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)
  60. delete coded_output;
  61. delete raw_output;
  62. fflush(stdout);
  63. return 0;
  64. }
  65. // Sets up a socket connected to the port passed as input - returns the socket FD on success and -1 on error.
  66. // Also prints the errno on error.
  67. int set_up_socket_connect(int port)
  68. {
  69. int sock = 0;
  70. if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  71. {
  72. printf("\n Error in socket call - errno is %d \n", errno);
  73. return -1;
  74. }
  75. struct sockaddr_in serv_addr;
  76. memset(&serv_addr, '0', sizeof(serv_addr));
  77. serv_addr.sin_family = AF_INET;
  78. serv_addr.sin_port = htons(port);
  79. // Convert IPv4 and IPv6 addresses from text to binary form
  80. if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
  81. {
  82. printf("\nError in inet_pton - errno is %d\n", errno);
  83. return -1;
  84. }
  85. if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
  86. {
  87. printf("\nError in connect - errno is %d \n", errno);
  88. return -1;
  89. }
  90. return sock;
  91. }
  92. int local_attestation_initiator(int port, unsigned char* base64_encoded_token)
  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. // uint8_t encrypted_hash[32]; uint8_t encrypted_tag[16]; size_t post_la_bytes_written;
  100. // uint8_t tokenT_and_tag[176]; size_t bytes_read;
  101. // For socket to listen to the Apache enclave.
  102. // int server_fd=0; int accept_fd = 0;
  103. // std::string protobuf_encrypted_msg_string(protobuf_encrypted_msg.msg());
  104. // memcpy(tokenT_and_tag, protobuf_encrypted_msg_string.c_str(), 176);
  105. // struct sockaddr_in own_addr;
  106. // struct sockaddr_storage apache_addr; socklen_t apache_addr_size = sizeof(apache_addr);
  107. uint32_t session_id;
  108. // int counter;
  109. int decryptor_fd;
  110. setbuf(stdout,NULL);
  111. decryptor_fd=set_up_socket_connect(port);
  112. if(decryptor_fd == -1)
  113. {
  114. perror("\nCould not set up the socket: had the following error: ");
  115. fflush(stderr);
  116. }
  117. // printf("");
  118. if(read_protobuf_msg_from_fd(decryptor_fd, protobuf_msg1)!=0)
  119. return -1;
  120. protobuf_sgx_ret = process_protobuf_dh_msg1_generate_protobuf_dh_msg2(protobuf_msg1, protobuf_msg2, &session_id);
  121. if(protobuf_sgx_ret != 0)
  122. {
  123. printf("Error in process_protobuf_dh_msg1_generate_protobuf_dh_msg2: 0x%x", protobuf_sgx_ret); fflush(stdout); return protobuf_sgx_ret;
  124. }
  125. if(write_protobuf_msg_to_fd(decryptor_fd, protobuf_msg2)!=0)
  126. return -1;
  127. if(read_protobuf_msg_from_fd(decryptor_fd, protobuf_msg3)!=0)
  128. return -1;
  129. protobuf_sgx_ret = process_protobuf_dh_msg3(protobuf_msg3, &session_id);
  130. if(protobuf_sgx_ret != 0)
  131. {
  132. printf("Error in process_protobuf_dh_msg3: 0x%x", protobuf_sgx_ret); fflush(stdout); return protobuf_sgx_ret;
  133. }
  134. unsigned char tokenT_and_tag[176]; int protobuf_encrypted_msg_size;
  135. protobuf_post_LA_encrypted_msg_t protobuf_encrypted_msg;
  136. if(read_protobuf_msg_from_fd(decryptor_fd, protobuf_encrypted_msg)!=0)
  137. {
  138. printf("Not all of the post-LA message was read\n"); fflush(stdout); return 0xfe;
  139. }
  140. std::string protobuf_encrypted_msg_string(protobuf_encrypted_msg.msg());
  141. memcpy(tokenT_and_tag, protobuf_encrypted_msg_string.c_str(), 176);
  142. printf("Read encrypted signature and tag from the decryptor socket.\n"); fflush(stdout);
  143. uint32_t count;
  144. printf("Encrypted data:\n");
  145. for(count=0;count<176;count++)
  146. printf("0x%02x ", tokenT_and_tag[count]);
  147. printf("\n"); fflush(stdout);
  148. uint8_t plaintext_token[160]; int plaintext_length=0;
  149. uint32_t ret_decrypt=aes_gcm_wrapper(0, tokenT_and_tag, 160, plaintext_token, &plaintext_length, tokenT_and_tag+160);
  150. //int aes_cipher(int enc, unsigned char *key, unsigned char *iv, unsigned char* plaintext, int plaintext_len, unsigned char *ciphertext, int* op_ciphertext_len, unsigned char* tag);
  151. if(ret_decrypt!=0)
  152. {
  153. printf("Errorin decryption 0x%x", ret_decrypt); fflush(stdout); return 0xed;
  154. }
  155. printf("Decryption keypair\n");
  156. for(count=0;count<64;count++)
  157. printf("0x%02x ", plaintext_token[count]);
  158. printf("\n"); fflush(stdout);
  159. printf("Decryption verifier mrenclave\n");
  160. for(count=64;count<96;count++)
  161. printf("0x%02x ", plaintext_token[count]);
  162. printf("\n"); fflush(stdout);
  163. count=base64_encoding_wrapper(plaintext_token, base64_encoded_token, 160);
  164. if(count != 216)
  165. {
  166. printf("Somehow not the entire token was encoded in base64:0x%x\n", count); fflush(stdout); return 0x55;
  167. }
  168. global_decryptor_fd=decryptor_fd;
  169. printf("Successfully done Local attestation\n");
  170. fflush(stdout);
  171. return 0;
  172. }
  173. int extract_binary_key_and_client_ciphertext_data_from_base64(unsigned char* base64_encoded_client_pub_key, unsigned char* base64_ciphertext, uint32_t base64_ciphertext_length, unsigned char* op_client_public_key_and_ciphertext, uint32_t* op_ciphertext_length)
  174. {
  175. int openssl_ret_status;
  176. openssl_ret_status=base64_decoding_wrapper(base64_encoded_client_pub_key, op_client_public_key_and_ciphertext, 88);
  177. if(openssl_ret_status == -1)
  178. return 0xfe;
  179. *op_ciphertext_length = base64_decoding_wrapper(base64_ciphertext, op_client_public_key_and_ciphertext + 64, base64_ciphertext_length);
  180. if(*op_ciphertext_length == -1)
  181. {
  182. return 0x33;
  183. }
  184. return 0;
  185. }
  186. // should be called with the base64encoded public key and data to be decrypted
  187. // base64 encoded public key should be of length 4*(ceil(64/3)) = 88 (at least)
  188. int decrypt_client_data_through_decryptor(unsigned char* base64_client_public_key, unsigned char* base64_ciphertext_client_data, uint32_t base64_ciphertext_client_data_length, unsigned char* plaintext_client_data, uint32_t* op_plaintext_client_data_length)
  189. {
  190. int ret_status;
  191. uint32_t count;
  192. unsigned char* client_data_to_decryptor;
  193. unsigned char* client_data_encrypted_to_decryptor;
  194. unsigned char* client_data_from_decryptor;
  195. unsigned char tag[16];
  196. const char* msg;
  197. protobuf_post_LA_encrypted_msg_t protobuf_encrypted_msg;
  198. int output_length;
  199. int input_length;
  200. uint32_t ciphertext_length;
  201. for(count=0; count<base64_ciphertext_client_data_length; count++)
  202. plaintext_client_data[count] = base64_ciphertext_client_data[count];
  203. *op_plaintext_client_data_length = base64_ciphertext_client_data_length;
  204. client_data_to_decryptor = (unsigned char*) malloc(64 + (base64_ciphertext_client_data_length * 3/4));
  205. // If ciphertext_length != base64_ciphertext_client_data_length * 3/4 then it will write outside the allocated memory
  206. ret_status = extract_binary_key_and_client_ciphertext_data_from_base64(base64_client_public_key, base64_ciphertext_client_data, base64_ciphertext_client_data_length, client_data_to_decryptor, &ciphertext_length);
  207. if(ret_status != 0)
  208. return ret_status;
  209. input_length = ciphertext_length + 64; // normal case - no writing outside the allocated memory.
  210. printf("Public key and ciphertext:\n"); fflush(stdout);
  211. for(count = 0; count<input_length; count++)
  212. printf("0x%02x ", client_data_to_decryptor[count]);
  213. printf("\n"); fflush(stdout);
  214. /* client_data_encrypted_to_decryptor = (unsigned char*) malloc(input_length + 16);
  215. // TODO: try to call aes_gcm *in encryption mode* on ciphertext data of ciphertext_length (where is the iv?)
  216. // if output_length != ciphertext_length + 64 then tag will be overwritten.
  217. ret_status = aes_gcm_wrapper(1, client_data_to_decryptor, input_length, client_data_encrypted_to_decryptor, &output_length, tag);
  218. if(ret_status != 0 || output_length != input_length)
  219. {
  220. free(client_data_to_decryptor);
  221. free(client_data_encrypted_to_decryptor);
  222. return ret_status;
  223. }
  224. for(count=0; count<16; count++)
  225. client_data_encrypted_to_decryptor[output_length+count]=tag[count];
  226. // TODO: Increment AESGCM IV here.
  227. */
  228. protobuf_encrypted_msg.set_msg((void*)client_data_to_decryptor, input_length);
  229. free(client_data_to_decryptor);
  230. // free(client_data_encrypted_to_decryptor);
  231. /*
  232. printf("Writing this to the decryptor: \n"); fflush(stdout);
  233. for(count=8;count<output_length+16+8;count++)
  234. printf("0x%02x ", client_data_to_decryptor[count]);
  235. printf("\n"); fflush(stdout);
  236. */
  237. // write message to decryptor
  238. if(write_protobuf_msg_to_fd(global_decryptor_fd, protobuf_encrypted_msg)!=0)
  239. {
  240. printf("Not all of the client's pub key and ciphertext data was written\n"); fflush(stdout); return 0xfe;
  241. }
  242. // clear the message before setting it again
  243. protobuf_encrypted_msg.clear_msg();
  244. // read encrypted data
  245. printf("Reading msg from decryptor"); fflush(stdout);
  246. if(read_protobuf_msg_from_fd(global_decryptor_fd, protobuf_encrypted_msg)!=0)
  247. {
  248. printf("Not all of the decryptor's message was read\n"); fflush(stdout); return 0xf3;
  249. }
  250. printf("Done reading msg from decryptor\n"); fflush(stdout);
  251. // define plaintext array
  252. input_length = protobuf_encrypted_msg.msg().length();
  253. msg = protobuf_encrypted_msg.msg().c_str();
  254. for(count=0; count<input_length; count++)
  255. plaintext_client_data[count] = msg[count];
  256. *op_plaintext_client_data_length = input_length;
  257. /* client_data_from_decryptor=(unsigned char*)malloc(input_length);
  258. memcpy(client_data_from_decryptor, protobuf_encrypted_msg.msg().c_str(), input_length); //TODO: THIS might be a problem - c_str might terminate at null bytes?
  259. // TODO: decrypt data received in the protobuf_encrypted_msg, set bytes at plaintext_client_data to it.
  260. for(count=0;count<input_length;count++)
  261. {
  262. plaintext_client_data[count]=client_data_from_decryptor[count];
  263. printf("0x%02x ", client_data_from_decryptor[count]);
  264. }
  265. printf("\n"); fflush(stdout);
  266. *op_plaintext_client_data_length=input_length;
  267. ret_status = aes_gcm_wrapper(0, client_data_from_decryptor, input_length, plaintext_client_data, &output_length, client_data_from_decryptor + input_length - 16);
  268. if(ret_status == 0)
  269. *op_plaintext_client_data_length = output_length;
  270. free(client_data_from_decryptor);
  271. */
  272. return 0;
  273. }