flow.c 32 KB

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