flow.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* Slitheen - a decoy routing system for censorship resistance
  2. * Copyright (C) 2017 Cecylia Bocovich (cbocovic@uwaterloo.ca)
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, version 3.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * Additional permission under GNU GPL version 3 section 7
  17. *
  18. * If you modify this Program, or any covered work, by linking or combining
  19. * it with the OpenSSL library (or a modified version of that library),
  20. * containing parts covered by the terms of the OpenSSL Licence and the
  21. * SSLeay license, the licensors of this Program grant you additional
  22. * permission to convey the resulting work. Corresponding Source for a
  23. * non-source form of such a combination shall include the source code
  24. * for the parts of the OpenSSL library used as well as that of the covered
  25. * work.
  26. */
  27. #ifndef FLOW_H
  28. #define FLOW_H
  29. #include <netinet/in.h>
  30. #include <semaphore.h>
  31. #include <openssl/bn.h>
  32. #include <openssl/ssl.h>
  33. #include <openssl/modes.h>
  34. #include <openssl/aes.h>
  35. #include "ptwist.h"
  36. #include "packet.h"
  37. #include "util.h"
  38. #define MAX_FLOWS 10
  39. #define SLITHEEN_ID_LEN 28
  40. struct client_st;
  41. typedef struct client_st client;
  42. typedef struct packet_st{
  43. uint32_t seq_num;
  44. uint16_t len;
  45. uint8_t *data;
  46. uint32_t expiration;
  47. struct packet_st *next;
  48. } packet;
  49. typedef struct packet_chain_st packet_chain;
  50. typedef struct queue_block_st{
  51. int32_t len;
  52. int32_t offset;
  53. uint8_t *data;
  54. struct queue_block_st *next;
  55. uint16_t stream_id;
  56. } queue_block;
  57. typedef struct data_queue_st {
  58. queue_block *first_block;
  59. } data_queue;
  60. typedef struct app_data_queue_st {
  61. packet *first_packet;
  62. } app_data_queue;
  63. typedef struct frame_st {
  64. uint8_t *packet;
  65. const struct pcap_pkthdr *header;
  66. struct inject_args *iargs;
  67. uint32_t seq_num;
  68. struct frame_st *next;
  69. } frame;
  70. typedef struct frame_queue_st {
  71. frame *first_frame;
  72. } frame_queue;
  73. typedef struct session_st {
  74. uint8_t session_id_len;
  75. uint8_t session_id[SSL_MAX_SSL_SESSION_ID_LENGTH];
  76. struct session_st *next;
  77. uint8_t master_secret[SSL3_MASTER_SECRET_SIZE];
  78. uint8_t client_random[SSL3_RANDOM_SIZE];
  79. uint8_t server_random[SSL3_RANDOM_SIZE];
  80. uint32_t session_ticket_len;
  81. uint8_t *session_ticket;
  82. } session;
  83. typedef struct flow_st {
  84. sem_t flow_lock;
  85. uint32_t ref_ctr;
  86. uint8_t removed;
  87. struct in_addr src_ip, dst_ip; /* Source (client) and Destination (server) addresses */
  88. uint16_t src_port, dst_port; /* Source and Destination ports */
  89. uint32_t upstream_seq_num; /* sequence number */
  90. uint32_t downstream_seq_num; /* sequence number */
  91. app_data_queue *upstream_app_data; /* Saved application-layer data for packet retransmits */
  92. app_data_queue *downstream_app_data;
  93. frame_queue *us_frame_queue; /*Held misordered Ethernet frames to be processed and written out later */
  94. frame_queue *ds_frame_queue; /*Held misordered Ethernet frames to be processed and written out later */
  95. byte key[16]; /* negotiated key */
  96. int state; /* TLS handshake state */
  97. int in_encrypted; /* indicates whether incoming flow is encrypted */
  98. int out_encrypted; /* indicates whether outgoing flow is encrypted */
  99. int application; /* indicates handshake is complete */
  100. int stall; /* indicates the Finished message is expected and relay station should stall */
  101. int resume_session;
  102. data_queue *downstream_queue; //TODO: delete (reference client)
  103. client *client_ptr;
  104. packet_chain *ds_packet_chain;
  105. packet_chain *us_packet_chain;
  106. queue *ds_hs_queue;
  107. queue *us_hs_queue;
  108. sem_t packet_chain_lock;
  109. queue_block *upstream_queue;
  110. uint32_t upstream_remaining;
  111. DH *dh;
  112. EC_KEY *ecdh;
  113. EVP_PKEY *srvr_key;
  114. sem_t upstream_queue_lock;
  115. const EVP_CIPHER *cipher;
  116. const EVP_MD *message_digest;
  117. uint8_t keyex_alg;
  118. uint8_t extended_master_secret;
  119. uint8_t handshake_hash[EVP_MAX_MD_SIZE];
  120. EVP_CIPHER_CTX *clnt_read_ctx;
  121. EVP_CIPHER_CTX *clnt_write_ctx;
  122. EVP_CIPHER_CTX *srvr_read_ctx;
  123. EVP_CIPHER_CTX *srvr_write_ctx;
  124. EVP_MD_CTX *read_mac_ctx;
  125. EVP_MD_CTX *write_mac_ctx;
  126. EVP_MD_CTX *hs_md_ctx;
  127. GCM128_CONTEXT *gcm_ctx_out;
  128. uint8_t *gcm_ctx_iv;
  129. int32_t gcm_ctx_ivlen;
  130. AES_KEY *gcm_ctx_key;
  131. uint8_t client_random[SSL3_RANDOM_SIZE];
  132. uint8_t server_random[SSL3_RANDOM_SIZE];
  133. uint8_t master_secret[SSL3_MASTER_SECRET_SIZE];
  134. session *current_session;
  135. uint8_t read_seq[8];
  136. uint8_t write_seq[8];
  137. //for downstream processing
  138. uint32_t remaining_record_len;
  139. uint8_t httpstate;
  140. uint32_t remaining_response_len;
  141. uint8_t replace_response;
  142. uint8_t *outbox;
  143. int32_t outbox_len;
  144. int32_t outbox_offset;
  145. uint8_t *partial_record_header;
  146. uint8_t partial_record_header_len;
  147. uint8_t *partial_record;
  148. uint8_t *partial_record_dec;
  149. uint32_t partial_record_len;
  150. uint32_t partial_record_total_len;
  151. //locking
  152. //pthread_mutex_t flow_lock = PTHREAD_MUTEX_INITIALIZER;
  153. } flow;
  154. int init_tables(void);
  155. flow *add_flow(struct packet_info *info);
  156. int remove_flow(flow *f);
  157. flow *check_flow(struct packet_info *info);
  158. int init_session_cache (void);
  159. int add_packet(flow *f, struct packet_info *info);
  160. #endif /* FLOW_H */