flow.c 21 KB

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