flow.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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. EVP_MD_CTX_destroy(f->finish_md_ctx);
  305. //Clean up cipher ctxs
  306. if(f->clnt_read_ctx != NULL){
  307. EVP_CIPHER_CTX_free(f->clnt_read_ctx);
  308. }
  309. if(f->clnt_write_ctx != NULL){
  310. EVP_CIPHER_CTX_free(f->clnt_write_ctx);
  311. }
  312. if(f->srvr_read_ctx != NULL){
  313. EVP_CIPHER_CTX_free(f->srvr_read_ctx);
  314. }
  315. if(f->srvr_write_ctx != NULL){
  316. EVP_CIPHER_CTX_free(f->srvr_write_ctx);
  317. }
  318. sem_wait(&flow_table_lock);
  319. flow_entry *entry = table->first_entry;
  320. if(entry->f == f){
  321. table->first_entry = entry->next;
  322. free(entry->f);
  323. free(entry);
  324. printf("flow removed!\n");
  325. fflush(stdout);
  326. table->len --;
  327. } else {
  328. flow_entry *next;
  329. for(int i=0; i< table->len; i++){
  330. if(entry->next != NULL){
  331. next = entry->next;
  332. } else {
  333. printf("Flow not in table\n");
  334. break;
  335. }
  336. if(next->f == f){
  337. entry->next = next->next;
  338. free(next->f);
  339. free(next);
  340. printf("flow removed!\n");
  341. table->len --;
  342. break;
  343. }
  344. entry = next;
  345. }
  346. }
  347. sem_post(&flow_table_lock);
  348. return 1;
  349. }
  350. /** Expands the flow table when we run out of space
  351. * TODO: implement and test
  352. */
  353. int grow_table() {
  354. return 0;
  355. }
  356. /** Returns the index of a flow in the flow table if
  357. * it exists, returns 0 if it is not present.
  358. *
  359. * Inputs:
  360. * observed: details for the observed flow
  361. *
  362. * Output:
  363. * index of flow in table or -1 if it doesn't exist
  364. */
  365. flow *check_flow(flow observed){
  366. /* Loop through flows in table and see if it exists */
  367. int i;
  368. flow_entry *entry = table->first_entry;
  369. flow *candidate;
  370. flow *found = NULL;
  371. if(entry == NULL)
  372. return NULL;
  373. sem_wait(&flow_table_lock);
  374. /* Check first in this direction */
  375. for(i=0; i<table->len; i++){
  376. if(entry == NULL){
  377. printf("Error: entry is null\n");
  378. break;
  379. }
  380. candidate = entry->f;
  381. if(candidate->src_ip.s_addr == observed.src_ip.s_addr){
  382. if(candidate->dst_ip.s_addr == observed.dst_ip.s_addr){
  383. if(candidate->src_port == observed.src_port){
  384. if(candidate->dst_port == observed.dst_port){
  385. found = candidate;
  386. }
  387. }
  388. }
  389. }
  390. entry = entry->next;
  391. }
  392. entry = table->first_entry;
  393. /* Then in the other direction */
  394. for(i=0; i<table->len; i++){
  395. if(entry == NULL){
  396. printf("Error: entry is null\n");
  397. break;
  398. }
  399. candidate = entry->f;
  400. if(candidate->src_ip.s_addr == observed.dst_ip.s_addr){
  401. if(candidate->dst_ip.s_addr == observed.src_ip.s_addr){
  402. if(candidate->src_port == observed.dst_port){
  403. if(candidate->dst_port == observed.src_port){
  404. found = candidate;
  405. }
  406. }
  407. }
  408. }
  409. entry = entry->next;
  410. }
  411. sem_post(&flow_table_lock);
  412. return found;
  413. }
  414. int init_session_cache(void){
  415. sessions = malloc(sizeof(session_cache));
  416. sessions->length = 0;
  417. sessions->first_session = NULL;
  418. return 0;
  419. }
  420. /** Called from ServerHello, verifies that the session id returned matches
  421. * the session id requested from the client hello
  422. *
  423. * Input:
  424. * f: the tagged flow
  425. * hs: a pointer to the ServerHello message
  426. *
  427. * Output:
  428. * 0 if success, 1 if failed
  429. */
  430. int verify_session_id(flow *f, uint8_t *hs){
  431. //increment pointer to point to sessionid
  432. uint8_t *p = hs + HANDSHAKE_HEADER_LEN;
  433. p += 2; //skip version
  434. p += SSL3_RANDOM_SIZE; //skip random
  435. uint8_t id_len = (uint8_t) p[0];
  436. p ++;
  437. //check to see if it matches flow's session id set by ClientHello
  438. if(f->current_session != NULL && f->current_session->session_id_len > 0 && !memcmp(f->current_session->session_id, p, id_len)){
  439. //if it matched, update flow with master secret :D
  440. session *last = sessions->first_session;
  441. int found = 0;
  442. for(int i=0; ((i<sessions->length) && (!found)); i++){
  443. if(!memcmp(last->session_id, f->current_session->session_id, id_len)){
  444. memcpy(f->master_secret, last->master_secret, SSL3_MASTER_SECRET_SIZE);
  445. found = 1;
  446. }
  447. last = last->next;
  448. }
  449. if((!found) && (f->current_session->session_ticket_len > 0)){
  450. last = sessions->first_session;
  451. for(int i=0; ((i<sessions->length) && (!found)); i++){
  452. if(!memcmp(last->session_ticket, f->current_session->session_ticket, f->current_session->session_ticket_len)){
  453. memcpy(f->master_secret, last->master_secret, SSL3_MASTER_SECRET_SIZE);
  454. found = 1;
  455. 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);
  456. for(int i=0; i< last->session_ticket_len; i++){
  457. printf("%02x ", last->session_ticket[i]);
  458. }
  459. printf("\n");
  460. }
  461. last = last->next;
  462. }
  463. }
  464. } else if (f->current_session != NULL && f->current_session->session_id_len > 0){
  465. //check to see if server's hello extension matches the ticket
  466. save_session_id(f, p);
  467. }
  468. //now check
  469. return 0;
  470. }
  471. /* Called from ClientHello. Checks to see if the session id len is > 0. If so,
  472. * saves sessionid for later verification. Also checks to see if a session
  473. * ticket is included as an extension.
  474. *
  475. * Input:
  476. * f: the tagged flow
  477. * hs: a pointer to the ServerHello message
  478. *
  479. * Output:
  480. * 0 if success, 1 if failed
  481. */
  482. int check_session(flow *f, uint8_t *hs, uint32_t len){
  483. uint8_t *p = hs + HANDSHAKE_HEADER_LEN;
  484. p += 2; //skip version
  485. p += SSL3_RANDOM_SIZE; //skip random
  486. session *new_session = calloc(1, sizeof(session));
  487. new_session->session_id_len = (uint8_t) p[0];
  488. new_session->session_ticket_len = 0;
  489. p ++;
  490. if(new_session->session_id_len > 0){
  491. f->resume_session = 1;
  492. memcpy(new_session->session_id, p, new_session->session_id_len);
  493. new_session->next = NULL;
  494. 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);
  495. f->current_session = new_session;
  496. }
  497. p += new_session->session_id_len;
  498. //check to see if there is a session ticket included
  499. //skip to extensions
  500. uint16_t ciphersuite_len = (p[0] << 8) + p[1];
  501. p += 2 + ciphersuite_len;
  502. uint8_t compress_meth_len = p[0];
  503. p += 1 + compress_meth_len;
  504. //search for SessionTicket TLS extension
  505. if(2 + SSL3_RANDOM_SIZE + new_session->session_id_len + 1 + 2 + ciphersuite_len + 1 + compress_meth_len > len){
  506. //no extension
  507. if(f->current_session == NULL)
  508. free(new_session);
  509. return 0;
  510. }
  511. uint16_t extensions_len = (p[0] << 8) + p[1];
  512. p += 2;
  513. while(extensions_len > 0){
  514. uint16_t type = (p[0] << 8) + p[1];
  515. p += 2;
  516. uint16_t ext_len = (p[0] << 8) + p[1];
  517. p += 2;
  518. if(type == 0x23){
  519. if(ext_len > 0){
  520. f->resume_session = 1;
  521. new_session->session_ticket_len = ext_len;
  522. new_session->session_ticket = calloc(1, ext_len);
  523. memcpy(new_session->session_ticket, p, ext_len);
  524. f->current_session = new_session;
  525. }
  526. }
  527. p += ext_len;
  528. extensions_len -= (4 + ext_len);
  529. }
  530. if(!f->resume_session){
  531. //see if a ticket is incuded
  532. free(new_session);
  533. }
  534. return 0;
  535. }
  536. /* Called from ServerHello during full handshake. Adds the session id to the
  537. * cache for later resumptions
  538. *
  539. * Input:
  540. * f: the tagged flow
  541. * hs: a pointer to the ServerHello message
  542. *
  543. * Output:
  544. * 0 if success, 1 if failed
  545. */
  546. int save_session_id(flow *f, uint8_t *hs){
  547. printf("saving session id\n");
  548. //increment pointer to point to sessionid
  549. uint8_t *p = hs + HANDSHAKE_HEADER_LEN;
  550. p += 2; //skip version
  551. p += SSL3_RANDOM_SIZE; //skip random
  552. session *new_session = calloc(1, sizeof(session));
  553. new_session->session_id_len = (uint8_t) p[0];
  554. if(new_session->session_id_len <= 0){
  555. //if this value is zero, the session is non-resumable or the
  556. //server will issue a NewSessionTicket handshake message
  557. free(new_session);
  558. return 0;
  559. }
  560. p++;
  561. memcpy(new_session->session_id, p, new_session->session_id_len);
  562. new_session->next = NULL;
  563. f->current_session = new_session;
  564. if(sessions->first_session == NULL){
  565. sessions->first_session = new_session;
  566. } else {
  567. session *last = sessions->first_session;
  568. for(int i=0; i< sessions->length; i++){
  569. if(last == NULL)
  570. printf("UH OH: last is null?\n");
  571. last = last->next;
  572. }
  573. last->next = new_session;
  574. }
  575. sessions->length ++;
  576. printf("Saved session id:");
  577. for(int i=0; i< new_session->session_id_len; i++){
  578. printf(" %02x", p[i]);
  579. }
  580. printf("\n");
  581. printf("THERE ARE NOW %d saved sessions\n", sessions->length);
  582. return 0;
  583. }
  584. /* Called from NewSessionTicket. Adds the session ticket to the
  585. * cache for later resumptions
  586. *
  587. * Input:
  588. * f: the tagged flow
  589. * hs: a pointer to the ServerHello message
  590. *
  591. * Output:
  592. * 0 if success, 1 if failed
  593. */
  594. int save_session_ticket(flow *f, uint8_t *hs, uint32_t len){
  595. uint8_t *p = hs + HANDSHAKE_HEADER_LEN;
  596. p += 4; //skip lifetime TODO: add to session struct
  597. session *new_session = calloc(1,sizeof(session));
  598. new_session->session_id_len = 0;
  599. new_session->session_ticket_len = (p[0] << 8) + p[1];
  600. printf("saving ticket of size %d\n", new_session->session_ticket_len);
  601. fflush(stdout);
  602. p += 2;
  603. uint8_t *ticket = calloc(1, new_session->session_ticket_len);
  604. memcpy(ticket, p, new_session->session_ticket_len);
  605. new_session->session_ticket = ticket;
  606. memcpy(new_session->master_secret, f->master_secret, SSL3_MASTER_SECRET_SIZE);
  607. if(sessions->first_session == NULL){
  608. sessions->first_session = new_session;
  609. } else {
  610. session *last = sessions->first_session;
  611. for(int i=0; i< (sessions->length-1); i++){
  612. if(last == NULL){
  613. printf("UH OH: last is null?\n");
  614. fflush(stdout);
  615. }
  616. last = last->next;
  617. }
  618. last->next = new_session;
  619. }
  620. sessions->length ++;
  621. printf("Saved session ticket:");
  622. for(int i=0; i< new_session->session_ticket_len; i++){
  623. printf(" %02x", p[i]);
  624. }
  625. printf("\n");
  626. fflush(stdout);
  627. printf("Saved session master secret:");
  628. for(int i=0; i< SSL3_MASTER_SECRET_SIZE; i++){
  629. printf(" %02x", new_session->master_secret[i]);
  630. }
  631. printf("\n");
  632. fflush(stdout);
  633. printf("THERE ARE NOW %d saved sessions\n", sessions->length);
  634. fflush(stdout);
  635. return 0;
  636. }
  637. /* Adds a packet the flow's packet chain */
  638. int add_packet(flow *f, struct packet_info *info){
  639. if (info->tcp_hdr == NULL){
  640. return 0;
  641. }
  642. packet *new_packet = calloc(1, sizeof(packet));
  643. new_packet->seq_num = htonl(info->tcp_hdr->sequence_num);
  644. new_packet->len = info->app_data_len;
  645. uint8_t *packet_data = calloc(1, new_packet->len);
  646. memcpy(packet_data, info->app_data, new_packet->len);
  647. new_packet->data = packet_data;
  648. new_packet->data_len = new_packet->len;
  649. new_packet->next = NULL;
  650. new_packet->incoming =
  651. (info->ip_hdr->src.s_addr == f->src_ip.s_addr) ? 0 : 1;
  652. /* Find appropriate place in chain */
  653. if(new_packet->data_len > 0){
  654. packet *previous = NULL;
  655. packet *next = f->packet_chain;
  656. while(next != NULL && (next->seq_num <= new_packet->seq_num)){
  657. previous = next;
  658. next = next->next;
  659. }
  660. //place packet after current
  661. if(previous == NULL){
  662. //goes at the beginning of chain
  663. new_packet->next = f->packet_chain;
  664. f->packet_chain = new_packet;
  665. } else {
  666. new_packet->next = next;
  667. previous->next = new_packet;
  668. }
  669. }
  670. return 0;
  671. }