flow.c 23 KB

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