flow.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #define MAX_FLOWS 10
  10. #define SLITHEEN_ID_LEN 28
  11. #define TLS_HELLO_REQ 0x00
  12. #define TLS_CLNT_HELLO 0x01
  13. #define TLS_SERV_HELLO 0x02
  14. #define TLS_NEW_SESS 0x04
  15. #define TLS_CERT 0x0b
  16. #define TLS_SRVR_KEYEX 0x0c
  17. #define TLS_CERT_REQ 0x0d
  18. #define TLS_SRVR_HELLO_DONE 0x0e
  19. #define TLS_CERT_VERIFY 0x0f
  20. #define TLS_CLNT_KEYEX 0x10
  21. #define TLS_FINISHED 0x14
  22. struct client_st;
  23. typedef struct client_st client;
  24. typedef struct stream_st {
  25. uint16_t stream_id;
  26. int32_t pipefd;
  27. struct stream_st *next;
  28. } stream;
  29. typedef struct stream_table_st {
  30. stream *first;
  31. } stream_table;
  32. typedef struct packet_st{
  33. uint32_t seq_num;
  34. uint16_t len;
  35. uint8_t *data;
  36. struct packet_st *next;
  37. } packet;
  38. typedef struct packet_chain_st {
  39. packet *first_packet;
  40. uint32_t expected_seq_num;
  41. uint32_t record_len;
  42. uint32_t remaining_record_len;
  43. } packet_chain;
  44. typedef struct queue_block_st{
  45. int32_t len;
  46. int32_t offset;
  47. uint8_t *data;
  48. struct queue_block_st *next;
  49. uint16_t stream_id;
  50. } queue_block;
  51. typedef struct data_queue_st {
  52. queue_block *first_block;
  53. } data_queue;
  54. typedef struct app_data_queue_st {
  55. packet *first_packet;
  56. } app_data_queue;
  57. typedef struct session_st {
  58. uint8_t session_id_len;
  59. uint8_t session_id[SSL_MAX_SSL_SESSION_ID_LENGTH];
  60. struct session_st *next;
  61. uint8_t master_secret[SSL3_MASTER_SECRET_SIZE];
  62. uint8_t client_random[SSL3_RANDOM_SIZE];
  63. uint8_t server_random[SSL3_RANDOM_SIZE];
  64. uint32_t session_ticket_len;
  65. uint8_t *session_ticket;
  66. } session;
  67. typedef struct session_cache_st {
  68. session *first_session;
  69. uint32_t length;
  70. } session_cache;
  71. typedef struct flow_st {
  72. sem_t flow_lock;
  73. struct in_addr src_ip, dst_ip; /* Source (client) and Destination (server) addresses */
  74. uint16_t src_port, dst_port; /* Source and Destination ports */
  75. uint32_t upstream_seq_num; /* sequence number */
  76. uint32_t downstream_seq_num; /* sequence number */
  77. app_data_queue *upstream_app_data; /* Saved application-layer data for packet retransmits */
  78. app_data_queue *downstream_app_data;
  79. byte key[16]; /* negotiated key */
  80. int state; /* TLS handshake state */
  81. int in_encrypted; /* indicates whether incoming flow is encrypted */
  82. int out_encrypted; /* indicates whether outgoing flow is encrypted */
  83. int application; /* indicates handshake is complete */
  84. int resume_session;
  85. stream_table *streams;
  86. data_queue *downstream_queue;
  87. client *client_ptr;
  88. packet_chain *ds_packet_chain;
  89. packet_chain *us_packet_chain;
  90. sem_t packet_chain_lock;
  91. queue_block *upstream_queue;
  92. uint32_t upstream_remaining;
  93. DH *dh;
  94. EC_KEY *ecdh;
  95. sem_t upstream_queue_lock;
  96. const EVP_CIPHER *cipher;
  97. const EVP_MD *message_digest;
  98. uint8_t keyex_alg;
  99. uint8_t handshake_hash[EVP_MAX_MD_SIZE];
  100. EVP_MD_CTX *finish_md_ctx;
  101. EVP_CIPHER_CTX *clnt_read_ctx;
  102. EVP_CIPHER_CTX *clnt_write_ctx;
  103. EVP_CIPHER_CTX *srvr_read_ctx;
  104. EVP_CIPHER_CTX *srvr_write_ctx;
  105. EVP_MD_CTX *read_mac_ctx;
  106. EVP_MD_CTX *write_mac_ctx;
  107. uint8_t client_random[SSL3_RANDOM_SIZE];
  108. uint8_t server_random[SSL3_RANDOM_SIZE];
  109. uint8_t master_secret[SSL3_MASTER_SECRET_SIZE];
  110. session *current_session;
  111. uint8_t read_seq[8];
  112. uint8_t write_seq[8];
  113. //for downstream processing
  114. uint32_t remaining_record_len;
  115. uint8_t httpstate;
  116. uint32_t remaining_response_len;
  117. uint8_t replace_response;
  118. uint8_t *outbox;
  119. int32_t outbox_len;
  120. int32_t outbox_offset;
  121. uint8_t *partial_record_header;
  122. uint8_t partial_record_header_len;
  123. //locking
  124. //pthread_mutex_t flow_lock = PTHREAD_MUTEX_INITIALIZER;
  125. } flow;
  126. typedef struct flow_entry_st {
  127. flow *f;
  128. struct flow_entry_st *next;
  129. } flow_entry;
  130. typedef struct flow_table_st {
  131. flow_entry *first_entry;
  132. int len;
  133. } flow_table;
  134. int init_tables(void);
  135. flow *add_flow(struct packet_info *info);
  136. int update_flow(flow *f, uint8_t *record, uint8_t incoming);
  137. int remove_flow(flow *f);
  138. flow *check_flow(struct packet_info *info);
  139. flow *get_flow(int index);
  140. int init_session_cache (void);
  141. int verify_session_id(flow *f, uint8_t *hs);
  142. int check_session(flow *f, uint8_t *hs, uint32_t len);
  143. int save_session_id(flow *f, uint8_t *hs);
  144. int save_session_ticket(flow *f, uint8_t *hs, uint32_t len);
  145. int add_packet(flow *f, struct packet_info *info);
  146. #endif /* __RELAY_H__ */