flow.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 stream_st {
  43. uint16_t stream_id;
  44. int32_t pipefd;
  45. struct stream_st *next;
  46. } stream;
  47. typedef struct stream_table_st {
  48. stream *first;
  49. } stream_table;
  50. typedef struct packet_st{
  51. uint32_t seq_num;
  52. uint16_t len;
  53. uint8_t *data;
  54. uint32_t expiration;
  55. struct packet_st *next;
  56. } packet;
  57. typedef struct packet_chain_st packet_chain;
  58. typedef struct queue_block_st{
  59. int32_t len;
  60. int32_t offset;
  61. uint8_t *data;
  62. struct queue_block_st *next;
  63. uint16_t stream_id;
  64. } queue_block;
  65. typedef struct data_queue_st {
  66. queue_block *first_block;
  67. } data_queue;
  68. typedef struct app_data_queue_st {
  69. packet *first_packet;
  70. } app_data_queue;
  71. typedef struct frame_st {
  72. uint8_t *packet;
  73. const struct pcap_pkthdr *header;
  74. struct inject_args *iargs;
  75. uint32_t seq_num;
  76. struct frame_st *next;
  77. } frame;
  78. typedef struct frame_queue_st {
  79. frame *first_frame;
  80. } frame_queue;
  81. typedef struct session_st {
  82. uint8_t session_id_len;
  83. uint8_t session_id[SSL_MAX_SSL_SESSION_ID_LENGTH];
  84. struct session_st *next;
  85. uint8_t master_secret[SSL3_MASTER_SECRET_SIZE];
  86. uint8_t client_random[SSL3_RANDOM_SIZE];
  87. uint8_t server_random[SSL3_RANDOM_SIZE];
  88. uint32_t session_ticket_len;
  89. uint8_t *session_ticket;
  90. } session;
  91. typedef struct flow_st {
  92. sem_t flow_lock;
  93. uint32_t ref_ctr;
  94. uint8_t removed;
  95. struct in_addr src_ip, dst_ip; /* Source (client) and Destination (server) addresses */
  96. uint16_t src_port, dst_port; /* Source and Destination ports */
  97. uint32_t upstream_seq_num; /* sequence number */
  98. uint32_t downstream_seq_num; /* sequence number */
  99. app_data_queue *upstream_app_data; /* Saved application-layer data for packet retransmits */
  100. app_data_queue *downstream_app_data;
  101. frame_queue *us_frame_queue; /*Held misordered Ethernet frames to be processed and written out later */
  102. frame_queue *ds_frame_queue; /*Held misordered Ethernet frames to be processed and written out later */
  103. byte key[16]; /* negotiated key */
  104. int state; /* TLS handshake state */
  105. int in_encrypted; /* indicates whether incoming flow is encrypted */
  106. int out_encrypted; /* indicates whether outgoing flow is encrypted */
  107. int application; /* indicates handshake is complete */
  108. int stall; /* indicates the Finished message is expected and relay station should stall */
  109. int resume_session;
  110. stream_table *streams; //TODO delete (reference client)
  111. data_queue *downstream_queue; //TODO: delete (reference client)
  112. client *client_ptr;
  113. packet_chain *ds_packet_chain;
  114. packet_chain *us_packet_chain;
  115. queue *ds_hs_queue;
  116. queue *us_hs_queue;
  117. sem_t packet_chain_lock;
  118. queue_block *upstream_queue;
  119. uint32_t upstream_remaining;
  120. DH *dh;
  121. EC_KEY *ecdh;
  122. EVP_PKEY *srvr_key;
  123. sem_t upstream_queue_lock;
  124. const EVP_CIPHER *cipher;
  125. const EVP_MD *message_digest;
  126. uint8_t keyex_alg;
  127. uint8_t extended_master_secret;
  128. uint8_t handshake_hash[EVP_MAX_MD_SIZE];
  129. EVP_CIPHER_CTX *clnt_read_ctx;
  130. EVP_CIPHER_CTX *clnt_write_ctx;
  131. EVP_CIPHER_CTX *srvr_read_ctx;
  132. EVP_CIPHER_CTX *srvr_write_ctx;
  133. EVP_MD_CTX *read_mac_ctx;
  134. EVP_MD_CTX *write_mac_ctx;
  135. EVP_MD_CTX *hs_md_ctx;
  136. GCM128_CONTEXT *gcm_ctx_out;
  137. uint8_t *gcm_ctx_iv;
  138. int32_t gcm_ctx_ivlen;
  139. AES_KEY *gcm_ctx_key;
  140. uint8_t client_random[SSL3_RANDOM_SIZE];
  141. uint8_t server_random[SSL3_RANDOM_SIZE];
  142. uint8_t master_secret[SSL3_MASTER_SECRET_SIZE];
  143. session *current_session;
  144. uint8_t read_seq[8];
  145. uint8_t write_seq[8];
  146. //for downstream processing
  147. uint32_t remaining_record_len;
  148. uint8_t httpstate;
  149. uint32_t remaining_response_len;
  150. uint8_t replace_response;
  151. uint8_t *outbox;
  152. int32_t outbox_len;
  153. int32_t outbox_offset;
  154. uint8_t *partial_record_header;
  155. uint8_t partial_record_header_len;
  156. uint8_t *partial_record;
  157. uint8_t *partial_record_dec;
  158. uint32_t partial_record_len;
  159. uint32_t partial_record_total_len;
  160. //locking
  161. //pthread_mutex_t flow_lock = PTHREAD_MUTEX_INITIALIZER;
  162. } flow;
  163. int init_tables(void);
  164. flow *add_flow(struct packet_info *info);
  165. int remove_flow(flow *f);
  166. flow *check_flow(struct packet_info *info);
  167. int init_session_cache (void);
  168. int add_packet(flow *f, struct packet_info *info);
  169. #endif /* FLOW_H */