flow.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  1. /* Name: flow.c
  2. *
  3. * This file contains functions for manipulating tagged flows.
  4. *
  5. * Slitheen - a decoy routing system for censorship resistance
  6. * Copyright (C) 2017 Cecylia Bocovich (cbocovic@uwaterloo.ca)
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, version 3.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * Additional permission under GNU GPL version 3 section 7
  21. *
  22. * If you modify this Program, or any covered work, by linking or combining
  23. * it with the OpenSSL library (or a modified version of that library),
  24. * containing parts covered by the terms of the OpenSSL Licence and the
  25. * SSLeay license, the licensors of this Program grant you additional
  26. * permission to convey the resulting work. Corresponding Source for a
  27. * non-source form of such a combination shall include the source code
  28. * for the parts of the OpenSSL library used as well as that of the covered
  29. * work.
  30. */
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <stdint.h>
  34. #include <pthread.h>
  35. #include <errno.h>
  36. #include <semaphore.h>
  37. #include <string.h>
  38. #include "flow.h"
  39. #include "crypto.h"
  40. #include "packet.h"
  41. #include "relay.h"
  42. #include "util.h"
  43. /* Data structures */
  44. typedef struct packet_chain_st {
  45. packet *first_packet;
  46. uint32_t expected_seq_num;
  47. uint32_t record_len;
  48. uint32_t remaining_record_len;
  49. } packet_chain;
  50. typedef struct session_cache_st {
  51. session *first_session;
  52. uint32_t length;
  53. } session_cache;
  54. typedef struct flow_entry_st {
  55. flow *f;
  56. struct flow_entry_st *next;
  57. } flow_entry;
  58. typedef struct flow_table_st {
  59. flow_entry *first_entry;
  60. int len;
  61. } flow_table;
  62. static flow_table *table;
  63. static session_cache *sessions;
  64. client_table *clients;
  65. sem_t flow_table_lock;
  66. #define TLS_HELLO_REQ 0x00
  67. #define TLS_CLNT_HELLO 0x01
  68. #define TLS_SERV_HELLO 0x02
  69. #define TLS_NEW_SESS 0x04
  70. #define TLS_CERT 0x0b
  71. #define TLS_SRVR_KEYEX 0x0c
  72. #define TLS_CERT_REQ 0x0d
  73. #define TLS_SRVR_HELLO_DONE 0x0e
  74. #define TLS_CERT_VERIFY 0x0f
  75. #define TLS_CLNT_KEYEX 0x10
  76. #define TLS_FINISHED 0x14
  77. #define TLS_CERT_STATUS 0x16
  78. static int update_flow(flow *f, uint8_t *record, uint8_t incoming);
  79. static int verify_session_id(flow *f, uint8_t *hs);
  80. static int check_extensions(flow *f, uint8_t *hs, uint32_t len);
  81. static int verify_extensions(flow *f, uint8_t *hs, uint32_t len);
  82. static int save_session_id(flow *f, uint8_t *hs);
  83. static int save_session_ticket(flow *f, uint8_t *hs, uint32_t len);
  84. /* Initialize the table of tagged flows */
  85. int init_tables(void) {
  86. table = smalloc(sizeof(flow_table));
  87. table->first_entry = NULL;
  88. table->len = 0;
  89. sem_init(&flow_table_lock, 0, 1);
  90. clients = smalloc(sizeof(client_table));
  91. clients->first = NULL;
  92. printf("initialized downstream queue\n");
  93. return 0;
  94. }
  95. /* Add a new flow to the tagged flow table */
  96. flow *add_flow(struct packet_info *info) {
  97. flow_entry *entry = smalloc(sizeof(flow_entry));
  98. flow *new_flow = smalloc(sizeof(flow));
  99. entry->f = new_flow;
  100. entry->next = NULL;
  101. new_flow->src_ip = info->ip_hdr->src;
  102. new_flow->dst_ip = info->ip_hdr->dst;
  103. new_flow->src_port = info->tcp_hdr->src_port;
  104. new_flow->dst_port = info->tcp_hdr->dst_port;
  105. new_flow->ref_ctr = 1;
  106. new_flow->removed = 0;
  107. new_flow->upstream_app_data = smalloc(sizeof(app_data_queue));
  108. new_flow->upstream_app_data->first_packet = NULL;
  109. new_flow->downstream_app_data = smalloc(sizeof(app_data_queue));
  110. new_flow->downstream_app_data->first_packet = NULL;
  111. new_flow->upstream_seq_num = ntohl(info->tcp_hdr->sequence_num);
  112. new_flow->downstream_seq_num = ntohl(info->tcp_hdr->ack_num);
  113. new_flow->us_frame_queue = smalloc(sizeof(frame_queue));
  114. new_flow->us_frame_queue->first_frame = NULL;
  115. new_flow->ds_frame_queue = smalloc(sizeof(frame_queue));
  116. new_flow->ds_frame_queue->first_frame = NULL;
  117. new_flow->downstream_queue=NULL;
  118. new_flow->client_ptr=NULL;
  119. sem_init(&(new_flow->flow_lock), 0, 1);
  120. new_flow->state = TLS_CLNT_HELLO;
  121. new_flow->in_encrypted = 0;
  122. new_flow->out_encrypted = 0;
  123. new_flow->application = 0;
  124. new_flow->stall = 0;
  125. new_flow->extended_master_secret = 0;
  126. new_flow->resume_session = 0;
  127. new_flow->current_session = NULL;
  128. new_flow->us_hs_queue = init_queue();
  129. new_flow->ds_hs_queue = init_queue();
  130. new_flow->us_packet_chain = smalloc(sizeof(packet_chain));
  131. new_flow->us_packet_chain->expected_seq_num = ntohl(info->tcp_hdr->sequence_num);
  132. new_flow->us_packet_chain->record_len = 0;
  133. new_flow->us_packet_chain->remaining_record_len = 0;
  134. new_flow->us_packet_chain->first_packet = NULL;
  135. new_flow->ds_packet_chain = smalloc(sizeof(packet_chain));
  136. new_flow->ds_packet_chain->expected_seq_num = ntohl(info->tcp_hdr->ack_num);
  137. new_flow->ds_packet_chain->record_len = 0;
  138. new_flow->ds_packet_chain->remaining_record_len = 0;
  139. new_flow->ds_packet_chain->first_packet = NULL;
  140. sem_init(&(new_flow->packet_chain_lock), 0, 1);
  141. new_flow->upstream_queue = NULL;
  142. new_flow->upstream_remaining = 0;
  143. sem_init(&(new_flow->upstream_queue_lock), 0, 1);
  144. new_flow->outbox = NULL;
  145. new_flow->outbox_len = 0;
  146. new_flow->outbox_offset = 0;
  147. new_flow->partial_record_header = NULL;
  148. new_flow->partial_record_header_len = 0;
  149. new_flow->partial_record = NULL;
  150. new_flow->partial_record_dec = NULL;
  151. new_flow->partial_record_len = 0;
  152. new_flow->partial_record_total_len = 0;
  153. new_flow->remaining_record_len = 0;
  154. new_flow->remaining_response_len = 0;
  155. new_flow->httpstate = PARSE_HEADER;
  156. new_flow->webmstate = 0;
  157. new_flow->remaining_element = 0;
  158. new_flow->replace_response = 0;
  159. new_flow->ecdh = NULL;
  160. new_flow->srvr_key = NULL;
  161. new_flow->dh = NULL;
  162. new_flow->hs_md_ctx = EVP_MD_CTX_create();
  163. const EVP_MD *md = EVP_sha256();
  164. EVP_DigestInit_ex(new_flow->hs_md_ctx, md, NULL);
  165. new_flow->cipher = NULL;
  166. new_flow->clnt_read_ctx = NULL;
  167. new_flow->clnt_write_ctx = NULL;
  168. new_flow->srvr_read_ctx = NULL;
  169. new_flow->srvr_write_ctx = NULL;
  170. new_flow->gcm_ctx_out = NULL;
  171. new_flow->gcm_ctx_iv = NULL;
  172. new_flow->gcm_ctx_key = NULL;
  173. memset(new_flow->read_seq, 0, 8);
  174. memset(new_flow->write_seq, 0, 8);
  175. sem_wait(&flow_table_lock);
  176. flow_entry *last = table->first_entry;
  177. if(last == NULL){
  178. table->first_entry = entry;
  179. } else {
  180. for(int i=0; i< table->len-1; i++){
  181. last = last->next;
  182. }
  183. last->next = entry;
  184. }
  185. table->len ++;
  186. sem_post(&flow_table_lock);
  187. return new_flow;
  188. }
  189. /** Observes TLS handshake messages and updates the state of
  190. * the flow
  191. *
  192. * Inputs:
  193. * f: the tagged flow
  194. * record: a complete TLS record
  195. *
  196. * Output:
  197. * 0 on success, 1 on failure
  198. */
  199. static int update_flow(flow *f, uint8_t *record, uint8_t incoming) {
  200. const struct record_header *record_hdr;
  201. const struct handshake_header *handshake_hdr;
  202. uint8_t *p;
  203. record_hdr = (struct record_header*) record;
  204. int record_len;
  205. record_len = RECORD_LEN(record_hdr)+RECORD_HEADER_LEN;
  206. switch(record_hdr->type){
  207. case HS:
  208. p = record;
  209. DEBUG_MSG(DEBUG_HS, "Received handshake packet (%x:%d -> %x:%d) (incoming: %d)\n", f->src_ip.s_addr, ntohs(f->src_port), f->dst_ip.s_addr, ntohs(f->dst_port), incoming);
  210. p += RECORD_HEADER_LEN;
  211. if((incoming && f->in_encrypted) || (!incoming && f->out_encrypted)){
  212. DEBUG_MSG(DEBUG_HS, "Decrypting finished (%d bytes) (%x:%d -> %x:%d) (incoming: %d)\n", record_len - RECORD_HEADER_LEN, f->src_ip.s_addr, ntohs(f->src_port), f->dst_ip.s_addr, ntohs(f->dst_port), incoming);
  213. DEBUG_BYTES(DEBUG_HS, record, record_len);
  214. int32_t n = encrypt(f, p, p, record_len - RECORD_HEADER_LEN, incoming, 0x16, 0, 0);
  215. if(n<=0){
  216. printf("Error decrypting finished (%x:%d -> %x:%d) (incoming: %d)\n", f->src_ip.s_addr, ntohs(f->src_port), f->dst_ip.s_addr, ntohs(f->dst_port), incoming);
  217. printf("record:\n");
  218. for(int i=0; i< 12; i++){
  219. printf("%02x ", p[i]);
  220. }
  221. }
  222. DEBUG_MSG(DEBUG_HS, "Finished decrypted: (%x:%d -> %x:%d)\n", f->src_ip.s_addr, ntohs(f->src_port), f->dst_ip.s_addr, ntohs(f->dst_port));
  223. p += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  224. DEBUG_BYTES(DEBUG_HS, p, n);
  225. if(p[0] != 0x14){
  226. p[0] = 0x20; //trigger error
  227. }
  228. if(incoming){
  229. f->in_encrypted = 2;
  230. } else {
  231. f->out_encrypted = 2;
  232. }
  233. }
  234. handshake_hdr = (struct handshake_header*) p;
  235. f->state = handshake_hdr->type;
  236. switch(f->state){
  237. case TLS_CLNT_HELLO:
  238. DEBUG_MSG(DEBUG_HS, "Received tagged client hello (%x:%d -> %x:%d)\n", f->src_ip.s_addr, ntohs(f->src_port), f->dst_ip.s_addr, ntohs(f->dst_port));
  239. if(check_extensions(f, p, HANDSHAKE_MESSAGE_LEN(handshake_hdr))){
  240. fprintf(stderr, "Error checking session, might cause problems\n");
  241. }
  242. if(update_handshake_hash(f, p)){
  243. fprintf(stderr, "Error updating finish has with CLNT_HELLO msg\n");
  244. remove_flow(f);
  245. goto err;
  246. }
  247. break;
  248. case TLS_SERV_HELLO:
  249. DEBUG_MSG(DEBUG_HS, "Received server hello (%x:%d -> %x:%d)\n", f->src_ip.s_addr, ntohs(f->src_port), f->dst_ip.s_addr, ntohs(f->dst_port));
  250. if(f->resume_session){
  251. if(verify_session_id(f,p)){
  252. DEBUG_MSG(DEBUG_HS, "Failed to verify session id\n");
  253. }
  254. } else {
  255. if(save_session_id(f,p)){
  256. DEBUG_MSG(DEBUG_HS, "Failed to save session id\n");
  257. }
  258. }
  259. if(verify_extensions(f,p, HANDSHAKE_MESSAGE_LEN(handshake_hdr))){
  260. DEBUG_MSG(DEBUG_HS, "Failed to verify extensions\n");
  261. }
  262. if(extract_server_random(f, p)){
  263. DEBUG_MSG(DEBUG_HS, "Failed to extract server random nonce\n");
  264. remove_flow(f);
  265. goto err;
  266. }
  267. if(update_handshake_hash(f, p)){
  268. DEBUG_MSG(DEBUG_HS, "Error updating finish has with CLNT_HELLO msg\n");
  269. remove_flow(f);
  270. goto err;
  271. }
  272. break;
  273. case TLS_NEW_SESS:
  274. DEBUG_MSG(DEBUG_HS, "Received new session\n");
  275. if(save_session_ticket(f, p, HANDSHAKE_MESSAGE_LEN(handshake_hdr))){
  276. DEBUG_MSG(DEBUG_HS, "Failed to save session ticket\n");
  277. }
  278. break;
  279. case TLS_CERT:
  280. DEBUG_MSG(DEBUG_HS, "Received cert\n");
  281. if(update_handshake_hash(f, p)){
  282. remove_flow(f);
  283. goto err;
  284. }
  285. break;
  286. case TLS_CERT_STATUS:
  287. DEBUG_MSG(DEBUG_HS, "Received certificate status\n");
  288. if(update_handshake_hash(f, p)){
  289. remove_flow(f);
  290. goto err;
  291. }
  292. break;
  293. case TLS_SRVR_KEYEX:
  294. DEBUG_MSG(DEBUG_HS, "Received server keyex\n");
  295. if(extract_parameters(f, p)){
  296. DEBUG_MSG(DEBUG_HS, "Error extracting params\n");
  297. remove_flow(f);
  298. goto err;
  299. }
  300. if(update_handshake_hash(f, p)){
  301. remove_flow(f);
  302. goto err;
  303. }
  304. break;
  305. case TLS_CERT_REQ:
  306. if(update_handshake_hash(f, p)){
  307. remove_flow(f);
  308. goto err;
  309. }
  310. break;
  311. case TLS_SRVR_HELLO_DONE:
  312. DEBUG_MSG(DEBUG_HS, "Received server hello done\n");
  313. if(update_handshake_hash(f, p)){
  314. remove_flow(f);
  315. goto err;
  316. }
  317. break;
  318. case TLS_CERT_VERIFY:
  319. DEBUG_MSG(DEBUG_HS, "received cert verify\n");
  320. if(update_handshake_hash(f, p)){
  321. remove_flow(f);
  322. goto err;
  323. }
  324. break;
  325. case TLS_CLNT_KEYEX:
  326. DEBUG_MSG(DEBUG_HS, "Received client key exchange\n");
  327. if(update_handshake_hash(f, p)){
  328. remove_flow(f);
  329. goto err;
  330. }
  331. if(compute_master_secret(f)){
  332. DEBUG_MSG(DEBUG_HS, "Error computing master secret\n");
  333. remove_flow(f);
  334. goto err;
  335. }
  336. break;
  337. case TLS_FINISHED:
  338. DEBUG_MSG(DEBUG_HS, "Received finished (%d) (%x:%d -> %x:%d)\n", incoming, f->src_ip.s_addr, ntohs(f->src_port), f->dst_ip.s_addr, ntohs(f->dst_port));
  339. if((f->in_encrypted == 2) && (f->out_encrypted == 2)){
  340. f->application = 1;
  341. DEBUG_MSG(DEBUG_HS, "Handshake complete!\n");
  342. }
  343. if(!incoming) {
  344. // We only care about incoming
  345. // Finished messages
  346. break;
  347. }
  348. if(mark_finished_hash(f, p)){
  349. DEBUG_MSG(DEBUG_HS, "Error marking finished hash\n");
  350. remove_flow(f);
  351. goto err;
  352. }
  353. //re-encrypt finished message
  354. int32_t n = encrypt(f, record+RECORD_HEADER_LEN, record+RECORD_HEADER_LEN, record_len - (RECORD_HEADER_LEN+16), incoming, 0x16, 1, 1);
  355. DEBUG_MSG(DEBUG_HS, "New finished ciphertext:\n");
  356. DEBUG_BYTES(DEBUG_HS, record, record_len);
  357. if(n<=0){
  358. DEBUG_MSG(DEBUG_HS, "Error re-encrypting finished (%x:%d -> %x:%d)\n", f->src_ip.s_addr, ntohs(f->src_port),
  359. f->dst_ip.s_addr, ntohs(f->dst_port));
  360. }
  361. break;
  362. default:
  363. DEBUG_MSG(DEBUG_HS, "Error: unrecognized hs message? (%x:%d -> %x:%d)...\n", f->src_ip.s_addr, ntohs(f->src_port), f->dst_ip.s_addr, ntohs(f->dst_port));
  364. remove_flow(f);
  365. goto err;
  366. }
  367. break;
  368. case APP:
  369. DEBUG_MSG(DEBUG_HS, "Application Data (%x:%d -> %x:%d)...\n", f->src_ip.s_addr, ntohs(f->src_port), f->dst_ip.s_addr, ntohs(f->dst_port));
  370. break;
  371. case CCS:
  372. DEBUG_MSG(DEBUG_HS, "CCS (%x:%d -> %x:%d) \n", f->src_ip.s_addr, ntohs(f->src_port), f->dst_ip.s_addr, ntohs(f->dst_port));
  373. /*Initialize ciphers */
  374. if ((!f->in_encrypted) && (!f->out_encrypted)){
  375. if(init_ciphers(f)){
  376. remove_flow(f);
  377. goto err;
  378. }
  379. }
  380. if(incoming){
  381. f->in_encrypted = 1;
  382. } else {
  383. f->out_encrypted = 1;
  384. }
  385. break;
  386. case ALERT:
  387. p = record;
  388. p += RECORD_HEADER_LEN;
  389. if(((incoming) && (f->in_encrypted > 0)) || ((!incoming) && (f->out_encrypted > 0))){
  390. //decrypt alert
  391. int32_t n = encrypt(f, p, p, record_len - RECORD_HEADER_LEN, incoming, 0x15, 0, 0);
  392. if(n <= 0){
  393. printf("Error decrypting Alert\n");
  394. }
  395. DEBUG_MSG(DEBUG_HS, "Decrypted alert:\n");
  396. DEBUG_BYTES(DEBUG_HS, p, n);
  397. p += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  398. }
  399. DEBUG_MSG(DEBUG_HS, "Alert (%x:%d -> %x:%d) (%s) %02x %02x \n", f->src_ip.s_addr, ntohs(f->src_port), f->dst_ip.s_addr, ntohs(f->dst_port), (incoming) ? "incoming" : "outgoing", p[0], p[1]);
  400. //re-encrypt alert
  401. if(((incoming) && (f->in_encrypted > 0)) || ((!incoming) && (f->out_encrypted > 0))){
  402. int32_t n = encrypt(f, record+RECORD_HEADER_LEN, record+RECORD_HEADER_LEN, record_len - (RECORD_HEADER_LEN+16), incoming, 0x15, 1, 1);
  403. if(n <= 0){
  404. DEBUG_MSG(DEBUG_HS, "Error re-encrypting alert\n");
  405. }
  406. }
  407. break;
  408. case HB:
  409. DEBUG_MSG(DEBUG_HS, "Heartbeat\n");
  410. break;
  411. default:
  412. DEBUG_MSG(DEBUG_HS, "Error: Not a Record (%x:%d -> %x:%d)\n", f->src_ip.s_addr, ntohs(f->src_port), f->dst_ip.s_addr, ntohs(f->dst_port));
  413. remove_flow(f);
  414. goto err;
  415. }
  416. return 0;
  417. err:
  418. return 1;
  419. }
  420. /** Removes the tagged flow from the flow table: happens when
  421. * the station receives a TCP RST or FIN packet
  422. *
  423. * Input:
  424. * index: the index into the flow table of the tagged flow
  425. *
  426. * Output:
  427. * 0 on success, 1 on failure
  428. */
  429. int remove_flow(flow *f) {
  430. sem_wait(&flow_table_lock);
  431. //decrement reference counter
  432. f->ref_ctr--;
  433. if(f->ref_ctr){ //if there are still references to f, wait to free it
  434. printf("Cannot free %p, still %d reference(s)\n", f, f->ref_ctr);
  435. f->removed = 1;
  436. sem_post(&flow_table_lock);
  437. return 0;
  438. }
  439. if(f->removed)
  440. printf("Trying again to free\n");
  441. frame *first_frame = f->us_frame_queue->first_frame;
  442. while(first_frame != NULL){
  443. printf("Injecting delayed frame (seq = %u )\n", first_frame->seq_num);
  444. inject_packet(first_frame->iargs, first_frame->header, first_frame->packet);
  445. frame *tmp = first_frame->next;
  446. free(first_frame);
  447. first_frame = tmp;
  448. }
  449. free(f->us_frame_queue);
  450. first_frame = f->ds_frame_queue->first_frame;
  451. while(first_frame != NULL){
  452. printf("Injecting delayed frame (seq = %u )\n", first_frame->seq_num);
  453. inject_packet(first_frame->iargs, first_frame->header, first_frame->packet);
  454. frame *tmp = first_frame->next;
  455. free(first_frame);
  456. first_frame = tmp;
  457. }
  458. free(f->ds_frame_queue);
  459. //Empty application data queues
  460. packet *tmp = f->upstream_app_data->first_packet;
  461. while(tmp != NULL){
  462. f->upstream_app_data->first_packet = tmp->next;
  463. free(tmp->data);
  464. free(tmp);
  465. tmp = f->upstream_app_data->first_packet;
  466. }
  467. free(f->upstream_app_data);
  468. tmp = f->downstream_app_data->first_packet;
  469. while(tmp != NULL){
  470. f->downstream_app_data->first_packet = tmp->next;
  471. free(tmp->data);
  472. free(tmp);
  473. tmp = f->downstream_app_data->first_packet;
  474. }
  475. free(f->downstream_app_data);
  476. if(f->ds_hs_queue != NULL){
  477. remove_queue(f->ds_hs_queue);
  478. }
  479. if(f->us_hs_queue != NULL){
  480. remove_queue(f->us_hs_queue);
  481. }
  482. //free partial record headers
  483. if(f->partial_record_header_len > 0){
  484. f->partial_record_header_len = 0;
  485. free(f->partial_record_header);
  486. }
  487. if(f->partial_record_dec != NULL){
  488. free(f->partial_record_dec);
  489. }
  490. if(f->partial_record != NULL){
  491. free(f->partial_record);
  492. }
  493. //Clean up cipher ctxs
  494. #if OPENSSL_VERSION_NUMBER >= 0x1010000eL
  495. EVP_MD_CTX_free(f->hs_md_ctx);
  496. #else
  497. EVP_MD_CTX_cleanup(f->hs_md_ctx);
  498. if(f->hs_md_ctx != NULL){
  499. EVP_MD_CTX_destroy(f->hs_md_ctx);
  500. }
  501. #endif
  502. if(f->clnt_read_ctx != NULL){
  503. EVP_CIPHER_CTX_cleanup(f->clnt_read_ctx);
  504. OPENSSL_free(f->clnt_read_ctx);
  505. f->clnt_read_ctx = NULL;
  506. }
  507. if(f->clnt_write_ctx != NULL){
  508. EVP_CIPHER_CTX_cleanup(f->clnt_write_ctx);
  509. OPENSSL_free(f->clnt_write_ctx);
  510. f->clnt_write_ctx = NULL;
  511. }
  512. if(f->srvr_read_ctx != NULL){
  513. EVP_CIPHER_CTX_free(f->srvr_read_ctx);
  514. }
  515. if(f->srvr_write_ctx != NULL){
  516. EVP_CIPHER_CTX_free(f->srvr_write_ctx);
  517. }
  518. if(f->ecdh != NULL){
  519. EC_KEY_free(f->ecdh);
  520. }
  521. if(f->gcm_ctx_out != NULL){
  522. CRYPTO_gcm128_release(f->gcm_ctx_out);
  523. }
  524. if(f->gcm_ctx_iv != NULL){
  525. free(f->gcm_ctx_iv);
  526. }
  527. if(f->gcm_ctx_key != NULL){
  528. free(f->gcm_ctx_key);
  529. }
  530. if(f->dh != NULL){
  531. DH_free(f->dh);
  532. }
  533. if(f->current_session != NULL && f->resume_session == 1){
  534. if( f->current_session->session_ticket != NULL){
  535. free(f->current_session->session_ticket);
  536. }
  537. free(f->current_session);
  538. }
  539. if(f->ds_packet_chain != NULL){
  540. packet *tmp = f->ds_packet_chain->first_packet;
  541. while(tmp != NULL){
  542. f->ds_packet_chain->first_packet = tmp->next;
  543. printf("Freed data %p\n", tmp->data);
  544. printf("Freed packet %p\n", tmp);
  545. free(tmp->data);
  546. free(tmp);
  547. tmp = f->ds_packet_chain->first_packet;
  548. }
  549. }
  550. free(f->ds_packet_chain);
  551. if(f->us_packet_chain != NULL){
  552. packet *tmp = f->us_packet_chain->first_packet;
  553. while(tmp != NULL){
  554. f->us_packet_chain->first_packet = tmp->next;
  555. printf("Freed data %p\n", tmp->data);
  556. printf("Freed packet %p\n", tmp);
  557. free(tmp->data);
  558. free(tmp);
  559. tmp = f->us_packet_chain->first_packet;
  560. }
  561. }
  562. free(f->us_packet_chain);
  563. if(f->upstream_queue != NULL){
  564. queue_block *tmp = f->upstream_queue;
  565. while(tmp != NULL){
  566. f->upstream_queue = tmp->next;
  567. printf("Freed data %p\n", tmp->data);
  568. printf("Freed packet %p\n", tmp);
  569. free(tmp->data);
  570. free(tmp);
  571. tmp = f->upstream_queue;
  572. }
  573. }
  574. flow_entry *entry = table->first_entry;
  575. if(entry->f == f){
  576. table->first_entry = entry->next;
  577. free(entry->f);
  578. free(entry);
  579. table->len --;
  580. } else {
  581. flow_entry *next;
  582. for(int i=0; i< table->len; i++){
  583. if(entry->next != NULL){
  584. next = entry->next;
  585. } else {
  586. printf("Flow not in table\n");
  587. break;
  588. }
  589. if(next->f == f){
  590. entry->next = next->next;
  591. free(next->f);
  592. free(next);
  593. table->len --;
  594. break;
  595. }
  596. entry = next;
  597. }
  598. }
  599. sem_post(&flow_table_lock);
  600. return 1;
  601. }
  602. /** Returns the index of a flow in the flow table if
  603. * it exists, returns 0 if it is not present.
  604. *
  605. * Inputs:
  606. * observed: details for the observed flow
  607. *
  608. * Output:
  609. * flow struct from table or NULL if it doesn't exist
  610. */
  611. flow *check_flow(struct packet_info *info){
  612. /* Loop through flows in table and see if it exists */
  613. int i;
  614. flow_entry *entry = table->first_entry;
  615. flow *candidate;
  616. flow *found = NULL;
  617. if(entry == NULL)
  618. return NULL;
  619. sem_wait(&flow_table_lock);
  620. /* Check first in this direction */
  621. for(i=0; i<table->len; i++){
  622. if(entry == NULL){
  623. printf("Error: entry is null\n");
  624. break;
  625. }
  626. candidate = entry->f;
  627. if(candidate->src_ip.s_addr == info->ip_hdr->src.s_addr){
  628. if(candidate->dst_ip.s_addr == info->ip_hdr->dst.s_addr){
  629. if(candidate->src_port == info->tcp_hdr->src_port){
  630. if(candidate->dst_port == info->tcp_hdr->dst_port){
  631. found = candidate;
  632. }
  633. }
  634. }
  635. }
  636. entry = entry->next;
  637. }
  638. entry = table->first_entry;
  639. /* Then in the other direction */
  640. for(i=0; i<table->len; i++){
  641. if(entry == NULL){
  642. printf("Error: entry is null\n");
  643. break;
  644. }
  645. candidate = entry->f;
  646. if(candidate->src_ip.s_addr == info->ip_hdr->dst.s_addr){
  647. if(candidate->dst_ip.s_addr == info->ip_hdr->src.s_addr){
  648. if(candidate->src_port == info->tcp_hdr->dst_port){
  649. if(candidate->dst_port == info->tcp_hdr->src_port){
  650. found = candidate;
  651. }
  652. }
  653. }
  654. }
  655. entry = entry->next;
  656. }
  657. if(found != NULL){
  658. found->ref_ctr++;
  659. }
  660. sem_post(&flow_table_lock);
  661. if(found != NULL && found->removed){
  662. remove_flow(found);
  663. found=NULL;
  664. }
  665. return found;
  666. }
  667. int init_session_cache(void){
  668. sessions = smalloc(sizeof(session_cache));
  669. sessions->length = 0;
  670. sessions->first_session = NULL;
  671. return 0;
  672. }
  673. /** Called from ServerHello, verifies that the session id returned matches
  674. * the session id requested from the client hello
  675. *
  676. * Input:
  677. * f: the tagged flow
  678. * hs: a pointer to the ServerHello message
  679. *
  680. * Output:
  681. * 0 if success, 1 if failed
  682. */
  683. static int verify_session_id(flow *f, uint8_t *hs){
  684. if (f->current_session == NULL)
  685. return 1;
  686. //increment pointer to point to sessionid
  687. uint8_t *p = hs + HANDSHAKE_HEADER_LEN;
  688. p += 2; //skip version
  689. p += SSL3_RANDOM_SIZE; //skip random
  690. uint8_t id_len = (uint8_t) p[0];
  691. p ++;
  692. //check to see if it matches flow's session id set by ClientHello
  693. if(f->current_session->session_id_len > 0 && !memcmp(f->current_session->session_id, p, id_len)){
  694. //if it matched, update flow with master secret :D
  695. DEBUG_MSG(DEBUG_HS, "Session id matched!\n");
  696. session *last = sessions->first_session;
  697. int found = 0;
  698. for(int i=0; ((i<sessions->length) && (!found)); i++){
  699. if(!memcmp(last->session_id, f->current_session->session_id, id_len)){
  700. memcpy(f->master_secret, last->master_secret, SSL3_MASTER_SECRET_SIZE);
  701. found = 1;
  702. }
  703. last = last->next;
  704. }
  705. if((!found) && (f->current_session->session_ticket_len > 0)){
  706. last = sessions->first_session;
  707. for(int i=0; ((i<sessions->length) && (!found)); i++){
  708. if( (last->session_ticket != NULL) && (last->session_ticket_len == f->current_session->session_ticket_len)){
  709. if(!memcmp(last->session_ticket, f->current_session->session_ticket, f->current_session->session_ticket_len)){
  710. memcpy(f->master_secret, last->master_secret, SSL3_MASTER_SECRET_SIZE);
  711. found = 1;
  712. DEBUG_MSG(DEBUG_HS, "Found new session ticket (%x:%d -> %x:%d)\n", f->src_ip.s_addr, f->src_port, f->dst_ip.s_addr, f->dst_port);
  713. DEBUG_BYTES(DEBUG_HS, last->session_ticket, last->session_ticket_len);
  714. }
  715. }
  716. last = last->next;
  717. }
  718. }
  719. } else if (f->current_session->session_id_len == 0){
  720. //search for session ticket in session cache
  721. printf("clnt session id was empty, looking for ticket\n");
  722. session *last = sessions->first_session;
  723. if(f->current_session->session_ticket_len > 0){
  724. last = sessions->first_session;
  725. for(int i=0; i<sessions->length; i++){
  726. if(last->session_ticket_len == f->current_session->session_ticket_len){
  727. if(!memcmp(last->session_ticket, f->current_session->session_ticket, f->current_session->session_ticket_len)){
  728. memcpy(f->master_secret, last->master_secret, SSL3_MASTER_SECRET_SIZE);
  729. DEBUG_MSG(DEBUG_HS, "Found new session ticket (%x:%d -> %x:%d)\n", f->src_ip.s_addr, f->src_port, f->dst_ip.s_addr, f->dst_port);
  730. DEBUG_BYTES(DEBUG_HS, last->session_ticket, last->session_ticket_len);
  731. break;
  732. }
  733. }
  734. last = last->next;
  735. }
  736. }
  737. } else if (f->current_session->session_id_len > 0){
  738. //server refused resumption, save new session id
  739. DEBUG_MSG(DEBUG_HS, "session ids did not match, saving new id\n");
  740. save_session_id(f, p);
  741. }
  742. return 0;
  743. }
  744. /* Called from ClientHello. Checks to see if the session id len is > 0. If so,
  745. * saves sessionid for later verification. Also checks to see if a session
  746. * ticket is included as an extension.
  747. *
  748. * Input:
  749. * f: the tagged flow
  750. * hs: a pointer to the ServerHello message
  751. *
  752. * Output:
  753. * 0 if success, 1 if failed
  754. */
  755. static int check_extensions(flow *f, uint8_t *hs, uint32_t len){
  756. uint8_t *p = hs + HANDSHAKE_HEADER_LEN;
  757. p += 2; //skip version
  758. p += SSL3_RANDOM_SIZE; //skip random
  759. session *new_session = smalloc(sizeof(session));
  760. new_session->session_id_len = (uint8_t) p[0];
  761. new_session->session_ticket_len = 0;
  762. new_session->session_ticket = NULL;
  763. p ++;
  764. if(new_session->session_id_len > 0){
  765. f->resume_session = 1;
  766. memcpy(new_session->session_id, p, new_session->session_id_len);
  767. new_session->next = NULL;
  768. DEBUG_MSG(DEBUG_HS, "Requested new session (%x:%d -> %x:%d)\n", f->src_ip.s_addr, f->src_port, f->dst_ip.s_addr, f->dst_port);
  769. DEBUG_MSG(DEBUG_HS, "session id: \n");
  770. DEBUG_BYTES(DEBUG_HS, p, new_session->session_id_len);
  771. f->current_session = new_session;
  772. }
  773. p += new_session->session_id_len;
  774. //check to see if there is a session ticket included
  775. //skip to extensions
  776. uint16_t ciphersuite_len = (p[0] << 8) + p[1];
  777. p += 2 + ciphersuite_len;
  778. uint8_t compress_meth_len = p[0];
  779. p += 1 + compress_meth_len;
  780. //search for SessionTicket TLS extension
  781. if(2 + SSL3_RANDOM_SIZE + new_session->session_id_len + 1 + 2 + ciphersuite_len + 1 + compress_meth_len > len){
  782. //no extension
  783. if(f->current_session == NULL)
  784. free(new_session);
  785. return 0;
  786. }
  787. uint16_t extensions_len = (p[0] << 8) + p[1];
  788. p += 2;
  789. while(extensions_len > 0){
  790. uint16_t type = (p[0] << 8) + p[1];
  791. p += 2;
  792. uint16_t ext_len = (p[0] << 8) + p[1];
  793. p += 2;
  794. if(type == 0x23){
  795. if(ext_len > 0){
  796. f->resume_session = 1;
  797. new_session->session_ticket_len = ext_len;
  798. new_session->session_ticket = scalloc(1, ext_len);
  799. memcpy(new_session->session_ticket, p, ext_len);
  800. f->current_session = new_session;
  801. }
  802. }
  803. if(type == 0x17){//Extended Master Secret
  804. f->extended_master_secret = 1;
  805. }
  806. p += ext_len;
  807. extensions_len -= (4 + ext_len);
  808. }
  809. if(!f->resume_session){
  810. free(new_session);
  811. f->stall = 0; //unstall the next packet
  812. }
  813. return 0;
  814. }
  815. /* Called from ServerHello. Cycles through extensions and verifies their use
  816. * in the flow.
  817. *
  818. * Input:
  819. * f: the tagged flow
  820. * hs: a pointer to the ServerHello message
  821. *
  822. * Output:
  823. * 0 if success, 1 if failed
  824. */
  825. static int verify_extensions(flow *f, uint8_t *hs, uint32_t len){
  826. uint8_t extended_master_secret = 0;
  827. uint32_t remaining_len = len;
  828. uint8_t *p = hs + HANDSHAKE_HEADER_LEN;
  829. p += 2; //skip version
  830. p += SSL3_RANDOM_SIZE; //skip random
  831. remaining_len -= (2 + SSL3_RANDOM_SIZE);
  832. remaining_len -= (uint8_t) p[0] + 1;
  833. p += (uint8_t) p[0] + 1; //skip session id
  834. p += 2; //skip cipher suite
  835. remaining_len -= 2;
  836. p ++; //skip compression method
  837. remaining_len --;
  838. if(remaining_len < 2){
  839. return 0;
  840. }
  841. //cycle through extensions
  842. uint16_t extensions_len = (p[0] << 8) + p[1];
  843. p += 2;
  844. while(extensions_len > 0){
  845. uint16_t type = (p[0] << 8) + p[1];
  846. p += 2;
  847. uint16_t ext_len = (p[0] << 8) + p[1];
  848. p += 2;
  849. if(type == 0x17){
  850. extended_master_secret = 1;
  851. }
  852. p += ext_len;
  853. extensions_len -= (4 + ext_len);
  854. }
  855. //Check to make sure both client and server included extension
  856. if(!f->extended_master_secret || !extended_master_secret){
  857. f->extended_master_secret = 0;
  858. } else {
  859. DEBUG_MSG(DEBUG_HS, "Extended master secret extension\n");
  860. }
  861. return 0;
  862. }
  863. /* Called from ServerHello during full handshake. Adds the session id to the
  864. * cache for later resumptions
  865. *
  866. * Input:
  867. * f: the tagged flow
  868. * hs: a pointer to the ServerHello message
  869. *
  870. * Output:
  871. * 0 if success, 1 if failed
  872. */
  873. static int save_session_id(flow *f, uint8_t *hs){
  874. //increment pointer to point to sessionid
  875. uint8_t *p = hs + HANDSHAKE_HEADER_LEN;
  876. p += 2; //skip version
  877. p += SSL3_RANDOM_SIZE; //skip random
  878. session *new_session = smalloc(sizeof(session));
  879. new_session->session_id_len = (uint8_t) p[0];
  880. if((new_session->session_id_len <= 0) || (new_session->session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH)){
  881. //if this value is zero, the session is non-resumable or the
  882. //server will issue a NewSessionTicket handshake message
  883. free(new_session);
  884. return 0;
  885. }
  886. p++;
  887. memcpy(new_session->session_id, p, new_session->session_id_len);
  888. new_session->session_ticket_len = 0;
  889. new_session->session_ticket = NULL;
  890. new_session->next = NULL;
  891. if(f->current_session != NULL){
  892. free(f->current_session);
  893. }
  894. f->resume_session = 0;
  895. f->current_session = new_session;
  896. if(sessions->first_session == NULL){
  897. sessions->first_session = new_session;
  898. printf("First session id (%p->%p):", sessions, sessions->first_session);
  899. for(int i=0; i< new_session->session_id_len; i++){
  900. printf(" %02x", sessions->first_session->session_id[i]);
  901. }
  902. printf("\n");
  903. } else {
  904. session *last = sessions->first_session;
  905. for(int i=0; i< sessions->length -1; i++){
  906. if(last == NULL){
  907. printf("UH OH: last is null?\n");
  908. fflush(stdout);
  909. }
  910. last = last->next;
  911. }
  912. last->next = new_session;
  913. }
  914. sessions->length ++;
  915. DEBUG_MSG(DEBUG_HS, "Saved session id:");
  916. DEBUG_BYTES(DEBUG_HS, new_session->session_id, new_session->session_id_len);
  917. return 0;
  918. }
  919. /* Called from NewSessionTicket. Adds the session ticket to the
  920. * cache for later resumptions
  921. *
  922. * Input:
  923. * f: the tagged flow
  924. * hs: a pointer to the ServerHello message
  925. *
  926. * Output:
  927. * 0 if success, 1 if failed
  928. */
  929. int save_session_ticket(flow *f, uint8_t *hs, uint32_t len){
  930. DEBUG_MSG(DEBUG_HS, "TICKET HDR:");
  931. DEBUG_BYTES(DEBUG_HS, hs, HANDSHAKE_HEADER_LEN);
  932. uint8_t *p = hs + HANDSHAKE_HEADER_LEN;
  933. p += 4;
  934. session *new_session = scalloc(1, sizeof(session));
  935. new_session->session_id_len = 0;
  936. new_session->session_ticket_len = (p[0] << 8) + p[1];
  937. new_session->next = NULL;
  938. p += 2;
  939. uint8_t *ticket = smalloc(new_session->session_ticket_len);
  940. memcpy(ticket, p, new_session->session_ticket_len);
  941. new_session->session_ticket = ticket;
  942. memcpy(new_session->master_secret, f->master_secret, SSL3_MASTER_SECRET_SIZE);
  943. if(sessions->first_session == NULL){
  944. sessions->first_session = new_session;
  945. } else {
  946. session *last = sessions->first_session;
  947. for(int i=0; i< (sessions->length-1); i++){
  948. if(last == NULL){
  949. printf("UH OH: last is null?\n");
  950. fflush(stdout);
  951. }
  952. last = last->next;
  953. }
  954. last->next = new_session;
  955. }
  956. sessions->length ++;
  957. DEBUG_MSG(DEBUG_HS, "Saved session ticket:");
  958. DEBUG_BYTES(DEBUG_HS, p, new_session->session_ticket_len);
  959. DEBUG_MSG(DEBUG_HS, "Saved session master secret:");
  960. DEBUG_BYTES(DEBUG_HS, new_session->master_secret, SSL3_MASTER_SECRET_SIZE);
  961. return 0;
  962. }
  963. /* Adds a (handshake) packet to the flow's packet chain. If it can complete a record, passes
  964. * this record to update_flow
  965. *
  966. * Note: the code in slitheen.c should ensure that this function only ever gets the next
  967. * expected sequence number
  968. */
  969. int add_packet(flow *f, struct packet_info *info){
  970. if (info->tcp_hdr == NULL || info->app_data_len <= 0){
  971. return 0;
  972. }
  973. packet *new_packet = smalloc(sizeof(packet));
  974. new_packet->seq_num = ntohl(info->tcp_hdr->sequence_num);
  975. new_packet->len = info->app_data_len;
  976. uint8_t *packet_data = smalloc(new_packet->len);
  977. memcpy(packet_data, info->app_data, new_packet->len);
  978. new_packet->data = packet_data;
  979. new_packet->next = NULL;
  980. uint8_t incoming = (info->ip_hdr->src.s_addr == f->src_ip.s_addr) ? 0 : 1;
  981. packet_chain *chain = (incoming) ? f->ds_packet_chain : f->us_packet_chain;
  982. queue *packet_queue = (incoming) ? f->ds_hs_queue : f->us_hs_queue;
  983. if(new_packet->seq_num < chain->expected_seq_num){
  984. //see if this packet contains any data we are missing
  985. //TODO: figure out how/why this happens and what should follow
  986. printf("ERROR: Received replayed packet O.o\n");
  987. free(new_packet->data);
  988. free(new_packet);
  989. remove_flow(f);
  990. return 1;
  991. }
  992. if(new_packet->seq_num > chain->expected_seq_num) {
  993. printf("ERROR: Received future packet O.o\n");
  994. free(new_packet->data);
  995. free(new_packet);
  996. remove_flow(f);
  997. return 1;
  998. }
  999. //temporary: see if it's the only packet, if so is new record
  1000. if(peek(packet_queue, 0) == NULL){
  1001. if(new_packet->seq_num == chain->expected_seq_num){
  1002. const struct record_header *record_hdr = (struct record_header *) new_packet->data;
  1003. chain->record_len = RECORD_LEN(record_hdr)+RECORD_HEADER_LEN;
  1004. chain->remaining_record_len = chain->record_len;
  1005. }
  1006. }
  1007. //append packet to queue
  1008. enqueue(packet_queue, new_packet);
  1009. chain->expected_seq_num += new_packet->len;
  1010. uint32_t record_offset = 0; //offset into record for updating info with any changes
  1011. uint32_t info_offset = 0; //offset into info for updating with changes
  1012. uint32_t info_len = 0; //number of bytes that possibly changed
  1013. //while there is still data left:
  1014. uint32_t available_data = new_packet->len;
  1015. while(available_data > 0){
  1016. //if full record, give to update_flow
  1017. if(chain->remaining_record_len <= new_packet->len){//we have enough to make a record
  1018. chain->remaining_record_len = 0;
  1019. uint8_t *record = smalloc(chain->record_len);
  1020. uint32_t record_len = chain->record_len;
  1021. uint32_t tmp_len = chain->record_len;
  1022. packet *next = peek(packet_queue, 0);
  1023. while(tmp_len > 0){
  1024. if(tmp_len >= next->len){
  1025. memcpy(record+chain->record_len - tmp_len, next->data, next->len);
  1026. if(next == new_packet){
  1027. new_packet = NULL;//TODO: why?
  1028. record_offset = chain->record_len - tmp_len;
  1029. info_len = next->len;
  1030. }
  1031. tmp_len -= next->len;
  1032. //remove packet from queue
  1033. next = dequeue(packet_queue);
  1034. free(next->data);
  1035. free(next);
  1036. next = peek(packet_queue, 0); //TODO: Do we need this???
  1037. available_data = 0;
  1038. } else { //didn't use up entire packet
  1039. memcpy(record+chain->record_len - tmp_len, next->data, tmp_len);
  1040. if(next == new_packet){//TODO: opposite shouldn't happen?
  1041. record_offset = chain->record_len - tmp_len;
  1042. info_len = tmp_len;
  1043. }
  1044. memmove(next->data, next->data+tmp_len, next->len - tmp_len);
  1045. next->len -= tmp_len;
  1046. available_data -= tmp_len;
  1047. tmp_len = 0;
  1048. //Last part of packet is a new record
  1049. const struct record_header *record_hdr = (struct record_header *) next->data;
  1050. chain->record_len = RECORD_LEN(record_hdr)+RECORD_HEADER_LEN;
  1051. chain->remaining_record_len = chain->record_len;
  1052. }
  1053. }
  1054. //if handshake is complete, send to relay code
  1055. if(f->application == 1){
  1056. //update packet info and send to replace_packet
  1057. struct packet_info *copy_info = copy_packet_info(info);
  1058. copy_info->app_data = record;
  1059. copy_info->app_data_len = record_len;
  1060. replace_packet(f, copy_info);
  1061. free(copy_info->app_data);
  1062. free(copy_info);
  1063. } else {
  1064. if(update_flow(f, record, incoming)){
  1065. free(record);
  1066. return 1;//error occurred and flow was removed
  1067. }
  1068. if(f->in_encrypted ==2 && incoming){
  1069. //if server finished message was received, copy changes back to packet
  1070. DEBUG_MSG(DEBUG_HS, "Replacing info->data with finished message (%d bytes).\n", info_len);
  1071. DEBUG_MSG(DEBUG_HS, "Previous bytes:\n");
  1072. DEBUG_BYTES(DEBUG_HS, (info->app_data + info_offset), info_len);
  1073. DEBUG_MSG(DEBUG_HS, "New bytes:\n");
  1074. DEBUG_BYTES(DEBUG_HS, (record + record_offset), info_len);
  1075. DEBUG_MSG(DEBUG_HS, "Previous packet contents:\n");
  1076. DEBUG_BYTES(DEBUG_HS, info->app_data, info->app_data_len);
  1077. memcpy(info->app_data+info_offset, record+record_offset, info_len);
  1078. DEBUG_MSG(DEBUG_HS, "SLITHEEN: Current packet contents:\n");
  1079. DEBUG_BYTES(DEBUG_HS, info->app_data, info->app_data_len);
  1080. //update TCP checksum
  1081. tcp_checksum(info);
  1082. }
  1083. free(record);
  1084. if(new_packet != NULL){
  1085. info_offset += info_len;
  1086. }
  1087. }
  1088. } else {//can't make a full record yet
  1089. chain->remaining_record_len -= new_packet->len;
  1090. available_data = 0;
  1091. }
  1092. } //exhausted new packet len
  1093. return 0;
  1094. }