flow.h 6.2 KB

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