flow.c 42 KB

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