flow.c 38 KB

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