flow.c 19 KB

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