flow.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #ifndef __RELAY_H__
  2. #define __RELAY_H__
  3. #include <netinet/in.h>
  4. #include <openssl/bn.h>
  5. #include <openssl/ssl.h>
  6. #include "ptwist.h"
  7. #include "slitheen.h"
  8. #define MAX_FLOWS 10
  9. #define TLS_HELLO_REQ 0x00
  10. #define TLS_CLNT_HELLO 0x01
  11. #define TLS_SERV_HELLO 0x02
  12. #define TLS_NEW_SESS 0x04
  13. #define TLS_CERT 0x0b
  14. #define TLS_SRVR_KEYEX 0x0c
  15. #define TLS_CERT_REQ 0x0d
  16. #define TLS_SRVR_HELLO_DONE 0x0e
  17. #define TLS_CERT_VERIFY 0x0f
  18. #define TLS_CLNT_KEYEX 0x10
  19. #define TLS_FINISHED 0x14
  20. struct packet_st{
  21. uint32_t seq_num;
  22. uint16_t len;
  23. uint16_t data_len;
  24. uint8_t *data;
  25. struct packet_st *next;
  26. int incoming; //0 for outgoing, 1 for incoming
  27. };
  28. typedef struct queue_block_st{
  29. int32_t len;
  30. int32_t offset;
  31. uint8_t *data;
  32. struct queue_block_st *next;
  33. uint8_t stream_id;
  34. } queue_block;
  35. typedef struct data_queue_st {
  36. queue_block *first_block;
  37. } data_queue;
  38. extern data_queue *downstream_queue;
  39. typedef struct packet_st packet;
  40. typedef struct session_st {
  41. uint8_t session_id_len;
  42. uint8_t session_id[SSL_MAX_SSL_SESSION_ID_LENGTH];
  43. struct session_st *next;
  44. uint8_t master_secret[SSL3_MASTER_SECRET_SIZE];
  45. uint8_t client_random[SSL3_RANDOM_SIZE];
  46. uint8_t server_random[SSL3_RANDOM_SIZE];
  47. uint32_t session_ticket_len;
  48. uint8_t *session_ticket;
  49. } session;
  50. typedef struct session_cache_st {
  51. session *first_session;
  52. uint32_t length;
  53. } session_cache;
  54. typedef struct flow_st {
  55. struct in_addr src_ip, dst_ip; /* Source (client) and Destination (server) addresses */
  56. uint16_t src_port, dst_port; /* Source and Destination ports */
  57. uint32_t seq_num; /* sequence number */
  58. byte key[16]; /* negotiated key */
  59. int state; /* TLS handshake state */
  60. int in_encrypted; /* indicates whether incoming flow is encrypted */
  61. int out_encrypted; /* indicates whether outgoing flow is encrypted */
  62. int application; /* indicates handshake is complete */
  63. int resume_session;
  64. packet *packet_chain; /* currently held data */
  65. queue_block *censored_queue;
  66. DH *dh;
  67. uint8_t handshake_hash[EVP_MAX_MD_SIZE];
  68. EVP_MD_CTX *finish_md_ctx;
  69. EVP_CIPHER_CTX *clnt_read_ctx;
  70. EVP_CIPHER_CTX *clnt_write_ctx;
  71. EVP_CIPHER_CTX *srvr_read_ctx;
  72. EVP_CIPHER_CTX *srvr_write_ctx;
  73. EVP_MD_CTX *read_mac_ctx;
  74. EVP_MD_CTX *write_mac_ctx;
  75. uint8_t client_random[SSL3_RANDOM_SIZE];
  76. uint8_t server_random[SSL3_RANDOM_SIZE];
  77. uint8_t master_secret[SSL3_MASTER_SECRET_SIZE];
  78. session *current_session;
  79. uint8_t read_seq[8];
  80. uint8_t write_seq[8];
  81. //for downstream processing
  82. uint32_t remaining_record_len;
  83. uint8_t httpstate;
  84. uint32_t remaining_response_len;
  85. uint8_t replace_response;
  86. uint8_t *outbox;
  87. int32_t outbox_len;
  88. int32_t outbox_offset;
  89. //locking
  90. //pthread_mutex_t flow_lock = PTHREAD_MUTEX_INITIALIZER;
  91. } flow;
  92. typedef struct flow_entry_st {
  93. flow *f;
  94. struct flow_entry_st *next;
  95. } flow_entry;
  96. typedef struct flow_table_st {
  97. flow_entry *first_entry;
  98. int len;
  99. } flow_table;
  100. int init_tables(void);
  101. flow *add_flow(flow newFlow);
  102. int update_flow(flow *f);
  103. int remove_flow(flow *f);
  104. flow *check_flow(flow observed);
  105. flow *get_flow(int index);
  106. int init_session_cache (void);
  107. int verify_session_id(flow *f, uint8_t *hs);
  108. int check_session(flow *f, uint8_t *hs, uint32_t len);
  109. int save_session_id(flow *f, uint8_t *hs);
  110. int save_session_ticket(flow *f, uint8_t *hs, uint32_t len);
  111. int add_packet(flow *f, struct packet_info *info);
  112. #endif /* __RELAY_H__ */