flow.c 43 KB

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