flow.c 38 KB

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