flow.c 38 KB

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