flow.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. } queue_block;
  34. typedef struct packet_st packet;
  35. typedef struct session_st {
  36. uint8_t session_id_len;
  37. uint8_t session_id[SSL_MAX_SSL_SESSION_ID_LENGTH];
  38. struct session_st *next;
  39. uint8_t master_secret[SSL3_MASTER_SECRET_SIZE];
  40. uint8_t client_random[SSL3_RANDOM_SIZE];
  41. uint8_t server_random[SSL3_RANDOM_SIZE];
  42. uint32_t session_ticket_len;
  43. uint8_t *session_ticket;
  44. } session;
  45. typedef struct session_cache_st {
  46. session *first_session;
  47. uint32_t length;
  48. } session_cache;
  49. typedef struct flow_st {
  50. struct in_addr src_ip, dst_ip; /* Source (client) and Destination (server) addresses */
  51. uint16_t src_port, dst_port; /* Source and Destination ports */
  52. uint32_t seq_num; /* sequence number */
  53. byte key[16]; /* negotiated key */
  54. int state; /* TLS handshake state */
  55. int in_encrypted; /* indicates whether incoming flow is encrypted */
  56. int out_encrypted; /* indicates whether outgoing flow is encrypted */
  57. int application; /* indicates handshake is complete */
  58. int resume_session;
  59. packet *packet_chain; /* currently held data */
  60. queue_block *censored_queue;
  61. DH *dh;
  62. uint8_t handshake_hash[EVP_MAX_MD_SIZE];
  63. EVP_MD_CTX *finish_md_ctx;
  64. EVP_CIPHER_CTX *clnt_read_ctx;
  65. EVP_CIPHER_CTX *clnt_write_ctx;
  66. EVP_CIPHER_CTX *srvr_read_ctx;
  67. EVP_CIPHER_CTX *srvr_write_ctx;
  68. EVP_MD_CTX *read_mac_ctx;
  69. EVP_MD_CTX *write_mac_ctx;
  70. uint8_t client_random[SSL3_RANDOM_SIZE];
  71. uint8_t server_random[SSL3_RANDOM_SIZE];
  72. uint8_t master_secret[SSL3_MASTER_SECRET_SIZE];
  73. session *current_session;
  74. uint8_t read_seq[8];
  75. uint8_t write_seq[8];
  76. uint8_t *outbox;
  77. int32_t outbox_len;
  78. //locking
  79. //pthread_mutex_t flow_lock = PTHREAD_MUTEX_INITIALIZER;
  80. } flow;
  81. typedef struct flow_table_st {
  82. flow *table;
  83. int len;
  84. int max_len;
  85. } flow_table;
  86. int init_flow_table (void);
  87. flow *add_flow(flow newFlow);
  88. int update_flow(flow *f);
  89. int remove_flow(int index);
  90. int check_flow(flow observed);
  91. flow *get_flow(int index);
  92. int init_session_cache (void);
  93. int verify_session_id(flow *f, uint8_t *hs);
  94. int check_session(flow *f, uint8_t *hs, uint32_t len);
  95. int save_session_id(flow *f, uint8_t *hs);
  96. int save_session_ticket(flow *f, uint8_t *hs, uint32_t len);
  97. int add_packet(flow *f, struct packet_info *info);
  98. #endif /* __RELAY_H__ */