flow.c 15 KB

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