flow.c 44 KB

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