flow.h 6.9 KB

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