flow.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 __RELAY_H__
  28. #define __RELAY_H__
  29. #include <netinet/in.h>
  30. #include <semaphore.h>
  31. #include <openssl/bn.h>
  32. #include <openssl/ssl.h>
  33. #include "ptwist.h"
  34. #include "slitheen.h"
  35. #include "util.h"
  36. #define MAX_FLOWS 10
  37. #define SLITHEEN_ID_LEN 28
  38. #define TLS_HELLO_REQ 0x00
  39. #define TLS_CLNT_HELLO 0x01
  40. #define TLS_SERV_HELLO 0x02
  41. #define TLS_NEW_SESS 0x04
  42. #define TLS_CERT 0x0b
  43. #define TLS_SRVR_KEYEX 0x0c
  44. #define TLS_CERT_REQ 0x0d
  45. #define TLS_SRVR_HELLO_DONE 0x0e
  46. #define TLS_CERT_VERIFY 0x0f
  47. #define TLS_CLNT_KEYEX 0x10
  48. #define TLS_FINISHED 0x14
  49. struct client_st;
  50. typedef struct client_st client;
  51. typedef struct stream_st {
  52. uint16_t stream_id;
  53. int32_t pipefd;
  54. struct stream_st *next;
  55. } stream;
  56. typedef struct stream_table_st {
  57. stream *first;
  58. } stream_table;
  59. typedef struct packet_st{
  60. uint32_t seq_num;
  61. uint16_t len;
  62. uint8_t *data;
  63. uint32_t expiration;
  64. struct packet_st *next;
  65. } packet;
  66. typedef struct packet_chain_st {
  67. packet *first_packet;
  68. uint32_t expected_seq_num;
  69. uint32_t record_len;
  70. uint32_t remaining_record_len;
  71. } packet_chain;
  72. typedef struct queue_block_st{
  73. int32_t len;
  74. int32_t offset;
  75. uint8_t *data;
  76. struct queue_block_st *next;
  77. uint16_t stream_id;
  78. } queue_block;
  79. typedef struct data_queue_st {
  80. queue_block *first_block;
  81. } data_queue;
  82. typedef struct app_data_queue_st {
  83. packet *first_packet;
  84. } app_data_queue;
  85. typedef struct frame_st {
  86. uint8_t *packet;
  87. const struct pcap_pkthdr *header;
  88. struct inject_args *iargs;
  89. uint32_t seq_num;
  90. struct frame_st *next;
  91. } frame;
  92. typedef struct frame_queue_st {
  93. frame *first_frame;
  94. } frame_queue;
  95. typedef struct session_st {
  96. uint8_t session_id_len;
  97. uint8_t session_id[SSL_MAX_SSL_SESSION_ID_LENGTH];
  98. struct session_st *next;
  99. uint8_t master_secret[SSL3_MASTER_SECRET_SIZE];
  100. uint8_t client_random[SSL3_RANDOM_SIZE];
  101. uint8_t server_random[SSL3_RANDOM_SIZE];
  102. uint32_t session_ticket_len;
  103. uint8_t *session_ticket;
  104. } session;
  105. typedef struct session_cache_st {
  106. session *first_session;
  107. uint32_t length;
  108. } session_cache;
  109. typedef struct flow_st {
  110. sem_t flow_lock;
  111. uint32_t ref_ctr;
  112. uint8_t removed;
  113. struct in_addr src_ip, dst_ip; /* Source (client) and Destination (server) addresses */
  114. uint16_t src_port, dst_port; /* Source and Destination ports */
  115. uint32_t upstream_seq_num; /* sequence number */
  116. uint32_t downstream_seq_num; /* sequence number */
  117. app_data_queue *upstream_app_data; /* Saved application-layer data for packet retransmits */
  118. app_data_queue *downstream_app_data;
  119. frame_queue *us_frame_queue; /*Held misordered Ethernet frames to be processed and written out later */
  120. frame_queue *ds_frame_queue; /*Held misordered Ethernet frames to be processed and written out later */
  121. byte key[16]; /* negotiated key */
  122. int state; /* TLS handshake state */
  123. int in_encrypted; /* indicates whether incoming flow is encrypted */
  124. int out_encrypted; /* indicates whether outgoing flow is encrypted */
  125. int application; /* indicates handshake is complete */
  126. int stall; /* indicates the Finished message is expected and relay station should stall */
  127. int resume_session;
  128. stream_table *streams; //TODO delete (reference client)
  129. data_queue *downstream_queue; //TODO: delete (reference client)
  130. client *client_ptr;
  131. packet_chain *ds_packet_chain;
  132. packet_chain *us_packet_chain;
  133. queue *ds_hs_queue;
  134. queue *us_hs_queue;
  135. sem_t packet_chain_lock;
  136. queue_block *upstream_queue;
  137. uint32_t upstream_remaining;
  138. DH *dh;
  139. EC_KEY *ecdh;
  140. sem_t upstream_queue_lock;
  141. const EVP_CIPHER *cipher;
  142. const EVP_MD *message_digest;
  143. uint8_t keyex_alg;
  144. uint8_t handshake_hash[EVP_MAX_MD_SIZE];
  145. EVP_MD_CTX *finish_md_ctx;
  146. EVP_CIPHER_CTX *clnt_read_ctx;
  147. EVP_CIPHER_CTX *clnt_write_ctx;
  148. EVP_CIPHER_CTX *srvr_read_ctx;
  149. EVP_CIPHER_CTX *srvr_write_ctx;
  150. EVP_MD_CTX *read_mac_ctx;
  151. EVP_MD_CTX *write_mac_ctx;
  152. uint8_t client_random[SSL3_RANDOM_SIZE];
  153. uint8_t server_random[SSL3_RANDOM_SIZE];
  154. uint8_t master_secret[SSL3_MASTER_SECRET_SIZE];
  155. session *current_session;
  156. uint8_t read_seq[8];
  157. uint8_t write_seq[8];
  158. //for downstream processing
  159. uint32_t remaining_record_len;
  160. uint8_t httpstate;
  161. uint32_t remaining_response_len;
  162. uint8_t replace_response;
  163. uint8_t *outbox;
  164. int32_t outbox_len;
  165. int32_t outbox_offset;
  166. uint8_t *partial_record_header;
  167. uint8_t partial_record_header_len;
  168. //locking
  169. //pthread_mutex_t flow_lock = PTHREAD_MUTEX_INITIALIZER;
  170. } flow;
  171. typedef struct flow_entry_st {
  172. flow *f;
  173. struct flow_entry_st *next;
  174. } flow_entry;
  175. typedef struct flow_table_st {
  176. flow_entry *first_entry;
  177. int len;
  178. } flow_table;
  179. int init_tables(void);
  180. flow *add_flow(struct packet_info *info);
  181. int update_flow(flow *f, uint8_t *record, uint8_t incoming);
  182. int remove_flow(flow *f);
  183. flow *check_flow(struct packet_info *info);
  184. flow *get_flow(int index);
  185. int init_session_cache (void);
  186. int verify_session_id(flow *f, uint8_t *hs);
  187. int check_session(flow *f, uint8_t *hs, uint32_t len);
  188. int save_session_id(flow *f, uint8_t *hs);
  189. int save_session_ticket(flow *f, uint8_t *hs, uint32_t len);
  190. int add_packet(flow *f, struct packet_info *info);
  191. #endif /* __RELAY_H__ */