flow.h 3.7 KB

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