flow.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #ifndef __RELAY_H__
  2. #define __RELAY_H__
  3. #include <netinet/in.h>
  4. #include <semaphore.h>
  5. #include <openssl/bn.h>
  6. #include <openssl/ssl.h>
  7. #include "ptwist.h"
  8. #include "slitheen.h"
  9. #include "util.h"
  10. #define MAX_FLOWS 10
  11. #define SLITHEEN_ID_LEN 28
  12. #define TLS_HELLO_REQ 0x00
  13. #define TLS_CLNT_HELLO 0x01
  14. #define TLS_SERV_HELLO 0x02
  15. #define TLS_NEW_SESS 0x04
  16. #define TLS_CERT 0x0b
  17. #define TLS_SRVR_KEYEX 0x0c
  18. #define TLS_CERT_REQ 0x0d
  19. #define TLS_SRVR_HELLO_DONE 0x0e
  20. #define TLS_CERT_VERIFY 0x0f
  21. #define TLS_CLNT_KEYEX 0x10
  22. #define TLS_FINISHED 0x14
  23. struct client_st;
  24. typedef struct client_st client;
  25. typedef struct stream_st {
  26. uint16_t stream_id;
  27. int32_t pipefd;
  28. struct stream_st *next;
  29. } stream;
  30. typedef struct stream_table_st {
  31. stream *first;
  32. } stream_table;
  33. typedef struct packet_st{
  34. uint32_t seq_num;
  35. uint16_t len;
  36. uint8_t *data;
  37. uint32_t expiration;
  38. struct packet_st *next;
  39. } packet;
  40. typedef struct packet_chain_st {
  41. packet *first_packet;
  42. uint32_t expected_seq_num;
  43. uint32_t record_len;
  44. uint32_t remaining_record_len;
  45. } packet_chain;
  46. typedef struct queue_block_st{
  47. int32_t len;
  48. int32_t offset;
  49. uint8_t *data;
  50. struct queue_block_st *next;
  51. uint16_t stream_id;
  52. } queue_block;
  53. typedef struct data_queue_st {
  54. queue_block *first_block;
  55. } data_queue;
  56. typedef struct app_data_queue_st {
  57. packet *first_packet;
  58. } app_data_queue;
  59. typedef struct frame_st {
  60. uint8_t *packet;
  61. const struct pcap_pkthdr *header;
  62. struct inject_args *iargs;
  63. uint32_t seq_num;
  64. struct frame_st *next;
  65. } frame;
  66. typedef struct frame_queue_st {
  67. frame *first_frame;
  68. } frame_queue;
  69. typedef struct session_st {
  70. uint8_t session_id_len;
  71. uint8_t session_id[SSL_MAX_SSL_SESSION_ID_LENGTH];
  72. struct session_st *next;
  73. uint8_t master_secret[SSL3_MASTER_SECRET_SIZE];
  74. uint8_t client_random[SSL3_RANDOM_SIZE];
  75. uint8_t server_random[SSL3_RANDOM_SIZE];
  76. uint32_t session_ticket_len;
  77. uint8_t *session_ticket;
  78. } session;
  79. typedef struct session_cache_st {
  80. session *first_session;
  81. uint32_t length;
  82. } session_cache;
  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. stream_table *streams; //TODO delete (reference client)
  103. data_queue *downstream_queue; //TODO: delete (reference client)
  104. client *client_ptr;
  105. packet_chain *ds_packet_chain;
  106. packet_chain *us_packet_chain;
  107. queue *ds_hs_queue;
  108. queue *us_hs_queue;
  109. sem_t packet_chain_lock;
  110. queue_block *upstream_queue;
  111. uint32_t upstream_remaining;
  112. DH *dh;
  113. EC_KEY *ecdh;
  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 handshake_hash[EVP_MAX_MD_SIZE];
  119. EVP_MD_CTX *finish_md_ctx;
  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. uint8_t client_random[SSL3_RANDOM_SIZE];
  127. uint8_t server_random[SSL3_RANDOM_SIZE];
  128. uint8_t master_secret[SSL3_MASTER_SECRET_SIZE];
  129. session *current_session;
  130. uint8_t read_seq[8];
  131. uint8_t write_seq[8];
  132. //for downstream processing
  133. uint32_t remaining_record_len;
  134. uint8_t httpstate;
  135. uint32_t remaining_response_len;
  136. uint8_t replace_response;
  137. uint8_t *outbox;
  138. int32_t outbox_len;
  139. int32_t outbox_offset;
  140. uint8_t *partial_record_header;
  141. uint8_t partial_record_header_len;
  142. //locking
  143. //pthread_mutex_t flow_lock = PTHREAD_MUTEX_INITIALIZER;
  144. } flow;
  145. typedef struct flow_entry_st {
  146. flow *f;
  147. struct flow_entry_st *next;
  148. } flow_entry;
  149. typedef struct flow_table_st {
  150. flow_entry *first_entry;
  151. int len;
  152. } flow_table;
  153. int init_tables(void);
  154. flow *add_flow(struct packet_info *info);
  155. int update_flow(flow *f, uint8_t *record, uint8_t incoming);
  156. int remove_flow(flow *f);
  157. flow *check_flow(struct packet_info *info);
  158. flow *get_flow(int index);
  159. int init_session_cache (void);
  160. int verify_session_id(flow *f, uint8_t *hs);
  161. int check_session(flow *f, uint8_t *hs, uint32_t len);
  162. int save_session_id(flow *f, uint8_t *hs);
  163. int save_session_ticket(flow *f, uint8_t *hs, uint32_t len);
  164. int add_packet(flow *f, struct packet_info *info);
  165. #endif /* __RELAY_H__ */