flow.c 38 KB

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