flow.c 30 KB

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