flow.c 43 KB

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