flow.c 38 KB

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