flow.c 27 KB

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