flow.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  1. /* Name: flow.c
  2. * Author: Cecylia Bocovich <cbocovic@uwaterloo.ca>
  3. *
  4. * This file contains functions for manipulating tagged flows.
  5. *
  6. * The check_flow function checks to see whether the packet that is currently
  7. * being processed belongs to a tagged flow.
  8. *
  9. * The add_packet function attempts to reconstruct a TLS record containing
  10. * handshake data from the contents of previously received packets
  11. *
  12. * The update_flow function processes TLS handshake messages and calls the
  13. * necessary functions from crypto.c when a message has been received.
  14. *
  15. * There are several functions associated with the resumption of previously
  16. * tagged TLS sessions.
  17. *
  18. */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <stdint.h>
  22. #include <pthread.h>
  23. #include <errno.h>
  24. #include <semaphore.h>
  25. #include "flow.h"
  26. #include "crypto.h"
  27. #include "slitheen.h"
  28. #include "relay.h"
  29. #include "util.h"
  30. static flow_table *table;
  31. static session_cache *sessions;
  32. data_queue *downstream_queue;
  33. client_table *clients;
  34. sem_t flow_table_lock;
  35. /* Initialize the table of tagged flows */
  36. int init_tables(void) {
  37. table = emalloc(sizeof(flow_table));
  38. table->first_entry = NULL;
  39. table->len = 0;
  40. sem_init(&flow_table_lock, 0, 1);
  41. clients = emalloc(sizeof(client_table));
  42. clients->first = NULL;
  43. printf("initialized downstream queue\n");
  44. return 0;
  45. }
  46. /* Add a new flow to the tagged flow table */
  47. flow *add_flow(struct packet_info *info) {
  48. flow_entry *entry = emalloc(sizeof(flow_entry));
  49. flow *new_flow = emalloc(sizeof(flow));
  50. entry->f = new_flow;
  51. entry->next = NULL;
  52. new_flow->src_ip = info->ip_hdr->src;
  53. new_flow->dst_ip = info->ip_hdr->dst;
  54. new_flow->src_port = info->tcp_hdr->src_port;
  55. new_flow->dst_port = info->tcp_hdr->dst_port;
  56. new_flow->upstream_app_data = emalloc(sizeof(app_data_queue));
  57. new_flow->upstream_app_data->first_packet = NULL;
  58. new_flow->downstream_app_data = emalloc(sizeof(app_data_queue));
  59. new_flow->downstream_app_data->first_packet = NULL;
  60. new_flow->upstream_seq_num = ntohl(info->tcp_hdr->sequence_num);
  61. new_flow->downstream_seq_num = ntohl(info->tcp_hdr->ack_num);
  62. new_flow->streams=NULL;
  63. new_flow->downstream_queue=NULL;
  64. sem_init(&(new_flow->flow_lock), 0, 1);
  65. new_flow->state = TLS_CLNT_HELLO;
  66. new_flow->in_encrypted = 0;
  67. new_flow->out_encrypted = 0;
  68. new_flow->application = 0;
  69. new_flow->resume_session = 0;
  70. new_flow->current_session = NULL;
  71. new_flow->us_packet_chain = emalloc(sizeof(packet_chain));
  72. new_flow->us_packet_chain->expected_seq_num = ntohl(info->tcp_hdr->sequence_num);
  73. new_flow->us_packet_chain->record_len = 0;
  74. new_flow->us_packet_chain->remaining_record_len = 0;
  75. new_flow->us_packet_chain->first_packet = NULL;
  76. new_flow->ds_packet_chain = emalloc(sizeof(packet_chain));
  77. new_flow->ds_packet_chain->expected_seq_num = ntohl(info->tcp_hdr->ack_num);
  78. new_flow->ds_packet_chain->record_len = 0;
  79. new_flow->ds_packet_chain->remaining_record_len = 0;
  80. new_flow->ds_packet_chain->first_packet = NULL;
  81. sem_init(&(new_flow->packet_chain_lock), 0, 1);
  82. new_flow->upstream_queue = NULL;
  83. new_flow->upstream_remaining = 0;
  84. sem_init(&(new_flow->upstream_queue_lock), 0, 1);
  85. new_flow->outbox = NULL;
  86. new_flow->outbox_len = 0;
  87. new_flow->outbox_offset = 0;
  88. new_flow->partial_record_header = NULL;
  89. new_flow->partial_record_header_len = 0;
  90. new_flow->remaining_record_len = 0;
  91. new_flow->remaining_response_len = 0;
  92. new_flow->httpstate = PARSE_HEADER;
  93. new_flow->replace_response = 0;
  94. new_flow->ecdh = NULL;
  95. new_flow->finish_md_ctx = EVP_MD_CTX_create();
  96. const EVP_MD *md = EVP_sha384();
  97. EVP_DigestInit_ex(new_flow->finish_md_ctx, md, NULL);
  98. new_flow->cipher = NULL;
  99. new_flow->clnt_read_ctx = NULL;
  100. new_flow->clnt_write_ctx = NULL;
  101. new_flow->srvr_read_ctx = NULL;
  102. new_flow->srvr_write_ctx = NULL;
  103. memset(new_flow->read_seq, 0, 8);
  104. memset(new_flow->write_seq, 0, 8);
  105. sem_wait(&flow_table_lock);
  106. flow_entry *last = table->first_entry;
  107. if(last == NULL){
  108. table->first_entry = entry;
  109. } else {
  110. for(int i=0; i< table->len-1; i++){
  111. last = last->next;
  112. }
  113. last->next = entry;
  114. }
  115. table->len ++;
  116. sem_post(&flow_table_lock);
  117. return new_flow;
  118. }
  119. /** Observes TLS handshake messages and updates the state of
  120. * the flow
  121. *
  122. * Inputs:
  123. * f: the tagged flow
  124. * record: a complete TLS record
  125. *
  126. * Output:
  127. * 0 on success, 1 on failure
  128. */
  129. int update_flow(flow *f, uint8_t *record, uint8_t incoming) {
  130. const struct record_header *record_hdr;
  131. const struct handshake_header *handshake_hdr;
  132. uint8_t *p;
  133. record_hdr = (struct record_header*) record;
  134. int record_len;
  135. record_len = RECORD_LEN(record_hdr)+RECORD_HEADER_LEN;
  136. switch(record_hdr->type){
  137. case HS:
  138. p = record;
  139. p += RECORD_HEADER_LEN;
  140. if((incoming && f->in_encrypted) || (!incoming && f->out_encrypted)){
  141. #ifdef DEBUG_HS
  142. printf("Decrypting finished (%d bytes) (%x:%d -> %x:%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));
  143. printf("Finished ciphertext:\n");
  144. for(int i=0; i< record_len; i++){
  145. printf("%02x ", record[i]);
  146. }
  147. printf("\n");
  148. #endif
  149. int32_t n = encrypt(f, p, p, record_len - RECORD_HEADER_LEN, incoming, 0x16, 0);
  150. if(n<=0){
  151. printf("Error decrypting finished (%x:%d -> %x:%d)\n", f->src_ip.s_addr, ntohs(f->src_port), f->dst_ip.s_addr, ntohs(f->dst_port));
  152. }
  153. #ifdef DEBUG_HS
  154. 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));
  155. #endif
  156. p += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  157. #ifdef DEBUG_HS
  158. printf("record:\n");
  159. for(int i=0; i< n; i++){
  160. printf("%02x ", p[i]);
  161. }
  162. printf("\n");
  163. #endif
  164. if(p[0] != 0x14){
  165. p[0] = 0x20; //trigger error
  166. }
  167. if(incoming){
  168. f->in_encrypted = 2;
  169. } else {
  170. f->out_encrypted = 2;
  171. update_context(f, p, n, incoming, 0x16, 0);
  172. }
  173. }
  174. handshake_hdr = (struct handshake_header*) p;
  175. f->state = handshake_hdr->type;
  176. switch(f->state){
  177. case TLS_CLNT_HELLO:
  178. #ifdef DEBUG_HS
  179. 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));
  180. #endif
  181. if(update_finish_hash(f, p)){
  182. fprintf(stderr, "Error updating finish has with CLNT_HELLO msg\n");
  183. remove_flow(f);
  184. goto err;
  185. }
  186. if(check_session(f, p, HANDSHAKE_MESSAGE_LEN(handshake_hdr))){
  187. fprintf(stderr, "Error checking session, might cause problems\n");
  188. }
  189. break;
  190. case TLS_SERV_HELLO:
  191. #ifdef DEBUG_HS
  192. 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));
  193. #endif
  194. if(f->resume_session){
  195. if(verify_session_id(f,p)){
  196. fprintf(stderr, "Failed to verify session id\n");
  197. }
  198. } else {
  199. if(save_session_id(f,p)){
  200. fprintf(stderr, "Failed to save session id\n");
  201. }
  202. }
  203. if(extract_server_random(f, p)){
  204. fprintf(stderr, "Failed to extract server random nonce\n");
  205. remove_flow(f);
  206. goto err;
  207. }
  208. if(update_finish_hash(f, p)){
  209. fprintf(stderr, "Error updating finish hash with SRVR_HELLO msg\n");
  210. remove_flow(f);
  211. goto err;
  212. }
  213. break;
  214. case TLS_NEW_SESS:
  215. #ifdef DEBUG_HS
  216. printf("Received new session\n");
  217. #endif
  218. if(save_session_ticket(f, p, HANDSHAKE_MESSAGE_LEN(handshake_hdr))){
  219. fprintf(stderr, "Failed to save session ticket\n");
  220. }
  221. if(update_finish_hash(f, p)){
  222. fprintf(stderr, "Error updating finish hash with NEW_SESS msg\n");
  223. remove_flow(f);
  224. goto err;
  225. }
  226. break;
  227. case TLS_CERT:
  228. #ifdef DEBUG_HS
  229. printf("Received cert\n");
  230. #endif
  231. if(update_finish_hash(f, p)){
  232. fprintf(stderr, "Error updating finish hash with CERT msg\n");
  233. remove_flow(f);
  234. goto err;
  235. }
  236. break;
  237. case TLS_SRVR_KEYEX:
  238. #ifdef DEBUG_HS
  239. printf("Received server keyex\n");
  240. #endif
  241. if(update_finish_hash(f, p)){
  242. fprintf(stderr, "Error updating finish hash with SRVR_KEYEX msg\n");
  243. remove_flow(f);
  244. goto err;
  245. }
  246. if(extract_parameters(f, p)){
  247. printf("Error extracting params\n");
  248. remove_flow(f);
  249. goto err;
  250. }
  251. if(compute_master_secret(f)){
  252. printf("Error computing master secret\n");
  253. remove_flow(f);
  254. goto err;
  255. }
  256. break;
  257. case TLS_CERT_REQ:
  258. if(update_finish_hash(f, p)){
  259. fprintf(stderr, "Error updating finish hash with CERT_REQ msg\n");
  260. remove_flow(f);
  261. goto err;
  262. }
  263. break;
  264. case TLS_SRVR_HELLO_DONE:
  265. #ifdef DEBUG_HS
  266. printf("Received server hello done\n");
  267. #endif
  268. if(update_finish_hash(f, p)){
  269. fprintf(stderr, "Error updating finish hash with HELLO_DONE msg\n");
  270. remove_flow(f);
  271. goto err;
  272. }
  273. break;
  274. case TLS_CERT_VERIFY:
  275. #ifdef DEBUG_HS
  276. printf("received cert verify\n");
  277. #endif
  278. if(update_finish_hash(f, p)){
  279. fprintf(stderr, "Error updating finish hash with CERT_VERIFY msg\n");
  280. remove_flow(f);
  281. goto err;
  282. }
  283. break;
  284. case TLS_CLNT_KEYEX:
  285. #ifdef DEBUG_HS
  286. printf("Received client key exchange\n");
  287. #endif
  288. if(update_finish_hash(f, p)){
  289. fprintf(stderr, "Error updating finish hash with CLNT_KEYEX msg\n");
  290. remove_flow(f);
  291. goto err;
  292. }
  293. break;
  294. case TLS_FINISHED:
  295. #ifdef DEBUG_HS
  296. 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));
  297. #endif
  298. verify_finish_hash(f,p, incoming);
  299. if(update_finish_hash(f, p)){
  300. fprintf(stderr, "Error updating finish hash with FINISHED msg\n");
  301. remove_flow(f);
  302. goto err;
  303. }
  304. //re-encrypt finished message
  305. if(incoming){
  306. //revert the sequence number
  307. memset(f->read_seq, 0, 8);
  308. int32_t n = encrypt(f, record+RECORD_HEADER_LEN, record+RECORD_HEADER_LEN, record_len - (RECORD_HEADER_LEN+16), incoming, 0x16, 1);
  309. #ifdef HS_DEBUG
  310. printf("New finished ciphertext:\n");
  311. for(int i=0; i< record_len; i++){
  312. printf("%02x ", record[i]);
  313. }
  314. printf("\n");
  315. #endif
  316. if(n<=0){
  317. printf("Error re-encrypting finished (%x:%d -> %x:%d)\n", f->src_ip.s_addr, ntohs(f->src_port),
  318. f->dst_ip.s_addr, ntohs(f->dst_port));
  319. }
  320. }
  321. if((f->in_encrypted == 2) && (f->out_encrypted == 2)){
  322. printf("Handshake complete!\n");
  323. f->application = 1;
  324. }
  325. break;
  326. default:
  327. printf("Error? (%x:%d -> %x:%d)...\n", f->src_ip.s_addr, ntohs(f->src_port), f->dst_ip.s_addr, ntohs(f->dst_port));
  328. remove_flow(f);
  329. goto err;
  330. }
  331. break;
  332. case APP:
  333. 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));
  334. break;
  335. case CCS:
  336. #ifdef DEBUG_HS
  337. 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));
  338. #endif
  339. /*Initialize ciphers */
  340. if ((!f->in_encrypted) && (!f->out_encrypted)){
  341. if(init_ciphers(f)){
  342. fprintf(stderr, "Failed to initialize ciphers\n");
  343. remove_flow(f);
  344. goto err;
  345. }
  346. }
  347. if(incoming){
  348. f->in_encrypted = 1;
  349. } else {
  350. f->out_encrypted = 1;
  351. }
  352. break;
  353. case ALERT:
  354. p = record;
  355. p += RECORD_HEADER_LEN;
  356. if(((incoming) && (f->in_encrypted > 0)) || ((!incoming) && (f->out_encrypted > 0))){
  357. encrypt(f, p, p, record_len - RECORD_HEADER_LEN, incoming, 0x16, 0);
  358. p += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  359. }
  360. 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]);
  361. fflush(stdout);
  362. break;
  363. case HB:
  364. printf("Heartbeat\n");
  365. break;
  366. default:
  367. 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));
  368. fflush(stdout);
  369. remove_flow(f);
  370. goto err;
  371. }
  372. return 0;
  373. err:
  374. return 1;
  375. }
  376. /** Removes the tagged flow from the flow table: happens when
  377. * the station receives a TCP RST or FIN packet
  378. *
  379. * Input:
  380. * index: the index into the flow table of the tagged flow
  381. *
  382. * Output:
  383. * 0 on success, 1 on failure
  384. */
  385. int remove_flow(flow *f) {
  386. //Empty application data queues
  387. packet *tmp = f->upstream_app_data->first_packet;
  388. while(tmp != NULL){
  389. f->upstream_app_data->first_packet = tmp->next;
  390. free(tmp->data);
  391. free(tmp);
  392. tmp = f->upstream_app_data->first_packet;
  393. }
  394. free(f->upstream_app_data);
  395. tmp = f->downstream_app_data->first_packet;
  396. while(tmp != NULL){
  397. f->downstream_app_data->first_packet = tmp->next;
  398. free(tmp->data);
  399. free(tmp);
  400. tmp = f->downstream_app_data->first_packet;
  401. }
  402. free(f->downstream_app_data);
  403. //Clean up cipher ctxs
  404. EVP_MD_CTX_cleanup(f->finish_md_ctx);
  405. if(f->finish_md_ctx != NULL){
  406. EVP_MD_CTX_destroy(f->finish_md_ctx);
  407. }
  408. if(f->clnt_read_ctx != NULL){
  409. EVP_CIPHER_CTX_cleanup(f->clnt_read_ctx);
  410. OPENSSL_free(f->clnt_read_ctx);
  411. f->clnt_read_ctx = NULL;
  412. }
  413. if(f->clnt_write_ctx != NULL){
  414. EVP_CIPHER_CTX_cleanup(f->clnt_write_ctx);
  415. OPENSSL_free(f->clnt_write_ctx);
  416. f->clnt_write_ctx = NULL;
  417. }
  418. if(f->srvr_read_ctx != NULL){
  419. EVP_CIPHER_CTX_free(f->srvr_read_ctx);
  420. }
  421. if(f->srvr_write_ctx != NULL){
  422. EVP_CIPHER_CTX_free(f->srvr_write_ctx);
  423. }
  424. if(f->ecdh != NULL){
  425. EC_KEY_free(f->ecdh);
  426. }
  427. if(f->resume_session == 1){
  428. if(f->current_session->session_ticket != NULL){
  429. free(f->current_session->session_ticket);
  430. }
  431. free(f->current_session);
  432. }
  433. if(f->ds_packet_chain != NULL){
  434. packet *tmp = f->ds_packet_chain->first_packet;
  435. while(tmp != NULL){
  436. f->ds_packet_chain->first_packet = tmp->next;
  437. printf("Freed data %p\n", tmp->data);
  438. printf("Freed packet %p\n", tmp);
  439. free(tmp->data);
  440. free(tmp);
  441. tmp = f->ds_packet_chain->first_packet;
  442. }
  443. }
  444. free(f->ds_packet_chain);
  445. if(f->us_packet_chain != NULL){
  446. packet *tmp = f->us_packet_chain->first_packet;
  447. while(tmp != NULL){
  448. f->us_packet_chain->first_packet = tmp->next;
  449. printf("Freed data %p\n", tmp->data);
  450. printf("Freed packet %p\n", tmp);
  451. free(tmp->data);
  452. free(tmp);
  453. tmp = f->us_packet_chain->first_packet;
  454. }
  455. }
  456. free(f->us_packet_chain);
  457. if(f->upstream_queue != NULL){
  458. queue_block *tmp = f->upstream_queue;
  459. while(tmp != NULL){
  460. f->upstream_queue = tmp->next;
  461. printf("Freed data %p\n", tmp->data);
  462. printf("Freed packet %p\n", tmp);
  463. free(tmp->data);
  464. free(tmp);
  465. tmp = f->upstream_queue;
  466. }
  467. }
  468. sem_wait(&flow_table_lock);
  469. flow_entry *entry = table->first_entry;
  470. if(entry->f == f){
  471. table->first_entry = entry->next;
  472. free(entry->f);
  473. free(entry);
  474. printf("flow removed!\n");
  475. fflush(stdout);
  476. table->len --;
  477. } else {
  478. flow_entry *next;
  479. for(int i=0; i< table->len; i++){
  480. if(entry->next != NULL){
  481. next = entry->next;
  482. } else {
  483. printf("Flow not in table\n");
  484. break;
  485. }
  486. if(next->f == f){
  487. entry->next = next->next;
  488. free(next->f);
  489. free(next);
  490. printf("flow removed!\n");
  491. table->len --;
  492. break;
  493. }
  494. entry = next;
  495. }
  496. }
  497. sem_post(&flow_table_lock);
  498. return 1;
  499. }
  500. /** Returns the index of a flow in the flow table if
  501. * it exists, returns 0 if it is not present.
  502. *
  503. * Inputs:
  504. * observed: details for the observed flow
  505. *
  506. * Output:
  507. * index of flow in table or -1 if it doesn't exist
  508. new_flow->src_ip = info->ip_hdr->src;
  509. new_flow->dst_ip = info->ip_hdr->dst;
  510. new_flow->src_port = info->tcp_hdr->src_port;
  511. new_flow->dst_port = info->tcp_hdr->dst_port;
  512. */
  513. flow *check_flow(struct packet_info *info){
  514. /* Loop through flows in table and see if it exists */
  515. int i;
  516. flow_entry *entry = table->first_entry;
  517. flow *candidate;
  518. flow *found = NULL;
  519. if(entry == NULL)
  520. return NULL;
  521. sem_wait(&flow_table_lock);
  522. /* Check first in this direction */
  523. for(i=0; i<table->len; i++){
  524. if(entry == NULL){
  525. printf("Error: entry is null\n");
  526. break;
  527. }
  528. candidate = entry->f;
  529. if(candidate->src_ip.s_addr == info->ip_hdr->src.s_addr){
  530. if(candidate->dst_ip.s_addr == info->ip_hdr->dst.s_addr){
  531. if(candidate->src_port == info->tcp_hdr->src_port){
  532. if(candidate->dst_port == info->tcp_hdr->dst_port){
  533. found = candidate;
  534. }
  535. }
  536. }
  537. }
  538. entry = entry->next;
  539. }
  540. entry = table->first_entry;
  541. /* Then in the other direction */
  542. for(i=0; i<table->len; i++){
  543. if(entry == NULL){
  544. printf("Error: entry is null\n");
  545. break;
  546. }
  547. candidate = entry->f;
  548. if(candidate->src_ip.s_addr == info->ip_hdr->dst.s_addr){
  549. if(candidate->dst_ip.s_addr == info->ip_hdr->src.s_addr){
  550. if(candidate->src_port == info->tcp_hdr->dst_port){
  551. if(candidate->dst_port == info->tcp_hdr->src_port){
  552. found = candidate;
  553. }
  554. }
  555. }
  556. }
  557. entry = entry->next;
  558. }
  559. sem_post(&flow_table_lock);
  560. return found;
  561. }
  562. int init_session_cache(void){
  563. sessions = emalloc(sizeof(session_cache));
  564. sessions->length = 0;
  565. sessions->first_session = NULL;
  566. return 0;
  567. }
  568. /** Called from ServerHello, verifies that the session id returned matches
  569. * the session id requested from the client hello
  570. *
  571. * Input:
  572. * f: the tagged flow
  573. * hs: a pointer to the ServerHello message
  574. *
  575. * Output:
  576. * 0 if success, 1 if failed
  577. */
  578. int verify_session_id(flow *f, uint8_t *hs){
  579. //increment pointer to point to sessionid
  580. uint8_t *p = hs + HANDSHAKE_HEADER_LEN;
  581. p += 2; //skip version
  582. p += SSL3_RANDOM_SIZE; //skip random
  583. uint8_t id_len = (uint8_t) p[0];
  584. p ++;
  585. //check to see if it matches flow's session id set by ClientHello
  586. if(f->current_session != NULL && f->current_session->session_id_len > 0 && !memcmp(f->current_session->session_id, p, id_len)){
  587. //if it matched, update flow with master secret :D
  588. #ifdef DEBUG_HS
  589. printf("Session id matched!\n");
  590. printf("First session id (%p->%p):", sessions, sessions->first_session);
  591. #endif
  592. session *last = sessions->first_session;
  593. int found = 0;
  594. for(int i=0; ((i<sessions->length) && (!found)); i++){
  595. #ifdef DEBUG_HS
  596. printf("Checking saved session id: ");
  597. for (int j=0; j< last->session_id_len; j++){
  598. printf("%02x ", last->session_id[j]);
  599. }
  600. printf("\n");
  601. #endif
  602. if(!memcmp(last->session_id, f->current_session->session_id, id_len)){
  603. memcpy(f->master_secret, last->master_secret, SSL3_MASTER_SECRET_SIZE);
  604. found = 1;
  605. }
  606. last = last->next;
  607. }
  608. if((!found) && (f->current_session->session_ticket_len > 0)){
  609. last = sessions->first_session;
  610. for(int i=0; ((i<sessions->length) && (!found)); i++){
  611. if(last->session_ticket_len == f->current_session->session_ticket_len){
  612. if(!memcmp(last->session_ticket, f->current_session->session_ticket, f->current_session->session_ticket_len)){
  613. memcpy(f->master_secret, last->master_secret, SSL3_MASTER_SECRET_SIZE);
  614. found = 1;
  615. #ifdef DEBUG_HS
  616. 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);
  617. for(int i=0; i< last->session_ticket_len; i++){
  618. printf("%02x ", last->session_ticket[i]);
  619. }
  620. printf("\n");
  621. #endif
  622. }
  623. }
  624. last = last->next;
  625. }
  626. }
  627. } else if (f->current_session != NULL && f->current_session->session_id_len > 0){
  628. //check to see if server's hello extension matches the ticket
  629. save_session_id(f, p);
  630. }
  631. return 0;
  632. }
  633. /* Called from ClientHello. Checks to see if the session id len is > 0. If so,
  634. * saves sessionid for later verification. Also checks to see if a session
  635. * ticket is included as an extension.
  636. *
  637. * Input:
  638. * f: the tagged flow
  639. * hs: a pointer to the ServerHello message
  640. *
  641. * Output:
  642. * 0 if success, 1 if failed
  643. */
  644. int check_session(flow *f, uint8_t *hs, uint32_t len){
  645. uint8_t *p = hs + HANDSHAKE_HEADER_LEN;
  646. p += 2; //skip version
  647. p += SSL3_RANDOM_SIZE; //skip random
  648. session *new_session = emalloc(sizeof(session));
  649. new_session->session_id_len = (uint8_t) p[0];
  650. new_session->session_ticket_len = 0;
  651. p ++;
  652. if(new_session->session_id_len > 0){
  653. f->resume_session = 1;
  654. memcpy(new_session->session_id, p, new_session->session_id_len);
  655. new_session->next = NULL;
  656. #ifdef DEBUG_HS
  657. 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);
  658. printf("session id: \n");
  659. for(int i=0; i< new_session->session_id_len; i++){
  660. printf("%02x ", p[i]);
  661. }
  662. printf("\n");
  663. #endif
  664. f->current_session = new_session;
  665. }
  666. p += new_session->session_id_len;
  667. //check to see if there is a session ticket included
  668. //skip to extensions
  669. uint16_t ciphersuite_len = (p[0] << 8) + p[1];
  670. p += 2 + ciphersuite_len;
  671. uint8_t compress_meth_len = p[0];
  672. p += 1 + compress_meth_len;
  673. //search for SessionTicket TLS extension
  674. if(2 + SSL3_RANDOM_SIZE + new_session->session_id_len + 1 + 2 + ciphersuite_len + 1 + compress_meth_len > len){
  675. //no extension
  676. if(f->current_session == NULL)
  677. free(new_session);
  678. return 0;
  679. }
  680. uint16_t extensions_len = (p[0] << 8) + p[1];
  681. p += 2;
  682. while(extensions_len > 0){
  683. uint16_t type = (p[0] << 8) + p[1];
  684. p += 2;
  685. uint16_t ext_len = (p[0] << 8) + p[1];
  686. p += 2;
  687. if(type == 0x23){
  688. if(ext_len > 0){
  689. f->resume_session = 1;
  690. new_session->session_ticket_len = ext_len;
  691. new_session->session_ticket = emalloc(ext_len);
  692. memcpy(new_session->session_ticket, p, ext_len);
  693. f->current_session = new_session;
  694. }
  695. }
  696. p += ext_len;
  697. extensions_len -= (4 + ext_len);
  698. }
  699. if(!f->resume_session){
  700. //see if a ticket is incuded
  701. free(new_session);
  702. }
  703. return 0;
  704. }
  705. /* Called from ServerHello during full handshake. Adds the session id to the
  706. * cache for later resumptions
  707. *
  708. * Input:
  709. * f: the tagged flow
  710. * hs: a pointer to the ServerHello message
  711. *
  712. * Output:
  713. * 0 if success, 1 if failed
  714. */
  715. int save_session_id(flow *f, uint8_t *hs){
  716. //increment pointer to point to sessionid
  717. uint8_t *p = hs + HANDSHAKE_HEADER_LEN;
  718. p += 2; //skip version
  719. p += SSL3_RANDOM_SIZE; //skip random
  720. session *new_session = emalloc(sizeof(session));
  721. new_session->session_id_len = (uint8_t) p[0];
  722. if(new_session->session_id_len <= 0){
  723. //if this value is zero, the session is non-resumable or the
  724. //server will issue a NewSessionTicket handshake message
  725. free(new_session);
  726. return 0;
  727. }
  728. p++;
  729. memcpy(new_session->session_id, p, new_session->session_id_len);
  730. new_session->next = NULL;
  731. if(f->current_session != NULL){
  732. free(f->current_session);
  733. }
  734. f->resume_session = 0;
  735. f->current_session = new_session;
  736. if(sessions->first_session == NULL){
  737. sessions->first_session = new_session;
  738. printf("First session id (%p->%p):", sessions, sessions->first_session);
  739. for(int i=0; i< new_session->session_id_len; i++){
  740. printf(" %02x", sessions->first_session->session_id[i]);
  741. }
  742. printf("\n");
  743. } else {
  744. session *last = sessions->first_session;
  745. for(int i=0; i< sessions->length -1; i++){
  746. if(last == NULL){
  747. printf("UH OH: last is null?\n");
  748. fflush(stdout);
  749. }
  750. last = last->next;
  751. }
  752. last->next = new_session;
  753. }
  754. sessions->length ++;
  755. printf("Saved session id:");
  756. for(int i=0; i< new_session->session_id_len; i++){
  757. printf(" %02x", new_session->session_id[i]);
  758. }
  759. printf("\n");
  760. printf("THERE ARE NOW %d saved sessions\n", sessions->length);
  761. return 0;
  762. }
  763. /* Called from NewSessionTicket. Adds the session ticket to the
  764. * cache for later resumptions
  765. *
  766. * Input:
  767. * f: the tagged flow
  768. * hs: a pointer to the ServerHello message
  769. *
  770. * Output:
  771. * 0 if success, 1 if failed
  772. */
  773. int save_session_ticket(flow *f, uint8_t *hs, uint32_t len){
  774. #ifdef DEBUG_HS
  775. printf("TICKET HDR:");
  776. for(int i=0; i< HANDSHAKE_HEADER_LEN; i++){
  777. printf("%02x ", hs[i]);
  778. }
  779. printf("\n");
  780. #endif
  781. uint8_t *p = hs + HANDSHAKE_HEADER_LEN;
  782. p += 4;
  783. session *new_session = ecalloc(1, sizeof(session));
  784. new_session->session_id_len = 0;
  785. new_session->session_ticket_len = (p[0] << 8) + p[1];
  786. p += 2;
  787. uint8_t *ticket = emalloc(new_session->session_ticket_len);
  788. memcpy(ticket, p, new_session->session_ticket_len);
  789. new_session->session_ticket = ticket;
  790. memcpy(new_session->master_secret, f->master_secret, SSL3_MASTER_SECRET_SIZE);
  791. if(sessions->first_session == NULL){
  792. sessions->first_session = new_session;
  793. } else {
  794. session *last = sessions->first_session;
  795. for(int i=0; i< (sessions->length-1); i++){
  796. if(last == NULL){
  797. printf("UH OH: last is null?\n");
  798. fflush(stdout);
  799. }
  800. last = last->next;
  801. }
  802. last->next = new_session;
  803. }
  804. sessions->length ++;
  805. #ifdef DEBUG_HS
  806. printf("Saved session ticket:");
  807. for(int i=0; i< new_session->session_ticket_len; i++){
  808. printf(" %02x", p[i]);
  809. }
  810. printf("\n");
  811. fflush(stdout);
  812. printf("Saved session master secret:");
  813. for(int i=0; i< SSL3_MASTER_SECRET_SIZE; i++){
  814. printf(" %02x", new_session->master_secret[i]);
  815. }
  816. printf("\n");
  817. fflush(stdout);
  818. printf("THERE ARE NOW %d saved sessions\n", sessions->length);
  819. fflush(stdout);
  820. #endif
  821. return 0;
  822. }
  823. /* Adds a packet the flow's packet chain. If it can complete a record, gives
  824. * this record to update_flow */
  825. int add_packet(flow *f, struct packet_info *info){
  826. if (info->tcp_hdr == NULL || info->app_data_len <= 0){
  827. return 0;
  828. }
  829. packet *new_packet = emalloc(sizeof(packet));
  830. new_packet->seq_num = ntohl(info->tcp_hdr->sequence_num);
  831. new_packet->len = info->app_data_len;
  832. uint8_t *packet_data = emalloc(new_packet->len);
  833. memcpy(packet_data, info->app_data, new_packet->len);
  834. new_packet->data = packet_data;
  835. new_packet->next = NULL;
  836. uint8_t incoming = (info->ip_hdr->src.s_addr == f->src_ip.s_addr) ? 0 : 1;
  837. packet_chain *chain =
  838. (info->ip_hdr->src.s_addr == f->src_ip.s_addr) ? f->us_packet_chain : f->ds_packet_chain;
  839. if(new_packet->seq_num < chain->expected_seq_num){
  840. //see if this packet contains any data we are missing
  841. printf("Received replayed packet O.o\n");
  842. free(new_packet->data);
  843. free(new_packet);
  844. } else {//new_packet->seq_num >= chain->expected_seq_num
  845. //Find appropriate place in chain
  846. packet *previous = NULL;
  847. packet *next = chain->first_packet;
  848. while(next != NULL && (next->seq_num <= new_packet->seq_num)){
  849. previous = next;
  850. next = next->next;
  851. }
  852. //place packet after current
  853. if(previous == NULL){
  854. //goes at the beginning of chain
  855. new_packet->next = chain->first_packet;
  856. chain->first_packet = new_packet;
  857. //if this is a new record, find lengths
  858. if(new_packet->seq_num == chain->expected_seq_num){
  859. const struct record_header *record_hdr = (struct record_header *) new_packet->data;
  860. chain->record_len = RECORD_LEN(record_hdr)+RECORD_HEADER_LEN;
  861. chain->remaining_record_len = chain->record_len;
  862. }
  863. } else {
  864. new_packet->next = next;
  865. previous->next = new_packet;
  866. }
  867. if(new_packet->seq_num == chain->expected_seq_num){
  868. chain->expected_seq_num += new_packet->len;
  869. uint32_t record_offset = 0; //offset into record for updating info with any changes
  870. uint32_t info_offset = 0; //offset into info for updating with changes
  871. uint32_t info_len = 0; //number of bytes that possibly changed
  872. //while there is still data left:
  873. uint32_t available_data = new_packet->len;
  874. while(available_data > 0){
  875. //if full record, give to update_flow
  876. if(chain->remaining_record_len <= new_packet->len){
  877. chain->remaining_record_len = 0;
  878. uint8_t *record = emalloc(chain->record_len);
  879. uint32_t record_len = chain->record_len;
  880. uint32_t tmp_len = chain->record_len;
  881. packet *next = chain->first_packet;
  882. while(tmp_len > 0){
  883. if(tmp_len >= next->len){
  884. memcpy(record+chain->record_len - tmp_len, next->data, next->len);
  885. if(next == new_packet){
  886. new_packet = NULL;
  887. record_offset = chain->record_len - tmp_len;
  888. info_len = next->len;
  889. }
  890. tmp_len -= next->len;
  891. chain->first_packet = next->next;
  892. free(next->data);
  893. free(next);
  894. next = chain->first_packet;
  895. available_data = 0;
  896. } else {
  897. memcpy(record+chain->record_len - tmp_len, next->data, tmp_len);
  898. if(next == new_packet){
  899. record_offset = chain->record_len - tmp_len;
  900. info_len = tmp_len;
  901. }
  902. memmove(next->data, next->data+tmp_len, next->len - tmp_len);
  903. next->len -= tmp_len;
  904. available_data -= tmp_len;
  905. tmp_len = 0;
  906. //this is going to be a new record
  907. const struct record_header *record_hdr = (struct record_header *) next->data;
  908. chain->record_len = RECORD_LEN(record_hdr)+RECORD_HEADER_LEN;
  909. chain->remaining_record_len = chain->record_len;
  910. #ifdef DEBUG
  911. printf("Found record of type %d\n", record_hdr->type);
  912. fflush(stdout);
  913. #endif
  914. }
  915. }
  916. //if handshake is complete, send to relay code
  917. if(f->application == 1){
  918. //update packet info and send to replace_packet
  919. printf("Packet contains application data!\n");
  920. struct packet_info *copy_info = copy_packet_info(info);
  921. copy_info->app_data = record;
  922. copy_info->app_data_len = record_len;
  923. replace_packet(f, copy_info);
  924. free(copy_info->app_data);
  925. free(copy_info);
  926. } else {
  927. if(update_flow(f, record, incoming)){
  928. free(record);
  929. return 1;//error occurred and flow was removed
  930. }
  931. //check to see if last finished message received
  932. if(f->in_encrypted ==2){
  933. #ifdef DEBUG
  934. printf("Replacing info->data with finished message (%d bytes).\n", info_len);
  935. printf("Previous bytes:\n");
  936. for(int i=0; i<info_len; i++){
  937. printf("%02x ", info->app_data[info_offset+i]);
  938. }
  939. printf("\n");
  940. printf("New bytes:\n");
  941. for(int i=0; i<info_len; i++){
  942. printf("%02x ", record[record_offset+i]);
  943. }
  944. printf("\n");
  945. printf("SLITHEEN: Previous packet contents:\n");
  946. for(int i=0; i< info->app_data_len; i++){
  947. printf("%02x ", info->app_data[i]);
  948. }
  949. printf("\n");
  950. #endif
  951. memcpy(info->app_data+info_offset, record+record_offset, info_len);
  952. #ifdef DEBUG
  953. printf("SLITHEEN: Current packet contents:\n");
  954. for(int i=0; i< info->app_data_len; i++){
  955. printf("%02x ", info->app_data[i]);
  956. }
  957. printf("\n");
  958. #endif
  959. //update TCP checksum
  960. tcp_checksum(info);
  961. }
  962. free(record);
  963. if(new_packet != NULL){
  964. info_offset += info_len;
  965. }
  966. }
  967. } else {
  968. chain->remaining_record_len -= new_packet->len;
  969. //see if this packet filled a hole
  970. new_packet = new_packet->next;
  971. if(new_packet != NULL &&
  972. new_packet->seq_num == chain->expected_seq_num){
  973. available_data = new_packet->len;
  974. chain->expected_seq_num += new_packet->len;
  975. } else {
  976. available_data = 0;
  977. }
  978. }
  979. }
  980. } else {//
  981. //add to end of packet_chain
  982. printf("Missing packet (expected %d, received %d)\n", chain->expected_seq_num, new_packet->seq_num);
  983. }
  984. }
  985. return 0;
  986. }