flow.c 27 KB

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