flow.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <pthread.h>
  5. #include <errno.h>
  6. #include <semaphore.h>
  7. #include "flow.h"
  8. #include "crypto.h"
  9. #include "slitheen.h"
  10. #include "relay.h"
  11. static flow_table *table;
  12. static session_cache *sessions;
  13. data_queue *downstream_queue;
  14. stream_table *streams;
  15. sem_t flow_table_lock;
  16. /* Initialize the table of tagged flows */
  17. int init_tables(void) {
  18. table = calloc(1, sizeof(flow_table));
  19. table->first_entry = NULL;
  20. table->len = 0;
  21. sem_init(&flow_table_lock, 0, 1);
  22. downstream_queue = calloc(1, sizeof(data_queue));
  23. downstream_queue->first_block = NULL;
  24. streams = calloc(1, sizeof(stream_table));
  25. streams->first = NULL;
  26. printf("initialized downstream queue\n");
  27. return 0;
  28. }
  29. /* Add a new flow to the tagged flow table */
  30. flow *add_flow(flow newFlow) {
  31. flow_entry *entry = calloc(1, sizeof(flow_entry));
  32. flow *ptr = calloc(1, sizeof(flow));
  33. entry->f = ptr;
  34. entry->next = NULL;
  35. printf("there are %d flows in the table\n", table->len);
  36. sem_init(&(newFlow.flow_lock), 0, 1);
  37. newFlow.state = TLS_CLNT_HELLO;
  38. newFlow.in_encrypted = 0;
  39. newFlow.out_encrypted = 0;
  40. newFlow.application = 0;
  41. newFlow.resume_session = 0;
  42. newFlow.current_session = NULL;
  43. newFlow.packet_chain = NULL;
  44. sem_init(&(newFlow.packet_chain_lock), 0, 1);
  45. newFlow.upstream_queue = NULL;
  46. newFlow.upstream_remaining = 0;
  47. newFlow.outbox = NULL;
  48. newFlow.outbox_len = 0;
  49. newFlow.outbox_offset = 0;
  50. newFlow.partial_record_header = NULL;
  51. newFlow.partial_record_header_len = 0;
  52. newFlow.remaining_record_len = 0;
  53. newFlow.remaining_response_len = 0;
  54. newFlow.httpstate = PARSE_HEADER;
  55. newFlow.replace_response = 0;
  56. newFlow.finish_md_ctx = EVP_MD_CTX_create();
  57. const EVP_MD *md = EVP_sha384();
  58. EVP_DigestInit_ex(newFlow.finish_md_ctx, md, NULL);
  59. newFlow.clnt_read_ctx = NULL;
  60. newFlow.clnt_write_ctx = NULL;
  61. newFlow.srvr_read_ctx = NULL;
  62. newFlow.srvr_write_ctx = NULL;
  63. memset(newFlow.read_seq, 0, 8);
  64. memset(newFlow.write_seq, 0, 8);
  65. *ptr = newFlow;
  66. sem_wait(&flow_table_lock);
  67. flow_entry *last = table->first_entry;
  68. if(last == NULL){
  69. table->first_entry = entry;
  70. } else {
  71. for(int i=0; i< table->len-1; i++){
  72. last = last->next;
  73. }
  74. last->next = entry;
  75. }
  76. table->len ++;
  77. sem_post(&flow_table_lock);
  78. return ptr;
  79. }
  80. /** Observes TLS handshake messages and updates the state of
  81. * the flow
  82. *
  83. * Inputs:
  84. * f: the tagged flow
  85. *
  86. * Output:
  87. * 0 on success, 1 on failure
  88. */
  89. int update_flow(flow *f) {
  90. uint8_t *record;
  91. const struct record_header *record_hdr;
  92. const struct handshake_header *handshake_hdr;
  93. sem_wait(&(f->packet_chain_lock));
  94. if(f->packet_chain == NULL){
  95. sem_post(&(f->packet_chain_lock));
  96. return 0;
  97. }
  98. uint8_t *p = f->packet_chain->data;
  99. record_hdr = (struct record_header*) p;
  100. int record_len;
  101. int data_len;
  102. record_len = RECORD_LEN(record_hdr)+RECORD_HEADER_LEN;
  103. data_len = f->packet_chain->data_len;
  104. packet *current = f->packet_chain;
  105. int incoming = current->incoming;
  106. record = calloc(1, record_len);
  107. for(int i=0; (i<data_len) && (i<record_len); i++){
  108. record[i] = p[i];
  109. }
  110. while(record_len > data_len) {
  111. if(current->next == NULL){
  112. goto err;
  113. }
  114. if(current->next->seq_num != current->seq_num + current->len){
  115. printf("Missing packet: seq_num= %d, datalen= %d, nextseq= %d\n", current->seq_num, current->len, current->next->seq_num);
  116. goto err;
  117. }
  118. current = current->next;
  119. p = current->data;
  120. int i;
  121. for(i=0; (i<current->data_len) && (i+data_len < record_len); i++){
  122. record[data_len+i] = p[i];
  123. }
  124. data_len += current->data_len;
  125. }
  126. switch(record_hdr->type){
  127. case HS:
  128. p = record;
  129. p += RECORD_HEADER_LEN;
  130. if((incoming && f->in_encrypted) || (!incoming && f->out_encrypted)){
  131. printf("Decrypting finished (%d bytes) (%x:%d -> %x:%d)\n", record_len - RECORD_HEADER_LEN, f->src_ip.s_addr, f->src_port, f->dst_ip.s_addr, f->dst_port);
  132. int32_t n = encrypt(f, p, p, record_len - RECORD_HEADER_LEN, incoming, 0x16, 0);
  133. if(n<=0){
  134. printf("Error decrypting finished (%x:%d -> %x:%d)\n", f->src_ip.s_addr, f->src_port, f->dst_ip.s_addr, f->dst_port);
  135. }
  136. printf("Finished decrypted: (%x:%d -> %x:%d)\n", f->src_ip.s_addr, f->src_port, f->dst_ip.s_addr, f->dst_port);
  137. p += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  138. printf("record:\n");
  139. for(int i=0; i< n; i++){
  140. printf("%02x ", p[i]);
  141. }
  142. printf("\n");
  143. update_context(f, p, n, incoming, 0x16, 0);
  144. if(incoming) f->in_encrypted = 2;
  145. else f->out_encrypted = 2;
  146. }
  147. handshake_hdr = (struct handshake_header*) p;
  148. f->state = handshake_hdr->type;
  149. switch(f->state){
  150. case TLS_CLNT_HELLO:
  151. printf("Received tagged client hello (%x:%d -> %x:%d)\n", f->src_ip.s_addr, f->src_port, f->dst_ip.s_addr, f->dst_port);
  152. update_finish_hash(f, p);
  153. check_session(f, p, HANDSHAKE_MESSAGE_LEN(handshake_hdr));
  154. break;
  155. case TLS_SERV_HELLO:
  156. printf("Received server hello (%x:%d -> %x:%d)\n", f->src_ip.s_addr, f->src_port, f->dst_ip.s_addr, f->dst_port);
  157. if(f->resume_session){
  158. verify_session_id(f,p);
  159. } else {
  160. save_session_id(f,p);
  161. }
  162. extract_server_random(f, p);
  163. update_finish_hash(f, p);
  164. break;
  165. case TLS_NEW_SESS:
  166. printf("Received new session\n");
  167. save_session_ticket(f, p, HANDSHAKE_MESSAGE_LEN(handshake_hdr));
  168. update_finish_hash(f, p);
  169. break;
  170. case TLS_CERT:
  171. printf("Received cert\n");
  172. update_finish_hash(f, p);
  173. break;
  174. case TLS_SRVR_KEYEX:
  175. printf("Received server keyex\n");
  176. update_finish_hash(f, p);
  177. if(extract_parameters(f, p)){
  178. printf("Error extracting params\n");
  179. }
  180. if(compute_master_secret(f)){
  181. printf("Error computing master secret\n");
  182. }
  183. break;
  184. case TLS_CERT_REQ:
  185. update_finish_hash(f, p);
  186. break;
  187. case TLS_SRVR_HELLO_DONE:
  188. printf("Received server hello done\n");
  189. update_finish_hash(f, p);
  190. break;
  191. case TLS_CERT_VERIFY:
  192. printf("received cert verify\n");
  193. update_finish_hash(f, p);
  194. break;
  195. case TLS_CLNT_KEYEX:
  196. printf("Received client key exchange\n");
  197. update_finish_hash(f, p);
  198. break;
  199. case TLS_FINISHED:
  200. printf("Received finished (%x:%d -> %x:%d)\n", f->src_ip.s_addr, f->src_port, f->dst_ip.s_addr, f->dst_port);
  201. verify_finish_hash(f,p, incoming);
  202. update_finish_hash(f, p);
  203. if((f->in_encrypted == 2) && (f->out_encrypted == 2)){
  204. printf("Handshake complete!\n");
  205. f->application = 1;
  206. if(current->incoming)
  207. f->seq_num = current->seq_num + current->len;
  208. while(current->next != NULL){
  209. current = current->next;
  210. if(current->incoming)
  211. f->seq_num = current->seq_num+ current->len;
  212. }
  213. }
  214. break;
  215. default:
  216. printf("Error? (%x:%d -> %x:%d)...\n", f->src_ip.s_addr, f->src_port, f->dst_ip.s_addr, f->dst_port);
  217. break;
  218. }
  219. break;
  220. case APP:
  221. printf("Application Data\n");
  222. break;
  223. case CCS:
  224. printf("CCS (%x:%d -> %x:%d) \n", f->src_ip.s_addr, f->src_port, f->dst_ip.s_addr, f->dst_port);
  225. if(incoming){
  226. f->in_encrypted = 1;
  227. } else {
  228. f->out_encrypted = 1;
  229. }
  230. /*Initialize ciphers */
  231. init_ciphers(f);
  232. break;
  233. case ALERT:
  234. p = record;
  235. p += RECORD_HEADER_LEN;
  236. if(((incoming) && (f->in_encrypted > 0)) || ((!incoming) && (f->out_encrypted > 0))){
  237. encrypt(f, p, p, record_len - RECORD_HEADER_LEN, incoming, 0x16, 0);
  238. p += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  239. }
  240. printf("Alert (%x:%d -> %x:%d) %02x %02x \n", f->src_ip.s_addr, f->src_port, f->dst_ip.s_addr, f->dst_port, p[0], p[1]);
  241. fflush(stdout);
  242. break;
  243. case HB:
  244. printf("Heartbeat\n");
  245. break;
  246. default:
  247. printf("Error: Not a Record (%x:%d -> %x:%d)\n", f->src_ip.s_addr, f->src_port, f->dst_ip.s_addr, f->dst_port);
  248. fflush(stdout);
  249. //TODO: later figure this out, for now delete
  250. packet *tmp = f->packet_chain;
  251. f->packet_chain = f->packet_chain->next;
  252. free(tmp->data);
  253. free(tmp);
  254. if( f->packet_chain != NULL){
  255. sem_post(&(f->packet_chain_lock));
  256. free(record);
  257. update_flow(f);
  258. return 0;
  259. }
  260. goto err;
  261. }
  262. //if(!f->application){
  263. f->seq_num = current->seq_num;
  264. if(record_len == data_len){
  265. /* record ended on packet boundary */
  266. current = current->next;
  267. packet *tmp = f->packet_chain;
  268. while(tmp != current){
  269. f->packet_chain = tmp->next;
  270. free(tmp->data);
  271. free(tmp);
  272. tmp = f->packet_chain;
  273. }
  274. } else {
  275. /* need to update data */
  276. packet *tmp = f->packet_chain;
  277. while(tmp != current){
  278. f->packet_chain = tmp->next;
  279. free(tmp->data);
  280. free(tmp);
  281. tmp = f->packet_chain;
  282. }
  283. memmove(current->data, current->data + (current->data_len - (data_len - record_len)), data_len - record_len);
  284. current->data_len = data_len - record_len;
  285. sem_post(&(f->packet_chain_lock));
  286. free(record);
  287. update_flow(f);
  288. return 0;
  289. }
  290. //}
  291. err:
  292. sem_post(&(f->packet_chain_lock));
  293. free(record);
  294. return 0;
  295. }
  296. /** Removes the tagged flow from the flow table: happens when
  297. * the station receives a TCP RST or FIN packet
  298. *
  299. * Input:
  300. * index: the index into the flow table of the tagged flow
  301. *
  302. * Output:
  303. * 0 on success, 1 on failure
  304. */
  305. int remove_flow(flow *f) {
  306. EVP_MD_CTX_destroy(f->finish_md_ctx);
  307. //Clean up cipher ctxs
  308. if(f->clnt_read_ctx != NULL){
  309. EVP_CIPHER_CTX_free(f->clnt_read_ctx);
  310. }
  311. if(f->clnt_write_ctx != NULL){
  312. EVP_CIPHER_CTX_free(f->clnt_write_ctx);
  313. }
  314. if(f->srvr_read_ctx != NULL){
  315. EVP_CIPHER_CTX_free(f->srvr_read_ctx);
  316. }
  317. if(f->srvr_write_ctx != NULL){
  318. EVP_CIPHER_CTX_free(f->srvr_write_ctx);
  319. }
  320. sem_wait(&flow_table_lock);
  321. flow_entry *entry = table->first_entry;
  322. if(entry->f == f){
  323. table->first_entry = entry->next;
  324. free(entry->f);
  325. free(entry);
  326. printf("flow removed!\n");
  327. fflush(stdout);
  328. table->len --;
  329. } else {
  330. flow_entry *next;
  331. for(int i=0; i< table->len; i++){
  332. if(entry->next != NULL){
  333. next = entry->next;
  334. } else {
  335. printf("Flow not in table\n");
  336. break;
  337. }
  338. if(next->f == f){
  339. entry->next = next->next;
  340. free(next->f);
  341. free(next);
  342. printf("flow removed!\n");
  343. table->len --;
  344. break;
  345. }
  346. entry = next;
  347. }
  348. }
  349. sem_post(&flow_table_lock);
  350. return 1;
  351. }
  352. /** Expands the flow table when we run out of space
  353. * TODO: implement and test
  354. */
  355. int grow_table() {
  356. return 0;
  357. }
  358. /** Returns the index of a flow in the flow table if
  359. * it exists, returns 0 if it is not present.
  360. *
  361. * Inputs:
  362. * observed: details for the observed flow
  363. *
  364. * Output:
  365. * index of flow in table or -1 if it doesn't exist
  366. */
  367. flow *check_flow(flow observed){
  368. /* Loop through flows in table and see if it exists */
  369. int i;
  370. flow_entry *entry = table->first_entry;
  371. flow *candidate;
  372. flow *found = NULL;
  373. if(entry == NULL)
  374. return NULL;
  375. sem_wait(&flow_table_lock);
  376. /* Check first in this direction */
  377. for(i=0; i<table->len; i++){
  378. if(entry == NULL){
  379. printf("Error: entry is null\n");
  380. break;
  381. }
  382. candidate = entry->f;
  383. if(candidate->src_ip.s_addr == observed.src_ip.s_addr){
  384. if(candidate->dst_ip.s_addr == observed.dst_ip.s_addr){
  385. if(candidate->src_port == observed.src_port){
  386. if(candidate->dst_port == observed.dst_port){
  387. found = candidate;
  388. }
  389. }
  390. }
  391. }
  392. entry = entry->next;
  393. }
  394. entry = table->first_entry;
  395. /* Then in the other direction */
  396. for(i=0; i<table->len; i++){
  397. if(entry == NULL){
  398. printf("Error: entry is null\n");
  399. break;
  400. }
  401. candidate = entry->f;
  402. if(candidate->src_ip.s_addr == observed.dst_ip.s_addr){
  403. if(candidate->dst_ip.s_addr == observed.src_ip.s_addr){
  404. if(candidate->src_port == observed.dst_port){
  405. if(candidate->dst_port == observed.src_port){
  406. found = candidate;
  407. }
  408. }
  409. }
  410. }
  411. entry = entry->next;
  412. }
  413. sem_post(&flow_table_lock);
  414. return found;
  415. }
  416. int init_session_cache(void){
  417. sessions = malloc(sizeof(session_cache));
  418. sessions->length = 0;
  419. sessions->first_session = NULL;
  420. return 0;
  421. }
  422. /** Called from ServerHello, verifies that the session id returned matches
  423. * the session id requested from the client hello
  424. *
  425. * Input:
  426. * f: the tagged flow
  427. * hs: a pointer to the ServerHello message
  428. *
  429. * Output:
  430. * 0 if success, 1 if failed
  431. */
  432. int verify_session_id(flow *f, uint8_t *hs){
  433. //increment pointer to point to sessionid
  434. uint8_t *p = hs + HANDSHAKE_HEADER_LEN;
  435. p += 2; //skip version
  436. p += SSL3_RANDOM_SIZE; //skip random
  437. uint8_t id_len = (uint8_t) p[0];
  438. p ++;
  439. //check to see if it matches flow's session id set by ClientHello
  440. if(f->current_session != NULL && f->current_session->session_id_len > 0 && !memcmp(f->current_session->session_id, p, id_len)){
  441. //if it matched, update flow with master secret :D
  442. session *last = sessions->first_session;
  443. int found = 0;
  444. for(int i=0; ((i<sessions->length) && (!found)); i++){
  445. if(!memcmp(last->session_id, f->current_session->session_id, id_len)){
  446. memcpy(f->master_secret, last->master_secret, SSL3_MASTER_SECRET_SIZE);
  447. found = 1;
  448. }
  449. last = last->next;
  450. }
  451. if((!found) && (f->current_session->session_ticket_len > 0)){
  452. last = sessions->first_session;
  453. for(int i=0; ((i<sessions->length) && (!found)); i++){
  454. if(!memcmp(last->session_ticket, f->current_session->session_ticket, f->current_session->session_ticket_len)){
  455. memcpy(f->master_secret, last->master_secret, SSL3_MASTER_SECRET_SIZE);
  456. found = 1;
  457. 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);
  458. for(int i=0; i< last->session_ticket_len; i++){
  459. printf("%02x ", last->session_ticket[i]);
  460. }
  461. printf("\n");
  462. }
  463. last = last->next;
  464. }
  465. }
  466. } else if (f->current_session != NULL && f->current_session->session_id_len > 0){
  467. //check to see if server's hello extension matches the ticket
  468. save_session_id(f, p);
  469. }
  470. //now check
  471. return 0;
  472. }
  473. /* Called from ClientHello. Checks to see if the session id len is > 0. If so,
  474. * saves sessionid for later verification. Also checks to see if a session
  475. * ticket is included as an extension.
  476. *
  477. * Input:
  478. * f: the tagged flow
  479. * hs: a pointer to the ServerHello message
  480. *
  481. * Output:
  482. * 0 if success, 1 if failed
  483. */
  484. int check_session(flow *f, uint8_t *hs, uint32_t len){
  485. uint8_t *p = hs + HANDSHAKE_HEADER_LEN;
  486. p += 2; //skip version
  487. p += SSL3_RANDOM_SIZE; //skip random
  488. session *new_session = calloc(1, sizeof(session));
  489. new_session->session_id_len = (uint8_t) p[0];
  490. new_session->session_ticket_len = 0;
  491. p ++;
  492. if(new_session->session_id_len > 0){
  493. f->resume_session = 1;
  494. memcpy(new_session->session_id, p, new_session->session_id_len);
  495. new_session->next = NULL;
  496. 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);
  497. f->current_session = new_session;
  498. }
  499. p += new_session->session_id_len;
  500. //check to see if there is a session ticket included
  501. //skip to extensions
  502. uint16_t ciphersuite_len = (p[0] << 8) + p[1];
  503. p += 2 + ciphersuite_len;
  504. uint8_t compress_meth_len = p[0];
  505. p += 1 + compress_meth_len;
  506. //search for SessionTicket TLS extension
  507. if(2 + SSL3_RANDOM_SIZE + new_session->session_id_len + 1 + 2 + ciphersuite_len + 1 + compress_meth_len > len){
  508. //no extension
  509. if(f->current_session == NULL)
  510. free(new_session);
  511. return 0;
  512. }
  513. uint16_t extensions_len = (p[0] << 8) + p[1];
  514. p += 2;
  515. while(extensions_len > 0){
  516. uint16_t type = (p[0] << 8) + p[1];
  517. p += 2;
  518. uint16_t ext_len = (p[0] << 8) + p[1];
  519. p += 2;
  520. if(type == 0x23){
  521. if(ext_len > 0){
  522. f->resume_session = 1;
  523. new_session->session_ticket_len = ext_len;
  524. new_session->session_ticket = calloc(1, ext_len);
  525. memcpy(new_session->session_ticket, p, ext_len);
  526. f->current_session = new_session;
  527. }
  528. }
  529. p += ext_len;
  530. extensions_len -= (4 + ext_len);
  531. }
  532. if(!f->resume_session){
  533. //see if a ticket is incuded
  534. free(new_session);
  535. }
  536. return 0;
  537. }
  538. /* Called from ServerHello during full handshake. Adds the session id to the
  539. * cache for later resumptions
  540. *
  541. * Input:
  542. * f: the tagged flow
  543. * hs: a pointer to the ServerHello message
  544. *
  545. * Output:
  546. * 0 if success, 1 if failed
  547. */
  548. int save_session_id(flow *f, uint8_t *hs){
  549. printf("saving session id\n");
  550. //increment pointer to point to sessionid
  551. uint8_t *p = hs + HANDSHAKE_HEADER_LEN;
  552. p += 2; //skip version
  553. p += SSL3_RANDOM_SIZE; //skip random
  554. session *new_session = calloc(1, sizeof(session));
  555. new_session->session_id_len = (uint8_t) p[0];
  556. if(new_session->session_id_len <= 0){
  557. //if this value is zero, the session is non-resumable or the
  558. //server will issue a NewSessionTicket handshake message
  559. free(new_session);
  560. return 0;
  561. }
  562. p++;
  563. memcpy(new_session->session_id, p, new_session->session_id_len);
  564. new_session->next = NULL;
  565. f->current_session = new_session;
  566. if(sessions->first_session == NULL){
  567. sessions->first_session = new_session;
  568. } else {
  569. session *last = sessions->first_session;
  570. for(int i=0; i< sessions->length; i++){
  571. if(last == NULL)
  572. printf("UH OH: last is null?\n");
  573. last = last->next;
  574. }
  575. last->next = new_session;
  576. }
  577. sessions->length ++;
  578. printf("Saved session id:");
  579. for(int i=0; i< new_session->session_id_len; i++){
  580. printf(" %02x", p[i]);
  581. }
  582. printf("\n");
  583. printf("THERE ARE NOW %d saved sessions\n", sessions->length);
  584. return 0;
  585. }
  586. /* Called from NewSessionTicket. Adds the session ticket to the
  587. * cache for later resumptions
  588. *
  589. * Input:
  590. * f: the tagged flow
  591. * hs: a pointer to the ServerHello message
  592. *
  593. * Output:
  594. * 0 if success, 1 if failed
  595. */
  596. int save_session_ticket(flow *f, uint8_t *hs, uint32_t len){
  597. uint8_t *p = hs + HANDSHAKE_HEADER_LEN;
  598. p += 4; //skip lifetime TODO: add to session struct
  599. session *new_session = calloc(1,sizeof(session));
  600. new_session->session_id_len = 0;
  601. new_session->session_ticket_len = (p[0] << 8) + p[1];
  602. printf("saving ticket of size %d\n", new_session->session_ticket_len);
  603. fflush(stdout);
  604. p += 2;
  605. uint8_t *ticket = calloc(1, new_session->session_ticket_len);
  606. memcpy(ticket, p, new_session->session_ticket_len);
  607. new_session->session_ticket = ticket;
  608. memcpy(new_session->master_secret, f->master_secret, SSL3_MASTER_SECRET_SIZE);
  609. if(sessions->first_session == NULL){
  610. sessions->first_session = new_session;
  611. } else {
  612. session *last = sessions->first_session;
  613. for(int i=0; i< (sessions->length-1); i++){
  614. if(last == NULL){
  615. printf("UH OH: last is null?\n");
  616. fflush(stdout);
  617. }
  618. last = last->next;
  619. }
  620. last->next = new_session;
  621. }
  622. sessions->length ++;
  623. printf("Saved session ticket:");
  624. for(int i=0; i< new_session->session_ticket_len; i++){
  625. printf(" %02x", p[i]);
  626. }
  627. printf("\n");
  628. fflush(stdout);
  629. printf("Saved session master secret:");
  630. for(int i=0; i< SSL3_MASTER_SECRET_SIZE; i++){
  631. printf(" %02x", new_session->master_secret[i]);
  632. }
  633. printf("\n");
  634. fflush(stdout);
  635. printf("THERE ARE NOW %d saved sessions\n", sessions->length);
  636. fflush(stdout);
  637. return 0;
  638. }
  639. /* Adds a packet the flow's packet chain */
  640. int add_packet(flow *f, struct packet_info *info){
  641. if (info->tcp_hdr == NULL){
  642. return 0;
  643. }
  644. packet *new_packet = calloc(1, sizeof(packet));
  645. new_packet->seq_num = htonl(info->tcp_hdr->sequence_num);
  646. new_packet->len = info->app_data_len;
  647. uint8_t *packet_data = calloc(1, new_packet->len);
  648. memcpy(packet_data, info->app_data, new_packet->len);
  649. new_packet->data = packet_data;
  650. new_packet->data_len = new_packet->len;
  651. new_packet->next = NULL;
  652. new_packet->incoming =
  653. (info->ip_hdr->src.s_addr == f->src_ip.s_addr) ? 0 : 1;
  654. /* Find appropriate place in chain */
  655. if(new_packet->data_len > 0){
  656. packet *previous = NULL;
  657. packet *next = f->packet_chain;
  658. while(next != NULL && (next->seq_num <= new_packet->seq_num)){
  659. previous = next;
  660. next = next->next;
  661. }
  662. //place packet after current
  663. if(previous == NULL){
  664. //goes at the beginning of chain
  665. new_packet->next = f->packet_chain;
  666. f->packet_chain = new_packet;
  667. } else {
  668. new_packet->next = next;
  669. previous->next = new_packet;
  670. }
  671. }
  672. return 0;
  673. }